You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.6 KiB

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