aboutsummaryrefslogtreecommitdiff
path: root/tests/alg_tests.c
blob: 9ac1595804cc112cb762e83a7f318f45bab2163f (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "alg_tests.h"

bool testmethod_append_move(void *);
bool testmethod_new_alg(void *);
/*
bool testmethod_compose_alg(void *);
bool testmethod_inverse_alg(void *);
bool testmethod_on_inverse(void *);
bool testmethod_unniss(void *);
*/

typedef struct {
	Move *move;
	bool *inv;
	int len;
	Move m;
	bool inverse;
} append_move_t;

Move m_app1[] = {F, x, D3};
bool i_app1[] = {true, false, false};
append_move_t append_move_case1 = {
	.move = m_app1,
	.inv = i_app1,
	.len = 3,
	.m = L3,
	.inverse = false,
};

Move m_app2[] = {S, U, x2, M2};
bool i_app2[] = {true, false, true, true};
append_move_t append_move_case2 = {
	.move = m_app2,
	.inv = i_app2,
	.len = 4,
	.m = R,
	.inverse = true,
};

Move m_app3[] = {U, U, U, U, U};
bool i_app3[] = {false, false, false, false, false};
append_move_t append_move_case3 = {
	.move = m_app3,
	.inv = i_app3,
	.len = 5,
	.m = U,
	.inverse = false,
};

append_move_t *append_move_cases[] = {
	&append_move_case1,
	&append_move_case2,
	&append_move_case3,
};

Test test_append_move = {
	.name  = "Appending a move to and alg",
	.t     = testmethod_append_move,
	.cases = (void **)append_move_cases,
};

typedef struct {
	char *str;
	Move *move;
	bool *inv;
	int len;
	Move *move_normal;
	int len_normal;
	Move *move_inverse;
	int len_inverse;
} new_alg_t;

/* Alg F U B' (L3 D) x M (S) y3 */
Move m_new1[] = {F, U, B3, L3, D, x, M, S, y3};
Move mn_new1[] = {F, U, B3, x, M, y3};
Move mi_new1[] = {L3, D, S};
bool i_new1[] = {false, false, false, true, true, false, false, true, false};
new_alg_t new_alg_case1 = {
	.str = "F U B' (L3 D) x M (S) y3",
	.move = m_new1,
	.inv = i_new1,
	.len = 9,
	.move_normal = mn_new1,
	.len_normal = 6,
	.move_inverse = mi_new1,
	.len_inverse = 3,
};
new_alg_t *new_alg_cases[] = {&new_alg_case1};

Test test_new_alg = {
	.name  = "Initializing an alg from a string",
	.t     = testmethod_new_alg,
	.cases = (void **)new_alg_cases,
};

Test *alg_all_tests[] = {
	&test_append_move,
	&test_new_alg,
/*
	&test_append_alg,
	&test_compose_alg,
	&test_inverse_alg,
	&test_on_inverse,
	&test_unniss,
*/
	NULL
};
TestSuite alg_suite = {
	.setup    = NULL,
	.tests    = alg_all_tests,
	.teardown = NULL,
};

TestSuite *alg_suites[] = {
	&alg_suite,
	NULL
};

bool
testmethod_append_move(void *a)
{
	append_move_t *b = (append_move_t *)a;
	int li, ln;
	Alg *alg;

	/* Small to test reallocation */
	alg = malloc(sizeof(Alg));
	alg->allocated    = 5;
	alg->move         = malloc(alg->allocated * sizeof(Move));
	alg->inv          = malloc(alg->allocated * sizeof(bool));
	alg->len          = b->len;
	memcpy(alg->move, b->move, alg->len * sizeof(Move));
	memcpy(alg->inv,  b->inv,  alg->len * sizeof(bool));
	alg->move_normal  = malloc(alg->allocated * sizeof(Move));
	alg->move_inverse = malloc(alg->allocated * sizeof(Move));

	li = ln = 0;
	for (int i = 0; i < alg->len; i++) {
		if (alg->inv[i])
			alg->move_inverse[li++] = alg->move[i];
		else
			alg->move_normal[ln++] = alg->move[i];
	}
	alg->len_inverse = li;
	alg->len_normal = ln;

	append_move(alg, b->m, b->inverse);

	if (alg->len != b->len + 1) {
		printf("Alg has wrong len (%d instead of %d)\n",
			alg->len, b->len + 1);
		goto append_move_fail;
	}
	if (alg->move[alg->len-1] != b->m) {
		printf("Wrong last move (%s instead of %s)\n",
			move_string(alg->move[alg->len-1]),
			move_string(b->m));
		goto append_move_fail;
	}
	if (alg->inv[alg->len-1] != b->inverse) {
		printf("Wrong inverse flag for last move "
			"(%s instead of %s)\n",
			b->inverse ? "normal" : "inverse",
			b->inverse ? "inverse" : "normal");
		goto append_move_fail;
	}
	if (b->inverse) {
		if (alg->len_inverse != li + 1 ||
		    alg->len_normal  != ln) {
			printf("%d moves on normal (should be %d)"
			       " and %d on inverse (should be %d)\n",
			       alg->len_normal, ln,
			       alg->len_inverse, li + 1);
			goto append_move_fail;
		}
		if (alg->move_inverse[alg->len_inverse-1] != b->m) {
			printf("Wrong move on inverse (%s instead of %s)\n",
				move_string(alg->move_inverse[alg->len-1]),
				move_string(b->m));
			goto append_move_fail;
		}
	} else {
		if (alg->len_inverse != li ||
		    alg->len_normal  != ln + 1) {
			printf("%d moves on normal (should be %d)"
			       " and %d on inverse (should be %d)\n",
			       alg->len_normal, ln,
			       alg->len_inverse, li + 1);
			goto append_move_fail;
		}
		if (alg->move_normal[alg->len_normal-1] != b->m) {
			printf("Wrong move on normal (%s instead of %s)\n",
				move_string(alg->move_normal[alg->len-1]),
				move_string(b->m));
			goto append_move_fail;
		}
	}

	free(alg);
	return true;

append_move_fail:
	free(alg);
	return false;
}

bool
testmethod_new_alg(void *a)
{
	new_alg_t *b = (new_alg_t *)a;
	Alg *alg = new_alg(b->str);

	if (alg->len != b->len) {
		printf("Algs have different length (%d instead of %d)\n",
			alg->len, b->len);
		goto new_alg_fail;
	}

	for (int i = 0; i < alg->len; i++) {
		if (alg->move[i] != b->move[i] || alg->inv[i] != b->inv[i]) {
			printf("Algs differ on move %d\n", i);
			printf("Expected: %s\nActual:   ", b->str);
			print_alg(alg, false);
			goto new_alg_fail;
		}
	}

	if (alg->len_normal != b->len_normal) {
		printf("Algs have different number of moves on normal"
			" (%d instead of %d)\n",
			alg->len_normal, b->len_normal);
		goto new_alg_fail;
	}
	for (int i = 0; i < alg->len_normal; i++) {
		if (alg->move_normal[i] != b->move_normal[i]) {
			printf("Algs have different move %d on normal"
				" (%s instead of %s)\n", i,
				move_string(alg->move_normal[i]),
				move_string(b->move_normal[i]));
			goto new_alg_fail;
		}
	}

	if (alg->len_inverse != b->len_inverse) {
		printf("Algs have different number of moves on inverse"
			" (%d instead of %d)\n",
			alg->len_inverse, b->len_inverse);
		goto new_alg_fail;
	}
	for (int i = 0; i < alg->len_inverse; i++) {
		if (alg->move_inverse[i] != b->move_inverse[i]) {
			printf("Algs have different move %d on inverse:\n"
				" (%s instead of %s)\n", i,
				move_string(alg->move_inverse[i]),
				move_string(b->move_inverse[i]));
			goto new_alg_fail;
		}
	}

	free(alg);
	return true;

new_alg_fail:
	free(alg);
	return false;
}

Generated with cgit - Back to sebastiano.tronto.net