excit_composition.c 2.69 KB
Newer Older
Brice Videau's avatar
Brice Videau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "excit.h"
#include "excit_test.h"

excit_t create_test_range(ssize_t start, ssize_t stop, ssize_t step)
{
	excit_t it;

	it = excit_alloc_test(EXCIT_RANGE);
	assert(excit_range_init(it, start, stop, step) == ES);
	return it;
}

18
void test_alloc_init_composition(excit_t source, excit_t indexer)
Brice Videau's avatar
Brice Videau committed
19
20
21
22
{
	excit_t it;
	ssize_t dim, expected_dim, size, expected_size;

23
	it = excit_alloc_test(EXCIT_COMPOSITION);
Brice Videau's avatar
Brice Videau committed
24
25
26
	assert(excit_dimension(it, &dim) == ES);
	assert(dim == 0);

27
	assert(excit_composition_init(it, excit_dup(source), excit_dup(indexer)) ==
Brice Videau's avatar
Brice Videau committed
28
	       ES);
Brice Videau's avatar
Brice Videau committed
29
30
31
32
33
34
35
36
37
38
	assert(excit_dimension(it, &dim) == ES);
	assert(excit_dimension(source, &expected_dim) == ES);
	assert(dim == expected_dim);
	assert(excit_size(it, &size) == ES);
	assert(excit_size(indexer, &expected_size) == ES);
	assert(size == expected_size);

	excit_free(it);
}

39
excit_t create_test_composition(excit_t source, excit_t indexer)
Brice Videau's avatar
Brice Videau committed
40
41
42
{
	excit_t it;

43
44
	it = excit_alloc_test(EXCIT_COMPOSITION);
	assert(excit_composition_init(it, excit_dup(source), excit_dup(indexer)) ==
Brice Videau's avatar
Brice Videau committed
45
	       ES);
Brice Videau's avatar
Brice Videau committed
46
47
48
	return it;
}

49
void test_next_composition(excit_t source, excit_t indexer)
Brice Videau's avatar
Brice Videau committed
50
{
51
	excit_t it = create_test_composition(source, indexer);
Brice Videau's avatar
Brice Videau committed
52
53
54
55
56
57

	excit_t iit = excit_dup(indexer);

	ssize_t *indexes1, *indexes2;
	ssize_t index;
	ssize_t dim;
Brice Videau's avatar
Brice Videau committed
58

Brice Videau's avatar
Brice Videau committed
59
	assert(excit_dimension(source, &dim) == ES);
Brice Videau's avatar
Brice Videau committed
60
61
	indexes1 = (ssize_t *) malloc(dim * sizeof(ssize_t));
	indexes2 = (ssize_t *) malloc(dim * sizeof(ssize_t));
Brice Videau's avatar
Brice Videau committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75

	while (excit_next(iit, &index) == ES) {
		assert(excit_nth(source, index, indexes2) == ES);
		assert(excit_next(it, indexes1) == ES);
		assert(memcmp(indexes1, indexes2, dim * sizeof(ssize_t)) == 0);
	}
	assert(excit_next(it, indexes1) == EXCIT_STOPIT);

	free(indexes1);
	free(indexes2);
	excit_free(it);
	excit_free(iit);
}

76
void test_composition_iterator(excit_t source, excit_t indexer)
Brice Videau's avatar
Brice Videau committed
77
{
78
	test_alloc_init_composition(source, indexer);
Brice Videau's avatar
Brice Videau committed
79

80
	test_next_composition(source, indexer);
Brice Videau's avatar
Brice Videau committed
81
82

	int i = 0;
Brice Videau's avatar
Brice Videau committed
83

Brice Videau's avatar
Brice Videau committed
84
	while (synthetic_tests[i]) {
85
		excit_t it = create_test_composition(source, indexer);
Brice Videau's avatar
Brice Videau committed
86

Brice Videau's avatar
Brice Videau committed
87
		synthetic_tests[i] (it);
Brice Videau's avatar
Brice Videau committed
88
89
90
91
92
93
94
95
96
97
98
		excit_free(it);
		i++;
	}
}

int main(int argc, char *argv[])
{
	excit_t source, indexer, source2, indexer2;

	source = create_test_range(-15, 14, 2);
	indexer = create_test_range(4, 12, 3);
99
	test_composition_iterator(source, indexer);
Brice Videau's avatar
Brice Videau committed
100
101
102
103
104

	source2 = excit_alloc_test(EXCIT_PRODUCT);
	assert(excit_product_add(source2, create_test_range(-15, 14, 2)) == ES);
	assert(excit_product_add(source2, create_test_range(4, 12, 3)) == ES);
	indexer2 = create_test_range(4, 35, 2);
105
	test_composition_iterator(source2, indexer2);
Brice Videau's avatar
Brice Videau committed
106
107
108
109
110
111

	excit_free(source);
	excit_free(source2);
	excit_free(indexer);
	excit_free(indexer2);
}