excit.c 19.1 KB
Newer Older
1 2 3 4 5
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "excit.h"

6 7
#define ES EXCIT_SUCCESS

Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
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
#define TLEAF_DEPTH 3
#define TLEAF_NSPLIT 2

void test_tleaf_iterator(void)
{
	excit_t it;
	ssize_t depth = TLEAF_DEPTH+1;
	ssize_t arities[(TLEAF_DEPTH)] = {3, 2, 4};
	ssize_t i = 0, value;
	ssize_t size = 1, it_size = 0;
	int err = EXCIT_SUCCESS;

	for (i = 0; i < depth-1; i++)
		size *= arities[i];

	it = excit_alloc(EXCIT_TLEAF);

	err = excit_tleaf_init(it, depth, arities,
			       TLEAF_POLICY_ROUND_ROBIN, NULL);
	assert(err == EXCIT_SUCCESS);

	err = excit_size(it, &it_size);
	assert(err == EXCIT_SUCCESS);

	assert(it_size == size);

	for (i = 0; i < size; i++) {
		err = excit_next(it, &value);
		assert(err == EXCIT_SUCCESS);
		assert(value == i);
	}
	err = excit_next(it, &value);
	assert(err == EXCIT_STOPIT);

	excit_t split[TLEAF_NSPLIT];

	for (i = 0; i < TLEAF_NSPLIT; i++)
		split[i] = excit_alloc(EXCIT_TLEAF);

Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
47
	err = tleaf_it_split(it, 2, TLEAF_NSPLIT, split);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
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
	assert(err == EXCIT_SUCCESS);

	err = excit_rewind(split[1]);
	assert(err == EXCIT_SUCCESS);

	err = excit_size(split[1], &it_size);
	assert(err == EXCIT_SUCCESS);

	assert(it_size == size/TLEAF_NSPLIT);

	err = excit_next(split[1], &value);
	assert(err == EXCIT_SUCCESS);
	assert(value == 2);
	err = excit_next(split[1], &value);
	assert(err == EXCIT_SUCCESS);
	assert(value == 3);
	err = excit_next(split[1], &value);
	assert(err == EXCIT_SUCCESS);
	assert(value == 6);
	err = excit_next(split[1], &value);
	assert(err == EXCIT_SUCCESS);
	assert(value == 7);
	err = excit_next(split[1], &value);
	assert(err == EXCIT_SUCCESS);
	assert(value == 10);
	err = excit_next(split[1], &value);
	assert(err == EXCIT_SUCCESS);
	assert(value == 11);

	for (i = 0; i < TLEAF_NSPLIT; i++)
		excit_free(split[i]);

	excit_free(it);
}

