/
bytes_mock_test.go
134 lines (113 loc) · 2.49 KB
/
bytes_mock_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
package extsort_test
// this test package is heavily bases on the one for psilva261's timsort
// https://github.com/psilva261/timsort/blob/master/timsort_test.go
import (
"context"
"testing"
"github.com/lanrat/extsort"
)
func sortForMockTest(inputData []val, lessFunc extsort.CompareLessFunc) error {
// make array of all data in chan
inputChan := make(chan extsort.SortType, 2)
go func() {
for _, d := range inputData {
inputChan <- d
}
close(inputChan)
}()
config := extsort.DefaultConfig()
config.ChunkSize = len(inputData)/20 + 100
sort, outChan, errChan := extsort.NewMock(inputChan, fromBytesForTest, lessFunc, config, 0)
sort.Sort(context.Background())
i := 0
for rec := range outChan {
inputData[i] = rec.(val)
i++
}
if err := <-errChan; err != nil {
return err
}
return nil
}
func TestMock50(t *testing.T) {
a := makeTestArray(50)
if IsSorted(a, KeyLessThan) {
t.Error("sorted before starting")
}
err := sortForMockTest(a, KeyLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSorted(a, KeyLessThan) {
t.Error("not sorted")
}
}
func TestMockSmoke(t *testing.T) {
a := make([]val, 3)
a[0] = val{3, 0}
a[1] = val{1, 1}
a[2] = val{2, 2}
err := sortForMockTest(a, KeyLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSorted(a, KeyLessThan) {
t.Error("not sorted")
}
}
func TestMockSmokeStability(t *testing.T) {
a := make([]val, 3)
a[0] = val{3, 0}
a[1] = val{2, 1}
a[2] = val{2, 2}
err := sortForMockTest(a, KeyOrderLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSorted(a, KeyOrderLessThan) {
t.Error("not sorted")
}
}
func TestMock1K(t *testing.T) {
a := makeTestArray(1024)
err := sortForMockTest(a, KeyOrderLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSorted(a, KeyOrderLessThan) {
t.Error("not sorted")
}
}
func TestMock1M(t *testing.T) {
a := makeTestArray(1024 * 1024)
err := sortForMockTest(a, KeyOrderLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSorted(a, KeyOrderLessThan) {
t.Error("not sorted")
}
}
func TestMockRandom1M(t *testing.T) {
size := 1024 * 1024
a := makeRandomArray(size)
b := make([]val, size)
copy(b, a)
err := sortForMockTest(a, KeyLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSorted(a, KeyLessThan) {
t.Error("not sorted")
}
// sort by order
err = sortForTest(a, OrderLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
for i := 0; i < len(b); i++ {
if !Equals(b[i], a[i]) {
t.Error("oops")
}
}
}