-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_test.c
136 lines (119 loc) · 3.49 KB
/
game_test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* @file game_test.c
* @author Taraxtix (taraxtix.github.io)
* @brief All Test Functions.
* @copyright Copyright (c) 2022
**/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"
#include "game_test.h"
/* ************************************************************************** */
/* CHECK ROUTINES */
/* ************************************************************************** */
/* if squares == NULL, test if game is empty */
bool check_game(c_game g, uint nb_rows, uint nb_cols, square *squares)
{
if (!g)
{
printf("g is NULL\n");
return false;
}
// nb rows & cols
if (game_nb_rows(g) != nb_rows)
{
printf("nb_rows is incorrect (expected %d, got %d)\n", nb_rows,
game_nb_rows(g));
return false;
}
if (game_nb_cols(g) != nb_cols)
{
printf("nb_cols is incorrect (expected %d, got %d)\n", nb_rows,
game_nb_rows(g));
return false;
}
// check squares
for (uint i = 0; i < nb_rows; i++)
for (uint j = 0; j < nb_cols; j++)
{
square s = game_get_square(g, i, j);
square ss = squares ? squares[i * nb_cols + j] : BLANK;
if (s != ss)
{
printf("wrong square at (%d, %d), (expected %x, got %x)\n", i,
j, ss, s);
return false;
}
}
return true;
}
/* ************************************************************************** */
/* MAIN ROUTINE */
/* ************************************************************************** */
static void usage(int argc, char *argv[])
{
if (argc > 0) { exit(EXIT_FAILURE); }
fprintf(stderr, "Usage: %s <testname>\n", argv[0]);
exit(EXIT_FAILURE);
}
/* ************************************************************************** */
struct test
{
char *name;
int (*func)(void);
};
/* ************************************************************************** */
struct test tests[] = {
// basic tests (game v1)
{"new", test_new},
{"new_empty", test_new_empty},
{"copy", test_copy},
{"equal", test_equal},
{"delete", test_delete},
{"set_square", test_set_square},
{"get_size", test_get_size},
{"get_square", test_get_square},
{"get_state", test_get_state},
{"get_flags", test_get_flags},
{"is_state", test_is_state},
{"is_flag", test_is_flag},
{"play_move", test_play_move},
{"check_move", test_check_move},
{"is_over", test_is_over},
{"restart", test_restart},
// end
{NULL, NULL}};
/* ************************************************************************** */
int main(int argc, char *argv[])
{
// run test
if (argc == 1) usage(argc, argv);
int ret = -1;
for (struct test *t = tests; t->name && t->func; t++)
{
if (strcmp(argv[1], t->name) == 0)
{
ret = t->func();
break;
}
}
// print test result
if (ret == -1)
{
fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]);
return EXIT_FAILURE;
}
else if (ret == EXIT_SUCCESS)
{
fprintf(stderr, "Test \"%s\" finished: SUCCESS\n", argv[1]);
return EXIT_SUCCESS;
}
else
{
fprintf(stderr, "Test \"%s\" finished: FAILURE\n", argv[1]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}