83 84 85
void test_range_iterator(void)
{
	excit_t it;
86 87 88
	ssize_t dim;
	ssize_t size;
	ssize_t indexes[1];
89 90 91 92
	enum excit_type_e type;
	excit_t its[3];
	int looped;
	int i;
93
	ssize_t ith;
94 95 96

	it = excit_alloc(EXCIT_RANGE);
	assert(it != NULL);
97
	assert(excit_type(it, &type) == ES);
98
	assert(type == EXCIT_RANGE);
99
	assert(excit_dimension(it, &dim) == ES);
100 101
	assert(dim == 0);

102 103
	assert(excit_range_init(it, 0, 3, 1) == ES);
	assert(excit_dimension(it, &dim) == ES);
104
	assert(dim == 1);
105
	assert(excit_size(it, &size) == ES);
106 107 108
	assert(size == 4);

	for (i = 0; i < 4; i++) {
109
		assert(excit_nth(it, i, indexes) == ES);
110
		assert(indexes[0] == i);
111
		assert(excit_rank(it, indexes, &ith) == ES);
112 113
		assert(ith == i);
		ith = -1;
114
		assert(excit_pos(it, &ith) == ES);
115
		assert(ith == i);
116
		assert(excit_peek(it, indexes) == ES);
117
		assert(indexes[0] == i);
118
		assert(excit_next(it, indexes) == ES);
119 120
		assert(indexes[0] == i);
	}
Brice Videau's avatar
Brice Videau committed
121 122
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
	assert(excit_peek(it, indexes) == EXCIT_STOPIT);
123

124
	assert(excit_rewind(it) == ES);
125 126 127
	looped = 0;
	i = 0;
	while (!looped) {
128
		assert(excit_cyclic_next(it, indexes, &looped) == ES);
129 130 131
		assert(indexes[0] == i);
		i++;
	}
132
	assert(excit_peek(it, indexes) == ES);
133
	assert(indexes[0] == 0);
134
	assert(excit_cyclic_next(it, indexes, &looped) == ES);
135 136 137 138
	assert(indexes[0] == 0);
	assert(looped == 0);

	for (i = 1; i < 4; i++) {
139
		assert(excit_next(it, indexes) == ES);
140 141
		assert(indexes[0] == i);
	}
Brice Videau's avatar
Brice Videau committed
142 143
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
144
	assert(excit_cyclic_next(it, indexes, &looped) == ES);
145 146 147
	assert(indexes[0] == 0);
	assert(looped == 1);

148 149
	assert(excit_range_init(it, 3, 0, -1) == ES);
	assert(excit_size(it, &size) == ES);
150 151 152
	assert(size == 4);

	for (i = 3; i >= 0; i--) {
153
		assert(excit_next(it, indexes) == ES);
154 155
		assert(indexes[0] == i);
	}
Brice Videau's avatar
Brice Videau committed
156
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
157

158 159
	assert(excit_range_init(it, 3, 0, 1) == ES);
	assert(excit_size(it, &size) == ES);
160 161
	assert(size == 0);

162 163
	assert(excit_range_init(it, 0, 9, 1) == ES);
	assert(excit_size(it, &size) == ES);
164
	assert(size == 10);
165 166
	assert(excit_split(it, 3, its) == ES);
	assert(excit_size(its[0], &size) == ES);
167 168
	assert(size == 4);
	for (int i = 0; i < 4; i++) {
169
		assert(excit_next(its[0], indexes) == ES);
170 171
		assert(indexes[0] == i);
	}
172
	assert(excit_size(its[1], &size) == ES);
173 174
	assert(size == 3);
	for (int i = 4; i < 7; i++) {
175
		assert(excit_next(its[1], indexes) == ES);
176 177
		assert(indexes[0] == i);
	}
178
	assert(excit_size(its[2], &size) == ES);
179 180
	assert(size == 3);
	for (int i = 7; i < 10; i++) {
181
		assert(excit_next(its[2], indexes) == ES);
182 183 184 185 186 187
		assert(indexes[0] == i);
	}
	excit_free(its[0]);
	excit_free(its[1]);
	excit_free(its[2]);

Brice Videau's avatar
Brice Videau committed
188 189 190
	assert(excit_split(it, 24, its) == -EXCIT_EDOM);
	assert(excit_split(it, 0, its) == -EXCIT_EDOM);
	assert(excit_split(it, -1, its) == -EXCIT_EDOM);
191 192 193 194 195 196 197 198 199
	excit_free(it);
}

