PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cmd/syncthing/random_test.go

https://gitlab.com/qb1t/syncthing
Go | 87 lines | 62 code | 9 blank | 16 comment | 20 complexity | 605796f713dfc2e10c4ae5f0954d13be MD5 | raw file
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package main
  16. import (
  17. "sync"
  18. "testing"
  19. )
  20. var predictableRandomTest sync.Once
  21. func TestPredictableRandom(t *testing.T) {
  22. predictableRandomTest.Do(func() {
  23. // predictable random sequence is predictable
  24. e := 3440579354231278675
  25. if v := predictableRandom.Int(); v != e {
  26. t.Errorf("Unexpected random value %d != %d", v, e)
  27. }
  28. })
  29. }
  30. func TestSeedFromBytes(t *testing.T) {
  31. // should always return the same seed for the same bytes
  32. tcs := []struct {
  33. bs []byte
  34. v int64
  35. }{
  36. {[]byte("hello world"), -3639725434188061933},
  37. {[]byte("hello worlx"), -2539100776074091088},
  38. }
  39. for _, tc := range tcs {
  40. if v := seedFromBytes(tc.bs); v != tc.v {
  41. t.Errorf("Unexpected seed value %d != %d", v, tc.v)
  42. }
  43. }
  44. }
  45. func TestRandomString(t *testing.T) {
  46. for _, l := range []int{0, 1, 2, 3, 4, 8, 42} {
  47. s := randomString(l)
  48. if len(s) != l {
  49. t.Errorf("Incorrect length %d != %d", len(s), l)
  50. }
  51. }
  52. strings := make([]string, 1000)
  53. for i := range strings {
  54. strings[i] = randomString(8)
  55. for j := range strings {
  56. if i == j {
  57. continue
  58. }
  59. if strings[i] == strings[j] {
  60. t.Errorf("Repeated random string %q", strings[i])
  61. }
  62. }
  63. }
  64. }
  65. func TestRandomInt64(t *testing.T) {
  66. ints := make([]int64, 1000)
  67. for i := range ints {
  68. ints[i] = randomInt64()
  69. for j := range ints {
  70. if i == j {
  71. continue
  72. }
  73. if ints[i] == ints[j] {
  74. t.Errorf("Repeated random int64 %d", ints[i])
  75. }
  76. }
  77. }
  78. }