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.

116 lines
2.4 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 if firstOutput != output {
  41. t.Errorf("Run #%d's output was different from first run.\nfirst: %v\nlast: %v",
  42. i, firstOutput, output)
  43. }
  44. }
  45. }
  46. func testThemAll() string {
  47. // Such determinism.
  48. grand.reset(1)
  49. // Use it.
  50. out := new(bytes.Buffer)
  51. perm := RandPerm(10)
  52. blob, _ := json.Marshal(perm)
  53. fmt.Fprintf(out, "perm: %s\n", blob)
  54. fmt.Fprintf(out, "randInt: %d\n", RandInt())
  55. fmt.Fprintf(out, "randUint: %d\n", RandUint())
  56. fmt.Fprintf(out, "randIntn: %d\n", RandIntn(97))
  57. fmt.Fprintf(out, "randInt31: %d\n", RandInt31())
  58. fmt.Fprintf(out, "randInt32: %d\n", RandInt32())
  59. fmt.Fprintf(out, "randInt63: %d\n", RandInt63())
  60. fmt.Fprintf(out, "randInt64: %d\n", RandInt64())
  61. fmt.Fprintf(out, "randUint32: %d\n", RandUint32())
  62. fmt.Fprintf(out, "randUint64: %d\n", RandUint64())
  63. return out.String()
  64. }
  65. func TestRngConcurrencySafety(t *testing.T) {
  66. var wg sync.WaitGroup
  67. for i := 0; i < 100; i++ {
  68. wg.Add(1)
  69. go func() {
  70. defer wg.Done()
  71. _ = RandUint64()
  72. <-time.After(time.Millisecond * time.Duration(RandIntn(100)))
  73. _ = RandPerm(3)
  74. }()
  75. }
  76. wg.Wait()
  77. }
  78. func BenchmarkRandBytes10B(b *testing.B) {
  79. benchmarkRandBytes(b, 10)
  80. }
  81. func BenchmarkRandBytes100B(b *testing.B) {
  82. benchmarkRandBytes(b, 100)
  83. }
  84. func BenchmarkRandBytes1KiB(b *testing.B) {
  85. benchmarkRandBytes(b, 1024)
  86. }
  87. func BenchmarkRandBytes10KiB(b *testing.B) {
  88. benchmarkRandBytes(b, 10*1024)
  89. }
  90. func BenchmarkRandBytes100KiB(b *testing.B) {
  91. benchmarkRandBytes(b, 100*1024)
  92. }
  93. func BenchmarkRandBytes1MiB(b *testing.B) {
  94. benchmarkRandBytes(b, 1024*1024)
  95. }
  96. func benchmarkRandBytes(b *testing.B, n int) {
  97. for i := 0; i < b.N; i++ {
  98. _ = RandBytes(n)
  99. }
  100. b.ReportAllocs()
  101. }