-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
168 lines (135 loc) · 4.37 KB
/
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "CUnit/Basic.h"
#include "board/board.h"
#include <CUnit/CUError.h>
#include <CUnit/TestDB.h>
#include <board.h>
#define BOARD_HEIGHT (6)
#define BOARD_WIDTH (6)
/**
* Compares the cell values of two boards. If equal returns 1.
* @param b1 First board
* @param b2 Second board
*/
int board_compare(Board *b1, Board *b2) {
if (b1->height != b2->height || b1->width != b2->width)
return 0;
for (int i = 0; i < b1->height; i++) {
for (int j = 0; j < b1->width; j++) {
if (b1->cell[i][j] != b2->cell[i][j])
return 0;
}
}
return 1;
}
/** Test only one cell without any neighbours */
void test_lone_cell(void){
Board *b_actual = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
B_set_alive(b_actual, 2, 2);
b_actual = B_update(b_actual);
CU_ASSERT_FALSE(B_is_alive(b_actual->cell[2][2]))
}
// Test the block position
void test_still_block(void) {
Board *b_actual = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
Board *b_expect = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
B_set_alive(b_actual, 1, 1);
B_set_alive(b_actual, 1, 2);
B_set_alive(b_actual, 2, 1);
B_set_alive(b_actual, 2, 2);
b_actual = B_update(b_actual);
B_set_alive(b_expect, 1, 1);
B_set_alive(b_expect, 1, 2);
B_set_alive(b_expect, 2, 1);
B_set_alive(b_expect, 2, 2);
CU_ASSERT(board_compare(b_actual, b_expect));
b_actual = B_update(b_actual);
CU_ASSERT(board_compare(b_actual, b_expect));
}
/** Test the blinker oscillation */
void test_blinker(void) {
Board *b_actual = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
Board *b_expect = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
B_set_alive(b_actual, 2, 1);
B_set_alive(b_actual, 2, 2);
B_set_alive(b_actual, 2, 3);
b_actual = B_update(b_actual);
B_set_alive(b_expect, 1, 2);
B_set_alive(b_expect, 2, 2);
B_set_alive(b_expect, 3, 2);
CU_ASSERT(board_compare(b_actual, b_expect));
// Make one period
b_actual = B_update(b_actual);
b_actual = B_update(b_actual);
CU_ASSERT(board_compare(b_actual, b_expect));
}
/** test the toad oscillation */
void test_toad (){
Board *b_actual = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
Board *b_expect = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
B_set_alive(b_actual, 2, 1);
B_set_alive(b_actual, 2, 2);
B_set_alive(b_actual, 2, 3);
B_set_alive(b_actual, 3, 0);
B_set_alive(b_actual, 3, 1);
B_set_alive(b_actual, 3, 2);
b_actual = B_update(b_actual);
B_set_alive(b_expect, 2, 1);
B_set_alive(b_expect, 3, 1);
B_set_alive(b_expect, 4, 2);
B_set_alive(b_expect, 1, 3);
B_set_alive(b_expect, 2, 4);
B_set_alive(b_expect, 3, 4);
CU_ASSERT(board_compare(b_actual, b_expect));
// Make one period
b_actual = B_update(b_actual);
b_actual = B_update(b_actual);
CU_ASSERT(board_compare(b_actual, b_expect));
}
void test_circular_box(){
Board *b_actual = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
Board *b_expect = B_new(BOARD_HEIGHT, BOARD_WIDTH, CIRCULAR);
B_set_alive(b_actual, 0, 0);
B_set_alive(b_actual, 0, 1);
B_set_alive(b_actual, b_actual->height - 1, 0);
B_set_alive(b_actual, b_actual->height - 1, 1);
b_actual = B_update(b_actual);
B_set_alive(b_expect, 0, 0);
B_set_alive(b_expect, 0, 1);
B_set_alive(b_expect, b_expect->height - 1, 0);
B_set_alive(b_expect, b_expect->height - 1, 1);
CU_ASSERT(board_compare(b_actual, b_expect));
}
int main(int argc, char **argv) {
if (CU_initialize_registry() != CUE_SUCCESS)
return CU_get_error();
// Add suite
CU_pSuite suite1 = CU_add_suite("board", NULL, NULL);
if (suite1 == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if (CU_add_test(suite1, "Testing if lone cell dies", test_lone_cell) == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if (CU_add_test(suite1, "Testing if still block", test_still_block) == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if(CU_add_test(suite1, "Testing if blinker works", test_blinker) == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if(CU_add_test(suite1, "Testing if toad works", test_blinker) == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if(CU_add_test(suite1, "Testing if box in circular works", test_circular_box) == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_number_of_failures();
}