Commit | Line | Data |
---|---|---|
588a2232 BA |
1 | #include <stdlib.h> |
2 | #include "cgds/Set.h" | |
3 | #include "helpers.h" | |
4 | #include "lut.h" | |
5 | ||
6 | UInt getHash_int(void* item, size_t hashSize) { | |
7 | return *((int*)item) % hashSize; | |
8 | } | |
9 | ||
10 | void t_set_clear() | |
11 | { | |
12 | Set* s = set_new(int, 16, getHash_int); | |
13 | lu_assert(set_empty(s)); | |
14 | ||
15 | set_add(s, 0); | |
16 | set_add(s, 1); | |
17 | set_add(s, 2); | |
18 | ||
19 | set_destroy(s); | |
20 | s = set_new(int, 8, getHash_int); | |
21 | ||
22 | set_add(s, 1); | |
23 | set_add(s, 2); | |
24 | set_add(s, 3); | |
25 | ||
26 | set_clear(s); | |
27 | lu_assert(set_empty(s)); | |
28 | ||
29 | set_destroy(s); | |
30 | } | |
31 | ||
32 | void t_set_size() | |
33 | { | |
34 | Set* s = set_new(int, 16, getHash_int); | |
35 | lu_assert(set_empty(s)); | |
36 | ||
37 | set_add(s, 0); | |
38 | set_add(s, 1); | |
39 | set_add(s, 2); | |
40 | lu_assert_int_eq(set_size(s), 3); | |
41 | ||
42 | set_add(s, 3); | |
43 | set_add(s, 4); | |
44 | lu_assert_int_eq(set_size(s), 5); | |
45 | ||
46 | set_add(s, 5); | |
47 | set_add(s, 6); | |
48 | set_add(s, 7); | |
49 | lu_assert_int_eq(set_size(s), 8); | |
50 | ||
51 | set_destroy(s); | |
52 | } | |
53 | ||
54 | void t_set_add_remove_basic() | |
55 | { | |
56 | int n = 10; | |
57 | ||
58 | Set* s = set_new(double, 4, NULL); | |
59 | for (double i = 0.0; i < n; i++) | |
60 | set_add(s, i); | |
61 | lu_assert_int_eq(set_size(s), n); | |
62 | ||
63 | // Check values | |
64 | for (double i = 0.0; i < n; i++) | |
65 | lu_assert(set_has(s, &i)); | |
66 | ||
67 | // Remove items | |
68 | for (double i = 0.0; i < n; i++) | |
69 | set_delete(s, i); | |
70 | lu_assert_int_eq(set_size(s), 0); | |
71 | ||
72 | set_destroy(s); | |
73 | } | |
74 | ||
75 | void t_set_getnull_modify() | |
76 | { | |
77 | int n = 10; | |
78 | ||
79 | Set* s = set_new(StructTest1, 4, NULL); | |
80 | // NOTE: using calloc() because probably StructTest1 is 4 + 8 bytes, | |
81 | // aligned to 8 + 8 ==> 4 are left uninitialized ==> memcpy compares | |
82 | // some junk values. TODO: custom equality function instead. | |
83 | StructTest1* st1 = (StructTest1*) calloc(n , sizeof(StructTest1)); | |
84 | for (int i = 0; i < n; i++) | |
85 | { | |
86 | st1[i].a = random() % 42; | |
87 | st1[i].b = (double) random() / RAND_MAX; | |
88 | set_add(s, *(st1 + i)); | |
89 | } | |
90 | for (int i = 0; i < n; i++) | |
91 | { | |
92 | // Another way to access elements | |
93 | StructTest1* st1Cell = (StructTest1*) calloc(1, sizeof(StructTest1)); | |
94 | st1Cell->a = st1[7].a; | |
95 | st1Cell->b = st1[7].b; | |
96 | lu_assert(set_has(s, st1Cell)); | |
97 | free(st1Cell); | |
98 | } | |
99 | ||
100 | // has / has not: | |
101 | StructTest1* stmp = (StructTest1*) calloc(1, sizeof(StructTest1)); | |
102 | stmp->a = 51; | |
103 | stmp->b = 2.0; | |
104 | lu_assert(!set_has(s, stmp)); | |
105 | free(stmp); | |
106 | lu_assert(set_has(s, st1 + 4)); | |
107 | free(st1); | |
108 | ||
109 | set_destroy(s); | |
110 | } | |
111 | ||
112 | void t_set_copy() | |
113 | { | |
114 | int n = 10; | |
115 | ||
116 | Set* s = set_new(int, 8, getHash_int); | |
117 | for (int i = 0; i < n; i++) | |
118 | set_add(s, i + 1); | |
119 | Set* sc = set_copy(s); | |
120 | ||
121 | lu_assert_int_eq(s->size, sc->size); | |
122 | int *a, *b; | |
123 | for (int i = 0; i < n; i++) { | |
124 | int item = i + 1; | |
125 | lu_assert(set_has(sc, &item)); | |
126 | } | |
127 | set_destroy(s); | |
128 | set_destroy(sc); | |
129 | } | |
130 | ||
131 | void t_set_tovect() | |
132 | { | |
133 | int n = 10; | |
134 | ||
135 | Set* s = set_new(int, 8, getHash_int); | |
136 | for (int i = 0; i < n; i++) | |
137 | set_add(s, i); | |
138 | ||
139 | Vector* v = set_to_vector(s); | |
140 | lu_assert(vector_size(v) == set_size(s)); | |
141 | vector_destroy(v); | |
142 | set_destroy(s); | |
143 | } |