PageRenderTime 85ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/util/random_test.go

https://gitlab.com/shinvdu/syncthing
Go | 63 lines | 50 code | 7 blank | 6 comment | 18 complexity | 1ef7e0cb3b8495ca2416a973eaf33589 MD5 | raw file
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package util
  7. import "testing"
  8. func TestSeedFromBytes(t *testing.T) {
  9. // should always return the same seed for the same bytes
  10. tcs := []struct {
  11. bs []byte
  12. v int64
  13. }{
  14. {[]byte("hello world"), -3639725434188061933},
  15. {[]byte("hello worlx"), -2539100776074091088},
  16. }
  17. for _, tc := range tcs {
  18. if v := SeedFromBytes(tc.bs); v != tc.v {
  19. t.Errorf("Unexpected seed value %d != %d", v, tc.v)
  20. }
  21. }
  22. }
  23. func TestRandomString(t *testing.T) {
  24. for _, l := range []int{0, 1, 2, 3, 4, 8, 42} {
  25. s := RandomString(l)
  26. if len(s) != l {
  27. t.Errorf("Incorrect length %d != %d", len(s), l)
  28. }
  29. }
  30. strings := make([]string, 1000)
  31. for i := range strings {
  32. strings[i] = RandomString(8)
  33. for j := range strings {
  34. if i == j {
  35. continue
  36. }
  37. if strings[i] == strings[j] {
  38. t.Errorf("Repeated random string %q", strings[i])
  39. }
  40. }
  41. }
  42. }
  43. func TestRandomInt64(t *testing.T) {
  44. ints := make([]int64, 1000)
  45. for i := range ints {
  46. ints[i] = RandomInt64()
  47. for j := range ints {
  48. if i == j {
  49. continue
  50. }
  51. if ints[i] == ints[j] {
  52. t.Errorf("Repeated random int64 %d", ints[i])
  53. }
  54. }
  55. }
  56. }