-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeysort_test.go
83 lines (74 loc) · 1.97 KB
/
keysort_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
package keysort_test
import (
"reflect"
"strconv"
"testing"
"github.com/itroot/keysort"
)
func TestSort_Indentical(t *testing.T) {
slice := []int{2, 1, 3, 4, 5}
keysort.Sort(slice, func(i int) keysort.Sortable {
return keysort.Sequence{slice[i]}
})
if !reflect.DeepEqual(slice, []int{1, 2, 3, 4, 5}) {
t.Fatal()
}
}
func TestSort_String(t *testing.T) {
slice := []int{2, 1, 3, 4, 5}
keysort.Sort(slice, func(i int) keysort.Sortable {
return keysort.Sequence{strconv.Itoa(slice[i])}
})
if !reflect.DeepEqual(slice, []int{1, 2, 3, 4, 5}) {
t.Fatal()
}
}
func TestSort_MultisortString(t *testing.T) {
slice := []int{2, 1, 3, 4, 5}
keysort.Sort(slice, func(i int) keysort.Sortable {
return keysort.Sequence{int(1), strconv.Itoa(slice[i])}
})
if !reflect.DeepEqual(slice, []int{1, 2, 3, 4, 5}) {
t.Fatal(slice)
}
}
func TestSort_Reversed(t *testing.T) {
slice := []int{2, 1, 3, 4, 5}
keysort.Sort(slice, func(i int) keysort.Sortable {
return keysort.Sequence{-slice[i]}
})
if !reflect.DeepEqual(slice, []int{5, 4, 3, 2, 1}) {
t.Fatal()
}
}
func TestSort_EvenOdd(t *testing.T) {
slice := []int{2, 1, 3, 4, 5}
keysort.Sort(slice, func(i int) keysort.Sortable {
return keysort.Sequence{slice[i]%2 == 1, slice[i]}
})
if !reflect.DeepEqual(slice, []int{2, 4, 1, 3, 5}) {
t.Fatal(slice)
}
}
func TestSort_Desc(t *testing.T) {
slice := []int{2, 1, 3, 4, 5}
keysort.Sort(slice, func(i int) keysort.Sortable {
return keysort.Sequence{slice[i]%2 == 1, keysort.StringDesc(strconv.Itoa(slice[i]))}
})
if !reflect.DeepEqual(slice, []int{4, 2, 5, 3, 1}) {
t.Fatal()
}
}
func TestSort_CustomType(t *testing.T) {
type CustomInt int
type CustomString string
slice := []CustomInt{2, 1, 3, 4, 5}
keysort.Sort(slice, func(i int) keysort.Sortable {
var cs CustomString
cs = CustomString(strconv.Itoa(int(slice[i])))
return keysort.Sequence{slice[i]%2 == 1, keysort.StringDesc(cs)}
})
if !reflect.DeepEqual(slice, []CustomInt{4, 2, 5, 3, 1}) {
t.Fatal(slice)
}
}