-
Notifications
You must be signed in to change notification settings - Fork 225
/
orderby_test.go
158 lines (127 loc) · 4.05 KB
/
orderby_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package linq
import "testing"
func TestEmpty(t *testing.T) {
q := From([]string{}).OrderBy(func(in interface{}) interface{} {
return 0
})
_, ok := q.Iterate()()
if ok {
t.Errorf("Iterator for empty collection must return ok=false")
}
}
func TestOrderBy(t *testing.T) {
slice := make([]foo, 100)
for i := len(slice) - 1; i >= 0; i-- {
slice[i].f1 = i
}
q := From(slice).OrderBy(func(i interface{}) interface{} {
return i.(foo).f1
})
j := 0
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.(foo).f1 != j {
t.Errorf("OrderBy()[%v]=%v expected %v", j, item, foo{f1: j})
}
j++
}
}
func TestOrderByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "OrderByT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).OrderByT(func(item, j int) int { return item + 2 })
})
}
func TestOrderByDescending(t *testing.T) {
slice := make([]foo, 100)
for i := 0; i < len(slice); i++ {
slice[i].f1 = i
}
q := From(slice).OrderByDescending(func(i interface{}) interface{} {
return i.(foo).f1
})
j := len(slice) - 1
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.(foo).f1 != j {
t.Errorf("OrderByDescending()[%v]=%v expected %v", j, item, foo{f1: j})
}
j--
}
}
func TestOrderByDescendingT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "OrderByDescendingT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).OrderByDescendingT(func(item, j int) int { return item + 2 })
})
}
func TestThenBy(t *testing.T) {
slice := make([]foo, 1000)
for i := len(slice) - 1; i >= 0; i-- {
slice[i].f1 = i
slice[i].f2 = i%2 == 0
}
q := From(slice).OrderBy(func(i interface{}) interface{} {
return i.(foo).f2
}).ThenBy(func(i interface{}) interface{} {
return i.(foo).f1
})
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.(foo).f2 != (item.(foo).f1%2 == 0) {
t.Errorf("OrderBy().ThenBy()=%v", item)
}
}
}
func TestThenByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "ThenByT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)bool'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).
OrderByT(func(item int) int { return item }).
ThenByT(func(item, j int) bool { return true })
})
}
func TestThenByDescending(t *testing.T) {
slice := make([]foo, 1000)
for i := len(slice) - 1; i >= 0; i-- {
slice[i].f1 = i
slice[i].f2 = i%2 == 0
}
q := From(slice).OrderBy(func(i interface{}) interface{} {
return i.(foo).f2
}).ThenByDescending(func(i interface{}) interface{} {
return i.(foo).f1
})
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.(foo).f2 != (item.(foo).f1%2 == 0) {
t.Errorf("OrderBy().ThenByDescending()=%v", item)
}
}
}
func TestThenByDescendingT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "ThenByDescending: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)bool'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).
OrderByT(func(item int) int { return item }).
ThenByDescendingT(func(item, j int) bool { return true })
})
}
func TestSort(t *testing.T) {
slice := make([]foo, 100)
for i := len(slice) - 1; i >= 0; i-- {
slice[i].f1 = i
}
q := From(slice).Sort(func(i, j interface{}) bool {
return i.(foo).f1 < j.(foo).f1
})
j := 0
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.(foo).f1 != j {
t.Errorf("Sort()[%v]=%v expected %v", j, item, foo{f1: j})
}
j++
}
}
func TestSortT_PanicWhenLessFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "SortT: parameter [lessFn] has a invalid function signature. Expected: 'func(T,T)bool', actual: 'func(int,int)string'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).SortT(func(i, j int) string { return "" })
})
}