void test_product_iterators(void)
{
	int i, j, k;
	int looped;
	excit_t it;
	excit_t tmp;
200 201 202 203 204
	ssize_t dim;
	ssize_t count;
	ssize_t size;
	ssize_t indexes[3];
	ssize_t indexes2[3];
205 206
	enum excit_type_e type;
	excit_t its[5];
207
	ssize_t ith;
208 209 210

	it = excit_alloc(EXCIT_PRODUCT);
	assert(it != NULL);
211
	assert(excit_type(it, &type) == ES);
212
	assert(type == EXCIT_PRODUCT);
213
	assert(excit_dimension(it, &dim) == ES);
214
	assert(dim == 0);
215
	assert(excit_product_count(it, &count) == ES);
216
	assert(count == 0);
217
	assert(excit_size(it, &size) == ES);
218
	assert(size == 0);
Brice Videau's avatar
Brice Videau committed
219
	assert(excit_peek(it, indexes) == -EXCIT_EINVAL);
220 221 222

	tmp = excit_alloc(EXCIT_RANGE);
	excit_range_init(tmp, 0, 3, 1);
223 224
	assert(excit_product_add(it, tmp) == ES);
	assert(excit_dimension(it, &dim) == ES);
225
	assert(dim == 1);
226
	assert(excit_product_count(it, &count) == ES);
227
	assert(count == 1);
228
	assert(excit_size(it, &size) == ES);
229
	assert(size == 4);
230
	assert(excit_peek(it, indexes) == ES);
231 232 233 234
	assert(indexes[0] == 0);

	tmp = excit_alloc(EXCIT_RANGE);
	excit_range_init(tmp, 1, -1, -1);
235 236
	assert(excit_product_add(it, tmp) == ES);
	assert(excit_dimension(it, &dim) == ES);
237
	assert(dim == 2);
238
	assert(excit_product_count(it, &count) == ES);
239
	assert(count == 2);
240
	assert(excit_size(it, &size) == ES);
241
	assert(size == 3 * 4);
242
	assert(excit_peek(it, indexes) == ES);
243 244 245 246 247 248
	assert(indexes[0] == 0);
	assert(indexes[1] == 1);

	k = 0;
	for (i = 0; i <= 3; i++) {
		for (j = 1; j >= -1; j--, k++) {
249
			assert(excit_nth(it, k, indexes) == ES);
250 251
			assert(indexes[0] == i);
			assert(indexes[1] == j);
252
			assert(excit_pos(it, &ith) == ES);
253 254
			assert(ith == k);
			ith = -1;
255
			assert(excit_rank(it, indexes, &ith) == ES);
256
			assert(ith == k);
257
			assert(excit_peek(it, indexes) == ES);
258 259
			assert(indexes[0] == i);
			assert(indexes[1] == j);
260
			assert(excit_next(it, indexes) == ES);
261 262 263 264
			assert(indexes[0] == i);
			assert(indexes[1] == j);
		}
	}
Brice Videau's avatar
Brice Videau committed
265 266 267
	assert(excit_peek(it, indexes) == EXCIT_STOPIT);
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
268
	assert(excit_cyclic_next(it, indexes, &looped) == ES);
269 270 271 272
	assert(looped == 1);
	assert(indexes[0] == 0);
	assert(indexes[1] == 1);

273
	assert(excit_rewind(it) == ES);
274 275 276 277
	for (k = 0; k < 3; k++) {
		for (i = 0; i <= 3; i++) {
			for (j = 1; j >= -1; j--) {
				assert(excit_cyclic_next
278
				       (it, indexes, &looped) == ES);
279 280 281 282 283 284 285 286 287 288 289
				assert(indexes[0] == i);
				assert(indexes[1] == j);
			}
		}
		assert(looped == 1);
	}

	excit_rewind(it);

	tmp = excit_alloc(EXCIT_RANGE);
	excit_range_init(tmp, -5, 5, 1);
290 291
	assert(excit_product_add(it, tmp) == ES);
	assert(excit_dimension(it, &dim) == ES);
292
	assert(dim == 3);
293
	assert(excit_size(it, &size) == ES);
294 295 296
	assert(size == 3 * 4 * 11);

	//split first dimension
297
	assert(excit_product_split_dim(it, 0, 2, its) == ES);
298 299 300
	for (i = 0; i <= 1; i++) {
		for (j = 1; j >= -1; j--) {
			for (k = -5; k <= 5; k++) {
301
				assert(excit_next(its[0], indexes) == ES);
302 303 304 305 306 307
				assert(indexes[0] == i);
				assert(indexes[1] == j);
				assert(indexes[2] == k);
			}
		}
	}
Brice Videau's avatar
Brice Videau committed
308
	assert(excit_next(its[0], indexes) == EXCIT_STOPIT);
309 310 311
	for (i = 2; i <= 3; i++) {
		for (j = 1; j >= -1; j--) {
			for (k = -5; k <= 5; k++) {
312
				assert(excit_next(its[1], indexes) == ES);
313 314 315 316 317 318
				assert(indexes[0] == i);
				assert(indexes[1] == j);
				assert(indexes[2] == k);
			}
		}
	}
Brice Videau's avatar
Brice Videau committed
319
	assert(excit_next(its[1], indexes) == EXCIT_STOPIT);
320 321 322 323
	excit_free(its[0]);
	excit_free(its[1]);

	//split second dimension
324
	assert(excit_product_split_dim(it, 1, 2, its) == ES);
325 326 327
	for (i = 0; i <= 3; i++) {
		for (j = 1; j >= 0; j--) {
			for (k = -5; k <= 5; k++) {
328
				assert(excit_next(its[0], indexes) == ES);
329 330 331 332 333 334
				assert(indexes[0] == i);
				assert(indexes[1] == j);
				assert(indexes[2] == k);
			}
		}
	}
Brice Videau's avatar
Brice Videau committed
335
	assert(excit_next(its[0], indexes) == EXCIT_STOPIT);
336 337 338
	for (i = 0; i <= 3; i++) {
		for (j = -1; j >= -1; j--) {
			for (k = -5; k <= 5; k++) {
339
				assert(excit_next(its[1], indexes) == ES);
340 341 342 343 344 345
				assert(indexes[0] == i);
				assert(indexes[1] == j);
				assert(indexes[2] == k);
			}
		}
	}
Brice Videau's avatar
Brice Videau committed
346
	assert(excit_next(its[1], indexes) == EXCIT_STOPIT);
347 348 349 350
	excit_free(its[0]);
	excit_free(its[1]);

	//split third dimension
351
	assert(excit_product_split_dim(it, 2, 2, its) == ES);
352 353 354
	for (i = 0; i <= 3; i++) {
		for (j = 1; j >= -1; j--) {
			for (k = -5; k <= 0; k++) {
355
				assert(excit_next(its[0], indexes) == ES);
356 357 358 359 360 361
				assert(indexes[0] == i);
				assert(indexes[1] == j);
				assert(indexes[2] == k);
			}
		}
	}
Brice Videau's avatar
Brice Videau committed
362
	assert(excit_next(its[0], indexes) == EXCIT_STOPIT);
363 364 365
	for (i = 0; i <= 3; i++) {
		for (j = 1; j >= -1; j--) {
			for (k = 1; k <= 5; k++) {
366
				assert(excit_next(its[1], indexes) == ES);
367 368 369 370 371 372
				assert(indexes[0] == i);
				assert(indexes[1] == j);
				assert(indexes[2] == k);
			}
		}
	}
Brice Videau's avatar
Brice Videau committed
373
	assert(excit_next(its[1], indexes) == EXCIT_STOPIT);
374 375 376
	excit_free(its[0]);
	excit_free(its[1]);

Brice Videau's avatar
Brice Videau committed
377
	assert(excit_product_split_dim(it, 2, 15, its) == -EXCIT_EDOM);
378

379
	assert(excit_split(it, 5, its) == ES);
380

381
	assert(excit_rewind(it) == ES);
382
	for (i = 0; i < 5; i++) {
383 384
		while (excit_next(its[i], indexes) == ES) {
			assert(excit_next(it, indexes2) == ES);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
			assert(indexes[0] == indexes2[0]);
			assert(indexes[1] == indexes2[1]);
			assert(indexes[2] == indexes2[2]);
		}
	}

	for (i = 0; i < 5; i++)
		excit_free(its[i]);
	excit_free(it);

}

