|
| 1 | +// Copyright 2024 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 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{}) // hold every string produced |
| 15 | + var indexSet [26]map[rune]int // hold every char produced at every position |
| 16 | + for i := range indexSet { |
| 17 | + indexSet[i] = make(map[rune]int) |
| 18 | + } |
| 19 | + |
| 20 | + // not getting a char in a position: (31/32)¹⁰⁰⁰ = 1.6e-14 |
| 21 | + // test completion within 1000 rounds: (1-(31/32)¹⁰⁰⁰)²⁶ = 0.9999999999996 |
| 22 | + // empirically, this should complete within 400 rounds = 0.999921 |
| 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 | +} |
0 commit comments