-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils_test.go
133 lines (126 loc) · 2.99 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package govatar
import (
"math/rand"
"reflect"
"sort"
"testing"
)
func TestRandInt(t *testing.T) {
rnd := rand.New(rand.NewSource(5))
min, max := 5, 10
for i := 0; i < 20; i++ {
ri := randInt(rnd, min, max)
if ri < min || ri > max {
t.Fatalf("Random value %d is out of range (%d, %d)", ri, min, max)
}
}
}
func TestRandStringSliceItem(t *testing.T) {
slice := []string{"a", "b", "c", "d", "e"}
rnd := rand.New(rand.NewSource(10))
values := make([]string, 0, 5)
for i := 0; i < 5; i++ {
values = append(values, randStringSliceItem(rnd, slice))
}
hasRandomValues := false
for _, val := range values {
if val != slice[0] {
hasRandomValues = true
break
}
}
if !hasRandomValues {
t.Fatal("Values are the same")
}
}
func TestSortValid(t *testing.T) {
cases := []struct {
data, expected []string
}{
{
nil,
nil,
},
{
[]string{},
[]string{},
},
{
[]string{"a"},
[]string{"a"},
},
{
[]string{"0"},
[]string{"0"},
},
{
[]string{"data", "data20", "data3"},
[]string{"data", "data3", "data20"},
},
{
[]string{"1", "2", "30", "22", "0", "00", "3"},
[]string{"0", "00", "1", "2", "3", "22", "30"},
},
{
[]string{"A1", "A0", "A21", "A11", "A111", "A2"},
[]string{"A0", "A1", "A2", "A11", "A21", "A111"},
},
{
[]string{"A1BA1", "A11AA1", "A2AB0", "B1AA1", "A1AA1"},
[]string{"A1AA1", "A1BA1", "A2AB0", "A11AA1", "B1AA1"},
},
{
[]string{"1ax10", "1a10", "1ax2", "1ax"},
[]string{"1a10", "1ax", "1ax2", "1ax10"},
},
{
[]string{"z1a10", "z1ax2", "z1ax"},
[]string{"z1a10", "z1ax", "z1ax2"},
},
{
// regression test for #8
[]string{"a0000001", "a0001"},
[]string{"a0001", "a0000001"},
},
{
// regression test for #10 - Number sort before any symbols even if theyre lower on the ASCII table
[]string{"#1", "1", "_1", "a"},
[]string{"1", "#1", "_1", "a"},
},
{
// regression test for #10 - Number sort before any symbols even if theyre lower on the ASCII table
[]string{"#1", "1", "_1", "a"},
[]string{"1", "#1", "_1", "a"},
},
{ // test correct handling of space-only strings
[]string{"1", " ", "0"},
[]string{"0", "1", " "},
},
{ // test correct handling of multiple spaces being correctly ordered AFTER numbers
[]string{"1", " ", " 1", " "},
[]string{"1", " ", " 1", " "},
},
{
[]string{"1", "#1", "a#", "a1"},
[]string{"1", "#1", "a1", "a#"},
},
{
// regression test for #10
[]string{"111111111111111111112", "111111111111111111113", "1111111111111111111120"},
[]string{"111111111111111111112", "111111111111111111113", "1111111111111111111120"},
},
}
for i, c := range cases {
sort.Sort(naturalSort(c.data))
if !reflect.DeepEqual(c.data, c.expected) {
t.Fatalf("Wrong order in test case #%d.\nExpected=%v\nGot=%v", i, c.expected, c.data)
}
}
}
func BenchmarkSort(b *testing.B) {
var data = [...]string{"A1BA1", "A11AA1", "A2AB0", "B1AA1", "A1AA1"}
for ii := 0; ii < b.N; ii++ {
d := naturalSort(data[:])
sort.Sort(d)
}
}