-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_test.c
152 lines (125 loc) · 4.2 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @file game_test.c
* @brief Game Tests.
* @copyright University of Bordeaux. All rights reserved, 2021.
*
**/
#include "game.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game_ext.h"
#include "game_test.h"
/* ************************************************************************** */
/* CHECK ROUTINES */
/* ************************************************************************** */
/* if squares == NULL, test if game is empty */
bool check_game_ext(cgame g, uint nb_rows, uint nb_cols, square* squares, bool wrapping)
{
if (!g) return false;
// nb rows & cols
if (game_nb_rows(g) != nb_rows) return false;
if (game_nb_cols(g) != nb_cols) return false;
// options
if (game_is_wrapping(g) != wrapping) 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] : S_BLANK;
if (s != ss) return false;
}
return true;
}
/* ************************************************************************** */
bool check_game(game g, square* squares) { return check_game_ext(g, DEFAULT_SIZE, DEFAULT_SIZE, squares, false); }
/* ************************************************************************** */
/* MAIN ROUTINE */
/* ************************************************************************** */
static void usage(int argc, char* argv[])
{
argc = argc;
assert(argc > 0);
fprintf(stderr, "Usage: %s <testname>\n", argv[0]);
exit(EXIT_FAILURE);
}
/* ************************************************************************** */
struct test {
char* name;
int (*func)(void);
};
/* ************************************************************************** */
struct test tests[] = { // dummy test
// 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_square", test_get_square},
{"get_state", test_get_state},
{"get_flags", test_get_flags},
{"is_state", test_is_state},
{"has_flag", test_has_flag},
{"play_move", test_play_move},
{"check_move", test_check_move},
{"update_flags", test_update_flags},
{"is_over", test_is_over},
{"restart", test_restart},
// aux tests
{"default", test_default},
{"default_solution", test_default_solution},
{"print", test_print},
// ext tests (game v2)
/* rectangular board */
{"new_ext", test_new_ext},
{"new_empty_ext", test_new_empty_ext},
{"equal_ext", test_equal_ext},
{"copy_ext", test_copy_ext},
{"game_1d", test_game_1d},
/* wrapping option */
{"game_wrapping_2x2", test_game_wrapping_2x2},
{"game_wrapping_3x3", test_game_wrapping_3x3},
{"game_wrapping_5x3", test_game_wrapping_5x3},
{"game_wrapping_error", test_game_wrapping_error},
/* undo & redo */
{"undo_one", test_undo_one},
{"undo_redo_some", test_undo_redo_some},
{"undo_redo_all", test_undo_redo_all},
{"restart_undo", test_restart_undo},
/* load & save */
{"load", test_load},
{"save", test_save},
/* solve & nb_solutions*/
{"solve", test_game_solve},
{"solutions", test_game_nb_solutions},
// 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 == 0) {
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;
}
/* ************************************************************************** */