-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesort_test.go
117 lines (106 loc) · 2.12 KB
/
filesort_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
package filesort
import (
"bufio"
"fmt"
"io"
"strings"
"testing"
)
func testLessLine(a, b interface{}) bool { return a.(string) < b.(string) }
type testLineEncoder struct {
w io.WriteCloser
}
func newTestLineEncoder(w io.WriteCloser) Encoder {
return &testLineEncoder{w: w}
}
func (le *testLineEncoder) Encode(a interface{}) error {
if _, err := le.w.Write([]byte(a.(string) + "\n")); err != nil {
return err
}
return nil
}
func (le *testLineEncoder) Close() error {
return le.w.Close()
}
type testLineDecoder struct {
r *bufio.Reader
}
func newTestLineDecoder(r io.Reader) Decoder {
return &testLineDecoder{r: bufio.NewReader(r)}
}
func (ld *testLineDecoder) Decode() (interface{}, error) {
val, err := ld.r.ReadString(0xa)
if err != nil {
return nil, err
}
return strings.TrimRight(val, "\n"), nil
}
func TestSort(t *testing.T) {
sort, err := New(WithLess(testLessLine), WithEncoderNew(newTestLineEncoder), WithDecoderNew(newTestLineDecoder), WithMaxMemoryBuffer(3))
if err != nil {
t.Fatal(err)
}
lines := []string{
"aaaa",
"zzzz",
"yyyy",
"iiii",
"ffff",
"kkkk",
"qqqq",
"tttt",
}
for _, l := range lines {
if err := sort.Write(l); err != nil {
t.Fatal(err)
}
}
if err := sort.Close(); err != nil {
t.Fatal(err)
}
var n int
prev := ""
for {
out, err := sort.Read()
if err != nil {
t.Fatal(err)
}
if out == nil {
break
}
n++
str := out.(string)
if len(str) != 4 || str <= prev {
t.Errorf("%s came after %s", str, prev)
}
prev = str
}
if n != len(lines) {
t.Errorf("expected to read %d values, but got %d", len(lines), n)
}
}
func TestSortStable(t *testing.T) {
sort, err := New(
WithLess(func(a, b interface{}) bool { return false }),
WithEncoderNew(newTestLineEncoder),
WithDecoderNew(newTestLineDecoder),
WithMaxMemoryBuffer(3),
)
if err != nil {
t.Fatal(err)
}
for i := 0; i < 100; i++ {
sort.Write(fmt.Sprintf("%d", i))
}
sort.Close()
for i := 0; i < 100; i++ {
exp := fmt.Sprintf("%d", i)
s, err := sort.Read()
if err != nil {
t.Fatal(err)
}
if s.(string) != exp {
t.Fatalf("expected %s but got %s", exp, s.(string))
}
}
}