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.

118 lines
2.5 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. return out.String()
  66. }
  67. func TestRngConcurrencySafety(t *testing.T) {
  68. var wg sync.WaitGroup
  69. for i := 0; i < 100; i++ {
  70. wg.Add(1)
  71. go func() {
  72. defer wg.Done()
  73. _ = RandUint64()
  74. <-time.After(time.Millisecond * time.Duration(RandIntn(100)))
  75. _ = RandPerm(3)
  76. }()
  77. }
  78. wg.Wait()
  79. }
  80. func BenchmarkRandBytes10B(b *testing.B) {
  81. benchmarkRandBytes(b, 10)
  82. }
  83. func BenchmarkRandBytes100B(b *testing.B) {
  84. benchmarkRandBytes(b, 100)
  85. }
  86. func BenchmarkRandBytes1KiB(b *testing.B) {
  87. benchmarkRandBytes(b, 1024)
  88. }
  89. func BenchmarkRandBytes10KiB(b *testing.B) {
  90. benchmarkRandBytes(b, 10*1024)
  91. }
  92. func BenchmarkRandBytes100KiB(b *testing.B) {
  93. benchmarkRandBytes(b, 100*1024)
  94. }
  95. func BenchmarkRandBytes1MiB(b *testing.B) {
  96. benchmarkRandBytes(b, 1024*1024)
  97. }
  98. func benchmarkRandBytes(b *testing.B, n int) {
  99. for i := 0; i < b.N; i++ {
  100. _ = RandBytes(n)
  101. }
  102. b.ReportAllocs()
  103. }