Debian Codegraph @

seq/compare_test.go

raw symbols SCIP
1 package seq
2
3 import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7 )
8
9 type simple struct {
10 b bool
11 i int
12 u uint
13 f float64
14 s string
15 a []byte
16 p *bool
17 l []int
18 m map[string]int
19 }
20
21 func TestCompareSimple(t *testing.T) {
22 var vtrue, vfalse bool
23 vtrue = true
24 var tests = []struct {
25 msg string
26 s1, s2 simple
27 want int
28 }{
29 {msg: "zero", want: 0},
30 {msg: "s1 bool", s1: simple{b: true}, want: 1},
31 {msg: "s2 bool", s2: simple{b: true}, want: -1},
32 {msg: "s1 int", s1: simple{i: 1}, want: 1},
33 {msg: "s2 int", s2: simple{i: 1}, want: -1},
34 {msg: "s1 uint", s1: simple{u: 1}, want: 1},
35 {msg: "s2 uint", s2: simple{u: 1}, want: -1},
36 {msg: "s1 float", s1: simple{f: 1}, want: 1},
37 {msg: "s2 float", s2: simple{f: 1}, want: -1},
38 {msg: "s1 string", s1: simple{s: "a"}, want: 1},
39 {msg: "s2 string", s2: simple{s: "a"}, want: -1},
40 {msg: "s1 []byte", s1: simple{a: []byte{'a'}}, want: 1},
41 {msg: "s2 []byte", s2: simple{a: []byte{'a'}}, want: -1},
42 {msg: "s1 ptr vs nil", s1: simple{p: &vfalse}, want: 1},
43 {msg: "s1 ptr", s1: simple{p: &vtrue}, s2: simple{p: &vfalse}, want: 1},
44 {msg: "s2 ptr", s1: simple{p: &vfalse}, s2: simple{p: &vtrue}, want: -1},
45 {msg: "s1 slice", s1: simple{l: []int{1}}, want: 1},
46 {msg: "s2 slice", s2: simple{l: []int{1}}, want: -1},
47 {msg: "s1 slice", s1: simple{l: []int{0}}, s2: simple{l: []int{1}}, want: -1},
48 {msg: "s1 map", s1: simple{m: map[string]int{"a": 1}}, want: 1},
49 {msg: "s2 map", s2: simple{m: map[string]int{"a": 1}}, want: -1},
50 {msg: "s1 map", s1: simple{m: map[string]int{"a": 1}}, s2: simple{m: map[string]int{"a": 2}}, want: -1},
51 {msg: "s1 map", s1: simple{m: map[string]int{"a": 1}}, s2: simple{m: map[string]int{"b": 1}}, want: -1},
52 }
53
54 opts := cmp.AllowUnexported(simple{})
55 for i, test := range tests {
56 got := Compare(test.s1, test.s2)
57 if got != test.want {
58 diff := cmp.Diff(test.s1, test.s2, opts)
59 t.Errorf("%d %s: got=%d, want=%d, diff:\n%s", i, test.msg, got, test.want, diff)
60 }
61 }
62 }
63
64 type nested struct {
65 s simple
66 a []simple
67 p []*simple
68 }
69
70 func TestCompareNested(t *testing.T) {
71 var tests = []struct {
72 msg string
73 s1, s2 nested
74 want int
75 }{
76 {msg: "zero", want: 0},
77 {msg: "s1 struct", s1: nested{s: simple{i: 1}}, want: 1},
78 {msg: "s2 struct", s2: nested{s: simple{i: 1}}, want: -1},
79 {msg: "s1 slice struct", s1: nested{a: []simple{simple{i: 1}}}, want: 1},
80 {msg: "s2 slice struct", s2: nested{a: []simple{simple{i: 1}}}, want: -1},
81 {msg: "s1 slice struct", s1: nested{a: []simple{simple{i: 1}}}, s2: nested{a: []simple{simple{i: 0}}}, want: 1},
82 {msg: "s2 slice struct", s1: nested{a: []simple{simple{i: 0}}}, s2: nested{a: []simple{simple{i: 1}}}, want: -1},
83 {msg: "s2 slice struct", s1: nested{p: []*simple{&simple{i: 0}}}, s2: nested{p: []*simple{&simple{i: 1}}}, want: -1},
84 }
85
86 opts := cmp.AllowUnexported(simple{}, nested{})
87 for i, test := range tests {
88 got := Compare(test.s1, test.s2)
89 if got != test.want {
90 diff := cmp.Diff(test.s1, test.s2, opts)
91 t.Errorf("%d %s: got=%d, want=%d, diff:\n%s", i, test.msg, got, test.want, diff)
92 }
93
94 }
95 }
96
97 func TestCompareSlice(t *testing.T) {
98 var tests = []struct {
99 msg string
100 s1, s2 []int
101 want int
102 }{
103 {msg: "zero", want: 0},
104 {msg: "equal", s1: []int{1}, s2: []int{1}, want: 0},
105 {msg: "s1 smaller", s1: []int{-1}, s2: []int{1}, want: -1},
106 {msg: "s1 larger", s1: []int{-1}, s2: []int{-2}, want: 1},
107 {msg: "s1 shorter", s1: []int{1}, s2: []int{1, 1}, want: -1},
108 {msg: "s1 longer", s1: []int{1, 1}, s2: []int{1}, want: 1},
109 }
110
111 opts := cmp.AllowUnexported(simple{}, nested{})
112 for i, test := range tests {
113 got := Compare(test.s1, test.s2)
114 if got != test.want {
115 diff := cmp.Diff(test.s1, test.s2, opts)
116 t.Errorf("%d %s: got=%d, want=%d, diff:\n%s", i, test.msg, got, test.want, diff)
117 }
118
119 }
120 }