github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/random/random_test.go (about) 1 // From AllInBits, Inc, TendermintClassic: github.com/tendermint/classic. 2 // License: Apache2.0. 3 package random 4 5 import ( 6 "bytes" 7 "encoding/json" 8 "fmt" 9 mrand "math/rand" 10 "sync" 11 "testing" 12 "time" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestRandStr(t *testing.T) { 18 t.Parallel() 19 20 l := 243 21 s := RandStr(l) 22 assert.Equal(t, l, len(s)) 23 } 24 25 func TestRandBytes(t *testing.T) { 26 t.Parallel() 27 28 l := 243 29 b := RandBytes(l) 30 assert.Equal(t, l, len(b)) 31 } 32 33 func TestRandIntn(t *testing.T) { 34 t.Parallel() 35 36 n := 243 37 for i := 0; i < 100; i++ { 38 x := RandIntn(n) 39 assert.True(t, x < n) 40 } 41 } 42 43 // Test to make sure that we never call math.rand(). 44 // We do this by ensuring that outputs are deterministic. 45 func TestDeterminism(t *testing.T) { 46 var firstOutput string 47 48 // Set math/rand's seed for the sake of debugging this test. 49 // (It isn't strictly necessary). 50 mrand.Seed(1) 51 52 for i := 0; i < 100; i++ { 53 output := testThemAll() 54 if i == 0 { 55 firstOutput = output 56 } else if firstOutput != output { 57 t.Errorf("Run #%d's output was different from first run.\nfirst: %v\nlast: %v", 58 i, firstOutput, output) 59 } 60 } 61 } 62 63 func testThemAll() string { 64 // Such determinism. 65 grand.reset(1) 66 67 // Use it. 68 out := new(bytes.Buffer) 69 perm := RandPerm(10) 70 blob, _ := json.Marshal(perm) 71 fmt.Fprintf(out, "perm: %s\n", blob) 72 fmt.Fprintf(out, "randInt: %d\n", RandInt()) 73 fmt.Fprintf(out, "randUint: %d\n", RandUint()) 74 fmt.Fprintf(out, "randIntn: %d\n", RandIntn(97)) 75 fmt.Fprintf(out, "randInt31: %d\n", RandInt31()) 76 fmt.Fprintf(out, "randInt32: %d\n", RandInt32()) 77 fmt.Fprintf(out, "randInt63: %d\n", RandInt63()) 78 fmt.Fprintf(out, "randInt64: %d\n", RandInt64()) 79 fmt.Fprintf(out, "randUint32: %d\n", RandUint32()) 80 fmt.Fprintf(out, "randUint64: %d\n", RandUint64()) 81 return out.String() 82 } 83 84 func TestRngConcurrencySafety(t *testing.T) { 85 t.Parallel() 86 87 var wg sync.WaitGroup 88 for i := 0; i < 100; i++ { 89 wg.Add(1) 90 go func() { 91 defer wg.Done() 92 93 _ = RandUint64() 94 <-time.After(time.Millisecond * time.Duration(RandIntn(100))) 95 _ = RandPerm(3) 96 }() 97 } 98 wg.Wait() 99 } 100 101 func BenchmarkRandBytes10B(b *testing.B) { 102 benchmarkRandBytes(b, 10) 103 } 104 105 func BenchmarkRandBytes100B(b *testing.B) { 106 benchmarkRandBytes(b, 100) 107 } 108 109 func BenchmarkRandBytes1KiB(b *testing.B) { 110 benchmarkRandBytes(b, 1024) 111 } 112 113 func BenchmarkRandBytes10KiB(b *testing.B) { 114 benchmarkRandBytes(b, 10*1024) 115 } 116 117 func BenchmarkRandBytes100KiB(b *testing.B) { 118 benchmarkRandBytes(b, 100*1024) 119 } 120 121 func BenchmarkRandBytes1MiB(b *testing.B) { 122 benchmarkRandBytes(b, 1024*1024) 123 } 124 125 func benchmarkRandBytes(b *testing.B, n int) { 126 b.Helper() 127 128 for i := 0; i < b.N; i++ { 129 _ = RandBytes(n) 130 } 131 b.ReportAllocs() 132 }