github.com/Finschia/ostracon@v1.1.5/libs/rand/random_test.go (about)

     1  package rand
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRandStr(t *testing.T) {
    15  	l := 243
    16  	s := Str(l)
    17  	assert.Equal(t, l, len(s))
    18  }
    19  
    20  func TestRandBytes(t *testing.T) {
    21  	l := 243
    22  	b := Bytes(l)
    23  	assert.Equal(t, l, len(b))
    24  }
    25  
    26  func TestRandIntn(t *testing.T) {
    27  	n := 243
    28  	for i := 0; i < 100; i++ {
    29  		x := Intn(n)
    30  		assert.True(t, x < n)
    31  	}
    32  }
    33  
    34  // Test to make sure that we never call math.rand().
    35  // We do this by ensuring that outputs are deterministic.
    36  func TestDeterminism(t *testing.T) {
    37  	var firstOutput string
    38  
    39  	for i := 0; i < 100; i++ {
    40  		output := testThemAll()
    41  		if i == 0 {
    42  			firstOutput = output
    43  		} else if firstOutput != output {
    44  			t.Errorf("run #%d's output was different from first run.\nfirst: %v\nlast: %v",
    45  				i, firstOutput, output)
    46  		}
    47  	}
    48  }
    49  
    50  func testThemAll() string {
    51  
    52  	// Such determinism.
    53  	grand.reset(1)
    54  
    55  	// Use it.
    56  	out := new(bytes.Buffer)
    57  	perm := Perm(10)
    58  	blob, _ := json.Marshal(perm)
    59  	fmt.Fprintf(out, "perm: %s\n", blob)
    60  	fmt.Fprintf(out, "randInt: %d\n", Int())
    61  	fmt.Fprintf(out, "randUint: %d\n", Uint())
    62  	fmt.Fprintf(out, "randIntn: %d\n", Intn(97))
    63  	fmt.Fprintf(out, "randInt31: %d\n", Int31())
    64  	fmt.Fprintf(out, "randInt32: %d\n", Int32())
    65  	fmt.Fprintf(out, "randInt63: %d\n", Int63())
    66  	fmt.Fprintf(out, "randInt64: %d\n", Int64())
    67  	fmt.Fprintf(out, "randUint32: %d\n", Uint32())
    68  	fmt.Fprintf(out, "randUint64: %d\n", Uint64())
    69  	return out.String()
    70  }
    71  
    72  func TestRngConcurrencySafety(t *testing.T) {
    73  	var wg sync.WaitGroup
    74  	for i := 0; i < 100; i++ {
    75  		wg.Add(1)
    76  		go func() {
    77  			defer wg.Done()
    78  
    79  			_ = Uint64()
    80  			<-time.After(time.Millisecond * time.Duration(Intn(100)))
    81  			_ = Perm(3)
    82  		}()
    83  	}
    84  	wg.Wait()
    85  }
    86  
    87  func BenchmarkRandBytes10B(b *testing.B) {
    88  	benchmarkRandBytes(b, 10)
    89  }
    90  func BenchmarkRandBytes100B(b *testing.B) {
    91  	benchmarkRandBytes(b, 100)
    92  }
    93  func BenchmarkRandBytes1KiB(b *testing.B) {
    94  	benchmarkRandBytes(b, 1024)
    95  }
    96  func BenchmarkRandBytes10KiB(b *testing.B) {
    97  	benchmarkRandBytes(b, 10*1024)
    98  }
    99  func BenchmarkRandBytes100KiB(b *testing.B) {
   100  	benchmarkRandBytes(b, 100*1024)
   101  }
   102  func BenchmarkRandBytes1MiB(b *testing.B) {
   103  	benchmarkRandBytes(b, 1024*1024)
   104  }
   105  
   106  func benchmarkRandBytes(b *testing.B, n int) {
   107  	for i := 0; i < b.N; i++ {
   108  		_ = Bytes(n)
   109  	}
   110  	b.ReportAllocs()
   111  }