forked from emilybache/Tennis-Refactoring-Kata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTennisTest.c
106 lines (91 loc) · 2.92 KB
/
TennisTest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <cmocka.h>
#include "TennisGame.h"
struct Parameter
{
int player1Score;
int player2Score;
const char* expectedScore;
};
static int maxScore(int a, int b)
{
return a > b ? a : b;
}
static void checkScore(void** state)
{
const struct Parameter* param = (struct Parameter*)cast_to_pointer_integral_type(*state);
struct TennisGame* game = TennisGame_Create("player1", "player2");
for (int i = 0; i < maxScore(param->player1Score, param->player2Score); i++)
{
if (i < param->player1Score)
TennisGame_WonPoint(game, "player1");
if (i < param->player2Score)
TennisGame_WonPoint(game, "player2");
}
assert_string_equal(param->expectedScore, TennisGame_GetScore(game));
free(game);
}
static void checkRealisticTennisGame(void** state)
{
const char* points[] = { "player1", "player1", "player2",
"player2", "player1", "player1" };
const char* expectedScores[] = { "Fifteen-Love", "Thirty-Love", "Thirty-Fifteen",
"Thirty-All", "Forty-Thirty", "Win for player1" };
struct TennisGame* game = TennisGame_Create("player1", "player2");
for (int i = 0; i < 6; i++)
{
TennisGame_WonPoint(game, points[i]);
assert_string_equal(expectedScores[i], TennisGame_GetScore(game));
}
free(game);
}
int main(void)
{
struct Parameter param[] =
{
{ 0, 0, "Love-All" },
{ 1, 1, "Fifteen-All" },
{ 2, 2, "Thirty-All" },
{ 3, 3, "Deuce" },
{ 4, 4, "Deuce" },
{ 1, 0, "Fifteen-Love" },
{ 0, 1, "Love-Fifteen" },
{ 2, 0, "Thirty-Love" },
{ 0, 2, "Love-Thirty" },
{ 3, 0, "Forty-Love" },
{ 0, 3, "Love-Forty" },
{ 4, 0, "Win for player1" },
{ 0, 4, "Win for player2" },
{ 2, 1, "Thirty-Fifteen" },
{ 1, 2, "Fifteen-Thirty" },
{ 3, 1, "Forty-Fifteen" },
{ 1, 3, "Fifteen-Forty" },
{ 4, 1, "Win for player1" },
{ 1, 4, "Win for player2" },
{ 3, 2, "Forty-Thirty" },
{ 2, 3, "Thirty-Forty" },
{ 4, 2, "Win for player1" },
{ 2, 4, "Win for player2" },
{ 4, 3, "Advantage player1" },
{ 3, 4, "Advantage player2" },
{ 5, 4, "Advantage player1" },
{ 4, 5, "Advantage player2" },
{ 15, 14, "Advantage player1" },
{ 14, 15, "Advantage player2" },
{ 6, 4, "Win for player1" },
{ 4, 6, "Win for player2" },
{ 16, 14, "Win for player1" },
{ 14, 16, "Win for player2" }, };
struct CMUnitTest TennisTests[sizeof param / sizeof param[0] + 1] =
{ cmocka_unit_test(checkRealisticTennisGame) };
for (int i = 0; i < (sizeof param / sizeof param[0]); i++)
{
TennisTests[i + 1].name = param[i].expectedScore;
TennisTests[i + 1].test_func = checkScore;
TennisTests[i + 1].initial_state = ¶m[i];
}
return cmocka_run_group_tests(TennisTests, NULL, NULL);
}