-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsort_test.go
106 lines (89 loc) · 1.75 KB
/
sort_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
package funnelsort_test
import (
"encoding/binary"
"math/rand"
"testing"
"github.com/eikeon/funnelsort"
)
type intItem struct {
value uint64
}
func (i *intItem) Less(b funnelsort.Item) bool {
return i.value < b.(*intItem).value
}
func (i *intItem) Bytes() []byte {
b := make([]byte, 8)
b[0] = byte(i.value)
b[1] = byte(i.value >> 8)
b[2] = byte(i.value >> 16)
b[3] = byte(i.value >> 24)
b[4] = byte(i.value >> 32)
b[5] = byte(i.value >> 40)
b[6] = byte(i.value >> 48)
b[7] = byte(i.value >> 56)
return b
}
func newItem(b []byte) funnelsort.Item {
return &intItem{binary.LittleEndian.Uint64(b[0:8])}
}
type Increasing struct {
outOfOrder bool
last funnelsort.Item
}
func (w *Increasing) Write(item funnelsort.Item) {
if w.last != nil && item.Less(w.last) {
w.outOfOrder = true
}
w.last = item
}
type Random struct {
unread uint64
}
func (r *Random) Unread() uint64 {
return r.unread
}
func (r *Random) Read() funnelsort.Item {
if r.unread > 0 {
r.unread -= 1
return &intItem{uint64(rand.Int63())}
}
return nil
}
func TestFunnelSort(t *testing.T) {
n := uint64(1 << 14)
increasing := &Increasing{}
funnelsort.NewItem = newItem
funnelsort.FunnelSort(&Random{n}, increasing)
if increasing.outOfOrder {
t.Error("output not sorted")
t.FailNow()
}
}
func Sort(p uint) bool {
n := uint64(1 << p)
increasing := &Increasing{}
funnelsort.NewItem = newItem
funnelsort.FunnelSort(&Random{n}, increasing)
return increasing.outOfOrder
}
func Benchmark4(b *testing.B) {
for i := 0; i < b.N; i++ {
if Sort(4) {
b.FailNow()
}
}
}
func Benchmark5(b *testing.B) {
for i := 0; i < b.N; i++ {
if Sort(5) {
b.FailNow()
}
}
}
func Benchmark6(b *testing.B) {
for i := 0; i < b.N; i++ {
if Sort(6) {
b.FailNow()
}
}
}