-
Notifications
You must be signed in to change notification settings - Fork 6
/
orderby_test.go
322 lines (311 loc) · 8.98 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package go2linq
import (
"cmp"
"fmt"
"iter"
"math"
"slices"
"testing"
"github.com/solsw/errorhelper"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/OrderByTest.cs
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/OrderByDescendingTest.cs
func TestOrderBy_int(t *testing.T) {
type args struct {
source iter.Seq[int]
}
tests := []struct {
name string
args args
want iter.Seq[int]
wantErr bool
}{
{name: "1234",
args: args{
source: VarToSeq(4, 1, 3, 2),
},
want: VarToSeq(1, 2, 3, 4),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := OrderBy(tt.args.source)
if (err != nil) != tt.wantErr {
t.Errorf("OrderBy() error = %v, wantErr %v", err, tt.wantErr)
return
}
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("OrderBy() = %v, want %v", StringDef(got), StringDef(tt.want))
}
})
}
}
func TestOrderByLs_elelel(t *testing.T) {
type args struct {
source iter.Seq[elelel[int]]
less func(elelel[int], elelel[int]) bool
}
tests := []struct {
name string
args args
want iter.Seq[int]
}{
{name: "1",
args: args{
source: VarToSeq(elelel[int]{1, 10, 22}, elelel[int]{2, 12, 21}, elelel[int]{3, 10, 20}),
less: ThenLess(
func(x, y elelel[int]) bool { return x.e2 < y.e2 },
func(x, y elelel[int]) bool { return x.e3 < y.e3 },
),
},
want: VarToSeq(3, 1, 2),
},
{name: "PrimaryOrderingTakesPrecedence",
args: args{
source: VarToSeq(elelel[int]{1, 10, 20}, elelel[int]{2, 12, 21}, elelel[int]{3, 11, 22}),
less: ThenLess(
func(x, y elelel[int]) bool { return x.e2 < y.e2 },
func(x, y elelel[int]) bool { return x.e3 < y.e3 },
),
},
want: VarToSeq(1, 3, 2),
},
{name: "SecondOrderingIsUsedWhenPrimariesAreEqual",
args: args{
source: VarToSeq(elelel[int]{1, 10, 22}, elelel[int]{2, 12, 21}, elelel[int]{3, 10, 20}),
less: ThenLess(
func(x, y elelel[int]) bool { return x.e2 < y.e2 },
func(x, y elelel[int]) bool { return x.e3 < y.e3 },
),
},
want: VarToSeq(3, 1, 2),
},
{name: "ThenByAfterOrderByDescending",
args: args{
source: VarToSeq(elelel[int]{1, 10, 22}, elelel[int]{2, 12, 21}, elelel[int]{3, 10, 20}),
less: ThenLess(
func(x, y elelel[int]) bool { return y.e2 < x.e2 },
func(x, y elelel[int]) bool { return x.e3 < y.e3 },
),
},
want: VarToSeq(2, 3, 1),
},
{name: "OrderingIsStable",
args: args{
source: VarToSeq(elelel[int]{1, 1, 10}, elelel[int]{2, 1, 11}, elelel[int]{3, 1, 11}, elelel[int]{4, 1, 10}),
less: ThenLess(
func(x, y elelel[int]) bool { return x.e2 < y.e2 },
func(x, y elelel[int]) bool { return x.e3 < y.e3 },
),
},
want: VarToSeq(1, 4, 2, 3),
},
{name: "CustomLess",
args: args{
source: VarToSeq(elelel[int]{1, 1, 15}, elelel[int]{2, 1, -13}, elelel[int]{3, 1, 11}),
less: ThenLess(
func(x, y elelel[int]) bool { return x.e2 < y.e2 },
func(x, y elelel[int]) bool { return cmp.Less(math.Abs(float64(x.e3)), math.Abs(float64(y.e3))) },
),
},
want: VarToSeq(3, 2, 1),
},
{name: "CustomComparer",
args: args{
source: VarToSeq(elelel[int]{1, 1, 15}, elelel[int]{2, 1, -13}, elelel[int]{3, 1, 11}),
less: ThenLess(
func(x, y elelel[int]) bool { return x.e2 < y.e2 },
func(x, y elelel[int]) bool { return cmp.Compare(math.Abs(float64(x.e3)), math.Abs(float64(y.e3))) < 0 },
),
},
want: VarToSeq(3, 2, 1),
},
{name: "ThenByDescendingAfterOrderByDescending",
args: args{
source: VarToSeq(elelel[int]{1, 10, 22}, elelel[int]{2, 12, 21}, elelel[int]{3, 10, 20}),
less: ThenLess(
func(x, y elelel[int]) bool { return cmp.Less(y.e2, x.e2) },
func(x, y elelel[int]) bool { return y.e3 < x.e3 },
),
},
want: VarToSeq(2, 1, 3),
},
{name: "DescendingOrderingIsStable",
args: args{
source: VarToSeq(elelel[int]{1, 1, 10}, elelel[int]{2, 1, 11}, elelel[int]{3, 1, 11}, elelel[int]{4, 1, 10}),
less: ThenLess(
func(x, y elelel[int]) bool { return cmp.Less(y.e2, x.e2) },
func(x, y elelel[int]) bool { return y.e3 < x.e3 },
),
},
want: VarToSeq(2, 3, 1, 4),
},
{name: "CustomDescendingLess",
args: args{
source: VarToSeq(elelel[int]{1, 1, 15}, elelel[int]{2, 1, -13}, elelel[int]{3, 1, 11}),
less: ThenLess(
func(x, y elelel[int]) bool { return cmp.Less(y.e2, x.e2) },
func(x, y elelel[int]) bool { return cmp.Less(math.Abs(float64(y.e3)), math.Abs(float64(x.e3))) },
),
},
want: VarToSeq(1, 2, 3),
},
{name: "CustomDescendingComparer",
args: args{
source: VarToSeq(elelel[int]{1, 1, 15}, elelel[int]{2, 1, -13}, elelel[int]{3, 1, 11}),
less: ThenLess(
func(x, y elelel[int]) bool { return cmp.Less(y.e2, x.e2) },
func(x, y elelel[int]) bool { return cmp.Compare(math.Abs(float64(y.e3)), math.Abs(float64(x.e3))) < 0 },
),
},
want: VarToSeq(1, 2, 3),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got1, _ := OrderByLs(tt.args.source, tt.args.less)
got2, _ := Select(got1, func(e elelel[int]) int { return e.e1 })
equal, _ := SequenceEqual(got2, tt.want)
if !equal {
t.Errorf("OrderByLs() = %v, want %v", StringDef(got2), StringDef(tt.want))
}
})
}
}
func ExampleOrderBy() {
fmt.Println(StringDef[string](
errorhelper.Must(OrderBy(VarToSeq("zero", "one", "two", "three", "four", "five"))),
))
// Output:
// [five four one three two zero]
}
// OrderByEx1 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.orderby
func ExampleOrderByLs() {
pets := []Pet{
{Name: "Barley", Age: 8},
{Name: "Boots", Age: 4},
{Name: "Whiskers", Age: 1},
}
orderByLs, _ := OrderByLs(slices.Values(pets), func(p1, p2 Pet) bool { return p1.Age < p2.Age })
for pet := range orderByLs {
fmt.Printf("%s - %d\n", pet.Name, pet.Age)
}
// Output:
// Whiskers - 1
// Boots - 4
// Barley - 8
}
// OrderByDescendingEx1 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.orderbydescending
func ExampleOrderByDescLs() {
decimals := []float64{6.2, 8.3, 0.5, 1.3, 6.3, 9.7}
less := func(f1, f2 float64) bool {
_, fr1 := math.Modf(f1)
_, fr2 := math.Modf(f2)
if math.Abs(fr1-fr2) < 0.001 {
return f1 < f2
}
return fr1 < fr2
}
orderByDescLs, _ := OrderByDescLs(slices.Values(decimals), less)
for num := range orderByDescLs {
fmt.Println(num)
}
// Output:
// 9.7
// 0.5
// 8.3
// 6.3
// 1.3
// 6.2
}
// example from https://learn.microsoft.com/dotnet/api/system.linq.enumerable.thenby
func ExampleThenLess_1() {
// Sort the strings first by their length and then alphabetically.
orderByLs, _ := OrderByLs(
VarToSeq("grape", "passionfruit", "banana", "mango", "orange", "raspberry", "apple", "blueberry"),
ThenLess(
func(s1, s2 string) bool { return len(s1) < len(s2) },
func(s1, s2 string) bool { return s1 < s2 },
))
for fruit := range orderByLs {
fmt.Println(fruit)
}
// Output:
// apple
// grape
// mango
// banana
// orange
// blueberry
// raspberry
// passionfruit
}
// ThenByDescendingEx1 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.thenbydescending
func ExampleThenLess_2() {
// Sort the strings first ascending by their length and then descending using a custom case insensitive comparer.
orderByLs, _ := OrderByLs(
VarToSeq("apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE"),
ThenLess(
func(s1, s2 string) bool { return len(s1) < len(s2) },
ReverseLess(caseInsensitiveLess),
))
for fruit := range orderByLs {
fmt.Println(fruit)
}
// Output:
// apPLe
// apple
// APple
// apPLE
// orange
// ORANGE
// baNanA
// BAnana
}
func TestOrderByLs_string(t *testing.T) {
type args struct {
source iter.Seq[string]
less func(string, string) bool
}
tests := []struct {
name string
args args
want iter.Seq[string]
}{
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/sorting-data#secondary-sort-examples
{name: "Secondary Ascending Sort",
args: args{
source: VarToSeq("the", "quick", "brown", "fox", "jumps"),
less: ThenLess(
func(x, y string) bool { return len(x) < len(y) },
func(x, y string) bool { return []rune(x)[0] < []rune(y)[0] },
),
},
want: VarToSeq("fox", "the", "brown", "jumps", "quick"),
},
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/sorting-data#secondary-descending-sort
{name: "Secondary Descending Sort",
args: args{
source: VarToSeq("the", "quick", "brown", "fox", "jumps"),
less: ThenLess(
func(x, y string) bool { return len(x) < len(y) },
func(x, y string) bool { return []rune(y)[0] < []rune(x)[0] },
),
},
want: VarToSeq("the", "fox", "quick", "jumps", "brown"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := OrderByLs(tt.args.source, tt.args.less)
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("OrderByLs() = %v, want %v", StringDef(got), StringDef(tt.want))
}
})
}
}