-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
204 lines (176 loc) · 5.04 KB
/
util_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// util_test.go
// Copyright(c) 2022 Matt Pharr, licensed under the GNU Public License, Version 3.
// SPDX: GPL-3.0-only
package main
import (
"slices"
"testing"
"time"
)
func TestWrapText(t *testing.T) {
input := "this is a test_with_a_long_line of stuff"
expected := "this is \n a \n test_with_a_long_line \n of \n stuff"
wrap, lines := wrapText(input, 8, 2, false)
if wrap != expected {
t.Errorf("wrapping gave %q; expected %q", wrap, expected)
}
if lines != 5 {
t.Errorf("wrapping returned %d lines, expected 5", lines)
}
}
func TestStopShouting(t *testing.T) {
input := "UNITED AIRLINES (North America)"
expected := "United Airlines (North America)"
ss := stopShouting(input)
if ss != expected {
t.Errorf("Got %q, expected %q", ss, expected)
}
}
func TestTransientMap(t *testing.T) {
ts := NewTransientMap[int, int]()
ts.Add(1, 10, 250*time.Millisecond)
ts.Add(2, 20, 750*time.Millisecond)
// Should have both
if v, ok := ts.Get(1); !ok {
t.Errorf("transient set doesn't have expected entry")
} else if v != 10 {
t.Errorf("transient set didn't return expected value")
}
if v, ok := ts.Get(2); !ok {
t.Errorf("transient set doesn't have expected entry")
} else if v != 20 {
t.Errorf("transient set didn't return expected value")
}
// Note that after this point this test has the potential to be flaky,
// if the thread is not scheduled for ~250+ms; it's possible that more
// time will elapse than we think and thence some of the checks may not
// add up...
time.Sleep(500 * time.Millisecond)
// Should just have 2
if _, ok := ts.Get(1); ok {
t.Errorf("transient set still has value that it shouldn't")
}
if v, ok := ts.Get(2); !ok {
t.Errorf("transient set doesn't have expected entry")
} else if v != 20 {
t.Errorf("transient set didn't return expected value")
}
time.Sleep(250 * time.Millisecond)
if _, ok := ts.Get(1); ok {
t.Errorf("transient set still has value that it shouldn't")
}
if _, ok := ts.Get(2); ok {
t.Errorf("transient set still has value that it shouldn't")
}
}
func TestMapSlice(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := MapSlice[int, float32](a, func(i int) float32 { return 2 * float32(i) })
if len(a) != len(b) {
t.Errorf("lengths mismatch: %d vs %d", len(a), len(b))
}
for i := range a {
if float32(2*a[i]) != b[i] {
t.Errorf("value %d mismatch %f vs %f", i, float32(2*a[i]), b[i])
}
}
}
func TestDeleteSliceElement(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
a = DeleteSliceElement(a, 2)
if !slices.Equal(a, []int{1, 2, 4, 5}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 3)
if !slices.Equal(a, []int{1, 2, 4}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 0)
if !slices.Equal(a, []int{2, 4}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 1)
if !slices.Equal(a, []int{2}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 0)
if !slices.Equal(a, nil) {
t.Errorf("Slice element delete incorrect")
}
}
func TestInsertSliceElement(t *testing.T) {
a := []int{1, 2, 4, 5}
a = InsertSliceElement(a, 2, 3)
if !slices.Equal(a, []int{1, 2, 3, 4, 5}) {
t.Errorf("Slice insert incorrect: %+v", a)
}
a = InsertSliceElement(a, 0, 0)
if !slices.Equal(a, []int{0, 1, 2, 3, 4, 5}) {
t.Errorf("Slice insert incorrect: %+v", a)
}
a = InsertSliceElement(a, 6, 6)
if !slices.Equal(a, []int{0, 1, 2, 3, 4, 5, 6}) {
t.Errorf("Slice insert incorrect: %+v", a)
}
}
func TestFilterSlice(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := FilterSlice(a, func(i int) bool { return i%2 == 0 })
if len(b) != 2 || b[0] != 2 || b[1] != 4 {
t.Errorf("filter evens failed: %+v", b)
}
c := FilterSlice(a, func(i int) bool { return i >= 3 })
if len(c) != 3 || c[0] != 3 || c[1] != 4 || c[2] != 5 {
t.Errorf("filter >=3 failed: %+v", c)
}
}
func TestRingBuffer(t *testing.T) {
rb := NewRingBuffer[int](10)
if rb.Size() != 0 {
t.Errorf("empty should have zero size")
}
rb.Add(0, 1, 2, 3, 4)
if rb.Size() != 5 {
t.Errorf("expected size 5; got %d", rb.Size())
}
for i := 0; i < 5; i++ {
if rb.Get(i) != i {
t.Errorf("returned unexpected value")
}
}
for i := 5; i < 18; i++ {
rb.Add(i)
}
if rb.Size() != 10 {
t.Errorf("expected size 10")
}
for i := 0; i < 10; i++ {
if rb.Get(i) != 8+i {
t.Errorf("after filling, at %d got %d, expected %d", i, rb.Get(i), 8+i)
}
}
}
func TestReduceSlice(t *testing.T) {
v := []int{1, -2, 3, 4}
if r := ReduceSlice(v, func(v int, r int) int { return v + r }, 10); r != 16 {
t.Errorf("ReduceSlice with + got %d, not 16 expected", r)
}
if r := ReduceSlice(v, func(v int, r int) int { return v * r }, 2); r != -48 {
t.Errorf("ReduceSlice with * got %d, not -48 expected", r)
}
}
func TestReduceMap(t *testing.T) {
m := map[int]string{
0: "hello",
16: "foobar",
2: "greets",
7: "x",
}
reduce := func(k int, v string, length int) int {
return length + len(v)
}
length := ReduceMap[int, string, int](m, reduce, 5)
if length != 5+5+6+6+1 {
t.Errorf("Expected %d from ReduceMap; got %d", 5+5+6+6+1, length)
}
}