set_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2016 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 stringset
  5. import "testing"
  6. func TestStringSet(t *testing.T) {
  7. testCases := [][]string{
  8. {""},
  9. {"∫"},
  10. {"a", "b", "c"},
  11. {"", "a", "bb", "ccc"},
  12. {" ", "aaa", "bb", "c"},
  13. }
  14. test := func(tc int, b *Builder) {
  15. set := b.Set()
  16. if set.Len() != len(testCases[tc]) {
  17. t.Errorf("%d:Len() = %d; want %d", tc, set.Len(), len(testCases[tc]))
  18. }
  19. for i, s := range testCases[tc] {
  20. if x := b.Index(s); x != i {
  21. t.Errorf("%d:Index(%q) = %d; want %d", tc, s, x, i)
  22. }
  23. if p := Search(&set, s); p != i {
  24. t.Errorf("%d:Search(%q) = %d; want %d", tc, s, p, i)
  25. }
  26. if set.Elem(i) != s {
  27. t.Errorf("%d:Elem(%d) = %s; want %s", tc, i, set.Elem(i), s)
  28. }
  29. }
  30. if p := Search(&set, "apple"); p != -1 {
  31. t.Errorf(`%d:Search("apple") = %d; want -1`, tc, p)
  32. }
  33. }
  34. for i, tc := range testCases {
  35. b := NewBuilder()
  36. for _, s := range tc {
  37. b.Add(s)
  38. }
  39. b.Add(tc...)
  40. test(i, b)
  41. }
  42. for i, tc := range testCases {
  43. b := NewBuilder()
  44. b.Add(tc...)
  45. for _, s := range tc {
  46. b.Add(s)
  47. }
  48. test(i, b)
  49. }
  50. }