Skip to content

Commit 3d76abf

Browse files
authored
v3: fix borrowed array map and pointer sorting (#28242)
1 parent 2e1dc2d commit 3d76abf

10 files changed

Lines changed: 11283 additions & 436 deletions

vlib/v/tests/builtin_arrays/array_map_ref_it_test.v

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ fn test_array_map_ref_it_after_filter_keeps_struct_fields() {
2323
id: 2
2424
square: false
2525
}
26+
shapes.rectangles << MapRefRectangle{
27+
id: 3
28+
square: true
29+
}
2630

2731
squares := shapes.get_squares()
2832
rendered := '${squares}'
2933

30-
assert squares.len == 1
34+
assert squares.len == 2
35+
assert voidptr(squares[0]) != voidptr(squares[1])
3136
assert squares[0].id == 1
3237
assert squares[0].square
38+
assert squares[1].id == 3
39+
assert squares[1].square
3340
assert rendered.contains('id: 1')
3441
assert rendered.contains('square: true')
3542
}

vlib/v/tests/builtin_arrays/sorting_compare_fn_with_mut_reference_test.v

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,57 @@ fn test_sort_compare_fn_with_mut_ref_param() {
3838
assert t.items[1] == &Thing{2, 4, 3}
3939
assert t.items[2] == &Thing{5, 7, 6}
4040
}
41+
42+
fn compare_thing_slots(a &&Thing, b &&Thing) int {
43+
return (*a).a - (*b).a
44+
}
45+
46+
fn test_sort_compare_fn_with_pointer_slots() {
47+
mut items := [&Thing{a: 3}, &Thing{a: 1}, &Thing{a: 2}]
48+
items.sort_with_compare(compare_thing_slots)
49+
assert items.map(it.a) == [1, 2, 3]
50+
}
51+
52+
fn compare_thing_values(a &Thing, b &Thing) int {
53+
return a.a - b.a
54+
}
55+
56+
fn test_sort_compare_local_fn_with_pointer_values() {
57+
mut items := [&Thing{a: 3}, &Thing{a: 1}, &Thing{a: 2}]
58+
compare := compare_thing_values
59+
items.sort_with_compare(compare)
60+
assert items.map(it.a) == [1, 2, 3]
61+
}
62+
63+
fn get_thing_comparator() fn (&Thing, &Thing) int {
64+
return compare_thing_values
65+
}
66+
67+
fn test_sort_compare_call_fn_with_pointer_values() {
68+
mut items := [&Thing{a: 3}, &Thing{a: 1}, &Thing{a: 2}]
69+
items.sort_with_compare(get_thing_comparator())
70+
assert items.map(it.a) == [1, 2, 3]
71+
}
72+
73+
struct ThingComparators {
74+
@[required]
75+
compare fn (&Thing, &Thing) int
76+
}
77+
78+
fn test_sort_compare_selector_fn_with_pointer_values() {
79+
mut items := [&Thing{a: 3}, &Thing{a: 1}, &Thing{a: 2}]
80+
comparators := ThingComparators{
81+
compare: compare_thing_values
82+
}
83+
items.sort_with_compare(comparators.compare)
84+
assert items.map(it.a) == [1, 2, 3]
85+
}
86+
87+
type ThingCompare = fn (&Thing, &Thing) int
88+
89+
fn test_sort_compare_alias_fn_with_pointer_values() {
90+
mut items := [&Thing{a: 3}, &Thing{a: 1}, &Thing{a: 2}]
91+
compare := ThingCompare(compare_thing_values)
92+
items.sort_with_compare(compare)
93+
assert items.map(it.a) == [1, 2, 3]
94+
}

0 commit comments

Comments
 (0)