From 16079c8f9ff11c6fbe867c077a334e269c847e1e Mon Sep 17 00:00:00 2001 From: Emily Bache Date: Wed, 4 Jan 2012 09:53:16 +0100 Subject: [PATCH] version of three exercises, in c++ --- Tennis/cpp/defactored1/Makefile | 75 ++++++++ Tennis/cpp/defactored1/README | 14 ++ Tennis/cpp/defactored1/Tennis.cc | 227 ++++++++++++++++++++++ Tennis/cpp/defactored1/run-endless.sh | 1 + Tennis/cpp/defactored1/run-once.sh | 4 + Tennis/cpp/defactored2/Makefile | 75 ++++++++ Tennis/cpp/defactored2/README | 14 ++ Tennis/cpp/defactored2/Tennis.cc | 258 ++++++++++++++++++++++++++ Tennis/cpp/defactored2/run-endless.sh | 1 + Tennis/cpp/defactored2/run-once.sh | 4 + Tennis/cpp/defactored3/Makefile | 75 ++++++++ Tennis/cpp/defactored3/README | 14 ++ Tennis/cpp/defactored3/Tennis.cc | 183 ++++++++++++++++++ Tennis/cpp/defactored3/run-endless.sh | 1 + Tennis/cpp/defactored3/run-once.sh | 4 + Tennis/cpp/generate_tests.py | 55 ++++++ Tennis/cpp/starting/Makefile | 75 ++++++++ Tennis/cpp/starting/README | 14 ++ Tennis/cpp/starting/Tennis.cc | 171 +++++++++++++++++ Tennis/cpp/starting/run-endless.sh | 1 + Tennis/cpp/starting/run-once.sh | 4 + 21 files changed, 1270 insertions(+) create mode 100644 Tennis/cpp/defactored1/Makefile create mode 100644 Tennis/cpp/defactored1/README create mode 100644 Tennis/cpp/defactored1/Tennis.cc create mode 100755 Tennis/cpp/defactored1/run-endless.sh create mode 100755 Tennis/cpp/defactored1/run-once.sh create mode 100644 Tennis/cpp/defactored2/Makefile create mode 100644 Tennis/cpp/defactored2/README create mode 100644 Tennis/cpp/defactored2/Tennis.cc create mode 100755 Tennis/cpp/defactored2/run-endless.sh create mode 100755 Tennis/cpp/defactored2/run-once.sh create mode 100644 Tennis/cpp/defactored3/Makefile create mode 100644 Tennis/cpp/defactored3/README create mode 100644 Tennis/cpp/defactored3/Tennis.cc create mode 100755 Tennis/cpp/defactored3/run-endless.sh create mode 100755 Tennis/cpp/defactored3/run-once.sh create mode 100644 Tennis/cpp/generate_tests.py create mode 100644 Tennis/cpp/starting/Makefile create mode 100644 Tennis/cpp/starting/README create mode 100644 Tennis/cpp/starting/Tennis.cc create mode 100755 Tennis/cpp/starting/run-endless.sh create mode 100755 Tennis/cpp/starting/run-once.sh diff --git a/Tennis/cpp/defactored1/Makefile b/Tennis/cpp/defactored1/Makefile new file mode 100644 index 00000000..a97fcd8b --- /dev/null +++ b/Tennis/cpp/defactored1/Makefile @@ -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 $@ diff --git a/Tennis/cpp/defactored1/README b/Tennis/cpp/defactored1/README new file mode 100644 index 00000000..59cd984e --- /dev/null +++ b/Tennis/cpp/defactored1/README @@ -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. diff --git a/Tennis/cpp/defactored1/Tennis.cc b/Tennis/cpp/defactored1/Tennis.cc new file mode 100644 index 00000000..b2737310 --- /dev/null +++ b/Tennis/cpp/defactored1/Tennis.cc @@ -0,0 +1,227 @@ +#include + +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)); +} + + diff --git a/Tennis/cpp/defactored1/run-endless.sh b/Tennis/cpp/defactored1/run-endless.sh new file mode 100755 index 00000000..ad449aa3 --- /dev/null +++ b/Tennis/cpp/defactored1/run-endless.sh @@ -0,0 +1 @@ +codersdojo start run-once.sh diff --git a/Tennis/cpp/defactored1/run-once.sh b/Tennis/cpp/defactored1/run-once.sh new file mode 100755 index 00000000..1b1bffef --- /dev/null +++ b/Tennis/cpp/defactored1/run-once.sh @@ -0,0 +1,4 @@ +rm Tennis +rm Tennis.o +make +./Tennis \ No newline at end of file diff --git a/Tennis/cpp/defactored2/Makefile b/Tennis/cpp/defactored2/Makefile new file mode 100644 index 00000000..a97fcd8b --- /dev/null +++ b/Tennis/cpp/defactored2/Makefile @@ -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 $@ diff --git a/Tennis/cpp/defactored2/README b/Tennis/cpp/defactored2/README new file mode 100644 index 00000000..59cd984e --- /dev/null +++ b/Tennis/cpp/defactored2/README @@ -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. diff --git a/Tennis/cpp/defactored2/Tennis.cc b/Tennis/cpp/defactored2/Tennis.cc new file mode 100644 index 00000000..fe7e480f --- /dev/null +++ b/Tennis/cpp/defactored2/Tennis.cc @@ -0,0 +1,258 @@ +#include + +const std::string tennis_score(int p1Score, int p2Score) { + std::string score = ""; + std::string P1res = ""; + std::string P2res = ""; + if (p1Score == p2Score && p1Score < 4) + { + if (p1Score==0) + score = "Love"; + if (p1Score==1) + score = "Fifteen"; + if (p1Score==2) + score = "Thirty"; + if (p1Score==3) + score = "Forty"; + score += "-All"; + } + if (p1Score==p2Score && p1Score>3) + score = "Deuce"; + + if (p1Score > 0 && p2Score==0) + { + if (p1Score==1) + P1res = "Fifteen"; + if (p1Score==2) + P1res = "Thirty"; + if (p1Score==3) + P1res = "Forty"; + + P2res = "Love"; + score = P1res + "-" + P2res; + } + if (p2Score > 0 && p1Score==0) + { + if (p2Score==1) + P2res = "Fifteen"; + if (p2Score==2) + P2res = "Thirty"; + if (p2Score==3) + P2res = "Forty"; + + P1res = "Love"; + score = P1res + "-" + P2res; + } + + if (p1Score>p2Score && p1Score < 4) + { + if (p1Score==2) + P1res="Thirty"; + if (p1Score==3) + P1res="Forty"; + if (p2Score==1) + P2res="Fifteen"; + if (p2Score==2) + P2res="Thirty"; + score = P1res + "-" + P2res; + } + if (p2Score>p1Score && p2Score < 4) + { + if (p2Score==2) + P2res="Thirty"; + if (p2Score==3) + P2res="Forty"; + if (p1Score==1) + P1res="Fifteen"; + if (p1Score==2) + P1res="Thirty"; + score = P1res + "-" + P2res; + } + + if (p1Score > p2Score && p2Score >= 3) + { + score = "Advantage player1"; + } + + if (p2Score > p1Score && p1Score >= 3) + { + score = "Advantage player2"; + } + + if (p1Score>=4 && p2Score>=0 && (p1Score-p2Score)>=2) + { + score = "Win for player1"; + } + if (p2Score>=4 && p1Score>=0 && (p2Score-p1Score)>=2) + { + score = "Win for player2"; + } + 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)); +} + + diff --git a/Tennis/cpp/defactored2/run-endless.sh b/Tennis/cpp/defactored2/run-endless.sh new file mode 100755 index 00000000..ad449aa3 --- /dev/null +++ b/Tennis/cpp/defactored2/run-endless.sh @@ -0,0 +1 @@ +codersdojo start run-once.sh diff --git a/Tennis/cpp/defactored2/run-once.sh b/Tennis/cpp/defactored2/run-once.sh new file mode 100755 index 00000000..1b1bffef --- /dev/null +++ b/Tennis/cpp/defactored2/run-once.sh @@ -0,0 +1,4 @@ +rm Tennis +rm Tennis.o +make +./Tennis \ No newline at end of file diff --git a/Tennis/cpp/defactored3/Makefile b/Tennis/cpp/defactored3/Makefile new file mode 100644 index 00000000..a97fcd8b --- /dev/null +++ b/Tennis/cpp/defactored3/Makefile @@ -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 $@ diff --git a/Tennis/cpp/defactored3/README b/Tennis/cpp/defactored3/README new file mode 100644 index 00000000..59cd984e --- /dev/null +++ b/Tennis/cpp/defactored3/README @@ -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. diff --git a/Tennis/cpp/defactored3/Tennis.cc b/Tennis/cpp/defactored3/Tennis.cc new file mode 100644 index 00000000..15da13ac --- /dev/null +++ b/Tennis/cpp/defactored3/Tennis.cc @@ -0,0 +1,183 @@ +#include + +const std::string tennis_score(int p1, int p2) { + std::string s; + std::string p1N = "player1"; + std::string p2N = "player2"; + if (p1 < 4 && p2 < 4) { + std::string p[4] = {"Love", "Fifteen", "Thirty", "Forty"}; + s = p[p1]; + return (p1 == p2) ? s + "-All" : s + "-" + p[p2]; + } else { + if (p1 == p2) + return "Deuce"; + s = p1 > p2 ? p1N : p2N; + return ((p1-p2)*(p1-p2) == 1) ? "Advantage " + s : "Win for " + s; + } +} + +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)); +} + + diff --git a/Tennis/cpp/defactored3/run-endless.sh b/Tennis/cpp/defactored3/run-endless.sh new file mode 100755 index 00000000..ad449aa3 --- /dev/null +++ b/Tennis/cpp/defactored3/run-endless.sh @@ -0,0 +1 @@ +codersdojo start run-once.sh diff --git a/Tennis/cpp/defactored3/run-once.sh b/Tennis/cpp/defactored3/run-once.sh new file mode 100755 index 00000000..1b1bffef --- /dev/null +++ b/Tennis/cpp/defactored3/run-once.sh @@ -0,0 +1,4 @@ +rm Tennis +rm Tennis.o +make +./Tennis \ No newline at end of file diff --git a/Tennis/cpp/generate_tests.py b/Tennis/cpp/generate_tests.py new file mode 100644 index 00000000..2e645fde --- /dev/null +++ b/Tennis/cpp/generate_tests.py @@ -0,0 +1,55 @@ + +template = """\ +TEST(TennisTest, %(testcase_name)s) { + EXPECT_EQ("%(score)s", tennis_score(%(p1Points)s, %(p2Points)s)); +} +""" + +test_cases = [dict(p1Points=0, p2Points=0, score="Love-All"), + dict(p1Points=1, p2Points=1, score="Fifteen-All"), + dict(p1Points=2, p2Points=2, score="Thirty-All"), + dict(p1Points=3, p2Points=3, score="Forty-All"), + dict(p1Points=4, p2Points=4, score="Deuce"), + + dict(p1Points=1, p2Points=0, score="Fifteen-Love"), + dict(p1Points=0, p2Points=1, score="Love-Fifteen"), + dict(p1Points=2, p2Points=0, score="Thirty-Love"), + dict(p1Points=0, p2Points=2, score="Love-Thirty"), + dict(p1Points=3, p2Points=0, score="Forty-Love"), + dict(p1Points=0, p2Points=3, score="Love-Forty"), + dict(p1Points=4, p2Points=0, score="Win for player1"), + dict(p1Points=0, p2Points=4, score="Win for player2"), + + dict(p1Points=2, p2Points=1, score="Thirty-Fifteen"), + dict(p1Points=1, p2Points=2, score="Fifteen-Thirty"), + dict(p1Points=3, p2Points=1, score="Forty-Fifteen"), + dict(p1Points=1, p2Points=3, score="Fifteen-Forty"), + dict(p1Points=4, p2Points=1, score="Win for player1"), + dict(p1Points=1, p2Points=4, score="Win for player2"), + + dict(p1Points=3, p2Points=2, score="Forty-Thirty"), + dict(p1Points=2, p2Points=3, score="Thirty-Forty"), + dict(p1Points=4, p2Points=2, score="Win for player1"), + dict(p1Points=2, p2Points=4, score="Win for player2"), + + dict(p1Points=4, p2Points=3, score="Advantage player1"), + dict(p1Points=3, p2Points=4, score="Advantage player2"), + dict(p1Points=5, p2Points=4, score="Advantage player1"), + dict(p1Points=4, p2Points=5, score="Advantage player2"), + dict(p1Points=15, p2Points=14, score="Advantage player1"), + dict(p1Points=14, p2Points=15, score="Advantage player2"), + + dict(p1Points=6, p2Points=4, score="Win for player1"), + dict(p1Points=4, p2Points=6, score="Win for player2"), + dict(p1Points=16, p2Points=14, score="Win for player1"), + dict(p1Points=14, p2Points=16, score="Win for player2"), + ] + +for test in test_cases: + cleaned = test["score"] + cleaned = cleaned.replace("-", "") + cleaned = cleaned.replace(" ", "") + test["cleaned"] = cleaned + test["testcase_name"] = "%(cleaned)s_%(p1Points)s_%(p2Points)s" % test + print template % test + print \ No newline at end of file diff --git a/Tennis/cpp/starting/Makefile b/Tennis/cpp/starting/Makefile new file mode 100644 index 00000000..a97fcd8b --- /dev/null +++ b/Tennis/cpp/starting/Makefile @@ -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 $@ diff --git a/Tennis/cpp/starting/README b/Tennis/cpp/starting/README new file mode 100644 index 00000000..59cd984e --- /dev/null +++ b/Tennis/cpp/starting/README @@ -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. diff --git a/Tennis/cpp/starting/Tennis.cc b/Tennis/cpp/starting/Tennis.cc new file mode 100644 index 00000000..90fac43c --- /dev/null +++ b/Tennis/cpp/starting/Tennis.cc @@ -0,0 +1,171 @@ +#include + +const std::string tennis_score(int p1Score, int p2Score) { + return "foo"; +} + +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)); +} + + diff --git a/Tennis/cpp/starting/run-endless.sh b/Tennis/cpp/starting/run-endless.sh new file mode 100755 index 00000000..ad449aa3 --- /dev/null +++ b/Tennis/cpp/starting/run-endless.sh @@ -0,0 +1 @@ +codersdojo start run-once.sh diff --git a/Tennis/cpp/starting/run-once.sh b/Tennis/cpp/starting/run-once.sh new file mode 100755 index 00000000..1b1bffef --- /dev/null +++ b/Tennis/cpp/starting/run-once.sh @@ -0,0 +1,4 @@ +rm Tennis +rm Tennis.o +make +./Tennis \ No newline at end of file