make the regression test more thorough; prepare for the pure V version
parent
cc8f40df86
commit
4535b5edc2
|
@ -79,7 +79,13 @@ mut:
|
|||
real_sort_cb FnSortContextCB
|
||||
}
|
||||
|
||||
|
||||
// GLIBC:
|
||||
// void qsort_r(void *base, size_t nmemb, size_t size,
|
||||
// int (*compar)(const void *, const void *, void *),
|
||||
// void *arg);
|
||||
fn vqsort_context_pure_v(base voidptr, nmemb usize, size usize, sort_cb FnSortContextCB, context voidptr) {
|
||||
C.qsort_r(base, nmemb, size, voidptr(sort_cb), context)
|
||||
}
|
||||
|
||||
[inline; unsafe]
|
||||
|
|
|
@ -3,11 +3,16 @@ mut:
|
|||
comparisons []string
|
||||
}
|
||||
|
||||
fn test_sort_with_compare() {
|
||||
mut a := ['hi', '1', '5', '3']
|
||||
mut context := Context{}
|
||||
a.sort_with_compare_context(fn (a &string, b &string, mut context Context) int {
|
||||
context.comparisons << 'a: ${*a} | b: ${*b}'
|
||||
fn (c Context) str() string {
|
||||
mut res := []string{}
|
||||
for x in c.comparisons {
|
||||
res << x
|
||||
}
|
||||
return '\n' + res.join('\n')
|
||||
}
|
||||
|
||||
fn sort_cb(a &string, b &string, mut context Context) int {
|
||||
context.comparisons << 'a: "${*a}" | b: "${*b}"'
|
||||
if a < b {
|
||||
return -1
|
||||
}
|
||||
|
@ -15,15 +20,32 @@ fn test_sort_with_compare() {
|
|||
return 1
|
||||
}
|
||||
return 0
|
||||
}, context)
|
||||
}
|
||||
|
||||
fn test_sort_with_compare() {
|
||||
mut a := ['hi', '1', '5', '3']
|
||||
mut context := Context{}
|
||||
a.sort_with_compare_context(sort_cb, context)
|
||||
dump(a)
|
||||
dump(context)
|
||||
assert a == ['1', '3', '5', 'hi']
|
||||
assert context.comparisons == [
|
||||
'a: hi | b: 1',
|
||||
'a: 5 | b: 3',
|
||||
'a: 1 | b: 3',
|
||||
'a: hi | b: 3',
|
||||
'a: hi | b: 5',
|
||||
'a: "hi" | b: "1"',
|
||||
'a: "5" | b: "3"',
|
||||
'a: "1" | b: "3"',
|
||||
'a: "hi" | b: "3"',
|
||||
'a: "hi" | b: "5"',
|
||||
]
|
||||
//
|
||||
mut already_sorted_context := Context{}
|
||||
a.sort_with_compare_context(sort_cb, already_sorted_context)
|
||||
dump(a)
|
||||
dump(already_sorted_context)
|
||||
assert context != already_sorted_context
|
||||
assert already_sorted_context.comparisons == [
|
||||
'a: "1" | b: "3"',
|
||||
'a: "5" | b: "hi"',
|
||||
'a: "1" | b: "5"',
|
||||
'a: "3" | b: "5"',
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue