set_ts_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package set
  2. import (
  3. "reflect"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. )
  8. func TestSet_New(t *testing.T) {
  9. s := newTS()
  10. if s.Size() != 0 {
  11. t.Error("New: calling without any parameters should create a set with zero size")
  12. }
  13. }
  14. func TestSet_New_parameters(t *testing.T) {
  15. s := newTS()
  16. s.Add("string", "another_string", 1, 3.14)
  17. if s.Size() != 4 {
  18. t.Error("New: calling with parameters should create a set with size of four")
  19. }
  20. }
  21. func TestSet_Add(t *testing.T) {
  22. s := newTS()
  23. s.Add(1)
  24. s.Add(2)
  25. s.Add(2) // duplicate
  26. s.Add("fatih")
  27. s.Add("zeynep")
  28. s.Add("zeynep") // another duplicate
  29. if s.Size() != 4 {
  30. t.Error("Add: items are not unique. The set size should be four")
  31. }
  32. if !s.Has(1, 2, "fatih", "zeynep") {
  33. t.Error("Add: added items are not availabile in the set.")
  34. }
  35. }
  36. func TestSet_Add_multiple(t *testing.T) {
  37. s := newTS()
  38. s.Add("ankara", "san francisco", 3.14)
  39. if s.Size() != 3 {
  40. t.Error("Add: items are not unique. The set size should be three")
  41. }
  42. if !s.Has("ankara", "san francisco", 3.14) {
  43. t.Error("Add: added items are not availabile in the set.")
  44. }
  45. }
  46. func TestSet_Remove(t *testing.T) {
  47. s := newTS()
  48. s.Add(1)
  49. s.Add(2)
  50. s.Add("fatih")
  51. s.Remove(1)
  52. if s.Size() != 2 {
  53. t.Error("Remove: set size should be two after removing")
  54. }
  55. s.Remove(1)
  56. if s.Size() != 2 {
  57. t.Error("Remove: set size should be not change after trying to remove a non-existing item")
  58. }
  59. s.Remove(2)
  60. s.Remove("fatih")
  61. if s.Size() != 0 {
  62. t.Error("Remove: set size should be zero")
  63. }
  64. s.Remove("fatih") // try to remove something from a zero length set
  65. }
  66. func TestSet_Remove_multiple(t *testing.T) {
  67. s := newTS()
  68. s.Add("ankara", "san francisco", 3.14, "istanbul")
  69. s.Remove("ankara", "san francisco", 3.14)
  70. if s.Size() != 1 {
  71. t.Error("Remove: items are not unique. The set size should be four")
  72. }
  73. if !s.Has("istanbul") {
  74. t.Error("Add: added items are not availabile in the set.")
  75. }
  76. }
  77. func TestSet_Pop(t *testing.T) {
  78. s := newTS()
  79. s.Add(1)
  80. s.Add(2)
  81. s.Add("fatih")
  82. a := s.Pop()
  83. if s.Size() != 2 {
  84. t.Error("Pop: set size should be two after popping out")
  85. }
  86. if s.Has(a) {
  87. t.Error("Pop: returned item should not exist")
  88. }
  89. s.Pop()
  90. s.Pop()
  91. b := s.Pop()
  92. if b != nil {
  93. t.Error("Pop: should return nil because set is empty")
  94. }
  95. s.Pop() // try to remove something from a zero length set
  96. }
  97. func TestSet_Has(t *testing.T) {
  98. s := newTS()
  99. s.Add("1", "2", "3", "4")
  100. if !s.Has("1") {
  101. t.Error("Has: the item 1 exist, but 'Has' is returning false")
  102. }
  103. if !s.Has("1", "2", "3", "4") {
  104. t.Error("Has: the items all exist, but 'Has' is returning false")
  105. }
  106. }
  107. func TestSet_Clear(t *testing.T) {
  108. s := newTS()
  109. s.Add(1)
  110. s.Add("istanbul")
  111. s.Add("san francisco")
  112. s.Clear()
  113. if s.Size() != 0 {
  114. t.Error("Clear: set size should be zero")
  115. }
  116. }
  117. func TestSet_IsEmpty(t *testing.T) {
  118. s := newTS()
  119. empty := s.IsEmpty()
  120. if !empty {
  121. t.Error("IsEmpty: set is empty, it should be true")
  122. }
  123. s.Add(2)
  124. s.Add(3)
  125. notEmpty := s.IsEmpty()
  126. if notEmpty {
  127. t.Error("IsEmpty: set is filled, it should be false")
  128. }
  129. }
  130. func TestSet_IsEqual(t *testing.T) {
  131. // same size, same content
  132. s := newTS()
  133. s.Add("1", "2", "3")
  134. u := newTS()
  135. u.Add("1", "2", "3")
  136. ok := s.IsEqual(u)
  137. if !ok {
  138. t.Error("IsEqual: set s and t are equal. However it returns false")
  139. }
  140. // same size, different content
  141. a := newTS()
  142. a.Add("1", "2", "3")
  143. b := newTS()
  144. b.Add("4", "5", "6")
  145. ok = a.IsEqual(b)
  146. if ok {
  147. t.Error("IsEqual: set a and b are now equal (1). However it returns true")
  148. }
  149. // different size, similar content
  150. a = newTS()
  151. a.Add("1", "2", "3")
  152. b = newTS()
  153. b.Add("1", "2", "3", "4")
  154. ok = a.IsEqual(b)
  155. if ok {
  156. t.Error("IsEqual: set s and t are now equal (2). However it returns true")
  157. }
  158. }
  159. func TestSet_IsSubset(t *testing.T) {
  160. s := newTS()
  161. s.Add("1", "2", "3", "4")
  162. u := newTS()
  163. u.Add("1", "2", "3")
  164. ok := s.IsSubset(u)
  165. if !ok {
  166. t.Error("IsSubset: u is a subset of s. However it returns false")
  167. }
  168. ok = u.IsSubset(s)
  169. if ok {
  170. t.Error("IsSubset: s is not a subset of u. However it returns true")
  171. }
  172. }
  173. func TestSet_IsSuperset(t *testing.T) {
  174. s := newTS()
  175. s.Add("1", "2", "3", "4")
  176. u := newTS()
  177. u.Add("1", "2", "3")
  178. ok := u.IsSuperset(s)
  179. if !ok {
  180. t.Error("IsSuperset: s is a superset of u. However it returns false")
  181. }
  182. ok = s.IsSuperset(u)
  183. if ok {
  184. t.Error("IsSuperset: u is not a superset of u. However it returns true")
  185. }
  186. }
  187. func TestSet_String(t *testing.T) {
  188. s := newTS()
  189. if s.String() != "[]" {
  190. t.Errorf("String: output is not what is excepted '%s'", s.String())
  191. }
  192. if !strings.HasPrefix(s.String(), "[") {
  193. t.Error("String: output should begin with a square bracket")
  194. }
  195. if !strings.HasSuffix(s.String(), "]") {
  196. t.Error("String: output should end with a square bracket")
  197. }
  198. }
  199. func TestSet_List(t *testing.T) {
  200. s := newTS()
  201. s.Add("1", "2", "3", "4")
  202. // this returns a slice of interface{}
  203. if len(s.List()) != 4 {
  204. t.Error("List: slice size should be four.")
  205. }
  206. for _, item := range s.List() {
  207. r := reflect.TypeOf(item)
  208. if r.Kind().String() != "string" {
  209. t.Error("List: slice item should be a string")
  210. }
  211. }
  212. }
  213. func TestSet_Copy(t *testing.T) {
  214. s := newTS()
  215. s.Add("1", "2", "3", "4")
  216. r := s.Copy()
  217. if !s.IsEqual(r) {
  218. t.Error("Copy: set s and r are not equal")
  219. }
  220. }
  221. func TestSet_Merge(t *testing.T) {
  222. s := newTS()
  223. s.Add("1", "2", "3")
  224. r := newTS()
  225. r.Add("3", "4", "5")
  226. s.Merge(r)
  227. if s.Size() != 5 {
  228. t.Error("Merge: the set doesn't have all items in it.")
  229. }
  230. if !s.Has("1", "2", "3", "4", "5") {
  231. t.Error("Merge: merged items are not availabile in the set.")
  232. }
  233. }
  234. func TestSet_Separate(t *testing.T) {
  235. s := newTS()
  236. s.Add("1", "2", "3")
  237. r := newTS()
  238. r.Add("3", "5")
  239. s.Separate(r)
  240. if s.Size() != 2 {
  241. t.Error("Separate: the set doesn't have all items in it.")
  242. }
  243. if !s.Has("1", "2") {
  244. t.Error("Separate: items after separation are not availabile in the set.")
  245. }
  246. }
  247. func TestSet_RaceAdd(t *testing.T) {
  248. // Create two sets. Add concurrently items to each of them. Remove from the
  249. // other one.
  250. // "go test -race" should detect this if the library is not thread-safe.
  251. s := newTS()
  252. u := newTS()
  253. go func() {
  254. for i := 0; i < 1000; i++ {
  255. item := "item" + strconv.Itoa(i)
  256. go func(i int) {
  257. s.Add(item)
  258. u.Add(item)
  259. }(i)
  260. }
  261. }()
  262. for i := 0; i < 1000; i++ {
  263. item := "item" + strconv.Itoa(i)
  264. go func(i int) {
  265. s.Add(item)
  266. u.Add(item)
  267. }(i)
  268. }
  269. }