forked from extemporalgenome/sortutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortutil_test.go
More file actions
158 lines (144 loc) · 2.87 KB
/
sortutil_test.go
File metadata and controls
158 lines (144 loc) · 2.87 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sortutil
import (
"bytes"
"sort"
"testing"
)
func TestNewIntSeq(t *testing.T) {
for i, v := range NewIntSeq(8) {
if i != v {
t.FailNow()
}
}
}
func TestNewLetterSeq(t *testing.T) {
s := NewLetterSeq(27).String()
if s[:2] != "ab" || s[25:] != "za" {
t.Fail()
}
}
func TestLettersMark(t *testing.T) {
s := NewLetterSeq(10).Mark(2, 4)
if s[2] != 'C' || s[4] != 'E' || s[5] != 'f' {
t.Fail()
}
}
func TestNewSub(t *testing.T) {
data := [8]int{7, 6, 5, 4, 3, 2, 1, 0}
sort.Sort(NewSub(sort.IntSlice(data[:]), 4, 8))
if data != [8]int{7, 6, 5, 4, 0, 1, 2, 3} {
t.Fail()
}
}
func TestNewRev(t *testing.T) {
data := [8]int{0, 1, 2, 3, 4, 5, 6, 7}
sort.Sort(NewRev(sort.IntSlice(data[:])))
if data != [8]int{7, 6, 5, 4, 3, 2, 1, 0} {
t.Fail()
}
}
func TestNewProxy(t *testing.T) {
c := sort.IntSlice{0, 9, 1, 8, 2, 7, 3, 6, 4, 5}
p := NewLetterSeq(10)
q := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
sort.Sort(NewProxy(c, p, sort.IntSlice(q[:])))
r1 := p.String() == "acegijhfdb"
r2 := q == [10]int{0, 2, 4, 6, 8, 9, 7, 5, 3, 1}
if !sort.IsSorted(c) || !r1 || !r2 {
t.FailNow()
}
}
func TestStat(t *testing.T) {
var (
s = &Stat{I: ByteSlice{0}}
Len = 3
Less = 14
Swap = 7
)
for i := 0; i < Len; i++ {
s.Len()
}
for i := 0; i < Less; i++ {
s.Less(0, 0)
}
for i := 0; i < Swap; i++ {
s.Swap(0, 0)
}
if s.N.Len != Len || s.N.Less != Less || s.N.Swap != Swap {
t.Fail()
}
}
func TestReverse(t *testing.T) {
s := NewLetterSeq(26)
Reverse(s)
l := byte(len(s))
for i := range s {
i := byte(i)
if s[l-i-1] != 'a'+i {
t.Fail()
}
}
}
func TestShuffle(t *testing.T) {
b := NewLetterSeq(26)
s := b.String()
Shuffle(b)
if s == b.String() {
t.Fail()
}
}
func TestRotate(t *testing.T) {
const n = 29
b := NewLetterSeq(n)
c := make(Letters, n)
d := make(Letters, n)
for i := n; i > 0; i-- {
b, c, d = b[:i], c[:i], d[:i]
for j := -i - 1; j < i+1; j++ {
copy(c, b)
copy(d, b)
j := j % i
k := -j
if k < 0 {
k += i
}
copy(c, c[k:])
copy(c[i-k:], d[:k])
Rotate(d, j)
same := bytes.Equal([]byte(c), []byte(d))
if !same {
t.Errorf("%3d %s", j, c)
t.Errorf(" %s", d)
}
}
}
}
var skewTests = []struct {
r string
i, j, k int
}{
{"bcdefghijklma", 0, 12, 1},
{"fghijklmabcde", 0, 8, 5},
{"abcdeijklfghm", 5, 9, 3},
{"abjcdefghik", 2, 3, 7},
{"defabcghij", 0, 3, 3},
{"hijabcdefg", 7, 0, 3},
{"abcdehijfg", 7, 5, 3},
{"afgbcde", 1, 3, 4},
}
func TestSkew(t *testing.T) {
for i, v := range skewTests {
try := func(p, q int) {
b := NewLetterSeq(len(v.r))
Skew(b, p, q, v.k)
if string(b) != v.r {
t.Errorf("#%2d [%2d %2d %2d] %s", i, p, q, v.k, v.r)
t.Errorf(" %s", b)
}
}
try(v.i, v.j)
}
}