-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
68 lines (53 loc) · 1.21 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
package strand
import (
"sort"
"strings"
"testing"
"gotest.tools/v3/assert"
)
// --- Test Helpers ---
type ByRune []rune
func (r ByRune) Len() int { return len(r) }
func (r ByRune) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ByRune) Less(i, j int) bool { return r[i] < r[j] }
func stringToRuneSlice(s string) []rune {
var r []rune
for _, c := range s {
r = append(r, c)
}
return r
}
func isAnagram(s1, s2 string) bool {
var r1 ByRune = stringToRuneSlice(s1)
var r2 ByRune = stringToRuneSlice(s2)
sort.Sort(r1)
sort.Sort(r2)
return string(r1) == string(r2)
}
// --- /Test Helpers ---
func TestShuffle(t *testing.T) {
s := "Hello, World!"
s1 := shuffle(s)
assert.Assert(t, s != s1)
assert.Equal(t, len(s), len(s1))
assert.Assert(t, isAnagram(s, s1))
}
func TestRandInt(t *testing.T) {
for i := 0; i < 100; i++ {
n := randInt(1, 3)
assert.Assert(t, n == 1 || n == 2 || n == 3)
}
}
func TestRandSizedString(t *testing.T) {
s := randSizedString(1, 13, "abc")
l := len(s)
assert.Assert(t, l >= 1 && l <= 13)
// should only contain charset
s1 := ""
for _, c := range s {
if !strings.Contains("abc", string(c)) {
s1 = s1 + string(c)
}
assert.Assert(t, s1 == "")
}
}