-
Notifications
You must be signed in to change notification settings - Fork 0
/
valuesort_test.go
122 lines (106 loc) · 2.44 KB
/
valuesort_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
package polo
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValueSort_Panics(t *testing.T) {
t.Run("Array Length", func(t *testing.T) {
a := [4]string{}
b := [3]string{}
assert.PanicsWithValue(t, "array length must equal", func() {
ValueSort([]reflect.Value{reflect.ValueOf(a), reflect.ValueOf(b)})(0, 1)
})
})
t.Run("Invalid Type", func(t *testing.T) {
a := make([]string, 2)
b := make([]string, 4)
assert.PanicsWithValue(t, "unsupported key compare", func() {
ValueSort([]reflect.Value{reflect.ValueOf(a), reflect.ValueOf(b)})(0, 1)
})
})
}
func TestValueCmp_Panics(t *testing.T) {
t.Run("Array Length", func(t *testing.T) {
a := [4]string{}
b := [3]string{}
assert.PanicsWithValue(t, "array length must equal", func() {
ValueCmp(reflect.ValueOf(a), reflect.ValueOf(b))
})
})
t.Run("Invalid Type", func(t *testing.T) {
a := make([]string, 2)
b := make([]string, 4)
assert.PanicsWithValue(t, "unsupported key compare", func() {
ValueCmp(reflect.ValueOf(a), reflect.ValueOf(b))
})
})
}
func TestKey(t *testing.T) {
tests := []struct {
name string
idx int
val any
}{
{"Int Key", 0, 42},
{"String Key", 1, "test"},
{"Float Key", 2, 3.14},
{"Bool Key", 3, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key := NewKey(tt.idx, tt.val)
assert.Equal(t, tt.idx, key.Index())
assert.Equal(t, reflect.ValueOf(tt.val), key.val)
})
}
}
func TestKeySort(t *testing.T) {
tests := []struct {
name string
keys []Key
sorted []Key
}{
{
"Int Keys",
[]Key{NewKey(0, 3), NewKey(1, 1), NewKey(2, 2)},
[]Key{NewKey(1, 1), NewKey(2, 2), NewKey(0, 3)},
},
{
"String Keys",
[]Key{NewKey(0, "c"), NewKey(1, "a"), NewKey(2, "b")},
[]Key{NewKey(1, "a"), NewKey(2, "b"), NewKey(0, "c")},
},
{
"Float Keys",
[]Key{NewKey(0, 3.1), NewKey(1, 1.1), NewKey(2, 2.1)},
[]Key{NewKey(1, 1.1), NewKey(2, 2.1), NewKey(0, 3.1)},
},
{
"Bool Keys",
[]Key{NewKey(0, true), NewKey(1, false)},
[]Key{NewKey(1, false), NewKey(0, true)},
},
{
"Single Key",
[]Key{NewKey(0, 42)},
[]Key{NewKey(0, 42)},
},
{
"No Keys",
[]Key{},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ch := make(chan Key)
go KeySort(tt.keys, ch)
var sortedKeys []Key
for key := range ch {
sortedKeys = append(sortedKeys, key)
}
assert.Equal(t, tt.sorted, sortedKeys)
})
}
}