v2 / vlib / v / tests / structs / operator_overloading_cmp_test.v
35 lines · 31 sloc · 455 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Foo {
2 i int
3}
4
5fn (a Foo) < (b Foo) bool {
6 return a.i < b.i
7}
8
9fn (a Foo) == (b Foo) bool {
10 return a.i == b.i
11}
12
13fn test_operator_overloading_cmp() {
14 a := Foo{
15 i: 38
16 }
17 b := Foo{
18 i: 38
19 }
20 mut arr := [a, b]
21
22 assert (a > b) == false
23 assert (a < b) == false
24 //// /// //
25 assert a >= b
26 assert a <= b
27 //// /// //
28 assert b >= a
29 assert b <= a
30 //// /// //
31 arr.sort(a > b)
32 assert arr[0].i == 38
33 arr.sort(a < b)
34 assert arr[0].i == 38
35}
36