version of three exercises, in c++

This commit is contained in:
Emily Bache committed 2012-01-04 09:53:16 +01:00
1 parent 5453deb427
commit 16079c8f9f
21 files changed
+1270

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
# Makefile for building the kata file with the Google Testing Framework
#
# SYNOPSIS:
#
# make [all] - makes everything.
# make TARGET - makes the given target.
# make clean - removes all files generated by make.
# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.
# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = gtest
# Where to find user code.
USER_DIR = .
# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include
# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra
# All tests produced by this Makefile. Remember to add new tests you
# created to the list.
TESTS = Tennis
# All Google Test headers. Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h
# House-keeping build targets.
all : $(TESTS)
clean :
rm -f $(TESTS) gtest.a gtest_main.a *.o
# Builds gtest.a and gtest_main.a.
# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized. This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc
gtest_main.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest_main.cc
gtest.a : gtest-all.o
$(AR) $(ARFLAGS) $@ $^
gtest_main.a : gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $^
# Builds a sample test. A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.
Tennis.o : $(USER_DIR)/Tennis.cc \
$(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/Tennis.cc
Tennis : Tennis.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
+14
View File
@@ -0,0 +1,14 @@
These files were created:
README is what you are currently reading
run-once.sh runs your tests once
run-endless.sh runs your tests endlessly via run-once.sh
Run run-endless.sh and start your kata. (On Mac/Linux you have to call ./run-endless.sh.)
Assumptions:
- make and a C++ compiler (like gcc) is installed on your system and is in the PATH
- The GTest framework is in the directory gtest.
- If your IDE does the compilation and linking, you should remove the first 3 lines
in the run-once.sh file.
The support for C++/GTest was contributed by Stefan Roock.
+227
View File
@@ -0,0 +1,227 @@
#include <gtest/gtest.h>
const std::string tennis_score(int p1Score, int p2Score) {
std::string score = "";
int tempScore=0;
if (p1Score==p2Score)
{
switch (p1Score)
{
case 0:
score = "Love-All";
break;
case 1:
score = "Fifteen-All";
break;
case 2:
score = "Thirty-All";
break;
case 3:
score = "Forty-All";
break;
default:
score = "Deuce";
break;
}
}
else if (p1Score>=4 || p2Score>=4)
{
int minusResult = p1Score-p2Score;
if (minusResult==1) score ="Advantage player1";
else if (minusResult ==-1) score ="Advantage player2";
else if (minusResult>=2) score = "Win for player1";
else score ="Win for player2";
}
else
{
for (int i=1; i<3; i++)
{
if (i==1) tempScore = p1Score;
else { score+="-"; tempScore = p2Score;}
switch(tempScore)
{
case 0:
score+="Love";
break;
case 1:
score+="Fifteen";
break;
case 2:
score+="Thirty";
break;
case 3:
score+="Forty";
break;
}
}
}
return score;
}
TEST(TennisTest, LoveAll_0_0) {
EXPECT_EQ("Love-All", tennis_score(0, 0));
}
TEST(TennisTest, FifteenAll_1_1) {
EXPECT_EQ("Fifteen-All", tennis_score(1, 1));
}
TEST(TennisTest, ThirtyAll_2_2) {
EXPECT_EQ("Thirty-All", tennis_score(2, 2));
}
TEST(TennisTest, FortyAll_3_3) {
EXPECT_EQ("Forty-All", tennis_score(3, 3));
}
TEST(TennisTest, Deuce_4_4) {
EXPECT_EQ("Deuce", tennis_score(4, 4));
}
TEST(TennisTest, FifteenLove_1_0) {
EXPECT_EQ("Fifteen-Love", tennis_score(1, 0));
}
TEST(TennisTest, LoveFifteen_0_1) {
EXPECT_EQ("Love-Fifteen", tennis_score(0, 1));
}
TEST(TennisTest, ThirtyLove_2_0) {
EXPECT_EQ("Thirty-Love", tennis_score(2, 0));
}
TEST(TennisTest, LoveThirty_0_2) {
EXPECT_EQ("Love-Thirty", tennis_score(0, 2));
}
TEST(TennisTest, FortyLove_3_0) {
EXPECT_EQ("Forty-Love", tennis_score(3, 0));
}
TEST(TennisTest, LoveForty_0_3) {
EXPECT_EQ("Love-Forty", tennis_score(0, 3));
}
TEST(TennisTest, Winforplayer1_4_0) {
EXPECT_EQ("Win for player1", tennis_score(4, 0));
}
TEST(TennisTest, Winforplayer2_0_4) {
EXPECT_EQ("Win for player2", tennis_score(0, 4));
}
TEST(TennisTest, ThirtyFifteen_2_1) {
EXPECT_EQ("Thirty-Fifteen", tennis_score(2, 1));
}
TEST(TennisTest, FifteenThirty_1_2) {
EXPECT_EQ("Fifteen-Thirty", tennis_score(1, 2));
}
TEST(TennisTest, FortyFifteen_3_1) {
EXPECT_EQ("Forty-Fifteen", tennis_score(3, 1));
}
TEST(TennisTest, FifteenForty_1_3) {
EXPECT_EQ("Fifteen-Forty", tennis_score(1, 3));
}
TEST(TennisTest, Winforplayer1_4_1) {
EXPECT_EQ("Win for player1", tennis_score(4, 1));
}
TEST(TennisTest, Winforplayer2_1_4) {
EXPECT_EQ("Win for player2", tennis_score(1, 4));
}
TEST(TennisTest, FortyThirty_3_2) {
EXPECT_EQ("Forty-Thirty", tennis_score(3, 2));
}
TEST(TennisTest, ThirtyForty_2_3) {
EXPECT_EQ("Thirty-Forty", tennis_score(2, 3));
}
TEST(TennisTest, Winforplayer1_4_2) {
EXPECT_EQ("Win for player1", tennis_score(4, 2));
}
TEST(TennisTest, Winforplayer2_2_4) {
EXPECT_EQ("Win for player2", tennis_score(2, 4));
}
TEST(TennisTest, Advantageplayer1_4_3) {
EXPECT_EQ("Advantage player1", tennis_score(4, 3));
}
TEST(TennisTest, Advantageplayer2_3_4) {
EXPECT_EQ("Advantage player2", tennis_score(3, 4));
}
TEST(TennisTest, Advantageplayer1_5_4) {
EXPECT_EQ("Advantage player1", tennis_score(5, 4));
}
TEST(TennisTest, Advantageplayer2_4_5) {
EXPECT_EQ("Advantage player2", tennis_score(4, 5));
}
TEST(TennisTest, Advantageplayer1_15_14) {
EXPECT_EQ("Advantage player1", tennis_score(15, 14));
}
TEST(TennisTest, Advantageplayer2_14_15) {
EXPECT_EQ("Advantage player2", tennis_score(14, 15));
}
TEST(TennisTest, Winforplayer1_6_4) {
EXPECT_EQ("Win for player1", tennis_score(6, 4));
}
TEST(TennisTest, Winforplayer2_4_6) {
EXPECT_EQ("Win for player2", tennis_score(4, 6));
}
TEST(TennisTest, Winforplayer1_16_14) {
EXPECT_EQ("Win for player1", tennis_score(16, 14));
}
TEST(TennisTest, Winforplayer2_14_16) {
EXPECT_EQ("Win for player2", tennis_score(14, 16));
}
+1
View File
@@ -0,0 +1 @@
codersdojo start run-once.sh
+4
View File
@@ -0,0 +1,4 @@
rm Tennis
rm Tennis.o
make
./Tennis