Skip to content

Commit

Permalink
cgen: fix assert [2, 3, 1]!.sorted() == [1, 2, 3]!
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Nov 2, 2024
1 parent fc6e481 commit 9d4b90f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
35 changes: 35 additions & 0 deletions vlib/builtin/fixed_array_sort_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,30 @@ fn test_sorted_immutable_original_should_not_change() {
b := a.sorted()
assert a == ['hi', '1', '5', '3']!
assert b == ['1', '3', '5', 'hi']!
assert ['hi', '1', '5', '3']!.sorted() == ['1', '3', '5', 'hi']!
}

fn test_sorted_mutable_original_should_not_change() {
mut a := ['hi', '1', '5', '3']!
b := a.sorted()
assert a == ['hi', '1', '5', '3']!
assert b == ['1', '3', '5', 'hi']!
assert ['hi', '1', '5', '3']!.sorted() == ['1', '3', '5', 'hi']!
}

fn test_sorted_reversed() {
aa := ['hi', '1', '5', '3']!
bb := aa.sorted(a > b)
assert aa == ['hi', '1', '5', '3']!
assert bb == ['hi', '5', '3', '1']!
assert ['hi', '1', '5', '3']!.sorted(a > b) == ['hi', '5', '3', '1']!
}

fn test_sorted_by_len() {
a := ['hi', 'abc', 'a', 'zzzzz']!
c := a.sorted(a.len < b.len)
assert c == ['a', 'hi', 'abc', 'zzzzz']!
assert ['hi', 'abc', 'a', 'zzzzz']!.sorted(a.len < b.len) == ['a', 'hi', 'abc', 'zzzzz']!
}

fn test_sort_with_compare_1() {
Expand Down Expand Up @@ -97,6 +101,15 @@ fn test_sorted_with_compare_1() {
})
assert a == ['hi', '1', '5', '3']!
assert b == ['1', '3', '5', 'hi']!
assert ['hi', '1', '5', '3']!.sorted_with_compare(fn (a &string, b &string) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}) == ['1', '3', '5', 'hi']!
}

struct Ka {
Expand Down Expand Up @@ -163,4 +176,26 @@ fn test_sorted_with_compare_2() {
assert b[1].i == 100
assert b[2].s == 'ccc'
assert b[2].i == 102

b2 := [
Ka{
s: 'bbb'
i: 100
},
Ka{
s: 'aaa'
i: 101
},
Ka{
s: 'ccc'
i: 102
},
]!.sorted_with_compare(cmp)

assert b2[0].s == 'aaa'
assert b2[0].i == 101
assert b2[1].s == 'bbb'
assert b2[1].i == 100
assert b2[2].s == 'ccc'
assert b2[2].i == 102
}
12 changes: 10 additions & 2 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,11 @@ fn (mut g Gen) gen_array_sorted(node ast.CallExpr) {
} else {
g.writeln('${atype} ${past.tmp_var};')
g.write('memcpy(&${past.tmp_var}, &')
g.expr(node.left)
if node.left is ast.ArrayInit {
g.fixed_array_init_with_cast(node.left, node.left_type)
} else {
g.expr(node.left)
}
g.writeln(', sizeof(${atype}));')
}

Expand Down Expand Up @@ -794,7 +798,11 @@ fn (mut g Gen) gen_fixed_array_sorted_with_compare(node ast.CallExpr) {
atype := g.styp(node.return_type)
g.writeln('${atype} ${past.tmp_var};')
g.write('memcpy(&${past.tmp_var}, &')
g.expr(node.left)
if node.left is ast.ArrayInit {
g.fixed_array_init_with_cast(node.left, node.left_type)
} else {
g.expr(node.left)
}
g.writeln(', sizeof(${atype}));')

unsafe {
Expand Down

0 comments on commit 9d4b90f

Please sign in to comment.