random_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 https://mozilla.org/MPL/2.0/.
  6. package rand
  7. import "testing"
  8. func TestRandomString(t *testing.T) {
  9. for _, l := range []int{0, 1, 2, 3, 4, 8, 42} {
  10. s := String(l)
  11. if len(s) != l {
  12. t.Errorf("Incorrect length %d != %d", len(s), l)
  13. }
  14. }
  15. strings := make([]string, 1000)
  16. for i := range strings {
  17. strings[i] = String(8)
  18. for j := range strings {
  19. if i == j {
  20. continue
  21. }
  22. if strings[i] == strings[j] {
  23. t.Errorf("Repeated random string %q", strings[i])
  24. }
  25. }
  26. }
  27. }
  28. func TestRandomUint64(t *testing.T) {
  29. ints := make([]uint64, 1000)
  30. for i := range ints {
  31. ints[i] = Uint64()
  32. for j := range ints {
  33. if i == j {
  34. continue
  35. }
  36. if ints[i] == ints[j] {
  37. t.Errorf("Repeated random int64 %d", ints[i])
  38. }
  39. }
  40. }
  41. }
  42. func BenchmarkString(b *testing.B) {
  43. b.ReportAllocs()
  44. for i := 0; i < b.N; i++ {
  45. String(32)
  46. }
  47. }