-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorts_test.go
137 lines (125 loc) · 2.84 KB
/
sorts_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
134
135
136
137
package myplayground
import (
"reflect"
"sort"
"testing"
)
func TestPersonSorter(t *testing.T) {
t.Parallel()
persons := []Person{
{"leonardo", 30},
{"donatello", 10},
{"michelangelo", 100},
{"raphael", 50},
}
expected := []Person{
{"donatello", 10},
{"leonardo", 30},
{"raphael", 50},
{"michelangelo", 100},
}
if reflect.DeepEqual(persons, expected) {
t.Errorf("people should not be in their right order")
}
sort.Sort(PersonSorter(persons))
if !reflect.DeepEqual(persons, expected) {
t.Errorf("people are not in their right order")
}
}
func TestPersonPairSorter(t *testing.T) {
t.Parallel()
persons := []PersonPair{
{"l", Person{"leonardo", 30}},
{"d", Person{"donatello", 10}},
{"m", Person{"michelangelo", 100}},
{"r", Person{"raphael", 50}},
}
expected := []PersonPair{
{"d", Person{"donatello", 10}},
{"l", Person{"leonardo", 30}},
{"r", Person{"raphael", 50}},
{"m", Person{"michelangelo", 100}},
}
if reflect.DeepEqual(persons, expected) {
t.Errorf("people should not be in their right order")
}
sort.Sort(PersonPairSorter(persons))
if !reflect.DeepEqual(persons, expected) {
t.Errorf("people are not in their right order")
}
}
func TestSortMapByKey(t *testing.T) {
t.Parallel()
persons := map[string]Person{
"l": Person{"leonardo", 30},
"d": Person{"donatello", 10},
"m": Person{"michelangelo", 100},
"r": Person{"raphael", 50},
}
expected := []string{"d", "l", "m", "r"}
got := SortPersonMapByKey(persons)
if !reflect.DeepEqual(got, expected) {
t.Errorf("keys are not in their right order")
}
}
func TestSortMapByValue(t *testing.T) {
t.Parallel()
persons := map[string]Person{
"l": Person{"leonardo", 30},
"d": Person{"donatello", 10},
"m": Person{"michelangelo", 100},
"r": Person{"raphael", 50},
}
expected := []PersonPair{
{"d", Person{"donatello", 10}},
{"l", Person{"leonardo", 30}},
{"r", Person{"raphael", 50}},
{"m", Person{"michelangelo", 100}},
}
got := SortPersonMapByValue(persons)
if !reflect.DeepEqual(got, expected) {
t.Errorf("person pairs are not in their right order")
}
}
func isSorted(l []int) bool {
if len(l) == 0 {
return true
}
previous := l[0]
for i := 0; i < len(l); i++ {
if previous > l[i] {
return false
}
}
return true
}
func TestInsertionSort(t *testing.T) {
t.Parallel()
var testCases = [][]int{
[]int{},
[]int{1},
[]int{1, 2, 3, 4, 5},
[]int{10, 3, 6, 100, -1, -10, 0, 5},
}
for _, tc := range testCases {
InsertionSort(&tc)
if !isSorted(tc) {
t.Errorf("list %#v is not sorted (insertion)", tc)
}
}
}
func TestSelectionSort(t *testing.T) {
t.Parallel()
var testCases = [][]int{
[]int{},
[]int{1},
[]int{1, 2, 3, 4, 5},
[]int{10, 3, 6, 100, -1, -10, 0, 5},
}
for _, tc := range testCases {
SelectionSort(&tc)
if !isSorted(tc) {
t.Errorf("list %#v is not sorted (selection)", tc)
}
}
}