1
0

port_test.go 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package addrgen
  2. import (
  3. "math"
  4. "testing"
  5. )
  6. const testArraySize = 100
  7. const testIterCount = 100000
  8. func TestPortSimple(t *testing.T) {
  9. g := must(ParsePortRangeSpec("443"))
  10. for i := 0; i < 100; i++ {
  11. if p := g.Port(); p != 443 {
  12. t.Errorf("unexpected port value: %d", p)
  13. }
  14. }
  15. }
  16. func TestPortRange(t *testing.T) {
  17. var arr [testArraySize]int
  18. g := must(ParsePortRangeSpec("10000-20000"))
  19. for i := 0; i < testIterCount; i++ {
  20. p := g.Port()
  21. arr[p%testArraySize]++
  22. }
  23. sum := 0
  24. for i := 0; i < testArraySize; i++ {
  25. sum += int(arr[i])
  26. }
  27. if sum != testIterCount {
  28. t.Errorf("unexpected sum: %d", sum)
  29. }
  30. mx := float64(testIterCount) / float64(testArraySize)
  31. sigmaSquared := mx * float64(testArraySize-1) / float64(testArraySize)
  32. sigma := math.Sqrt(sigmaSquared)
  33. t.Logf("sigma = %.3f", sigma)
  34. t.Logf("5*sigma = %.3f", 5*sigma)
  35. for i := 0; i < testArraySize; i++ {
  36. if math.Abs(float64(arr[i])-mx) > 5*sigma {
  37. t.Errorf("arr[%d]=%d too far from mx=%.3f", i, arr[i], mx)
  38. }
  39. }
  40. }