void test_repeat_iterator(void)
{
	excit_t it;
	excit_t tmp;
401 402
	ssize_t dim;
	ssize_t size;
403
	enum excit_type_e type;
404
	ssize_t indexes[1];
405
	excit_t its[2];
406
	ssize_t ith;
407 408 409

	it = excit_alloc(EXCIT_REPEAT);
	assert(it != NULL);
410
	assert(excit_type(it, &type) == ES);
411
	assert(type == EXCIT_REPEAT);
412
	assert(excit_dimension(it, &dim) == ES);
413 414 415 416 417
	assert(dim == 0);

	tmp = excit_alloc(EXCIT_RANGE);
	excit_range_init(tmp, 0, 2, 1);
	excit_repeat_init(it, tmp, 2);
418
	assert(excit_dimension(it, &dim) == ES);
419
	assert(dim == 1);
420
	assert(excit_size(it, &size) == ES);
421 422 423 424
	assert(size == 6);

	for (int i = 0, k = 0; i <= 2; i++) {
		for (int j = 0; j < 2; j++, k++) {
425
			assert(excit_nth(it, k, indexes) == ES);
426
			assert(indexes[0] == i);
427
			assert(excit_pos(it, &ith) == ES);
428
			assert(ith == k);
429
			assert(excit_peek(it, indexes) == ES);
430
			assert(indexes[0] == i);
431
			assert(excit_next(it, indexes) == ES);
432 433 434
			assert(indexes[0] == i);
		}
	}
Brice Videau's avatar
Brice Videau committed
435 436
	assert(excit_peek(it, indexes) == EXCIT_STOPIT);
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
437

438
	assert(excit_rewind(it) == ES);
439 440
	for (int i = 0; i <= 2; i++) {
		for (int j = 0; j < 2; j++) {
441
			assert(excit_peek(it, indexes) == ES);
442
			assert(indexes[0] == i);
443
			assert(excit_next(it, indexes) == ES);
444 445 446
			assert(indexes[0] == i);
		}
	}
Brice Videau's avatar
Brice Videau committed
447 448
	assert(excit_peek(it, indexes) == EXCIT_STOPIT);
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
449

450
	assert(excit_repeat_split(it, 2, its) == ES);
451 452
	for (int i = 0; i <= 1; i++) {
		for (int j = 0; j < 2; j++) {
453
			assert(excit_peek(its[0], indexes) == ES);
454
			assert(indexes[0] == i);
455
			assert(excit_next(its[0], indexes) == ES);
456 457 458
			assert(indexes[0] == i);
		}
	}
Brice Videau's avatar
Brice Videau committed
459 460
	assert(excit_peek(its[0], indexes) == EXCIT_STOPIT);
	assert(excit_next(its[0], indexes) == EXCIT_STOPIT);
461 462
	for (int i = 2; i <= 2; i++) {
		for (int j = 0; j < 2; j++) {
463
			assert(excit_peek(its[1], indexes) == ES);
464
			assert(indexes[0] == i);
465
			assert(excit_next(its[1], indexes) == ES);
466 467 468
			assert(indexes[0] == i);
		}
	}
Brice Videau's avatar
Brice Videau committed
469 470
	assert(excit_peek(its[1], indexes) == EXCIT_STOPIT);
	assert(excit_next(its[1], indexes) == EXCIT_STOPIT);
471 472 473 474 475 476 477 478 479 480

	excit_free(its[0]);
	excit_free(its[1]);
	excit_free(it);
}

