summaryrefslogtreecommitdiff
path: root/src/util/util.test.c
blob: 53bb8b25c8eeb88e242908f34a70cdb48302a3b1 (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
#include <test/test.h>
#include "util.h"

LILY_FILE_BEGIN(util_suite)


LILY_TEST("expand NULL array")
{
	size_t count = 0;
	int *array = NULL;

	int *first = kai_expand_array((void**) &array, &count, sizeof(int));

	CHECK_NEQ(first, NULL, "%p");
	CHECK_NEQ(array, NULL, "%p");
	CHECK_EQ(first, array, "%p");
	CHECK_EQ(count, 1, "%d");

	int *second = kai_expand_array((void**) &array, &count, sizeof(int));

	CHECK_NEQ(second, NULL, "%p");
	CHECK_EQ(second, array+1, "%p");
	CHECK_EQ(count, 2, "%d");

	free(array);
}
#include LILY_PUSH_TEST()


struct test100 {
	int value;
	const char *str;
};

LILY_TEST("expand array 100 times")
{
	size_t count = 0;
	struct test100 *array = NULL;

	const char *str = "hello, world!";

	for (int i=0; i<100; i++) {
		struct test100 *element = kai_expand_array((void**) &array, &count, sizeof(struct test100));
		element->value = i;
		element->str = str;
	}

	CHECK_EQ(count, 100, "%ul");

	for (int i=0; i<100; i++) {
		CHECK_EQ(array[i].value, i, "%d");
		CHECK_EQ(array[i].str, str, "%p");
	}

	free(array);
}
#include LILY_PUSH_TEST()


LILY_TEST("convert string to real array")
{
	const char *str = "0 0.5 1 1.5 2";
	ka_real_t array[5];
	int conv = kai_text_to_reals(array, str, 5);

	CHECK_EQ(conv, 5, "%d");

	CHECK_EQF(array[0], 0.0f, "%f");
	CHECK_EQF(array[1], 0.5f, "%f");
	CHECK_EQF(array[2], 1.0f, "%f");
	CHECK_EQF(array[3], 1.5f, "%f");
	CHECK_EQF(array[4], 2.0f, "%f");
}
#include LILY_PUSH_TEST()


LILY_TEST("convert string to real array with fewer conversions than expected")
{
	const char *str = "0 0.5 1";
	ka_real_t array[5];
	int conv = kai_text_to_reals(array, str, 5);

	CHECK_EQ(conv, 3, "%d");

	CHECK_EQF(array[0], 0.0f, "%f");
	CHECK_EQF(array[1], 0.5f, "%f");
	CHECK_EQF(array[2], 1.0f, "%f");
	CHECK_EQF(array[3], 0.0f, "%f");
	CHECK_EQF(array[4], 0.0f, "%f");
}
#include LILY_PUSH_TEST()


LILY_TEST("convert string to int array")
{
	/* the lucas numbers 4 u math fans out there */
	const char *str = "2 1 3 4 7";
	long array[5];
	int conv = kai_text_to_longs(array, str, 5);

	CHECK_EQ(conv, 5, "%d");

	CHECK_EQ(array[0], 2, "%d");
	CHECK_EQ(array[1], 1, "%d");
	CHECK_EQ(array[2], 3, "%d");
	CHECK_EQ(array[3], 4, "%d");
	CHECK_EQ(array[4], 7, "%d");
}
#include LILY_PUSH_TEST()


LILY_TEST("convert string to int array with fewer conversions than expected")
{
	const char *str = "2 1";
	long array[5];
	int conv = kai_text_to_longs(array, str, 5);

	CHECK_EQ(conv, 2, "%d");

	CHECK_EQ(array[0], 2, "%d");
	CHECK_EQ(array[1], 1, "%d");
	CHECK_EQ(array[2], 0, "%d");
	CHECK_EQ(array[3], 0, "%d");
	CHECK_EQ(array[4], 0, "%d");
}
#include LILY_PUSH_TEST()



int f_read_int(int *dest, struct kai_tag_t *src)
{
	long num = kai_tag_attr_to_long(src, "num", -1);
	if (num == -1) { return -1; }
	*dest = num;
	return 0;
}

int release_calls;
void f_release_int(int i) { release_calls += 1; }


LILY_TEST("KAI_FILL_ARRAY_FROM_TAGS produces NULL when no tags are found")
{
	int result, count;
	int *buf;
	release_calls = 0;

	struct kai_tag_t *t = kai_parse_string(
		"<tag>"
		"	<xxx />"
		"	<yyy />"
		"	<zzz />"
		"</tag>"
	);

	KAI_FILL_ARRAY_FROM_TAGS(result, buf, count, int, t, "num", f_read_int, f_release_int);
	kai_tag_destroy(t);

	CHECK_EQ(result, 0, "%d");
	CHECK_EQ(release_calls, 0, "%d");
	CHECK_EQ(buf, NULL, "%p");
	CHECK_EQ(count, 0, "%d");
}
#include LILY_PUSH_TEST()


LILY_TEST("KAI_FILL_ARRAY_FROM_TAGS properly fills array")
{
	int result, count;
	int *buf;
	release_calls = 0;

	struct kai_tag_t *t = kai_parse_string(
		"<tag>"
		"	<xxx />"
		"	<num num=\"5\" />"
		"	<yyy />"
		"	<num num=\"4\" />"
		"	<num num=\"3\" />"
		"	<zzz />"
		"	<num num=\"2\" />"
		"</tag>"
	);

	KAI_FILL_ARRAY_FROM_TAGS(result, buf, count, int, t, "num", f_read_int, f_release_int);
	kai_tag_destroy(t);

	REQUIRE_EQ(result, 0, "%d");
	CHECK_EQ(release_calls, 0, "%d");
	REQUIRE_EQ(count, 4, "%d");
	CHECK_EQ(buf[0], 5, "%d");
	CHECK_EQ(buf[1], 4, "%d");
	CHECK_EQ(buf[2], 3, "%d");
	CHECK_EQ(buf[3], 2, "%d");

	free(buf);
}
#include LILY_PUSH_TEST()



LILY_TEST("KAI_FILL_ARRAY_FROM_TAGS properly releases on error")
{
	int result, count;
	int *buf;
	release_calls = 0;

	struct kai_tag_t *t = kai_parse_string(
		"<tag>"
		"	<xxx />"
		"	<num num=\"5\" />"
		"	<yyy />"
		"	<num num=\"4\" />"
		"	<num nm=\"3\" />"
		"	<zzz />"
		"	<num num=\"2\" />"
		"</tag>"
	);

	KAI_FILL_ARRAY_FROM_TAGS(result, buf, count, int, t, "num", f_read_int, f_release_int);
	kai_tag_destroy(t);

	CHECK_EQ(result, -1, "%d");
	CHECK_EQ(release_calls, 2, "%d");
	CHECK_EQ(count, 0, "%d");
	CHECK_EQ(buf, NULL, "%p");
}
#include LILY_PUSH_TEST()




#define LILY_FILE_END
#include LILY_REGISTER_TESTS()