set_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sets
  14. import (
  15. "reflect"
  16. "testing"
  17. )
  18. func TestStringSet(t *testing.T) {
  19. s := String{}
  20. s2 := String{}
  21. if len(s) != 0 {
  22. t.Errorf("Expected len=0: %d", len(s))
  23. }
  24. s.Insert("a", "b")
  25. if len(s) != 2 {
  26. t.Errorf("Expected len=2: %d", len(s))
  27. }
  28. s.Insert("c")
  29. if s.Has("d") {
  30. t.Errorf("Unexpected contents: %#v", s)
  31. }
  32. if !s.Has("a") {
  33. t.Errorf("Missing contents: %#v", s)
  34. }
  35. s.Delete("a")
  36. if s.Has("a") {
  37. t.Errorf("Unexpected contents: %#v", s)
  38. }
  39. s.Insert("a")
  40. if s.HasAll("a", "b", "d") {
  41. t.Errorf("Unexpected contents: %#v", s)
  42. }
  43. if !s.HasAll("a", "b") {
  44. t.Errorf("Missing contents: %#v", s)
  45. }
  46. s2.Insert("a", "b", "d")
  47. if s.IsSuperset(s2) {
  48. t.Errorf("Unexpected contents: %#v", s)
  49. }
  50. s2.Delete("d")
  51. if !s.IsSuperset(s2) {
  52. t.Errorf("Missing contents: %#v", s)
  53. }
  54. }
  55. func TestStringSetDeleteMultiples(t *testing.T) {
  56. s := String{}
  57. s.Insert("a", "b", "c")
  58. if len(s) != 3 {
  59. t.Errorf("Expected len=3: %d", len(s))
  60. }
  61. s.Delete("a", "c")
  62. if len(s) != 1 {
  63. t.Errorf("Expected len=1: %d", len(s))
  64. }
  65. if s.Has("a") {
  66. t.Errorf("Unexpected contents: %#v", s)
  67. }
  68. if s.Has("c") {
  69. t.Errorf("Unexpected contents: %#v", s)
  70. }
  71. if !s.Has("b") {
  72. t.Errorf("Missing contents: %#v", s)
  73. }
  74. }
  75. func TestNewStringSet(t *testing.T) {
  76. s := NewString("a", "b", "c")
  77. if len(s) != 3 {
  78. t.Errorf("Expected len=3: %d", len(s))
  79. }
  80. if !s.Has("a") || !s.Has("b") || !s.Has("c") {
  81. t.Errorf("Unexpected contents: %#v", s)
  82. }
  83. }
  84. func TestStringSetList(t *testing.T) {
  85. s := NewString("z", "y", "x", "a")
  86. if !reflect.DeepEqual(s.List(), []string{"a", "x", "y", "z"}) {
  87. t.Errorf("List gave unexpected result: %#v", s.List())
  88. }
  89. }
  90. func TestStringSetDifference(t *testing.T) {
  91. a := NewString("1", "2", "3")
  92. b := NewString("1", "2", "4", "5")
  93. c := a.Difference(b)
  94. d := b.Difference(a)
  95. if len(c) != 1 {
  96. t.Errorf("Expected len=1: %d", len(c))
  97. }
  98. if !c.Has("3") {
  99. t.Errorf("Unexpected contents: %#v", c.List())
  100. }
  101. if len(d) != 2 {
  102. t.Errorf("Expected len=2: %d", len(d))
  103. }
  104. if !d.Has("4") || !d.Has("5") {
  105. t.Errorf("Unexpected contents: %#v", d.List())
  106. }
  107. }
  108. func TestStringSetHasAny(t *testing.T) {
  109. a := NewString("1", "2", "3")
  110. if !a.HasAny("1", "4") {
  111. t.Errorf("expected true, got false")
  112. }
  113. if a.HasAny("0", "4") {
  114. t.Errorf("expected false, got true")
  115. }
  116. }
  117. func TestStringSetEquals(t *testing.T) {
  118. // Simple case (order doesn't matter)
  119. a := NewString("1", "2")
  120. b := NewString("2", "1")
  121. if !a.Equal(b) {
  122. t.Errorf("Expected to be equal: %v vs %v", a, b)
  123. }
  124. // It is a set; duplicates are ignored
  125. b = NewString("2", "2", "1")
  126. if !a.Equal(b) {
  127. t.Errorf("Expected to be equal: %v vs %v", a, b)
  128. }
  129. // Edge cases around empty sets / empty strings
  130. a = NewString()
  131. b = NewString()
  132. if !a.Equal(b) {
  133. t.Errorf("Expected to be equal: %v vs %v", a, b)
  134. }
  135. b = NewString("1", "2", "3")
  136. if a.Equal(b) {
  137. t.Errorf("Expected to be not-equal: %v vs %v", a, b)
  138. }
  139. b = NewString("1", "2", "")
  140. if a.Equal(b) {
  141. t.Errorf("Expected to be not-equal: %v vs %v", a, b)
  142. }
  143. // Check for equality after mutation
  144. a = NewString()
  145. a.Insert("1")
  146. if a.Equal(b) {
  147. t.Errorf("Expected to be not-equal: %v vs %v", a, b)
  148. }
  149. a.Insert("2")
  150. if a.Equal(b) {
  151. t.Errorf("Expected to be not-equal: %v vs %v", a, b)
  152. }
  153. a.Insert("")
  154. if !a.Equal(b) {
  155. t.Errorf("Expected to be equal: %v vs %v", a, b)
  156. }
  157. a.Delete("")
  158. if a.Equal(b) {
  159. t.Errorf("Expected to be not-equal: %v vs %v", a, b)
  160. }
  161. }
  162. func TestStringUnion(t *testing.T) {
  163. tests := []struct {
  164. s1 String
  165. s2 String
  166. expected String
  167. }{
  168. {
  169. NewString("1", "2", "3", "4"),
  170. NewString("3", "4", "5", "6"),
  171. NewString("1", "2", "3", "4", "5", "6"),
  172. },
  173. {
  174. NewString("1", "2", "3", "4"),
  175. NewString(),
  176. NewString("1", "2", "3", "4"),
  177. },
  178. {
  179. NewString(),
  180. NewString("1", "2", "3", "4"),
  181. NewString("1", "2", "3", "4"),
  182. },
  183. {
  184. NewString(),
  185. NewString(),
  186. NewString(),
  187. },
  188. }
  189. for _, test := range tests {
  190. union := test.s1.Union(test.s2)
  191. if union.Len() != test.expected.Len() {
  192. t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len())
  193. }
  194. if !union.Equal(test.expected) {
  195. t.Errorf("Expected union.Equal(expected) but not true. union:%v expected:%v", union.List(), test.expected.List())
  196. }
  197. }
  198. }
  199. func TestStringIntersection(t *testing.T) {
  200. tests := []struct {
  201. s1 String
  202. s2 String
  203. expected String
  204. }{
  205. {
  206. NewString("1", "2", "3", "4"),
  207. NewString("3", "4", "5", "6"),
  208. NewString("3", "4"),
  209. },
  210. {
  211. NewString("1", "2", "3", "4"),
  212. NewString("1", "2", "3", "4"),
  213. NewString("1", "2", "3", "4"),
  214. },
  215. {
  216. NewString("1", "2", "3", "4"),
  217. NewString(),
  218. NewString(),
  219. },
  220. {
  221. NewString(),
  222. NewString("1", "2", "3", "4"),
  223. NewString(),
  224. },
  225. {
  226. NewString(),
  227. NewString(),
  228. NewString(),
  229. },
  230. }
  231. for _, test := range tests {
  232. intersection := test.s1.Intersection(test.s2)
  233. if intersection.Len() != test.expected.Len() {
  234. t.Errorf("Expected intersection.Len()=%d but got %d", test.expected.Len(), intersection.Len())
  235. }
  236. if !intersection.Equal(test.expected) {
  237. t.Errorf("Expected intersection.Equal(expected) but not true. intersection:%v expected:%v", intersection.List(), test.expected.List())
  238. }
  239. }
  240. }