void test_cons_iterator(void)
{
	excit_t it;
	excit_t tmp;
481 482
	ssize_t dim;
	ssize_t size;
483
	enum excit_type_e type;
484
	ssize_t indexes[4];
485
	excit_t its[2];
486
	ssize_t ith;
487 488 489

	it = excit_alloc(EXCIT_CONS);
	assert(it != NULL);
490
	assert(excit_type(it, &type) == ES);
491
	assert(type == EXCIT_CONS);
492
	assert(excit_dimension(it, &dim) == ES);
493 494 495 496
	assert(dim == 0);

	tmp = excit_alloc(EXCIT_RANGE);
	excit_range_init(tmp, 0, 4, 1);
497 498
	assert(excit_cons_init(it, tmp, 3) == ES);
	assert(excit_dimension(it, &dim) == ES);
499
	assert(dim == 3);
500
	assert(excit_size(it, &size) == ES);
501 502 503
	assert(size == 3);
	for (int j = 0; j < 2; j++) {
		for (int i = 0, k = 0; i < 3; i++, k++) {
504
			assert(excit_nth(it, k, indexes) == ES);
505 506 507
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
508
			assert(excit_pos(it, &ith) == ES);
509 510
			assert(ith == k);
			ith = -1;
511
			assert(excit_rank(it, indexes, &ith) == ES);
512
			assert(ith == k);
513
			assert(excit_peek(it, indexes) == ES);
514 515 516
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
517
			assert(excit_next(it, indexes) == ES);
518 519 520 521
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
		}
Brice Videau's avatar
Brice Videau committed
522 523
		assert(excit_peek(it, indexes) == EXCIT_STOPIT);
		assert(excit_next(it, indexes) == EXCIT_STOPIT);
524
		assert(excit_rewind(it) == ES);
525 526
	}

527
	assert(excit_split(it, 2, its) == ES);
528 529 530

	for (int j = 0; j < 2; j++) {
		for (int i = 0, k = 0; i < 2; i++, k++) {
531
			assert(excit_nth(its[0], k, indexes) == ES);
532 533 534
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
535
			assert(excit_peek(its[0], indexes) == ES);
536 537 538
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
539
			assert(excit_next(its[0], indexes) == ES);
540 541 542 543
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
		}
Brice Videau's avatar
Brice Videau committed
544 545
		assert(excit_peek(its[0], indexes) == EXCIT_STOPIT);
		assert(excit_next(its[0], indexes) == EXCIT_STOPIT);
546
		assert(excit_rewind(its[0]) == ES);
547 548 549 550
	}

	for (int j = 0; j < 2; j++) {
		for (int i = 2, k = 0; i < 3; i++, k++) {
551
			assert(excit_nth(its[1], k, indexes) == ES);
552 553 554
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
555
			assert(excit_peek(its[1], indexes) == ES);
556 557 558
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
559
			assert(excit_next(its[1], indexes) == ES);
560 561 562 563
			assert(indexes[0] == i);
			assert(indexes[1] == i + 1);
			assert(indexes[2] == i + 2);
		}
Brice Videau's avatar
Brice Videau committed
564 565
		assert(excit_peek(its[1], indexes) == EXCIT_STOPIT);
		assert(excit_next(its[1], indexes) == EXCIT_STOPIT);
566
		assert(excit_rewind(its[1]) == ES);
567 568 569 570 571 572 573 574 575 576 577 578 579 580
	}
	excit_free(its[0]);
	excit_free(its[1]);
	excit_free(it);

	it = excit_alloc(EXCIT_PRODUCT);
	tmp = excit_alloc(EXCIT_RANGE);
	excit_range_init(tmp, 0, 3, 1);
	excit_product_add(it, tmp);
	tmp = excit_alloc(EXCIT_RANGE);
	excit_range_init(tmp, 1, -1, -1);
	excit_product_add(it, tmp);

	tmp = excit_alloc(EXCIT_CONS);
581 582
	assert(excit_cons_init(tmp, it, 2) == ES);
	assert(excit_dimension(tmp, &dim) == ES);
583
	assert(dim == 4);
584
	assert(excit_size(tmp, &size) == ES);
585
	assert(size == 11);
586
	assert(excit_peek(tmp, indexes) == ES);
587 588 589 590
	assert(indexes[0] == 0);
	assert(indexes[1] == 1);
	assert(indexes[2] == 0);
	assert(indexes[3] == 0);
591 592 593 594 595 596 597 598 599 600
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
	assert(excit_next(tmp, indexes) == ES);
601 602 603 604
	assert(indexes[0] == 3);
	assert(indexes[1] == 1);
	assert(indexes[2] == 3);
	assert(indexes[3] == 0);
605
	assert(excit_next(tmp, indexes) == ES);
606 607 608 609
	assert(indexes[0] == 3);
	assert(indexes[1] == 0);
	assert(indexes[2] == 3);
	assert(indexes[3] == -1);
Brice Videau's avatar
Brice Videau committed
610
	assert(excit_next(tmp, indexes) == EXCIT_STOPIT);
611 612 613 614 615 616 617

	excit_free(tmp);

}

