forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_test.go
More file actions
70 lines (62 loc) · 1.36 KB
/
string_test.go
File metadata and controls
70 lines (62 loc) · 1.36 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
package randstring
import "testing"
func validateChars(t *testing.T, u string, chars []byte) {
for _, c := range u {
var present bool
for _, a := range chars {
if rune(a) == c {
present = true
}
}
if !present {
t.Fatalf("chars not allowed in %q", u)
}
}
}
func TestNew_unique(t *testing.T) {
// Generate 1000 strings and check that they are unique
ss := make([]string, 1000)
for i := range ss {
ss[i] = NewLen(16)
}
for i, u := range ss {
for j, u2 := range ss {
if i != j && u == u2 {
t.Fatalf("not unique: %d:%q and %d:%q", i, u, j, u2)
}
}
}
}
func TestNewLen(t *testing.T) {
for i := 0; i < 100; i++ {
u := NewLen(i)
if len(u) != i {
t.Fatalf("request length %d, got %d", i, len(u))
}
}
}
func TestNewLenChars(t *testing.T) {
length := 10
chars := []byte("01234567")
u := NewLenChars(length, chars)
// Check length
if len(u) != length {
t.Fatalf("wrong length: expected %d, got %d", length, len(u))
}
// Check that only allowed characters are present
validateChars(t, u, chars)
// Check that two generated strings are different
u2 := NewLenChars(length, chars)
if u == u2 {
t.Fatalf("not unique: %q and %q", u, u2)
}
}
func TestNewLenCharsMaxLength(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("didn't panic")
}
}()
chars := make([]byte, 257)
NewLenChars(32, chars)
}