codekingpro/portable-devtools
114k
1// Copyright 2024 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5package rand_test6 7import (8 "crypto/rand"9 "fmt"10 "testing"11)12 13func TestText(t *testing.T) {14 set := make(map[string]struct{}) // hold every string produced15 var indexSet [26]map[rune]int // hold every char produced at every position16 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-1421 // test completion within 1000 rounds: (1-(31/32)¹⁰⁰⁰)²⁶ = 0.999999999999622 // empirically, this should complete within 400 rounds = 0.99992123 rounds := 100024 var done bool25 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 = true41 for i, r := range s {42 indexSet[i][r]++43 if len(indexSet[i]) != 32 {44 done = false45 }46 }47 if done {48 break49 }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 57func 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 