void test_hilbert2d_iterator(void)
{
618 619 620 621
	ssize_t dim;
	ssize_t size;
	ssize_t indexes[2];
	ssize_t indexes2[2];
622 623 624
	enum excit_type_e type;
	excit_t it;
	excit_t its[3];
625
	ssize_t ith;
626 627 628

	it = excit_alloc(EXCIT_HILBERT2D);
	assert(it != NULL);
629
	assert(excit_type(it, &type) == ES);
630
	assert(type == EXCIT_HILBERT2D);
631
	assert(excit_dimension(it, &dim) == ES);
632
	assert(dim == 0);
633 634
	assert(excit_hilbert2d_init(it, 2) == ES);
	assert(excit_dimension(it, &dim) == ES);
635
	assert(dim == 2);
636
	assert(excit_size(it, &size) == ES);
637 638
	assert(size == 16);

639
	assert(excit_peek(it, indexes) == ES);
640 641
	assert(indexes[0] == 0);
	assert(indexes[1] == 0);
642
	assert(excit_next(it, indexes) == ES);
643 644
	assert(indexes[0] == 0);
	assert(indexes[1] == 0);
645
	assert(excit_next(it, indexes) == ES);
646 647
	assert(indexes[0] == 1);
	assert(indexes[1] == 0);
648
	assert(excit_next(it, indexes) == ES);
649 650
	assert(indexes[0] == 1);
	assert(indexes[1] == 1);
651
	assert(excit_pos(it, &ith) == ES);
652
	assert(ith == 3);
653
	assert(excit_next(it, indexes) == ES);
654 655
	assert(indexes[0] == 0);
	assert(indexes[1] == 1);
656
	assert(excit_next(it, indexes) == ES);
657 658
	assert(indexes[0] == 0);
	assert(indexes[1] == 2);
659
	assert(excit_next(it, indexes) == ES);
660 661
	assert(indexes[0] == 0);
	assert(indexes[1] == 3);
662
	assert(excit_next(it, indexes) == ES);
663 664
	assert(indexes[0] == 1);
	assert(indexes[1] == 3);
665
	assert(excit_next(it, indexes) == ES);
666 667
	assert(indexes[0] == 1);
	assert(indexes[1] == 2);
668
	assert(excit_next(it, indexes) == ES);
669 670
	assert(indexes[0] == 2);
	assert(indexes[1] == 2);
671
	assert(excit_next(it, indexes) == ES);
672 673
	assert(indexes[0] == 2);
	assert(indexes[1] == 3);
674
	assert(excit_next(it, indexes) == ES);
675 676
	assert(indexes[0] == 3);
	assert(indexes[1] == 3);
677
	assert(excit_next(it, indexes) == ES);
678 679
	assert(indexes[0] == 3);
	assert(indexes[1] == 2);
680
	assert(excit_next(it, indexes) == ES);
681 682
	assert(indexes[0] == 3);
	assert(indexes[1] == 1);
683
	assert(excit_next(it, indexes) == ES);
684 685
	assert(indexes[0] == 2);
	assert(indexes[1] == 1);
686
	assert(excit_next(it, indexes) == ES);
687 688
	assert(indexes[0] == 2);
	assert(indexes[1] == 0);
689
	assert(excit_next(it, indexes) == ES);
690 691
	assert(indexes[0] == 3);
	assert(indexes[1] == 0);
Brice Videau's avatar
Brice Videau committed
692 693
	assert(excit_peek(it, indexes) == EXCIT_STOPIT);
	assert(excit_next(it, indexes) == EXCIT_STOPIT);
694 695
	assert(excit_rewind(it) == ES);
	assert(excit_peek(it, indexes) == ES);
696 697
	assert(indexes[0] == 0);
	assert(indexes[1] == 0);
698 699 700
	assert(excit_peek(it, NULL) == ES);
	assert(excit_skip(it) == ES);
	assert(excit_peek(it, indexes) == ES);
701 702 703
	assert(indexes[0] == 1);
	assert(indexes[1] == 0);

704
	assert(excit_rewind(it) == ES);
705 706
	int j = 0;

707 708
	while (excit_next(it, indexes) == ES) {
		assert(excit_nth(it, j, indexes2) == ES);
709 710
		assert(indexes[0] == indexes2[0]);
		assert(indexes[1] == indexes2[1]);
711
		assert(excit_rank(it, indexes, &ith) == ES);
712 713 714 715
		assert(j == ith);
		j++;
	}

716 717
	assert(excit_split(it, 3, its) == ES);
	assert(excit_rewind(it) == ES);
718
	for (int i = 0; i < 3; i++) {
719 720
		while (excit_next(its[i], indexes) == ES) {
			assert(excit_next(it, indexes2) == ES);
721 722 723 724 725 726 727 728 729
			assert(indexes[0] == indexes2[0]);
			assert(indexes[1] == indexes2[1]);
		}
	}

	excit_free(its[0]);
	excit_free(its[1]);
	excit_free(its[2]);

Brice Videau's avatar
Brice Videau committed
730
	assert(excit_split(it, 17, its) == -EXCIT_EDOM);
731 732 733 734 735 736 737 738 739 740 741

	excit_free(it);
}

int main(int argc, char *argv[])
{
	test_range_iterator();
	test_product_iterators();
	test_cons_iterator();
	test_repeat_iterator();
	test_hilbert2d_iterator();
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
742
	test_tleaf_iterator();
743 744
	return 0;
}