set_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package collection
  2. import (
  3. "sort"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/logx"
  7. )
  8. func init() {
  9. logx.Disable()
  10. }
  11. func BenchmarkRawSet(b *testing.B) {
  12. m := make(map[interface{}]struct{})
  13. for i := 0; i < b.N; i++ {
  14. m[i] = struct{}{}
  15. _ = m[i]
  16. }
  17. }
  18. func BenchmarkUnmanagedSet(b *testing.B) {
  19. s := NewUnmanagedSet()
  20. for i := 0; i < b.N; i++ {
  21. s.Add(i)
  22. _ = s.Contains(i)
  23. }
  24. }
  25. func BenchmarkSet(b *testing.B) {
  26. s := NewSet()
  27. for i := 0; i < b.N; i++ {
  28. s.AddInt(i)
  29. _ = s.Contains(i)
  30. }
  31. }
  32. func TestAdd(t *testing.T) {
  33. // given
  34. set := NewUnmanagedSet()
  35. values := []interface{}{1, 2, 3}
  36. // when
  37. set.Add(values...)
  38. // then
  39. assert.True(t, set.Contains(1) && set.Contains(2) && set.Contains(3))
  40. assert.Equal(t, len(values), len(set.Keys()))
  41. }
  42. func TestAddInt(t *testing.T) {
  43. // given
  44. set := NewSet()
  45. values := []int{1, 2, 3}
  46. // when
  47. set.AddInt(values...)
  48. // then
  49. assert.True(t, set.Contains(1) && set.Contains(2) && set.Contains(3))
  50. keys := set.KeysInt()
  51. sort.Ints(keys)
  52. assert.EqualValues(t, values, keys)
  53. }
  54. func TestAddInt64(t *testing.T) {
  55. // given
  56. set := NewSet()
  57. values := []int64{1, 2, 3}
  58. // when
  59. set.AddInt64(values...)
  60. // then
  61. assert.True(t, set.Contains(int64(1)) && set.Contains(int64(2)) && set.Contains(int64(3)))
  62. assert.Equal(t, len(values), len(set.KeysInt64()))
  63. }
  64. func TestAddUint(t *testing.T) {
  65. // given
  66. set := NewSet()
  67. values := []uint{1, 2, 3}
  68. // when
  69. set.AddUint(values...)
  70. // then
  71. assert.True(t, set.Contains(uint(1)) && set.Contains(uint(2)) && set.Contains(uint(3)))
  72. assert.Equal(t, len(values), len(set.KeysUint()))
  73. }
  74. func TestAddUint64(t *testing.T) {
  75. // given
  76. set := NewSet()
  77. values := []uint64{1, 2, 3}
  78. // when
  79. set.AddUint64(values...)
  80. // then
  81. assert.True(t, set.Contains(uint64(1)) && set.Contains(uint64(2)) && set.Contains(uint64(3)))
  82. assert.Equal(t, len(values), len(set.KeysUint64()))
  83. }
  84. func TestAddStr(t *testing.T) {
  85. // given
  86. set := NewSet()
  87. values := []string{"1", "2", "3"}
  88. // when
  89. set.AddStr(values...)
  90. // then
  91. assert.True(t, set.Contains("1") && set.Contains("2") && set.Contains("3"))
  92. assert.Equal(t, len(values), len(set.KeysStr()))
  93. }
  94. func TestContainsWithoutElements(t *testing.T) {
  95. // given
  96. set := NewSet()
  97. // then
  98. assert.False(t, set.Contains(1))
  99. }
  100. func TestContainsUnmanagedWithoutElements(t *testing.T) {
  101. // given
  102. set := NewUnmanagedSet()
  103. // then
  104. assert.False(t, set.Contains(1))
  105. }
  106. func TestRemove(t *testing.T) {
  107. // given
  108. set := NewSet()
  109. set.Add([]interface{}{1, 2, 3}...)
  110. // when
  111. set.Remove(2)
  112. // then
  113. assert.True(t, set.Contains(1) && !set.Contains(2) && set.Contains(3))
  114. }
  115. func TestCount(t *testing.T) {
  116. // given
  117. set := NewSet()
  118. set.Add([]interface{}{1, 2, 3}...)
  119. // then
  120. assert.Equal(t, set.Count(), 3)
  121. }
  122. func TestKeysIntMismatch(t *testing.T) {
  123. set := NewSet()
  124. set.add(int64(1))
  125. set.add(2)
  126. vals := set.KeysInt()
  127. assert.EqualValues(t, []int{2}, vals)
  128. }
  129. func TestKeysInt64Mismatch(t *testing.T) {
  130. set := NewSet()
  131. set.add(1)
  132. set.add(int64(2))
  133. vals := set.KeysInt64()
  134. assert.EqualValues(t, []int64{2}, vals)
  135. }
  136. func TestKeysUintMismatch(t *testing.T) {
  137. set := NewSet()
  138. set.add(1)
  139. set.add(uint(2))
  140. vals := set.KeysUint()
  141. assert.EqualValues(t, []uint{2}, vals)
  142. }
  143. func TestKeysUint64Mismatch(t *testing.T) {
  144. set := NewSet()
  145. set.add(1)
  146. set.add(uint64(2))
  147. vals := set.KeysUint64()
  148. assert.EqualValues(t, []uint64{2}, vals)
  149. }
  150. func TestKeysStrMismatch(t *testing.T) {
  151. set := NewSet()
  152. set.add(1)
  153. set.add("2")
  154. vals := set.KeysStr()
  155. assert.EqualValues(t, []string{"2"}, vals)
  156. }
  157. func TestSetType(t *testing.T) {
  158. set := NewUnmanagedSet()
  159. set.add(1)
  160. set.add("2")
  161. vals := set.Keys()
  162. assert.ElementsMatch(t, []interface{}{1, "2"}, vals)
  163. }