module nspkg #flag -lalpm #include #include fn C.alpm_list_free(&List) fn C.alpm_list_free_inner(&List, ListFnFree) fn C.alpm_list_add(&List, voidptr) &List fn C.alpm_list_append(&&List, voidptr) &List fn C.alpm_list_append_strdup(&&List, &u8) &List fn C.alpm_list_add_sorted(&List, voidptr, ListFnCmp) &List fn C.alpm_list_join(&List, &List) &List fn C.alpm_list_mmerge(&List, &List, ListFnCmp) &List fn C.alpm_list_msort(&List, uint, ListFnCmp) &List fn C.alpm_list_remove_item(&List, &List) &List fn C.alpm_list_remove(&List, voidptr, ListFnCmp, &voidptr) &List fn C.alpm_list_remove_str(&List, &u8, &&u8) &List fn C.alpm_list_remove_dupes(&List) &List fn C.alpm_list_strdup(&List) &List fn C.alpm_list_copy(&List) &List fn C.alpm_list_copy_data(&List, uint) &List fn C.alpm_list_reverse(&List) &List fn C.alpm_list_nth(&List, uint) &List fn C.alpm_list_next(&List) &List fn C.alpm_list_previous(&List) &List fn C.alpm_list_last(&List) &List fn C.alpm_list_count(&List) uint fn C.alpm_list_find(&List, voidptr, ListFnCmp) voidptr fn C.alpm_list_find_ptr(&List, voidptr) voidptr fn C.alpm_list_find_str(&List, &u8) &u8 fn C.alpm_list_cmp_unsorted(&List, &List, ListFnCmp) int fn C.alpm_list_diff_sorted(&List, &List, ListFnCmp, &&List, &&List) fn C.alpm_list_diff(&List, &List, ListFnCmp) &List fn C.alpm_list_to_array(&List, uint, uint) voidptr fn list_test_cmp(a voidptr, b voidptr) i32 { return if u64(a) < u64(b) { -1 } else if a == b { 0 } else { 1 } } fn list_test_make(values []int) &List { mut head := &List(unsafe { nil }) for value in values { head = head.add(voidptr(value)) } return head } fn list_test_reference(values []int) &List { mut head := &List(unsafe { nil }) for value in values { head = C.alpm_list_add(head, voidptr(value)) } return head } fn list_test_snapshot(head &List) []u64 { mut out := []u64{} mut previous := &List(unsafe { nil }) mut item := unsafe { head } for item != unsafe { nil } { assert out.len < 1000, 'cycle in next links' if previous != unsafe { nil } { assert item.prev == previous } assert item.previous() == previous out << u64(item.data) previous = item item = item.next } assert head.last() == previous if head != unsafe { nil } { assert head.prev == previous } assert head.count() == uint(out.len) return out } fn list_test_equal(actual &List, reference &List) { assert list_test_snapshot(actual) == list_test_snapshot(reference) assert actual.count() == C.alpm_list_count(reference) } fn test_list_access_and_append() { mut head := &List(unsafe { nil }) mut reference := &List(unsafe { nil }) assert head.count() == 0 assert head.last() == unsafe { nil } assert head.previous() == unsafe { nil } assert head.next() == unsafe { nil } assert head.nth(5) == unsafe { nil } assert head.to_array(0, 1) == none assert head.to_array(~uint(0), 2) == none for i in 1 .. 20 { node := (&head).append(voidptr(i)) other := C.alpm_list_append(&reference, voidptr(i)) assert node == head.last() assert other == C.alpm_list_last(reference) list_test_equal(head, reference) for j in 0 .. i { a := head.nth(uint(j)) b := C.alpm_list_nth(reference, uint(j)) assert a.data == b.data assert (a.next() == unsafe { nil }) == (C.alpm_list_next(b) == unsafe { nil }) assert (a.previous() == unsafe { nil }) == (C.alpm_list_previous(b) == unsafe { nil }) } assert head.nth(uint(i)) == unsafe { nil } } head.free() C.alpm_list_free(reference) } fn test_list_sort_copy_reverse_dedupe_and_find() { mut state := u64(42) for size in 0 .. 50 { mut values := []int{} for _ in 0 .. size { state = state * 6364136223846793005 + 1 values << int((state >> 32) % 11) } mut native := list_test_make(values) mut reference := list_test_reference(values) list_test_equal(native, reference) for operation in 0 .. 3 { a := match operation { 0 { native.copy() } 1 { native.reverse() } else { native.remove_dupes() } } b := match operation { 0 { C.alpm_list_copy(reference) } 1 { C.alpm_list_reverse(reference) } else { C.alpm_list_remove_dupes(reference) } } list_test_equal(a, b) a.free() C.alpm_list_free(b) list_test_equal(native, reference) } for needle in 0 .. 13 { want := C.alpm_list_find(reference, voidptr(needle), list_test_cmp) if found := native.find(voidptr(needle), list_test_cmp) { assert found == want } else { assert want == unsafe { nil } } ptr_want := C.alpm_list_find_ptr(reference, voidptr(needle)) if found := native.find_ptr(voidptr(needle)) { assert found == ptr_want } else { assert ptr_want == unsafe { nil } } } native = native.msort(native.count(), list_test_cmp) reference = C.alpm_list_msort(reference, C.alpm_list_count(reference), list_test_cmp) list_test_equal(native, reference) for needle in [0, 1, 5, 10, 11] { native = native.add_sorted(voidptr(needle), list_test_cmp) reference = C.alpm_list_add_sorted(reference, voidptr(needle), list_test_cmp) list_test_equal(native, reference) } native.free() C.alpm_list_free(reference) } // mergesort reuses nodes and keeps equal keys in their original order. head := list_test_make([2, 1, 2, 1]) nodes := [head.nth(1), head.nth(3), head.nth(0), head.nth(2)] sorted := head.msort(4, list_test_cmp) for i, node in nodes { assert sorted.nth(uint(i)) == node } sorted.free() } fn test_list_detach_and_remove() { for values in [[1], [1, 2], [1, 2, 3, 4]] { for index in 0 .. values.len { mut a := list_test_make(values) mut b := list_test_reference(values) node := a.nth(uint(index)) other := C.alpm_list_nth(b, uint(index)) a = a.remove_item(node) b = C.alpm_list_remove_item(b, other) list_test_equal(a, b) assert (node.prev == unsafe { nil }) == (other.prev == unsafe { nil }) assert (node.next == unsafe { nil }) == (other.next == unsafe { nil }) C.free(node) C.free(other) a.free() C.alpm_list_free(b) } } mut a := list_test_make([0, 1, 2, 2, 3, 0]) mut b := list_test_reference([0, 1, 2, 2, 3, 0]) for needle in [0, 4, 2, 1, 2, 3] { mut data := voidptr(123) b = C.alpm_list_remove(b, voidptr(needle), list_test_cmp, &data) head, removed := a.remove(voidptr(needle), list_test_cmp) a = head assert removed == data list_test_equal(a, b) } a.free() C.alpm_list_free(b) } fn test_list_join_merge_compare_and_difference() { for av in [[]int{}, [1], [2, 1, 1], [3, 1, 4, 2]] { for bv in [[]int{}, [1], [1, 2, 1], [2, 5, 4]] { a := list_test_make(av) b := list_test_make(bv) ar := list_test_reference(av) br := list_test_reference(bv) assert a.cmp_unsorted(b, list_test_cmp) == C.alpm_list_cmp_unsorted(ar, br, list_test_cmp) diff := a.diff(b, list_test_cmp) diffref := C.alpm_list_diff(ar, br, list_test_cmp) list_test_equal(diff, diffref) diff.free() C.alpm_list_free(diffref) joined := a.join(b) joinedref := C.alpm_list_join(ar, br) list_test_equal(joined, joinedref) joined.free() C.alpm_list_free(joinedref) left := list_test_make(av).msort(uint(av.len), list_test_cmp) right := list_test_make(bv).msort(uint(bv.len), list_test_cmp) lr := C.alpm_list_msort(list_test_reference(av), uint(av.len), list_test_cmp) rr := C.alpm_list_msort(list_test_reference(bv), uint(bv.len), list_test_cmp) x, y := left.diff_sorted(right, list_test_cmp) mut xr := &List(unsafe { nil }) mut yr := &List(unsafe { nil }) C.alpm_list_diff_sorted(lr, rr, list_test_cmp, &xr, &yr) list_test_equal(x, xr) list_test_equal(y, yr) x.free() y.free() C.alpm_list_free(xr) C.alpm_list_free(yr) merged := left.mmerge(right, list_test_cmp) mergedref := C.alpm_list_mmerge(lr, rr, list_test_cmp) list_test_equal(merged, mergedref) merged.free() C.alpm_list_free(mergedref) } } } fn test_list_strings_and_owned_data() { mut native := &List(unsafe { nil }) mut reference := &List(unsafe { nil }) for text in ['gamma', '', 'alpha', 'beta'] { node := (&native).append_strdup(text) other := C.alpm_list_append_strdup(&reference, text.str) assert unsafe { (&u8(node.data)).vstring() } == unsafe { (&u8(other.data)).vstring() } } for text in ['', 'alpha', 'missing'] { want := C.alpm_list_find_str(reference, text.str) if found := native.find_str(text) { assert found == unsafe { want.vstring() } } else { assert want == unsafe { nil } } } copy := native.strdup() copyref := C.alpm_list_strdup(reference) for i in 0 .. int(native.count()) { assert copy.nth(uint(i)).data != native.nth(uint(i)).data assert unsafe { (&u8(copy.nth(uint(i)).data)).vstring() } == unsafe { (&u8(copyref.nth(uint(i)).data)).vstring() } } for text in ['missing', 'gamma', '', 'beta', 'alpha'] { mut old := &u8(unsafe { nil }) reference = C.alpm_list_remove_str(reference, text.str, &old) head, data := native.remove_str(text) native = head assert (data == unsafe { nil }) == (old == unsafe { nil }) if data != unsafe { nil } { assert unsafe { data.vstring() } == unsafe { old.vstring() } } C.free(data) C.free(old) } native.free() C.alpm_list_free(reference) copy.free_inner(C.free) copy.free() C.alpm_list_free_inner(copyref, C.free) C.alpm_list_free(copyref) values := [37, 12, 99] mut numbers := &List(unsafe { nil }) for i in 0 .. values.len { numbers = numbers.add(unsafe { &values[i] }) } deep := numbers.copy_data(sizeof(int)) deepref := C.alpm_list_copy_data(numbers, sizeof(int)) array := unsafe { &int(numbers.to_array(3, sizeof(int)) or { panic('missing array') }) } refarray := unsafe { &int(C.alpm_list_to_array(numbers, 3, sizeof(int))) } for i, value in values { assert deep.nth(uint(i)).data != numbers.nth(uint(i)).data assert unsafe { *(&int(deep.nth(uint(i)).data)) } == value assert unsafe { *(&int(deepref.nth(uint(i)).data)) } == value assert unsafe { array[i] } == unsafe { refarray[i] } } C.free(array) C.free(refarray) deep.free_inner(C.free) deep.free() C.alpm_list_free_inner(deepref, C.free) C.alpm_list_free(deepref) numbers.free() }