Debian Codegraph @

internal/types/order_test.go

raw
1 // Copyright (c) 2022, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package types_test
8
9 import (
10 "reflect"
11 "testing"
12
13 "github.com/maxatome/go-testdeep/internal/test"
14 "github.com/maxatome/go-testdeep/internal/types"
15 )
16
17 type compareType int
18
19 func (i compareType) Compare(j compareType) int {
20 if i < j {
21 return -1
22 }
23 if i > j {
24 return 1
25 }
26 return 0
27 }
28
29 type lessType int
30
31 func (i lessType) Less(j lessType) bool {
32 return i < j
33 }
34
35 type badType1 int
36
37 func (i badType1) Compare(j ...badType1) int { return 0 } // IsVariadic()
38 func (i badType1) Less(j, k badType1) bool { return false } // NumIn() == 3
39
40 type badType2 int
41
42 func (i badType2) Compare() int { return 0 } // NumIn() == 1
43 func (i badType2) Less(j badType2) {} // NumOut() == 0
44
45 type badType3 int
46
47 func (i badType3) Compare(j badType3) (int, int) { return 0, 0 } // NumOut() == 2
48 func (i badType3) Less(j int) bool { return false } // In(1) ≠ in
49
50 type badType4 int
51
52 func (i badType4) Compare(j badType4) bool { return false } // Out(0) ≠ out
53 func (i badType4) Less(j badType4) int { return 0 } // Out(0) ≠ out
54
55 func TestOrder(t *testing.T) {
56 if types.NewOrder(reflect.TypeOf(0)) != nil {
57 t.Error("types.NewOrder(int) returned non-nil func")
58 }
59
60 fn := types.NewOrder(reflect.TypeOf(compareType(0)))
61 if fn == nil {
62 t.Error("types.NewOrder(compareType) returned nil func")
63 } else {
64 a, b := reflect.ValueOf(compareType(1)), reflect.ValueOf(compareType(2))
65 test.EqualInt(t, fn(a, b), -1)
66 test.EqualInt(t, fn(b, a), 1)
67 test.EqualInt(t, fn(a, a), 0)
68 }
69
70 fn = types.NewOrder(reflect.TypeOf(lessType(0)))
71 if fn == nil {
72 t.Error("types.NewOrder(lessType) returned nil func")
73 } else {
74 a, b := reflect.ValueOf(lessType(1)), reflect.ValueOf(lessType(2))
75 test.EqualInt(t, fn(a, b), -1)
76 test.EqualInt(t, fn(b, a), 1)
77 test.EqualInt(t, fn(a, a), 0)
78 }
79
80 if types.NewOrder(reflect.TypeOf(badType1(0))) != nil {
81 t.Error("types.NewOrder(badType1) returned non-nil func")
82 }
83 if types.NewOrder(reflect.TypeOf(badType2(0))) != nil {
84 t.Error("types.NewOrder(badType2) returned non-nil func")
85 }
86 if types.NewOrder(reflect.TypeOf(badType3(0))) != nil {
87 t.Error("types.NewOrder(badType3) returned non-nil func")
88 }
89 if types.NewOrder(reflect.TypeOf(badType4(0))) != nil {
90 t.Error("types.NewOrder(badType4) returned non-nil func")
91 }
92 }