nspkg / list_test.v
311 lines · 298 sloc · 9.88 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module nspkg
2
3#flag -lalpm
4#include <alpm.h>
5
6#include <alpm_list.h>
7
8fn C.alpm_list_free(&List)
9fn C.alpm_list_free_inner(&List, ListFnFree)
10fn C.alpm_list_add(&List, voidptr) &List
11fn C.alpm_list_append(&&List, voidptr) &List
12fn C.alpm_list_append_strdup(&&List, &u8) &List
13fn C.alpm_list_add_sorted(&List, voidptr, ListFnCmp) &List
14fn C.alpm_list_join(&List, &List) &List
15fn C.alpm_list_mmerge(&List, &List, ListFnCmp) &List
16fn C.alpm_list_msort(&List, uint, ListFnCmp) &List
17fn C.alpm_list_remove_item(&List, &List) &List
18fn C.alpm_list_remove(&List, voidptr, ListFnCmp, &voidptr) &List
19fn C.alpm_list_remove_str(&List, &u8, &&u8) &List
20fn C.alpm_list_remove_dupes(&List) &List
21fn C.alpm_list_strdup(&List) &List
22fn C.alpm_list_copy(&List) &List
23fn C.alpm_list_copy_data(&List, uint) &List
24fn C.alpm_list_reverse(&List) &List
25fn C.alpm_list_nth(&List, uint) &List
26fn C.alpm_list_next(&List) &List
27fn C.alpm_list_previous(&List) &List
28fn C.alpm_list_last(&List) &List
29fn C.alpm_list_count(&List) uint
30fn C.alpm_list_find(&List, voidptr, ListFnCmp) voidptr
31fn C.alpm_list_find_ptr(&List, voidptr) voidptr
32fn C.alpm_list_find_str(&List, &u8) &u8
33fn C.alpm_list_cmp_unsorted(&List, &List, ListFnCmp) int
34fn C.alpm_list_diff_sorted(&List, &List, ListFnCmp, &&List, &&List)
35fn C.alpm_list_diff(&List, &List, ListFnCmp) &List
36fn C.alpm_list_to_array(&List, uint, uint) voidptr
37
38fn list_test_cmp(a voidptr, b voidptr) i32 {
39 return if u64(a) < u64(b) {
40 -1
41 } else if a == b {
42 0
43 } else {
44 1
45 }
46}
47
48fn list_test_make(values []int) &List {
49 mut head := &List(unsafe { nil })
50 for value in values { head = head.add(voidptr(value)) }
51 return head
52}
53
54fn list_test_reference(values []int) &List {
55 mut head := &List(unsafe { nil })
56 for value in values { head = C.alpm_list_add(head, voidptr(value)) }
57 return head
58}
59
60fn list_test_snapshot(head &List) []u64 {
61 mut out := []u64{}
62 mut previous := &List(unsafe { nil })
63 mut item := unsafe { head }
64 for item != unsafe { nil } {
65 assert out.len < 1000, 'cycle in next links'
66 if previous != unsafe { nil } {
67 assert item.prev == previous
68 }
69 assert item.previous() == previous
70 out << u64(item.data)
71 previous = item
72 item = item.next
73 }
74 assert head.last() == previous
75 if head != unsafe { nil } {
76 assert head.prev == previous
77 }
78 assert head.count() == uint(out.len)
79 return out
80}
81
82fn list_test_equal(actual &List, reference &List) {
83 assert list_test_snapshot(actual) == list_test_snapshot(reference)
84 assert actual.count() == C.alpm_list_count(reference)
85}
86
87fn test_list_access_and_append() {
88 mut head := &List(unsafe { nil })
89 mut reference := &List(unsafe { nil })
90 assert head.count() == 0
91 assert head.last() == unsafe { nil }
92 assert head.previous() == unsafe { nil }
93 assert head.next() == unsafe { nil }
94 assert head.nth(5) == unsafe { nil }
95 assert head.to_array(0, 1) == none
96 assert head.to_array(~uint(0), 2) == none
97 for i in 1 .. 20 {
98 node := (&head).append(voidptr(i))
99 other := C.alpm_list_append(&reference, voidptr(i))
100 assert node == head.last()
101 assert other == C.alpm_list_last(reference)
102 list_test_equal(head, reference)
103 for j in 0 .. i {
104 a := head.nth(uint(j))
105 b := C.alpm_list_nth(reference, uint(j))
106 assert a.data == b.data
107 assert (a.next() == unsafe { nil }) == (C.alpm_list_next(b) == unsafe { nil })
108 assert (a.previous() == unsafe { nil }) == (C.alpm_list_previous(b) == unsafe { nil })
109 }
110 assert head.nth(uint(i)) == unsafe { nil }
111 }
112 head.free()
113 C.alpm_list_free(reference)
114}
115
116fn test_list_sort_copy_reverse_dedupe_and_find() {
117 mut state := u64(42)
118 for size in 0 .. 50 {
119 mut values := []int{}
120 for _ in 0 .. size {
121 state = state * 6364136223846793005 + 1
122 values << int((state >> 32) % 11)
123 }
124 mut native := list_test_make(values)
125 mut reference := list_test_reference(values)
126 list_test_equal(native, reference)
127 for operation in 0 .. 3 {
128 a := match operation {
129 0 { native.copy() }
130 1 { native.reverse() }
131 else { native.remove_dupes() }
132 }
133 b := match operation {
134 0 { C.alpm_list_copy(reference) }
135 1 { C.alpm_list_reverse(reference) }
136 else { C.alpm_list_remove_dupes(reference) }
137 }
138 list_test_equal(a, b)
139 a.free()
140 C.alpm_list_free(b)
141 list_test_equal(native, reference)
142 }
143 for needle in 0 .. 13 {
144 want := C.alpm_list_find(reference, voidptr(needle), list_test_cmp)
145 if found := native.find(voidptr(needle), list_test_cmp) {
146 assert found == want
147 } else {
148 assert want == unsafe { nil }
149 }
150 ptr_want := C.alpm_list_find_ptr(reference, voidptr(needle))
151 if found := native.find_ptr(voidptr(needle)) {
152 assert found == ptr_want
153 } else {
154 assert ptr_want == unsafe { nil }
155 }
156 }
157 native = native.msort(native.count(), list_test_cmp)
158 reference = C.alpm_list_msort(reference, C.alpm_list_count(reference), list_test_cmp)
159 list_test_equal(native, reference)
160 for needle in [0, 1, 5, 10, 11] {
161 native = native.add_sorted(voidptr(needle), list_test_cmp)
162 reference = C.alpm_list_add_sorted(reference, voidptr(needle), list_test_cmp)
163 list_test_equal(native, reference)
164 }
165 native.free()
166 C.alpm_list_free(reference)
167 }
168 // mergesort reuses nodes and keeps equal keys in their original order.
169 head := list_test_make([2, 1, 2, 1])
170 nodes := [head.nth(1), head.nth(3), head.nth(0), head.nth(2)]
171 sorted := head.msort(4, list_test_cmp)
172 for i, node in nodes {
173 assert sorted.nth(uint(i)) == node
174 }
175 sorted.free()
176}
177
178fn test_list_detach_and_remove() {
179 for values in [[1], [1, 2], [1, 2, 3, 4]] {
180 for index in 0 .. values.len {
181 mut a := list_test_make(values)
182 mut b := list_test_reference(values)
183 node := a.nth(uint(index))
184 other := C.alpm_list_nth(b, uint(index))
185 a = a.remove_item(node)
186 b = C.alpm_list_remove_item(b, other)
187 list_test_equal(a, b)
188 assert (node.prev == unsafe { nil }) == (other.prev == unsafe { nil })
189 assert (node.next == unsafe { nil }) == (other.next == unsafe { nil })
190 C.free(node)
191 C.free(other)
192 a.free()
193 C.alpm_list_free(b)
194 }
195 }
196 mut a := list_test_make([0, 1, 2, 2, 3, 0])
197 mut b := list_test_reference([0, 1, 2, 2, 3, 0])
198 for needle in [0, 4, 2, 1, 2, 3] {
199 mut data := voidptr(123)
200 b = C.alpm_list_remove(b, voidptr(needle), list_test_cmp, &data)
201 head, removed := a.remove(voidptr(needle), list_test_cmp)
202 a = head
203 assert removed == data
204 list_test_equal(a, b)
205 }
206 a.free()
207 C.alpm_list_free(b)
208}
209
210fn test_list_join_merge_compare_and_difference() {
211 for av in [[]int{}, [1], [2, 1, 1], [3, 1, 4, 2]] {
212 for bv in [[]int{}, [1], [1, 2, 1], [2, 5, 4]] {
213 a := list_test_make(av)
214 b := list_test_make(bv)
215 ar := list_test_reference(av)
216 br := list_test_reference(bv)
217 assert a.cmp_unsorted(b, list_test_cmp) == C.alpm_list_cmp_unsorted(ar, br, list_test_cmp)
218 diff := a.diff(b, list_test_cmp)
219 diffref := C.alpm_list_diff(ar, br, list_test_cmp)
220 list_test_equal(diff, diffref)
221 diff.free()
222 C.alpm_list_free(diffref)
223 joined := a.join(b)
224 joinedref := C.alpm_list_join(ar, br)
225 list_test_equal(joined, joinedref)
226 joined.free()
227 C.alpm_list_free(joinedref)
228 left := list_test_make(av).msort(uint(av.len), list_test_cmp)
229 right := list_test_make(bv).msort(uint(bv.len), list_test_cmp)
230 lr := C.alpm_list_msort(list_test_reference(av), uint(av.len), list_test_cmp)
231 rr := C.alpm_list_msort(list_test_reference(bv), uint(bv.len), list_test_cmp)
232 x, y := left.diff_sorted(right, list_test_cmp)
233 mut xr := &List(unsafe { nil })
234 mut yr := &List(unsafe { nil })
235 C.alpm_list_diff_sorted(lr, rr, list_test_cmp, &xr, &yr)
236 list_test_equal(x, xr)
237 list_test_equal(y, yr)
238 x.free()
239 y.free()
240 C.alpm_list_free(xr)
241 C.alpm_list_free(yr)
242 merged := left.mmerge(right, list_test_cmp)
243 mergedref := C.alpm_list_mmerge(lr, rr, list_test_cmp)
244 list_test_equal(merged, mergedref)
245 merged.free()
246 C.alpm_list_free(mergedref)
247 }
248 }
249}
250
251fn test_list_strings_and_owned_data() {
252 mut native := &List(unsafe { nil })
253 mut reference := &List(unsafe { nil })
254 for text in ['gamma', '', 'alpha', 'beta'] {
255 node := (&native).append_strdup(text)
256 other := C.alpm_list_append_strdup(&reference, text.str)
257 assert unsafe { (&u8(node.data)).vstring() } == unsafe { (&u8(other.data)).vstring() }
258 }
259 for text in ['', 'alpha', 'missing'] {
260 want := C.alpm_list_find_str(reference, text.str)
261 if found := native.find_str(text) {
262 assert found == unsafe { want.vstring() }
263 } else {
264 assert want == unsafe { nil }
265 }
266 }
267 copy := native.strdup()
268 copyref := C.alpm_list_strdup(reference)
269 for i in 0 .. int(native.count()) {
270 assert copy.nth(uint(i)).data != native.nth(uint(i)).data
271 assert unsafe { (&u8(copy.nth(uint(i)).data)).vstring() } == unsafe { (&u8(copyref.nth(uint(i)).data)).vstring() }
272 }
273 for text in ['missing', 'gamma', '', 'beta', 'alpha'] {
274 mut old := &u8(unsafe { nil })
275 reference = C.alpm_list_remove_str(reference, text.str, &old)
276 head, data := native.remove_str(text)
277 native = head
278 assert (data == unsafe { nil }) == (old == unsafe { nil })
279 if data != unsafe { nil } {
280 assert unsafe { data.vstring() } == unsafe { old.vstring() }
281 }
282 C.free(data)
283 C.free(old)
284 }
285 native.free()
286 C.alpm_list_free(reference)
287 copy.free_inner(C.free)
288 copy.free()
289 C.alpm_list_free_inner(copyref, C.free)
290 C.alpm_list_free(copyref)
291 values := [37, 12, 99]
292 mut numbers := &List(unsafe { nil })
293 for i in 0 .. values.len { numbers = numbers.add(unsafe { &values[i] }) }
294 deep := numbers.copy_data(sizeof(int))
295 deepref := C.alpm_list_copy_data(numbers, sizeof(int))
296 array := unsafe { &int(numbers.to_array(3, sizeof(int)) or { panic('missing array') }) }
297 refarray := unsafe { &int(C.alpm_list_to_array(numbers, 3, sizeof(int))) }
298 for i, value in values {
299 assert deep.nth(uint(i)).data != numbers.nth(uint(i)).data
300 assert unsafe { *(&int(deep.nth(uint(i)).data)) } == value
301 assert unsafe { *(&int(deepref.nth(uint(i)).data)) } == value
302 assert unsafe { array[i] } == unsafe { refarray[i] }
303 }
304 C.free(array)
305 C.free(refarray)
306 deep.free_inner(C.free)
307 deep.free()
308 C.alpm_list_free_inner(deepref, C.free)
309 C.alpm_list_free(deepref)
310 numbers.free()
311}
312