From 7e52c0ddfe6f6dcaacec49233a51c377d9580ba3 Mon Sep 17 00:00:00 2001 From: emilybache Date: Mon, 10 Dec 2012 14:49:20 +0100 Subject: [PATCH] test cases and code that work in cyber-dojo --- Tennis/cpp/all_tests.cpp | 171 ++++++++++++++++++++++++++++++++ Tennis/cpp/all_tests.hpp | 33 ++++++ Tennis/cpp/generate_tests.py | 5 +- Tennis/cpp/makefile | 5 + Tennis/cpp/tennis1.cc | 61 ++++++++++++ Tennis/cpp/tennis2.cc | 92 +++++++++++++++++ Tennis/cpp/tennis3.cc | 17 ++++ Tennis/cpp/test_suite.tests.cpp | 55 ++++++++++ 8 files changed, 437 insertions(+), 2 deletions(-) create mode 100644 Tennis/cpp/all_tests.cpp create mode 100644 Tennis/cpp/all_tests.hpp create mode 100644 Tennis/cpp/makefile create mode 100644 Tennis/cpp/tennis1.cc create mode 100644 Tennis/cpp/tennis2.cc create mode 100644 Tennis/cpp/tennis3.cc create mode 100644 Tennis/cpp/test_suite.tests.cpp diff --git a/Tennis/cpp/all_tests.cpp b/Tennis/cpp/all_tests.cpp new file mode 100644 index 00000000..c677cfdc --- /dev/null +++ b/Tennis/cpp/all_tests.cpp @@ -0,0 +1,171 @@ +#include "all_tests.hpp" + +/* change this to the version of tennis you want to work on */ +#include "tennis1.cc" + +#include + +void test_LoveAll_0_0() +{ + assert("Love-All" == tennis_score(0, 0)); +} + +void test_FifteenAll_1_1() +{ + assert("Fifteen-All" == tennis_score(1, 1)); +} + +void test_ThirtyAll_2_2() +{ + assert("Thirty-All" == tennis_score(2, 2)); +} + +void test_Deuce_3_3() +{ + assert("Deuce" == tennis_score(3, 3)); +} + +void test_Deuce_4_4() +{ + assert("Deuce" == tennis_score(4, 4)); +} + +void test_FifteenLove_1_0() +{ + assert("Fifteen-Love" == tennis_score(1, 0)); +} + +void test_LoveFifteen_0_1() +{ + assert("Love-Fifteen" == tennis_score(0, 1)); +} + +void test_ThirtyLove_2_0() +{ + assert("Thirty-Love" == tennis_score(2, 0)); +} + +void test_LoveThirty_0_2() +{ + assert("Love-Thirty" == tennis_score(0, 2)); +} + +void test_FortyLove_3_0() +{ + assert("Forty-Love" == tennis_score(3, 0)); +} + +void test_LoveForty_0_3() +{ + assert("Love-Forty" == tennis_score(0, 3)); +} + +void test_Winforplayer1_4_0() +{ + assert("Win for player1" == tennis_score(4, 0)); +} + +void test_Winforplayer2_0_4() +{ + assert("Win for player2" == tennis_score(0, 4)); +} + +void test_ThirtyFifteen_2_1() +{ + assert("Thirty-Fifteen" == tennis_score(2, 1)); +} + +void test_FifteenThirty_1_2() +{ + assert("Fifteen-Thirty" == tennis_score(1, 2)); +} + +void test_FortyFifteen_3_1() +{ + assert("Forty-Fifteen" == tennis_score(3, 1)); +} + +void test_FifteenForty_1_3() +{ + assert("Fifteen-Forty" == tennis_score(1, 3)); +} + +void test_Winforplayer1_4_1() +{ + assert("Win for player1" == tennis_score(4, 1)); +} + +void test_Winforplayer2_1_4() +{ + assert("Win for player2" == tennis_score(1, 4)); +} + +void test_FortyThirty_3_2() +{ + assert("Forty-Thirty" == tennis_score(3, 2)); +} + +void test_ThirtyForty_2_3() +{ + assert("Thirty-Forty" == tennis_score(2, 3)); +} + +void test_Winforplayer1_4_2() +{ + assert("Win for player1" == tennis_score(4, 2)); +} + +void test_Winforplayer2_2_4() +{ + assert("Win for player2" == tennis_score(2, 4)); +} + +void test_Advantageplayer1_4_3() +{ + assert("Advantage player1" == tennis_score(4, 3)); +} + +void test_Advantageplayer2_3_4() +{ + assert("Advantage player2" == tennis_score(3, 4)); +} + +void test_Advantageplayer1_5_4() +{ + assert("Advantage player1" == tennis_score(5, 4)); +} + +void test_Advantageplayer2_4_5() +{ + assert("Advantage player2" == tennis_score(4, 5)); +} + +void test_Advantageplayer1_15_14() +{ + assert("Advantage player1" == tennis_score(15, 14)); +} + +void test_Advantageplayer2_14_15() +{ + assert("Advantage player2" == tennis_score(14, 15)); +} + +void test_Winforplayer1_6_4() +{ + assert("Win for player1" == tennis_score(6, 4)); +} + +void test_Winforplayer2_4_6() +{ + assert("Win for player2" == tennis_score(4, 6)); +} + +void test_Winforplayer1_16_14() +{ + assert("Win for player1" == tennis_score(16, 14)); +} + +void test_Winforplayer2_14_16() +{ + assert("Win for player2" == tennis_score(14, 16)); +} \ No newline at end of file diff --git a/Tennis/cpp/all_tests.hpp b/Tennis/cpp/all_tests.hpp new file mode 100644 index 00000000..b0fed07f --- /dev/null +++ b/Tennis/cpp/all_tests.hpp @@ -0,0 +1,33 @@ +void test_LoveAll_0_0(); +void test_FifteenAll_1_1(); +void test_ThirtyAll_2_2(); +void test_Deuce_3_3(); +void test_Deuce_4_4(); +void test_FifteenLove_1_0(); +void test_LoveFifteen_0_1(); +void test_ThirtyLove_2_0(); +void test_LoveThirty_0_2(); +void test_FortyLove_3_0(); +void test_LoveForty_0_3(); +void test_Winforplayer1_4_0(); +void test_Winforplayer2_0_4(); +void test_ThirtyFifteen_2_1(); +void test_FifteenThirty_1_2(); +void test_FortyFifteen_3_1(); +void test_FifteenForty_1_3(); +void test_Winforplayer1_4_1(); +void test_Winforplayer2_1_4(); +void test_FortyThirty_3_2(); +void test_ThirtyForty_2_3(); +void test_Winforplayer1_4_2(); +void test_Winforplayer2_2_4(); +void test_Advantageplayer1_4_3(); +void test_Advantageplayer2_3_4(); +void test_Advantageplayer1_5_4(); +void test_Advantageplayer2_4_5(); +void test_Advantageplayer1_15_14(); +void test_Advantageplayer2_14_15(); +void test_Winforplayer1_6_4(); +void test_Winforplayer2_4_6(); +void test_Winforplayer1_16_14(); +void test_Winforplayer2_14_16(); \ No newline at end of file diff --git a/Tennis/cpp/generate_tests.py b/Tennis/cpp/generate_tests.py index bd8975b2..d2f2c585 100644 --- a/Tennis/cpp/generate_tests.py +++ b/Tennis/cpp/generate_tests.py @@ -53,6 +53,7 @@ def create_testcase_dicts(): testcase_dicts = create_testcase_dicts() +# This is not currently being used gtest_template = """\ TEST(TennisTest, %(testcase_name)s) { EXPECT_EQ("%(score)s", tennis_score(%(p1Points)s, %(p2Points)s)); @@ -65,11 +66,11 @@ void test_%(testcase_name)s() assert("%(score)s" == tennis_score(%(p1Points)s, %(p2Points)s)); } """ -# test cases +# test cases for all_tests.cpp for test in testcase_dicts: print template % test -# all_tests.cpp +# test_suite.tests.cpp for test in testcase_dicts: print " test_%(testcase_name)s," % test diff --git a/Tennis/cpp/makefile b/Tennis/cpp/makefile new file mode 100644 index 00000000..cfd4c776 --- /dev/null +++ b/Tennis/cpp/makefile @@ -0,0 +1,5 @@ +run.tests.output : run.tests + ./run.tests + +run.tests : *.cpp + g++ -Wall -Werror -O *.cpp -o run.tests \ No newline at end of file diff --git a/Tennis/cpp/tennis1.cc b/Tennis/cpp/tennis1.cc new file mode 100644 index 00000000..10ff0805 --- /dev/null +++ b/Tennis/cpp/tennis1.cc @@ -0,0 +1,61 @@ +#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 = "Deuce"; + 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; + +} diff --git a/Tennis/cpp/tennis2.cc b/Tennis/cpp/tennis2.cc new file mode 100644 index 00000000..3735dfeb --- /dev/null +++ b/Tennis/cpp/tennis2.cc @@ -0,0 +1,92 @@ +#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>2) + 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; + +} diff --git a/Tennis/cpp/tennis3.cc b/Tennis/cpp/tennis3.cc new file mode 100644 index 00000000..cc87f969 --- /dev/null +++ b/Tennis/cpp/tennis3.cc @@ -0,0 +1,17 @@ +#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 && !(p1 == 3 && p2 == 3)) { + 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; + } +} \ No newline at end of file diff --git a/Tennis/cpp/test_suite.tests.cpp b/Tennis/cpp/test_suite.tests.cpp new file mode 100644 index 00000000..cdadddfc --- /dev/null +++ b/Tennis/cpp/test_suite.tests.cpp @@ -0,0 +1,55 @@ +#include "all_tests.hpp" +#include +#include + +typedef void test(); + +static test * tests[ ] = +{ + test_LoveAll_0_0, + test_FifteenAll_1_1, + test_ThirtyAll_2_2, + test_Deuce_3_3, + test_Deuce_4_4, + test_FifteenLove_1_0, + test_LoveFifteen_0_1, + test_ThirtyLove_2_0, + test_LoveThirty_0_2, + test_FortyLove_3_0, + test_LoveForty_0_3, + test_Winforplayer1_4_0, + test_Winforplayer2_0_4, + test_ThirtyFifteen_2_1, + test_FifteenThirty_1_2, + test_FortyFifteen_3_1, + test_FifteenForty_1_3, + test_Winforplayer1_4_1, + test_Winforplayer2_1_4, + test_FortyThirty_3_2, + test_ThirtyForty_2_3, + test_Winforplayer1_4_2, + test_Winforplayer2_2_4, + test_Advantageplayer1_4_3, + test_Advantageplayer2_3_4, + test_Advantageplayer1_5_4, + test_Advantageplayer2_4_5, + test_Advantageplayer1_15_14, + test_Advantageplayer2_14_15, + test_Winforplayer1_6_4, + test_Winforplayer2_4_6, + test_Winforplayer1_16_14, + test_Winforplayer2_14_16, + static_cast(0), +}; + +int main() +{ + size_t at = 0; + while (tests[at]) + { + tests[at++](); + std::cout << '.'; + } + std::cout << std::endl << at << " tests passed" << std::endl; + return 0; +}