-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
176 lines (156 loc) · 3.5 KB
/
utils_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package runes
import (
"math/bits"
"slices"
"testing"
)
const maxUint32 = 1<<32 - 1
func withoutErr[T any](v T, err error) func(*testing.T) T {
return func(t *testing.T) T {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
return v
}
}
func shouldErr[T any](_ T, err error) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
if err == nil {
t.Fatalf("expected error")
}
}
}
func shouldType[T any](v any) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
x, ok := v.(T)
if !ok {
t.Fatalf("expected type %T, got %T", x, v)
}
}
}
// seq generates an inclusive sequence of runes starting at `start` and ending
// in `end`. If given, the runes in `extra` are later added and the final slice
// is returned sorted in ascending order.
func seq(from, to rune, extra ...rune) []rune {
if from > to {
panic("from cannot be greater than to")
}
s := make([]rune, 0, to+1-from+rune(len(extra)))
for i := from; i <= to; i++ {
s = append(s, i)
}
s = append(s, extra...)
slices.Sort(s)
return s
}
func TestCeilDiv(t *testing.T) {
testCases := []struct {
dividend, divisor, expected uint32
}{
{0, 1, 0},
{2, 1, 2},
{1023, 1024, 1},
{1024, 1024, 1},
{1025, 1024, 2},
{maxUint32, maxUint32, 1},
}
for _, tc := range testCases {
actual := ceilDiv(tc.dividend, tc.divisor)
if tc.expected != actual {
t.Errorf("ceilDiv(%d, %d)=%d, expected=%d", tc.dividend, tc.divisor,
actual, tc.expected)
}
}
}
func TestU32Mid(t *testing.T) {
testCases := []struct {
a, b, expected uint32
}{
{0, 0, 0},
{1, 0, 0},
{0, 1, 0},
{1, 1, 1},
{1000, 2000, 1500},
{1001, 2000, 1500},
{1000, 2001, 1500},
{0, maxUint32, 1<<31 - 1},
}
for _, tc := range testCases {
actual := u32Mid(tc.a, tc.b)
if tc.expected != actual {
t.Errorf("u32Mid(%d, %d)=%d, expected=%d", tc.a, tc.b, actual,
tc.expected)
}
}
}
func TestOnes(t *testing.T) {
for i := 0; i < 256; i++ {
b := byte(i)
if int(ones(b)) != bits.OnesCount8(b) {
t.Fatalf("failed for %v", b)
}
}
}
func TestMSBPos(t *testing.T) {
for i := 0; i < 256; i++ {
b := byte(i)
expected := 8 - bits.LeadingZeros8(b)
actual := int(leadingOnePos(b))
if expected != actual {
t.Fatalf("byte: %v, expected: %v, actual: %v", b, expected, actual)
}
}
}
func TestNthOnePos(t *testing.T) {
// map from `b` to the result for each value of `n`, adding 1 to the array
// index
testCases := map[byte][8]byte{
0b00000000: {},
0b00000001: {1},
0b10000000: {8},
0b00001000: {4},
0b10000001: {1, 8},
0b00000011: {1, 2},
0b00101000: {4, 6},
0b10101010: {2, 4, 6, 8},
0b01010101: {1, 3, 5, 7},
0b11111111: {1, 2, 3, 4, 5, 6, 7, 8},
}
for b, res := range testCases {
if got := nthOnePos(b, 0); got != 0 {
t.Errorf("b=0b%08b, n=%d, got=%d, exp=0", b, 0, got)
}
if got := nthOnePos(b, 9); got != 0 {
t.Errorf("b=0b%08b, n=%d, got=%d, exp=0", b, 9, got)
}
for n := byte(1); n <= 8; n++ {
exp := res[n-1]
got := nthOnePos(b, n)
if exp != got {
t.Errorf("b=0b%08b, n=%d, got=%d, exp=%d", b, n, got, exp)
}
}
}
}
func TestRemoveAtIndex(t *testing.T) {
testCases := []struct {
input []rune
idx int
expected []rune
}{
{},
{seq(1, 10), 0, seq(2, 10)},
{seq(1, 10), 1, seq(3, 10, 1)},
{seq(1, 10), 8, seq(1, 8, 10)},
{seq(1, 10), 9, seq(1, 9)},
}
for i, tc := range testCases {
removeAtIndex(&tc.input, tc.idx)
if !slices.Equal(tc.expected, tc.input) {
t.Fatalf("[%d]\nexpected: %v\n actual: %v", i, tc.expected,
tc.input)
}
}
}