mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 12:22:12 +00:00
test cases and code that work in cyber-dojo
This commit is contained in:
parent
5c951978d7
commit
7e52c0ddfe
171
Tennis/cpp/all_tests.cpp
Normal file
171
Tennis/cpp/all_tests.cpp
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
#include "all_tests.hpp"
|
||||||
|
|
||||||
|
/* change this to the version of tennis you want to work on */
|
||||||
|
#include "tennis1.cc"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
33
Tennis/cpp/all_tests.hpp
Normal file
33
Tennis/cpp/all_tests.hpp
Normal file
@ -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();
|
||||||
@ -53,6 +53,7 @@ def create_testcase_dicts():
|
|||||||
|
|
||||||
testcase_dicts = create_testcase_dicts()
|
testcase_dicts = create_testcase_dicts()
|
||||||
|
|
||||||
|
# This is not currently being used
|
||||||
gtest_template = """\
|
gtest_template = """\
|
||||||
TEST(TennisTest, %(testcase_name)s) {
|
TEST(TennisTest, %(testcase_name)s) {
|
||||||
EXPECT_EQ("%(score)s", tennis_score(%(p1Points)s, %(p2Points)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));
|
assert("%(score)s" == tennis_score(%(p1Points)s, %(p2Points)s));
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
# test cases
|
# test cases for all_tests.cpp
|
||||||
for test in testcase_dicts:
|
for test in testcase_dicts:
|
||||||
print template % test
|
print template % test
|
||||||
|
|
||||||
# all_tests.cpp
|
# test_suite.tests.cpp
|
||||||
for test in testcase_dicts:
|
for test in testcase_dicts:
|
||||||
print " test_%(testcase_name)s," % test
|
print " test_%(testcase_name)s," % test
|
||||||
|
|
||||||
|
|||||||
5
Tennis/cpp/makefile
Normal file
5
Tennis/cpp/makefile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
run.tests.output : run.tests
|
||||||
|
./run.tests
|
||||||
|
|
||||||
|
run.tests : *.cpp
|
||||||
|
g++ -Wall -Werror -O *.cpp -o run.tests
|
||||||
61
Tennis/cpp/tennis1.cc
Normal file
61
Tennis/cpp/tennis1.cc
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
92
Tennis/cpp/tennis2.cc
Normal file
92
Tennis/cpp/tennis2.cc
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
17
Tennis/cpp/tennis3.cc
Normal file
17
Tennis/cpp/tennis3.cc
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
55
Tennis/cpp/test_suite.tests.cpp
Normal file
55
Tennis/cpp/test_suite.tests.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include "all_tests.hpp"
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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<test*>(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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user