-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathorderBy_test.go
More file actions
59 lines (46 loc) · 1.12 KB
/
orderBy_test.go
File metadata and controls
59 lines (46 loc) · 1.12 KB
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
package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func Test_OrderBy_Asc(t *testing.T) {
set := u.Range(5, 0)
want := u.Range(0, 5)
result := u.OrderBy(set, func(left int, right int) bool {
return left > right
})
assert.Equal(t, want, result)
}
func Test_OrderBy_Desc(t *testing.T) {
set := u.Range(0, 5)
want := u.Range(5, 0)
result := u.OrderBy(set, func(left int, right int) bool {
return left < right
})
assert.Equal(t, want, result)
}
func BenchmarkOrderBy(b *testing.B) {
data := make([]int, 1000)
for i := range data {
data[i] = 1000 - i // Reverse order - worst case for bubble sort
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dataCopy := make([]int, len(data))
copy(dataCopy, data)
u.OrderBy(dataCopy, func(a, b int) bool { return a > b })
}
}
func BenchmarkOrderBySmall(b *testing.B) {
data := make([]int, 10)
for i := range data {
data[i] = 10 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dataCopy := make([]int, len(data))
copy(dataCopy, data)
u.OrderBy(dataCopy, func(a, b int) bool { return a > b })
}
}