utils_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2014, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "bytes"
  22. "fmt"
  23. "math"
  24. "testing"
  25. "time"
  26. )
  27. func TestMakeRandomPeriod(t *testing.T) {
  28. min := 1 * time.Nanosecond
  29. max := 10000 * time.Nanosecond
  30. res1, err := MakeRandomPeriod(min, max)
  31. if err != nil {
  32. t.Errorf("MakeRandomPeriod failed: %s", err)
  33. }
  34. if res1 < min {
  35. t.Error("duration should not be less than min")
  36. }
  37. if res1 > max {
  38. t.Error("duration should not be more than max")
  39. }
  40. res2, err := MakeRandomPeriod(min, max)
  41. if err != nil {
  42. t.Errorf("MakeRandomPeriod failed: %s", err)
  43. }
  44. if res1 == res2 {
  45. t.Error("duration should have randomness difference between calls")
  46. }
  47. }
  48. func TestJitter(t *testing.T) {
  49. testCases := []struct {
  50. n int64
  51. factor float64
  52. expectedMin int64
  53. expectedMax int64
  54. }{
  55. {100, 0.1, 90, 110},
  56. {1000, 0.3, 700, 1300},
  57. }
  58. for _, testCase := range testCases {
  59. t.Run(fmt.Sprintf("jitter case: %+v", testCase), func(t *testing.T) {
  60. min := int64(math.MaxInt64)
  61. max := int64(0)
  62. for i := 0; i < 100000; i++ {
  63. x := Jitter(testCase.n, testCase.factor)
  64. if x < min {
  65. min = x
  66. }
  67. if x > max {
  68. max = x
  69. }
  70. }
  71. if min != testCase.expectedMin {
  72. t.Errorf("unexpected minimum jittered value: %d", min)
  73. }
  74. if max != testCase.expectedMax {
  75. t.Errorf("unexpected maximum jittered value: %d", max)
  76. }
  77. })
  78. }
  79. }
  80. func TestCompress(t *testing.T) {
  81. originalData := []byte("test data")
  82. compressedData := Compress(originalData)
  83. decompressedData, err := Decompress(compressedData)
  84. if err != nil {
  85. t.Errorf("Uncompress failed: %s", err)
  86. }
  87. if bytes.Compare(originalData, decompressedData) != 0 {
  88. t.Error("decompressed data doesn't match original data")
  89. }
  90. }
  91. func TestFormatByteCount(t *testing.T) {
  92. testCases := []struct {
  93. n uint64
  94. expectedOutput string
  95. }{
  96. {500, "500B"},
  97. {1024, "1.0K"},
  98. {10000, "9.8K"},
  99. {1024*1024 + 1, "1.0M"},
  100. {100*1024*1024 + 99999, "100.1M"},
  101. }
  102. for _, testCase := range testCases {
  103. t.Run(testCase.expectedOutput, func(t *testing.T) {
  104. output := FormatByteCount(testCase.n)
  105. if output != testCase.expectedOutput {
  106. t.Errorf("unexpected output: %s", output)
  107. }
  108. })
  109. }
  110. }
  111. func TestWeightedCoinFlip(t *testing.T) {
  112. runs := 100000
  113. tolerance := 1000
  114. testCases := []struct {
  115. weight float64
  116. expectedTrues int
  117. }{
  118. {0.333, runs / 3},
  119. {0.5, runs / 2},
  120. {1.0, runs},
  121. {0.0, 0},
  122. }
  123. for _, testCase := range testCases {
  124. t.Run(fmt.Sprintf("%f", testCase.weight), func(t *testing.T) {
  125. trues := 0
  126. for i := 0; i < runs; i++ {
  127. if FlipWeightedCoin(testCase.weight) {
  128. trues++
  129. }
  130. }
  131. min := testCase.expectedTrues - tolerance
  132. if min < 0 {
  133. min = 0
  134. }
  135. max := testCase.expectedTrues + tolerance
  136. if trues < min || trues > max {
  137. t.Errorf("unexpected coin flip outcome: %f %d (+/-%d) %d",
  138. testCase.weight, testCase.expectedTrues, tolerance, trues)
  139. }
  140. })
  141. }
  142. }