1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package collate
17
18 import (
19 "math/rand"
20 "sort"
21 "testing"
22
23 "github.com/google/go-cmp/cmp"
24 )
25
26 func TestCompareString(t *testing.T) {
27 want := []string{
28
29 `"_"`, `"-"`, `","`, `";"`, `":"`, `"!"`, `"?"`,
30 `"."`, `"'"`, `"""`, `"("`, `")"`, `"["`, `"]"`, `"{"`, `"}"`,
31 `"@"`, `"*"`, `"/"`, `"\"`, `"&"`, `"#"`, `"%"`, `"+"`, `"<"`,
32 `"="`, `">"`, `"|"`, `"~"`, `"$"`, `"0"`, `"1"`, `"2"`, `"3"`,
33 `"4"`, `"5"`, `"6"`, `"7"`, `"8"`, `"9"`,
34 `"a"`, `"A"`, `"b"`, `"B"`, `"c"`, `"C"`, `"d"`, `"D"`, `"e"`,
35 `"E"`, `"f"`, `"F"`, `"g"`, `"G"`, `"h"`, `"H"`, `"i"`, `"I"`,
36 `"j"`, `"J"`, `"k"`, `"K"`, `"l"`, `"L"`, `"m"`, `"M"`, `"n"`,
37 `"N"`, `"o"`, `"O"`, `"p"`, `"P"`, `"q"`, `"Q"`, `"r"`, `"R"`,
38 `"s"`, `"S"`, `"t"`, `"T"`, `"u"`, `"U"`, `"v"`, `"V"`, `"w"`,
39 `"W"`, `"x"`, `"X"`, `"y"`, `"Y"`, `"z"`, `"Z"`,
40 }
41 input := make([]string, len(want))
42 copy(input, want)
43
44 rand.Shuffle(len(input), func(i, j int) { input[i], input[j] = input[j], input[i] })
45
46 sort.Slice(input, func(i, j int) bool {
47 return CompareString(input[i], input[j]) < 0
48 })
49
50 if d := cmp.Diff(want, input); d != "" {
51 t.Errorf("Unexpected result:\n%s", d)
52 }
53 }
54
55 func TestCompareObject(t *testing.T) {
56 want := []interface{}{
57 nil,
58 false,
59 true,
60
61
62 float64(1),
63 float64(2),
64 float64(3.0),
65 float64(4),
66
67
68 "a",
69 "A",
70 "aa",
71 "b",
72 "B",
73 "ba",
74 "bb",
75
76
77
78 []interface{}{"a"},
79 []interface{}{"b"},
80 []interface{}{"b", "c"},
81 []interface{}{"b", "c", "a"},
82 []interface{}{"b", "d"},
83 []interface{}{"b", "d", "e"},
84
85
86
87 map[string]interface{}{"a": float64(1)},
88 map[string]interface{}{"a": float64(2)},
89 map[string]interface{}{"b": float64(1)},
90 map[string]interface{}{"b": float64(2)},
91
92
93 map[string]interface{}{"b": float64(2), "c": float64(2)},
94 }
95
96 input := make([]interface{}, len(want))
97 copy(input, want)
98
99 rand.Shuffle(len(input), func(i, j int) { input[i], input[j] = input[j], input[i] })
100
101 sort.Slice(input, func(i, j int) bool {
102 return CompareObject(input[i], input[j]) < 0
103 })
104
105 if d := cmp.Diff(want, input); d != "" {
106 t.Errorf("Unexpected result:\n%s", d)
107 }
108 }
109
View as plain text