-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort_test.go
More file actions
92 lines (81 loc) · 1.87 KB
/
sort_test.go
File metadata and controls
92 lines (81 loc) · 1.87 KB
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
package easysort
import (
"sort"
"testing"
"time"
)
type TestStruct struct {
String string
Time time.Time
Float32 float64
Float64 float32
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint16 uint16
Uint32 uint32
Uint64 uint64
Byte byte
Bytes []byte
}
type TestStructs []TestStruct
func (ts TestStructs) Len() int {
return len(ts)
}
func (ts TestStructs) Less(i, j int) bool {
return ts[i].Int < ts[j].Int
}
func (ts TestStructs) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}
var testA = TestStruct{"a", time.Unix(0, 0), 1.11, 1.11, false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, []byte("testA")}
var testB = TestStruct{"b", time.Unix(100000, 0), 2.22, 2.22, false, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, []byte("testB")}
var testC = TestStruct{"c", time.Unix(100000000, 0), 3.33, 3.33, true, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, []byte("testC")}
func TestSortByField(t *testing.T) {
slice := []TestStruct{testA, testC, testB}
ByField(slice, "Bytes")
t.Logf("result: %v", slice)
}
/*
func TestSortByFieldMapStringInterface(t *testing.T) {
slice := []map[string]interface{}{
map[string]interface{}{"user":"testb", "age": 45},
map[string]interface{}{"user":"testa", "age": 35},
}
ByField(slice, "age")
t.Logf("result: %v", slice)
}
*/
func TestReverse(t *testing.T) {
slice := []TestStruct{testA, testC, testB}
Reverse(slice)
t.Logf("reverse: %v", slice)
}
func BenchmarkByFieldInt(b *testing.B) {
len := 100000
slice := make([]TestStruct, len)
for i := 0; i < len-2; i++ {
slice[i] = testB
}
slice[len-2] = testC
slice[len-1] = testA
for i := 0; i < b.N; i++ {
ByField(slice, "Int")
}
}
func BenchmarkNativeByInt(b *testing.B) {
len := 100000
slice := make(TestStructs, len)
for i := 0; i < len-2; i++ {
slice[i] = testB
}
slice[len-2] = testC
slice[len-1] = testA
for i := 0; i < b.N; i++ {
sort.Sort(slice)
}
}