randpool_test.go 827 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package randpool
  2. import (
  3. "math"
  4. "math/rand"
  5. "testing"
  6. )
  7. const testArraySize = 100
  8. const testIterCount = 100000
  9. func TestBorrow(t *testing.T) {
  10. var arr [testArraySize]int
  11. rp := New()
  12. rp.Borrow(func(r *rand.Rand) {
  13. for i := 0; i < testIterCount; i++ {
  14. arr[r.Intn(testArraySize)]++
  15. }
  16. })
  17. sum := 0
  18. for i := 0; i < testArraySize; i++ {
  19. sum += int(arr[i])
  20. }
  21. if sum != testIterCount {
  22. t.Errorf("unexpected sum: %d", sum)
  23. }
  24. mx := float64(testIterCount) / float64(testArraySize)
  25. sigmaSquared := mx * float64(testArraySize-1) / float64(testArraySize)
  26. sigma := math.Sqrt(sigmaSquared)
  27. t.Logf("sigma = %.3f", sigma)
  28. t.Logf("5*sigma = %.3f", 5*sigma)
  29. for i := 0; i < testArraySize; i++ {
  30. if math.Abs(float64(arr[i])-mx) > 5*sigma {
  31. t.Errorf("arr[%d]=%d too far from mx=%.3f", i, arr[i], mx)
  32. }
  33. }
  34. }