-
Notifications
You must be signed in to change notification settings - Fork 3
/
sorty_test.go
124 lines (105 loc) · 3.14 KB
/
sorty_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
package sorty
import (
"math/rand"
"testing"
"time"
)
func TestLessError(t *testing.T) {
// Less requires list of maps
stuff := make([]interface{}, 0)
stuff = append(stuff, map[string]string{"key": "val"})
stuff = append(stuff, []int{42})
// fail unless we get a panic
defer func() {
if r := recover(); r == nil {
t.Fail()
}
}()
NewSorter().ByKeys([]string{"key"}).Sort(stuff)
stuff[0], stuff[1] = stuff[1], stuff[0]
NewSorter().ByKeys([]string{"key"}).Sort(stuff)
}
func TestLessError_reversedItems(t *testing.T) {
// Less requires list of maps
stuff := make([]interface{}, 0)
stuff = append(stuff, []int{42})
stuff = append(stuff, map[string]string{"key": "val"})
// fail unless we get a panic
defer func() {
if r := recover(); r == nil {
t.Fail()
}
}()
NewSorter().ByKeys([]string{"key"}).Sort(stuff)
}
func TestDups(t *testing.T) {
// Less requires list of maps
stuff := make([]interface{}, 0)
stuff = append(stuff, map[string]string{"key": "val"})
stuff = append(stuff, map[string]string{"key": "val"})
NewSorter().ByKeys([]string{"key"}).Sort(stuff)
if len(stuff) != 2 {
t.Fail()
}
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func TestTypes(t *testing.T) {
rand.Seed(time.Now().Unix())
stuff := make([]map[string]interface{}, 0)
for i := 0; i < 1000; i++ {
stuff = append(stuff, map[string]interface{}{
"string": randString(25),
"int": rand.Int(),
"int8": int8(rand.Int()),
"int16": int16(rand.Int()),
"int32": int32(rand.Int()),
"int64": int64(rand.Int()),
"uint": uint(rand.Int()),
"uint8": uint8(rand.Int()),
"uint16": uint16(rand.Int()),
"uint32": uint32(rand.Int()),
"uint64": uint64(rand.Int()),
"float32": rand.Float32(),
"float64": rand.Float64(),
})
}
// sort stuff over and over with varying types
// this is just to verify that we get no errors
// when sorting by various builtin types
NewSorter().ByKeys([]string{"string"}).Sort(stuff)
NewSorter().ByKeys([]string{"-int"}).Sort(stuff)
NewSorter().ByKeys([]string{"int8"}).Sort(stuff)
NewSorter().ByKeys([]string{"-int16"}).Sort(stuff)
NewSorter().ByKeys([]string{"int32"}).Sort(stuff)
NewSorter().ByKeys([]string{"-int64"}).Sort(stuff)
NewSorter().ByKeys([]string{"uint"}).Sort(stuff)
NewSorter().ByKeys([]string{"-uint8"}).Sort(stuff)
NewSorter().ByKeys([]string{"uint16"}).Sort(stuff)
NewSorter().ByKeys([]string{"-uint32"}).Sort(stuff)
NewSorter().ByKeys([]string{"uint64"}).Sort(stuff)
NewSorter().ByKeys([]string{"-float32"}).Sort(stuff)
NewSorter().ByKeys([]string{"float64"}).Sort(stuff)
if len(stuff) != 1000 {
t.Fail()
}
}
func TestUnknownTypes(t *testing.T) {
// set up unsortable list, we dont know how to sort bool
stuff := make([]interface{}, 0)
stuff = append(stuff, map[string]bool{"key": true})
stuff = append(stuff, map[string]bool{"key": false})
// fail unless we get a panic
defer func() {
if r := recover(); r == nil {
t.Fail()
}
}()
NewSorter().ByKeys([]string{"key"}).Sort(stuff)
}