ints_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package set
  5. import (
  6. "math/rand"
  7. "testing"
  8. )
  9. const maxLimit = 1024
  10. var toSet, toClear [maxLimit]bool
  11. func init() {
  12. r := rand.New(rand.NewSource(0))
  13. for i := 0; i < maxLimit; i++ {
  14. toSet[i] = r.Intn(2) == 0
  15. toClear[i] = r.Intn(2) == 0
  16. }
  17. }
  18. func TestInts(t *testing.T) {
  19. ns := new(Ints)
  20. // Check that set starts empty.
  21. wantLen := 0
  22. if ns.Len() != wantLen {
  23. t.Errorf("init: Len() = %d, want %d", ns.Len(), wantLen)
  24. }
  25. for i := 0; i < maxLimit; i++ {
  26. if ns.Has(uint64(i)) {
  27. t.Errorf("init: Has(%d) = true, want false", i)
  28. }
  29. }
  30. // Set some numbers.
  31. for i, b := range toSet[:maxLimit] {
  32. if b {
  33. ns.Set(uint64(i))
  34. wantLen++
  35. }
  36. }
  37. // Check that integers were set.
  38. if ns.Len() != wantLen {
  39. t.Errorf("after Set: Len() = %d, want %d", ns.Len(), wantLen)
  40. }
  41. for i := 0; i < maxLimit; i++ {
  42. if got := ns.Has(uint64(i)); got != toSet[i] {
  43. t.Errorf("after Set: Has(%d) = %v, want %v", i, got, !got)
  44. }
  45. }
  46. // Clear some numbers.
  47. for i, b := range toClear[:maxLimit] {
  48. if b {
  49. ns.Clear(uint64(i))
  50. if toSet[i] {
  51. wantLen--
  52. }
  53. }
  54. }
  55. // Check that integers were cleared.
  56. if ns.Len() != wantLen {
  57. t.Errorf("after Clear: Len() = %d, want %d", ns.Len(), wantLen)
  58. }
  59. for i := 0; i < maxLimit; i++ {
  60. if got := ns.Has(uint64(i)); got != toSet[i] && !toClear[i] {
  61. t.Errorf("after Clear: Has(%d) = %v, want %v", i, got, !got)
  62. }
  63. }
  64. }