-
Notifications
You must be signed in to change notification settings - Fork 3
/
strings_test.go
133 lines (108 loc) · 2.19 KB
/
strings_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
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"
"fmt"
"math/rand"
"testing"
"github.com/lanrat/extsort"
)
func sortStringForTest(inputData []string) error {
// make array of all data in chan
inputChan := make(chan string, 2)
go func() {
for _, d := range inputData {
inputChan <- d
}
close(inputChan)
}()
config := extsort.DefaultConfig()
config.ChunkSize = len(inputData)/20 + 100
sort, outChan, errChan := extsort.Strings(inputChan, config)
sort.Sort(context.Background())
i := 0
for rec := range outChan {
inputData[i] = rec
i++
}
if err := <-errChan; err != nil {
return err
}
return nil
}
func makeTestStringArray(size int) []string {
a := make([]string, size)
for i := 0; i < size; i++ {
a[i] = fmt.Sprintf("%d", i&0xeeeeee)
}
return a
}
func Test50String(t *testing.T) {
a := makeTestStringArray(50)
if IsStringsSorted(a) {
t.Error("sorted before starting")
}
err := sortStringForTest(a)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsStringsSorted(a) {
t.Error("not sorted")
}
}
func IsStringsSorted(a []string) bool {
len := len(a)
if len < 2 {
return true
}
prev := a[0]
for i := 1; i < len; i++ {
if a[i] < prev {
return false
}
prev = a[i]
}
return true
}
func TestStringSmoke(t *testing.T) {
a := make([]string, 3)
a[0] = "banana"
a[1] = "orange"
a[2] = "apple"
err := sortStringForTest(a)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsStringsSorted(a) {
t.Error("not sorted")
}
}
func Test1KStrings(t *testing.T) {
a := makeTestStringArray(1024)
err := sortStringForTest(a)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsStringsSorted(a) {
t.Error("not sorted")
}
}
func makeRandomStringArray(size int) []string {
a := make([]string, size)
for i := 0; i < size; i++ {
a[i] = fmt.Sprintf("%d %d", rand.Intn(100), i)
}
return a
}
func TestRandom1MString(t *testing.T) {
size := 1024 * 1024
a := makeRandomStringArray(size)
err := sortStringForTest(a)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsStringsSorted(a) {
t.Error("not sorted")
}
}