set_nots_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package set
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. )
  7. func Test_New(t *testing.T) {
  8. s := New(ThreadSafe)
  9. s.Add(1, 2, 3, "testing")
  10. if s.Size() != 4 {
  11. t.Error("New: The set created was expected have 4 items")
  12. }
  13. }
  14. func TestSetNonTS_Add(t *testing.T) {
  15. s := New(NonThreadSafe)
  16. s.Add(1)
  17. s.Add(2)
  18. s.Add(2) // duplicate
  19. s.Add("fatih")
  20. s.Add("zeynep")
  21. s.Add("zeynep") // another duplicate
  22. if s.Size() != 4 {
  23. t.Error("Add: items are not unique. The set size should be four")
  24. }
  25. if !s.Has(1, 2, "fatih", "zeynep") {
  26. t.Error("Add: added items are not availabile in the set.")
  27. }
  28. }
  29. func TestSetNonTS_Add_multiple(t *testing.T) {
  30. s := newNonTS()
  31. s.Add("ankara", "san francisco", 3.14)
  32. if s.Size() != 3 {
  33. t.Error("Add: items are not unique. The set size should be three")
  34. }
  35. if !s.Has("ankara", "san francisco", 3.14) {
  36. t.Error("Add: added items are not availabile in the set.")
  37. }
  38. }
  39. func TestSetNonTS_Remove(t *testing.T) {
  40. s := newNonTS()
  41. s.Add(1)
  42. s.Add(2)
  43. s.Add("fatih")
  44. s.Remove(1)
  45. if s.Size() != 2 {
  46. t.Error("Remove: set size should be two after removing")
  47. }
  48. s.Remove(1)
  49. if s.Size() != 2 {
  50. t.Error("Remove: set size should be not change after trying to remove a non-existing item")
  51. }
  52. s.Remove(2)
  53. s.Remove("fatih")
  54. if s.Size() != 0 {
  55. t.Error("Remove: set size should be zero")
  56. }
  57. s.Remove("fatih") // try to remove something from a zero length set
  58. }
  59. func TestSetNonTS_Remove_multiple(t *testing.T) {
  60. s := newNonTS()
  61. s.Add("ankara", "san francisco", 3.14, "istanbul")
  62. s.Remove("ankara", "san francisco", 3.14)
  63. if s.Size() != 1 {
  64. t.Error("Remove: items are not unique. The set size should be four")
  65. }
  66. if !s.Has("istanbul") {
  67. t.Error("Add: added items are not availabile in the set.")
  68. }
  69. }
  70. func TestSetNonTS_Pop(t *testing.T) {
  71. s := newNonTS()
  72. s.Add(1)
  73. s.Add(2)
  74. s.Add("fatih")
  75. a := s.Pop()
  76. if s.Size() != 2 {
  77. t.Error("Pop: set size should be two after popping out")
  78. }
  79. if s.Has(a) {
  80. t.Error("Pop: returned item should not exist")
  81. }
  82. s.Pop()
  83. s.Pop()
  84. b := s.Pop()
  85. if b != nil {
  86. t.Error("Pop: should return nil because set is empty")
  87. }
  88. s.Pop() // try to remove something from a zero length set
  89. }
  90. func TestSetNonTS_Has(t *testing.T) {
  91. s := newNonTS()
  92. s.Add("1", "2", "3", "4")
  93. if !s.Has("1") {
  94. t.Error("Has: the item 1 exist, but 'Has' is returning false")
  95. }
  96. if !s.Has("1", "2", "3", "4") {
  97. t.Error("Has: the items all exist, but 'Has' is returning false")
  98. }
  99. }
  100. func TestSetNonTS_Clear(t *testing.T) {
  101. s := newNonTS()
  102. s.Add(1)
  103. s.Add("istanbul")
  104. s.Add("san francisco")
  105. s.Clear()
  106. if s.Size() != 0 {
  107. t.Error("Clear: set size should be zero")
  108. }
  109. }
  110. func TestSetNonTS_IsEmpty(t *testing.T) {
  111. s := newNonTS()
  112. empty := s.IsEmpty()
  113. if !empty {
  114. t.Error("IsEmpty: set is empty, it should be true")
  115. }
  116. s.Add(2)
  117. s.Add(3)
  118. notEmpty := s.IsEmpty()
  119. if notEmpty {
  120. t.Error("IsEmpty: set is filled, it should be false")
  121. }
  122. }
  123. func TestSetNonTS_IsEqual(t *testing.T) {
  124. s := newNonTS()
  125. s.Add("1", "2", "3")
  126. u := newNonTS()
  127. u.Add("1", "2", "3")
  128. ok := s.IsEqual(u)
  129. if !ok {
  130. t.Error("IsEqual: set s and t are equal. However it returns false")
  131. }
  132. // same size, different content
  133. a := newNonTS()
  134. a.Add("1", "2", "3")
  135. b := newNonTS()
  136. b.Add("4", "5", "6")
  137. ok = a.IsEqual(b)
  138. if ok {
  139. t.Error("IsEqual: set a and b are now equal (1). However it returns true")
  140. }
  141. // different size, similar content
  142. a = newNonTS()
  143. a.Add("1", "2", "3")
  144. b = newNonTS()
  145. b.Add("1", "2", "3", "4")
  146. ok = a.IsEqual(b)
  147. if ok {
  148. t.Error("IsEqual: set s and t are now equal (2). However it returns true")
  149. }
  150. }
  151. func TestSetNonTS_IsSubset(t *testing.T) {
  152. s := newNonTS()
  153. s.Add("1", "2", "3", "4")
  154. u := newNonTS()
  155. u.Add("1", "2", "3")
  156. ok := s.IsSubset(u)
  157. if !ok {
  158. t.Error("IsSubset: u is a subset of s. However it returns false")
  159. }
  160. ok = u.IsSubset(s)
  161. if ok {
  162. t.Error("IsSubset: s is not a subset of u. However it returns true")
  163. }
  164. }
  165. func TestSetNonTS_IsSuperset(t *testing.T) {
  166. s := newNonTS()
  167. s.Add("1", "2", "3", "4")
  168. u := newNonTS()
  169. u.Add("1", "2", "3")
  170. ok := u.IsSuperset(s)
  171. if !ok {
  172. t.Error("IsSuperset: s is a superset of u. However it returns false")
  173. }
  174. ok = s.IsSuperset(u)
  175. if ok {
  176. t.Error("IsSuperset: u is not a superset of u. However it returns true")
  177. }
  178. }
  179. func TestSetNonTS_String(t *testing.T) {
  180. s := newNonTS()
  181. if s.String() != "[]" {
  182. t.Errorf("String: output is not what is excepted '%s'", s.String())
  183. }
  184. s.Add("1", "2", "3", "4")
  185. if !strings.HasPrefix(s.String(), "[") {
  186. t.Error("String: output should begin with a square bracket")
  187. }
  188. if !strings.HasSuffix(s.String(), "]") {
  189. t.Error("String: output should end with a square bracket")
  190. }
  191. }
  192. func TestSetNonTS_List(t *testing.T) {
  193. s := newNonTS()
  194. s.Add("1", "2", "3", "4")
  195. s = newNonTS()
  196. s.Add("1", "2", "3", "4")
  197. // this returns a slice of interface{}
  198. if len(s.List()) != 4 {
  199. t.Error("List: slice size should be four.")
  200. }
  201. for _, item := range s.List() {
  202. r := reflect.TypeOf(item)
  203. if r.Kind().String() != "string" {
  204. t.Error("List: slice item should be a string")
  205. }
  206. }
  207. }
  208. func TestSetNonTS_Copy(t *testing.T) {
  209. s := newNonTS()
  210. s.Add("1", "2", "3", "4")
  211. r := s.Copy()
  212. if !s.IsEqual(r) {
  213. t.Error("Copy: set s and r are not equal")
  214. }
  215. }
  216. func TestSetNonTS_Merge(t *testing.T) {
  217. s := newNonTS()
  218. s.Add("1", "2", "3")
  219. r := newNonTS()
  220. r.Add("3", "4", "5")
  221. s.Merge(r)
  222. if s.Size() != 5 {
  223. t.Error("Merge: the set doesn't have all items in it.")
  224. }
  225. if !s.Has("1", "2", "3", "4", "5") {
  226. t.Error("Merge: merged items are not availabile in the set.")
  227. }
  228. }
  229. func TestSetNonTS_Separate(t *testing.T) {
  230. s := newNonTS()
  231. s.Add("1", "2", "3")
  232. r := newNonTS()
  233. r.Add("3", "5")
  234. s.Separate(r)
  235. if s.Size() != 2 {
  236. t.Error("Separate: the set doesn't have all items in it.")
  237. }
  238. if !s.Has("1", "2") {
  239. t.Error("Separate: items after separation are not availabile in the set.")
  240. }
  241. }