-
Notifications
You must be signed in to change notification settings - Fork 1
/
orderby_test.go
121 lines (94 loc) · 2.34 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
package flinx
import (
"testing"
"github.com/kom0055/go-flinx/generics"
)
func TestEmpty(t *testing.T) {
q := OrderBy(generics.NumericCompare[int], func(in string) int {
return 0
})(FromSlice([]string{}))
_, 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 := OrderBy(generics.NumericCompare[int], getF1)(FromSlice(slice))
j := 0
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.f1 != j {
t.Errorf("OrderBy()[%v]=%v expected %v", j, item, foo{f1: j})
}
j++
}
}
func TestOrderByDescending(t *testing.T) {
slice := make([]foo, 100)
for i := 0; i < len(slice); i++ {
slice[i].f1 = i
}
q := OrderByDescending(generics.NumericCompare[int],
getF1)(FromSlice(slice))
j := len(slice) - 1
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.f1 != j {
t.Errorf("OrderByDescending()[%v]=%v expected %v", j, item, foo{f1: j})
}
j--
}
}
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 := ThenBy(generics.OrderedCompare[int], getF1)(
OrderBy(generics.BoolCompare, getF2)(FromSlice(slice)),
)
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.f2 != (item.f1%2 == 0) {
t.Errorf("OrderBy().ThenBy()=%v", item)
}
}
}
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
}
orderByFn := OrderBy(generics.BoolCompare, getF2)(FromSlice(slice))
thenByDescFn := ThenByDescending(generics.NumericCompare[int], getF1)(orderByFn)
q := thenByDescFn
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.f2 != (item.f1%2 == 0) {
t.Errorf("OrderBy().ThenByDescending()=%v", item)
}
}
}
func TestSort(t *testing.T) {
slice := make([]foo, 100)
for i := len(slice) - 1; i >= 0; i-- {
slice[i].f1 = i
}
q := Sort(func(i, j foo) bool {
return i.f1 < j.f1
})(FromSlice(slice))
j := 0
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item.f1 != j {
t.Errorf("Sort()[%v]=%v expected %v", j, item, foo{f1: j})
}
j++
}
}