You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

54 lines
1.2 KiB

  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. }