strings_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "strconv"
  7. "testing"
  8. )
  9. func TestStrings(t *testing.T) {
  10. var ss Strings
  11. // Check that set starts empty.
  12. wantLen := 0
  13. if ss.Len() != wantLen {
  14. t.Errorf("init: Len() = %d, want %d", ss.Len(), wantLen)
  15. }
  16. for i := 0; i < maxLimit; i++ {
  17. if ss.Has(strconv.Itoa(i)) {
  18. t.Errorf("init: Has(%d) = true, want false", i)
  19. }
  20. }
  21. // Set some strings.
  22. for i, b := range toSet[:maxLimit] {
  23. if b {
  24. ss.Set(strconv.Itoa(i))
  25. wantLen++
  26. }
  27. }
  28. // Check that strings were set.
  29. if ss.Len() != wantLen {
  30. t.Errorf("after Set: Len() = %d, want %d", ss.Len(), wantLen)
  31. }
  32. for i := 0; i < maxLimit; i++ {
  33. if got := ss.Has(strconv.Itoa(i)); got != toSet[i] {
  34. t.Errorf("after Set: Has(%d) = %v, want %v", i, got, !got)
  35. }
  36. }
  37. // Clear some strings.
  38. for i, b := range toClear[:maxLimit] {
  39. if b {
  40. ss.Clear(strconv.Itoa(i))
  41. if toSet[i] {
  42. wantLen--
  43. }
  44. }
  45. }
  46. // Check that strings were cleared.
  47. if ss.Len() != wantLen {
  48. t.Errorf("after Clear: Len() = %d, want %d", ss.Len(), wantLen)
  49. }
  50. for i := 0; i < maxLimit; i++ {
  51. if got := ss.Has(strconv.Itoa(i)); got != toSet[i] && !toClear[i] {
  52. t.Errorf("after Clear: Has(%d) = %v, want %v", i, got, !got)
  53. }
  54. }
  55. }