-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathsort_test.go
117 lines (110 loc) · 1.99 KB
/
sort_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
package column
import (
"sort"
"testing"
)
func Cmp(a, b []Person, t *testing.T) {
if len(a) != len(b) {
t.Log("different lengths")
t.Logf("%s\n%s", a, b)
t.Fail()
return
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
t.Logf("different elements, starting at %d", i)
t.Logf("%s\n%s", a, b)
t.Fail()
return
}
}
}
func TestByColumns_Age(t *testing.T) {
people := []Person{
{"Alice", 20},
{"Alice", 12},
}
c := &ByColumns{people, nil, 2}
c.Select(c.LessAge)
sort.Sort(c)
Cmp(people, []Person{
{"Alice", 12}, {"Alice", 20},
}, t)
}
func TestByColumns_Name(t *testing.T) {
people := []Person{
{"Bob", 20},
{"Alice", 20},
}
c := &ByColumns{people, nil, 2}
c.Select(c.LessName)
sort.Sort(c)
Cmp(people, []Person{
{"Alice", 20},
{"Bob", 20},
}, t)
}
func TestByColumns_NameAge(t *testing.T) {
people := []Person{
{"Alice", 20},
{"Bob", 12},
{"Bob", 20},
{"Alice", 12},
}
c := &ByColumns{people, nil, 2}
c.Select(c.LessAge)
c.Select(c.LessName)
sort.Sort(c)
Cmp(people, []Person{
{"Alice", 12},
{"Alice", 20},
{"Bob", 12},
{"Bob", 20},
}, t)
}
func TestByColumns_AgeName(t *testing.T) {
people := []Person{
{"Alice", 20},
{"Bob", 12},
{"Bob", 20},
{"Alice", 12},
}
c := &ByColumns{people, nil, 2}
c.Select(c.LessName)
c.Select(c.LessAge)
sort.Sort(c)
Cmp(people, []Person{
{"Alice", 12},
{"Bob", 12},
{"Alice", 20},
{"Bob", 20},
}, t)
}
func TestByColumns_SumOfAgeDigitsNameAge(t *testing.T) {
people := []Person{
{"Aaron", 9},
{"Aaron", 81},
{"Alice", 20},
{"Bob", 12},
{"Bob", 20},
{"Alice", 12},
}
maxComparisons := 3
c := &ByColumns{people, nil, maxComparisons}
c.Select(c.LessAge)
c.Select(c.LessAge)
c.Select(c.LessName)
c.Select(c.LessSumOfAgeDigits)
sort.Sort(c)
Cmp(people, []Person{
{"Alice", 20},
{"Bob", 20},
{"Alice", 12},
{"Bob", 12},
{"Aaron", 9},
{"Aaron", 81},
}, t)
if len(c.columns) > maxComparisons {
t.Errorf("Want %d comparisons, got %d", maxComparisons, len(c.columns))
}
}