version of three exercises, in c++

This commit is contained in:
Emily Bache 2012-01-04 09:53:16 +01:00
parent 5453deb427
commit 16079c8f9f
21 changed files with 1270 additions and 0 deletions

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 $@

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.

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));
}

View File

@ -0,0 +1 @@
codersdojo start run-once.sh

View File

@ -0,0 +1,4 @@
rm Tennis
rm Tennis.o
make
./Tennis

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 $@

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.

View File

@ -0,0 +1,258 @@
#include <gtest/gtest.h>
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));
}

View File

@ -0,0 +1 @@
codersdojo start run-once.sh

View File

@ -0,0 +1,4 @@
rm Tennis
rm Tennis.o
make
./Tennis

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 $@

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.

View File

@ -0,0 +1,183 @@
#include <gtest/gtest.h>
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));
}

View File

@ -0,0 +1 @@
codersdojo start run-once.sh

View File

@ -0,0 +1,4 @@
rm Tennis
rm Tennis.o
make
./Tennis

View File

@ -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

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 $@

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.

View File

@ -0,0 +1,171 @@
#include <gtest/gtest.h>
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));
}

View File

@ -0,0 +1 @@
codersdojo start run-once.sh

View File

@ -0,0 +1,4 @@
rm Tennis
rm Tennis.o
make
./Tennis