sort: fix C compilation error for `fn abc(mut a []int) { a.sort() }`
parent
7dde82322a
commit
94ced907d2
|
@ -0,0 +1,40 @@
|
|||
const (
|
||||
unsorted = [2,30,10,20,1]
|
||||
sorted_asc = [1, 2, 10, 20, 30]
|
||||
sorted_desc = [30, 20, 10, 2, 1]
|
||||
)
|
||||
|
||||
fn test_sorting_simple() {
|
||||
mut a := unsorted
|
||||
a.sort()
|
||||
eprintln(' a: $a')
|
||||
assert a == sorted_asc
|
||||
}
|
||||
|
||||
fn test_sorting_with_condition_expression() {
|
||||
mut a := unsorted
|
||||
a.sort(a>b)
|
||||
eprintln(' a: $a')
|
||||
assert a == sorted_desc
|
||||
}
|
||||
|
||||
|
||||
fn mysort (mut a []int) {
|
||||
a.sort()
|
||||
}
|
||||
|
||||
fn test_sorting_by_passing_a_mut_array_to_a_function() {
|
||||
mut a := unsorted
|
||||
mysort(mut a)
|
||||
eprintln(' a: $a')
|
||||
assert a == sorted_asc
|
||||
}
|
||||
|
||||
/*
|
||||
fn test_sorting_by_passing_an_anonymous_sorting_function() {
|
||||
mut a := unsorted
|
||||
a.sort(fn(a &int, b &int) int { return *b - *a })
|
||||
eprintln(' a: $a')
|
||||
assert a == sort_desc
|
||||
}
|
||||
*/
|
|
@ -3997,13 +3997,15 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
|
|||
compare_fn += '_reverse'
|
||||
}
|
||||
//
|
||||
deref := if node.left_type.is_ptr() || node.left_type.is_pointer() { '->' } else { '.' }
|
||||
// eprintln('> qsort: pointer $node.left_type | deref: `$deref`')
|
||||
g.write('qsort(')
|
||||
g.expr(node.left)
|
||||
g.write('.data, ')
|
||||
g.write('${deref}data, ')
|
||||
g.expr(node.left)
|
||||
g.write('.len, ')
|
||||
g.write('${deref}len, ')
|
||||
g.expr(node.left)
|
||||
g.writeln('.element_size, $compare_fn);')
|
||||
g.writeln('${deref}element_size, $compare_fn);')
|
||||
}
|
||||
|
||||
// `nums.filter(it % 2 == 0)`
|
||||
|
|
Loading…
Reference in New Issue