-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_test.go
59 lines (48 loc) · 1.09 KB
/
example_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
package extsort_test
import (
"context"
"encoding/binary"
"fmt"
"math/rand"
"testing"
"github.com/lanrat/extsort"
)
var count = int(1e7) // 10M
type sortInt struct {
i int64
}
func (s sortInt) ToBytes() []byte {
buf := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(buf, s.i)
return buf
}
func sortIntFromBytes(b []byte) extsort.SortType {
i, _ := binary.Varint(b)
return sortInt{i: i}
}
func compareSortIntLess(a, b extsort.SortType) bool {
return a.(sortInt).i < b.(sortInt).i
}
func main() {
// create an input channel with unsorted data
inputChan := make(chan extsort.SortType)
go func() {
for i := 0; i < count; i++ {
inputChan <- sortInt{i: rand.Int63()}
}
close(inputChan)
}()
// create the sorter and start sorting
sorter, outputChan, errChan := extsort.New(inputChan, sortIntFromBytes, compareSortIntLess, nil)
sorter.Sort(context.Background())
// print output sorted data
for data := range outputChan {
fmt.Printf("%d\n", data.(sortInt).i)
}
if err := <-errChan; err != nil {
fmt.Printf("err: %s", err.Error())
}
}
func testMain(t *testing.T) {
main()
}