Source file
src/crypto/rand/text_test.go
1
2
3
4
5 package rand_test
6
7 import (
8 "crypto/rand"
9 "fmt"
10 "testing"
11 )
12
13 func TestText(t *testing.T) {
14 set := make(map[string]struct{})
15 var indexSet [26]map[rune]int
16 for i := range indexSet {
17 indexSet[i] = make(map[rune]int)
18 }
19
20
21
22
23 rounds := 1000
24 var done bool
25 for range rounds {
26 s := rand.Text()
27 if len(s) != 26 {
28 t.Errorf("len(Text()) = %d, want = 26", len(s))
29 }
30 for i, r := range s {
31 if ('A' > r || r > 'Z') && ('2' > r || r > '7') {
32 t.Errorf("Text()[%d] = %v, outside of base32 alphabet", i, r)
33 }
34 }
35 if _, ok := set[s]; ok {
36 t.Errorf("Text() = %s, duplicate of previously produced string", s)
37 }
38 set[s] = struct{}{}
39
40 done = true
41 for i, r := range s {
42 indexSet[i][r]++
43 if len(indexSet[i]) != 32 {
44 done = false
45 }
46 }
47 if done {
48 break
49 }
50 }
51 if !done {
52 t.Errorf("failed to produce every char at every index after %d rounds", rounds)
53 indexSetTable(t, indexSet)
54 }
55 }
56
57 func indexSetTable(t *testing.T, indexSet [26]map[rune]int) {
58 alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
59 line := " "
60 for _, r := range alphabet {
61 line += fmt.Sprintf(" %3s", string(r))
62 }
63 t.Log(line)
64 for i, set := range indexSet {
65 line = fmt.Sprintf("%2d:", i)
66 for _, r := range alphabet {
67 line += fmt.Sprintf(" %3d", set[r])
68 }
69 t.Log(line)
70 }
71 }
72
View as plain text