-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ranges.c
136 lines (111 loc) · 3.99 KB
/
test_ranges.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
#include "./testing.h/testing.h"
#include "./ranges.h"
#include <string.h>
#include <math.h>
#define STEP 10
#define RANGE_LEN 10
#define END_START_OFFSET RANGE_LEN * STEP
#define END_FROM_START(i) i + END_START_OFFSET
#define DIFF(a, b) fabs((a) - (b))
#define THRESHHOLD 0.
#define D_EQUALS(a, b) islessequal(DIFF(a, b), THRESHHOLD)
int test_range_itt()
{
// Test the default
range_t range = RANGE_T_DEFAULT;
ASSERT(range.current == 0);
ASSERT(range.start == 0);
ASSERT(range.end == 0);
ASSERT(range.step == 0);
// Test range start and, next
for (int i = 0; i < 100; i++) {
range_t range;
range.start = i;
range.end = END_FROM_START(i);
range.step = STEP;
range_start(&range);
ASSERT(range.start == i);
ASSERT(range.end == END_FROM_START(i));
ASSERT(range.step == STEP);
ASSERT(range.current == range.start);
double output;
for (int j = 0; j < RANGE_LEN; j++) {
ASSERT(range_next(&range, &output) == RANGE_GENERATING);
ASSERT(range.current == output);
ASSERT(D_EQUALS(output, range.start + STEP * (j + 1)));
ASSERT(range.start == i);
ASSERT(range.end == END_FROM_START(i));
ASSERT(range.step == STEP);
}
output = -1;
ASSERT(range_next(&range, &output) == RANGE_STOPPED);
ASSERT(output == -1);
}
return 1;
}
#define START 10
#define END 50
#define ITEMS_PER_RANGE (1 + (abs(START - END) / STEP))
#define ITEMS_PER_DIMENSION(d) pow(ITEMS_PER_RANGE, d)
#define DIMENSIONS 10
static int test_mdd_range_itt()
{
// Init ranges
range_t *ranges = malloc(sizeof(*ranges) * DIMENSIONS);
ASSERT(ranges != NULL);
for (size_t d = 0; d < DIMENSIONS; d++) {
ranges[d].start = START;
ranges[d].end = END;
ranges[d].step = STEP;
}
// test init and, next
for (size_t d/*imensions*/ = 1; d < DIMENSIONS; d++) {
multi_dimensional_range_t range;
memset(&range, 0, sizeof(range));
ASSERT(init_multi_dimensional_range_arr(&range, d, ranges));
ASSERT(range.dimensions == d);
ASSERT(range.ranges != NULL);
multi_dimensional_range_start(&range);
ASSERT(range.dimensions == d);
vector_t out;
for (size_t i = 0; i < floor(ITEMS_PER_DIMENSION(d)); i++) {
ASSERT(multi_dimensional_range_next(&range, &out) == RANGE_GENERATING);
ASSERT(out.dimensions == range.dimensions);
ASSERT(out.values != NULL);
free_vector(&out);
}
ASSERT(multi_dimensional_range_next(&range, &out) == RANGE_STOPPED);
free_multi_dimensional_range(&range);
}
free(ranges);
return 1;
}
static int test_mdd_range_itt_2()
{
multi_dimensional_range_t range;
memset(&range, 0, sizeof(range));
range_t range_1, range_2, range_3;
range_1.start = range_2.start = range_3.start = 1;
range_1.end = range_2.end = range_3.end = 3;
range_1.step = range_2.step = range_3.step = 1;
init_multi_dimensional_range(&range, range_1, range_2, range_3);
ASSERT(range.dimensions == 3);
ASSERT(range.ranges != NULL);
ASSERT(memcmp(&range.ranges[0], &range_1, sizeof(range_1)) == 0);
ASSERT(memcmp(&range.ranges[1], &range_2, sizeof(range_2)) == 0);
ASSERT(memcmp(&range.ranges[2], &range_3, sizeof(range_3)) == 0);
multi_dimensional_range_start(&range);
vector_t out;
for (size_t i = 0; i < 3 * 3 * 3; i++) {
ASSERT(multi_dimensional_range_next(&range, &out) == RANGE_GENERATING);
ASSERT(out.dimensions== range.dimensions);
ASSERT(out.values != NULL);
free_vector(&out);
}
ASSERT(multi_dimensional_range_next(&range, &out) == RANGE_STOPPED);
free_multi_dimensional_range(&range);
return 1;
}
SUB_TEST(test_ranges, {&test_range_itt, "Test range itt"},
{&test_mdd_range_itt, "Test multi dimensional range itt"},
{&test_mdd_range_itt_2, "Test multi dimensional range itt with other init method"})