set_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package set
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func Test_Union(t *testing.T) {
  7. s := newTS()
  8. s.Add("1", "2", "3")
  9. r := newTS()
  10. r.Add("3", "4", "5")
  11. x := newNonTS()
  12. x.Add("5", "6", "7")
  13. u := Union(s, r, x)
  14. if settype := reflect.TypeOf(u).String(); settype != "*set.Set" {
  15. t.Error("Union should derive its set type from the first passed set, got", settype)
  16. }
  17. if u.Size() != 7 {
  18. t.Error("Union: the merged set doesn't have all items in it.")
  19. }
  20. if !u.Has("1", "2", "3", "4", "5", "6", "7") {
  21. t.Error("Union: merged items are not availabile in the set.")
  22. }
  23. z := Union(x, r)
  24. if z.Size() != 5 {
  25. t.Error("Union: Union of 2 sets doesn't have the proper number of items.")
  26. }
  27. if settype := reflect.TypeOf(z).String(); settype != "*set.SetNonTS" {
  28. t.Error("Union should derive its set type from the first passed set, got", settype)
  29. }
  30. }
  31. func Test_Difference(t *testing.T) {
  32. s := newTS()
  33. s.Add("1", "2", "3")
  34. r := newTS()
  35. r.Add("3", "4", "5")
  36. x := newNonTS()
  37. x.Add("5", "6", "7")
  38. u := Difference(s, r, x)
  39. if u.Size() != 2 {
  40. t.Error("Difference: the set doesn't have all items in it.")
  41. }
  42. if !u.Has("1", "2") {
  43. t.Error("Difference: items are not availabile in the set.")
  44. }
  45. y := Difference(r, r)
  46. if y.Size() != 0 {
  47. t.Error("Difference: size should be zero")
  48. }
  49. }
  50. func Test_Intersection(t *testing.T) {
  51. s1 := newTS()
  52. s1.Add("1", "3", "4", "5")
  53. s2 := newTS()
  54. s2.Add("3", "5", "6")
  55. s3 := newTS()
  56. s3.Add("4", "5", "6", "7")
  57. u := Intersection(s1, s2, s3)
  58. if u.Size() != 1 {
  59. t.Error("Intersection: the set doesn't have all items in it.")
  60. }
  61. if !u.Has("5") {
  62. t.Error("Intersection: items after intersection are not availabile in the set.")
  63. }
  64. }
  65. func Test_Intersection2(t *testing.T) {
  66. s1 := newTS()
  67. s1.Add("1", "3", "4", "5")
  68. s2 := newTS()
  69. s2.Add("5", "6")
  70. i := Intersection(s1, s2)
  71. if i.Size() != 1 {
  72. t.Error("Intersection: size should be 1, it was", i.Size())
  73. }
  74. if !i.Has("5") {
  75. t.Error("Intersection: items after intersection are not availabile in the set.")
  76. }
  77. }
  78. func Test_SymmetricDifference(t *testing.T) {
  79. s := newTS()
  80. s.Add("1", "2", "3")
  81. r := newTS()
  82. r.Add("3", "4", "5")
  83. u := SymmetricDifference(s, r)
  84. if u.Size() != 4 {
  85. t.Error("SymmetricDifference: the set doesn't have all items in it.")
  86. }
  87. if !u.Has("1", "2", "4", "5") {
  88. t.Error("SymmetricDifference: items are not availabile in the set.")
  89. }
  90. }
  91. func Test_StringSlice(t *testing.T) {
  92. s := newTS()
  93. s.Add("san francisco", "istanbul", 3.14, 1321, "ankara")
  94. u := StringSlice(s)
  95. if len(u) != 3 {
  96. t.Error("StringSlice: slice should only have three items")
  97. }
  98. for _, item := range u {
  99. r := reflect.TypeOf(item)
  100. if r.Kind().String() != "string" {
  101. t.Error("StringSlice: slice item should be a string")
  102. }
  103. }
  104. }
  105. func Test_IntSlice(t *testing.T) {
  106. s := newTS()
  107. s.Add("san francisco", "istanbul", 3.14, 1321, "ankara", 8876)
  108. u := IntSlice(s)
  109. if len(u) != 2 {
  110. t.Error("IntSlice: slice should only have two items")
  111. }
  112. for _, item := range u {
  113. r := reflect.TypeOf(item)
  114. if r.Kind().String() != "int" {
  115. t.Error("Intslice: slice item should be a int")
  116. }
  117. }
  118. }
  119. func BenchmarkSetEquality(b *testing.B) {
  120. s := newTS()
  121. u := newTS()
  122. for i := 0; i < b.N; i++ {
  123. s.Add(i)
  124. u.Add(i)
  125. }
  126. b.ResetTimer()
  127. for i := 0; i < b.N; i++ {
  128. s.IsEqual(u)
  129. }
  130. }
  131. func BenchmarkSubset(b *testing.B) {
  132. s := newTS()
  133. u := newTS()
  134. for i := 0; i < b.N; i++ {
  135. s.Add(i)
  136. u.Add(i)
  137. }
  138. b.ResetTimer()
  139. for i := 0; i < b.N; i++ {
  140. s.IsSubset(u)
  141. }
  142. }
  143. func benchmarkIntersection(b *testing.B, numberOfItems int) {
  144. s1 := newTS()
  145. s2 := newTS()
  146. for i := 0; i < numberOfItems/2; i++ {
  147. s1.Add(i)
  148. }
  149. for i := 0; i < numberOfItems; i++ {
  150. s2.Add(i)
  151. }
  152. b.ResetTimer()
  153. for i := 0; i < b.N; i++ {
  154. Intersection(s1, s2)
  155. }
  156. }
  157. func BenchmarkIntersection10(b *testing.B) {
  158. benchmarkIntersection(b, 10)
  159. }
  160. func BenchmarkIntersection100(b *testing.B) {
  161. benchmarkIntersection(b, 100)
  162. }
  163. func BenchmarkIntersection1000(b *testing.B) {
  164. benchmarkIntersection(b, 1000)
  165. }
  166. func BenchmarkIntersection10000(b *testing.B) {
  167. benchmarkIntersection(b, 10000)
  168. }
  169. func BenchmarkIntersection100000(b *testing.B) {
  170. benchmarkIntersection(b, 100000)
  171. }
  172. func BenchmarkIntersection1000000(b *testing.B) {
  173. benchmarkIntersection(b, 1000000)
  174. }