-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_main.cpp
395 lines (308 loc) · 11.7 KB
/
test_main.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#ifdef __cplusplus
extern "C" {
#endif
#define idxarr_assert(message, test) do { \
if (!(test)) { \
printf("\033[0;31m");\
printf("TEST FAILED at line numer %d, %s\n", __LINE__, message);\
printf("\033[0m");\
return 0; \
} \
} while (0)
#define idxarr_run_test(test) do { \
idxarr_tests_run++; \
if(!test()) return 0;\
printf("\033[0;32m");\
printf("%s => PASSED\n", #test);\
printf("\033[0m");\
} while (0)
extern int idxarr_tests_run;
#include <time.h>
#include <string.h>
#include "indexed_array.h"
#include <unistd.h>
/****
g++ -std=c++11 indexed_array.c test_main.cpp
valgrind ./a.out // if you have valgrind
or
clang++ -std=c++11 indexed_array.c test_main.cpp
drmemory / valgrind to run
*
*/
typedef struct {
int val;
long val_l;
float val_f;
char *val_cstr;
char val_cstr2[10];
} my_node;
int cmp_group_func (const void *a, const void *b) {
return strcmp((*(my_node**)a)->val_cstr, (*(my_node**)b)->val_cstr);
}
void free_my_node(void *a) {
free(((my_node*)a)->val_cstr);
free(a);
}
int idxarr_tests_run = 0;
/***Uniq variable***/
idx_array_t *my_indexed_arr;
static int setup_test() {
// at first 50 quantity only
int n = 50;
my_indexed_arr = idxarr_create(n, 5);
idxarr_assert("First Start indexed array capacity should be 50", my_indexed_arr->capacity == 50);
if (idxarr_add_float_index(my_indexed_arr, my_node, val_f)
&& idxarr_add_int_index(my_indexed_arr, my_node, val)
&& idxarr_add_heap_str_index(my_indexed_arr, my_node, val_cstr)
&& idxarr_add_stack_str_index(my_indexed_arr, my_node, val_cstr2)
&& idxarr_add_long_index(my_indexed_arr, my_node, val_l)
) {
int i;
for (i = 0; i < 100; i++) {
my_node *s = (my_node*) malloc(sizeof(my_node));
if (i < 10) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Jack Prabas");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 20) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Abbar");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 30) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("BETTY");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 40) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Eason");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 50) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Cat");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 60) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Genus Gan");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 70) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Heat Jack");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 80) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Ian Cheng@");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 90) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Finn");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Denny");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
idxarr_push(my_indexed_arr, s);
}
idxarr_assert("indexed array size should become to 100", my_indexed_arr->size == 100);
} else {
idxarr_assert("indexed array indexing failed", 0);
}
return 1;
}
static int test_value_had_indexed() {
my_node **ss = (my_node**)idxarr_get_index_array(my_indexed_arr, 2); // val_cstr indexed
int i;
for (i = 0; i < 10; i++) {
idxarr_assert("Abbar should have val 10-19", ss[i]->val >= 10 && ss[i]->val < 20);
}
for (i = 10; i < 20; i++) {
idxarr_assert("BETTY should have val 20-29", ss[i]->val >= 20 && ss[i]->val < 30);
}
for (i = 20; i < 30; i++) {
idxarr_assert("Cat should have val 40-49", ss[i]->val >= 40 && ss[i]->val < 50);
}
return 1;
}
static int test_search_algorithm() {
int search_key1 = 10;
int search_key2 = 20;
int search_key3 = 30;
int search_key4 = 40;
int search_key5 = 50;
int search_key6 = 60;
int search_key7 = 70;
int search_key8 = 80;
int search_key9 = 90;
int search_key10 = 200;
char* search_key5c = "Abbar";
char* search_key6c = "Cat";
long search_key12 = 12;
idx_array_rs *rs = idxarr_search_multi_eq10(my_indexed_arr, my_node, val, &search_key1,
&search_key2, &search_key3, &search_key4, &search_key5, &search_key6, &search_key7,
&search_key8, &search_key9 , &search_key10 );
idxarr_assert("result size should be 9", rs->size == 9);
idxarr_free_rs(rs);
rs = idxarr_search_gt(my_indexed_arr, my_node, val_l, &search_key12);
idxarr_assert("result size should be 87", rs->size == 87);
idxarr_free_rs(rs);
rs = idxarr_search_lt(my_indexed_arr, my_node, val_l, &search_key12);
idxarr_assert("result size should be 12", rs->size == 12);
idxarr_free_rs(rs);
rs = idxarr_search_eq(my_indexed_arr, my_node, val_cstr, search_key5c);
idxarr_assert("result size should be 10", rs->size == 10);
idxarr_free_rs(rs);
idx_array_rs *rs1 = idxarr_search_eq(my_indexed_arr, my_node, val_cstr, search_key5c);
idx_array_rs *rs2 = idxarr_search_eq(my_indexed_arr, my_node, val_cstr2, search_key6c);
idx_array_rs *rs10 = idxarr_intersect_rs(rs1, rs2, false); // Join and false mean remain rs1 and rs2, true means free the result
idxarr_assert("result size should be 0 as join is not match", rs10->size == 0);
idxarr_free_rs(rs10);
rs10 = idxarr_union_rs(rs1, rs2, false); // Join
idxarr_assert("result size should be 20 as union ", rs10->size == 20);
idxarr_free_rs(rs10);
// Join more
rs10 = idxarr_intersect_rs(rs1, idxarr_union_rs(rs1, rs2, false), true);
idxarr_assert("result size should be 10 as join and the union ", rs10->size == 10);
#ifdef __APPLE__
idx_cmp_func f = ^int(const void *a, const void *b) {
my_node *anode = *(my_node**)a;
my_node *bnode = *(my_node**)b;
return strcmp((const char*)(uintptr_t)anode->val_cstr, (const char*)(uintptr_t)bnode->val_cstr);
};
idxarr_sort_rs_by_b(rs10, f);
#else
idxarr_cmp_func f = [&](const void*a, const void*b) -> int {
my_node *anode = *(my_node**)a;
my_node *bnode = *(my_node**)b;
return strcmp((const char*)(uintptr_t)anode->val_cstr, (const char*)(uintptr_t)bnode->val_cstr);
};
idxarr_sort_rs_by(rs10, f);
#endif
// idxarr_free_rs(rs1); // rs1 is already free at above
idxarr_free_rs(rs2);
idxarr_free_rs(rs10);
// Test String start with
char* search_keyStartWith = "A";
idx_array_rs *rs111 = idxarr_search_str_start_with(my_indexed_arr, my_node, val_cstr, search_keyStartWith);
idxarr_assert("result size should be 10 as Start with A only have 10 result ", rs111->size == 10);
int i;
for (i = 0; i < rs111->size; i++) {
idxarr_assert("result start with A should be Abbar only ", strcmp(((my_node*) rs111->ptrs[i])->val_cstr, "Abbar") == 0);
}
idxarr_free_rs(rs111);
search_keyStartWith = "F";
rs111 = idxarr_search_str_start_with(my_indexed_arr, my_node, val_cstr2, search_keyStartWith);
idxarr_assert("result size should be 10 as Start with F only have 10 result ", rs111->size == 10);
for (i = 0; i < rs111->size; i++) {
idxarr_assert("result start with F should be Finn only ", strcmp(((my_node*) rs111->ptrs[i])->val_cstr2, "Finn") == 0);
}
idxarr_free_rs(rs111);
return 1;
}
static int test_reload_data() {
// at first 50 quantity only
int n = 50;
idx_array_t *my_new_indexed_arr = idxarr_create(n, 5);
idxarr_assert("new indexed array capacity should be 50", my_new_indexed_arr->capacity == 50);
if (idxarr_add_float_index(my_new_indexed_arr, my_node, val_f)
&& idxarr_add_int_index(my_new_indexed_arr, my_node, val)
&& idxarr_add_heap_str_index(my_new_indexed_arr, my_node, val_cstr)
&& idxarr_add_stack_str_index(my_new_indexed_arr, my_node, val_cstr2)
&& idxarr_add_long_index(my_new_indexed_arr, my_node, val_l)
) {
int i;
for (i = 0; i < 50; i++) {
my_node *s = (my_node*) malloc(sizeof(my_node));
if (i < 10) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Jack Prabas");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 20) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Abbar");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 30) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("BETTY");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else if (i < 40) {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Eason");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
else {
s->val = i;
s->val_l = i * 1L;
s->val_cstr = strdup("Cat");
memcpy(s->val_cstr2, s->val_cstr, strlen(s->val_cstr) + 1);
s->val_f = i * 1.0f;
}
idxarr_push(my_new_indexed_arr, s);
}
} else {
idxarr_assert("new indexed array indexing failed", 0);
}
unsigned int buffer_milisecs_to_purge_old_array = 500;
idxarr_safety_swap(&my_indexed_arr, my_new_indexed_arr, free_my_node, buffer_milisecs_to_purge_old_array);
idxarr_assert("reloaded indexed array size should become to 50", my_indexed_arr->size == 50);
return 1;
}
static int all_tests() {
idxarr_run_test(setup_test);
idxarr_run_test(test_value_had_indexed);
idxarr_run_test(test_search_algorithm);
idxarr_run_test(test_reload_data);
idxarr_destroy(my_indexed_arr, free_my_node);
// Use Valgrind compile
return 1;
}
int main(int argc, char **argv) {
int test_status = all_tests();
if (test_status) {
printf("ALL TESTS PASSED\n");
}
printf("Tests run: %d\n", idxarr_tests_run);
return test_status;
}
#ifdef __cplusplus
}
#endif