@@ -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