github.com/zhongdalu/gf@v1.0.0/g/container/gset/gset_z_unit_int_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 // go test *.go 8 9 package gset_test 10 11 import ( 12 "github.com/zhongdalu/gf/g/container/garray" 13 "github.com/zhongdalu/gf/g/container/gset" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "strings" 16 "testing" 17 ) 18 19 func TestIntSet_Basic(t *testing.T) { 20 gtest.Case(t, func() { 21 s := gset.NewIntSet() 22 s.Add(1).Add(1).Add(2) 23 s.Add([]int{3, 4}...) 24 gtest.Assert(s.Size(), 4) 25 gtest.AssertIN(1, s.Slice()) 26 gtest.AssertIN(2, s.Slice()) 27 gtest.AssertIN(3, s.Slice()) 28 gtest.AssertIN(4, s.Slice()) 29 gtest.AssertNI(0, s.Slice()) 30 gtest.Assert(s.Contains(4), true) 31 gtest.Assert(s.Contains(5), false) 32 s.Remove(1) 33 gtest.Assert(s.Size(), 3) 34 s.Clear() 35 gtest.Assert(s.Size(), 0) 36 }) 37 } 38 39 func TestIntSet_Iterator(t *testing.T) { 40 gtest.Case(t, func() { 41 s := gset.NewIntSet() 42 s.Add(1).Add(2).Add(3) 43 gtest.Assert(s.Size(), 3) 44 45 a1 := garray.New() 46 a2 := garray.New() 47 s.Iterator(func(v int) bool { 48 a1.Append(1) 49 return false 50 }) 51 s.Iterator(func(v int) bool { 52 a2.Append(1) 53 return true 54 }) 55 gtest.Assert(a1.Len(), 1) 56 gtest.Assert(a2.Len(), 3) 57 }) 58 } 59 60 func TestIntSet_LockFunc(t *testing.T) { 61 gtest.Case(t, func() { 62 s := gset.NewIntSet() 63 s.Add(1).Add(2).Add(3) 64 gtest.Assert(s.Size(), 3) 65 s.LockFunc(func(m map[int]struct{}) { 66 delete(m, 1) 67 }) 68 gtest.Assert(s.Size(), 2) 69 s.RLockFunc(func(m map[int]struct{}) { 70 gtest.Assert(m, map[int]struct{}{ 71 3: struct{}{}, 72 2: struct{}{}, 73 }) 74 }) 75 }) 76 } 77 78 func TestIntSet_Equal(t *testing.T) { 79 gtest.Case(t, func() { 80 s1 := gset.NewIntSet() 81 s2 := gset.NewIntSet() 82 s3 := gset.NewIntSet() 83 s1.Add(1).Add(2).Add(3) 84 s2.Add(1).Add(2).Add(3) 85 s3.Add(1).Add(2).Add(3).Add(4) 86 gtest.Assert(s1.Equal(s2), true) 87 gtest.Assert(s1.Equal(s3), false) 88 }) 89 } 90 91 func TestIntSet_IsSubsetOf(t *testing.T) { 92 gtest.Case(t, func() { 93 s1 := gset.NewIntSet() 94 s2 := gset.NewIntSet() 95 s3 := gset.NewIntSet() 96 s1.Add(1).Add(2) 97 s2.Add(1).Add(2).Add(3) 98 s3.Add(1).Add(2).Add(3).Add(4) 99 gtest.Assert(s1.IsSubsetOf(s2), true) 100 gtest.Assert(s2.IsSubsetOf(s3), true) 101 gtest.Assert(s1.IsSubsetOf(s3), true) 102 gtest.Assert(s2.IsSubsetOf(s1), false) 103 gtest.Assert(s3.IsSubsetOf(s2), false) 104 }) 105 } 106 107 func TestIntSet_Union(t *testing.T) { 108 gtest.Case(t, func() { 109 s1 := gset.NewIntSet() 110 s2 := gset.NewIntSet() 111 s1.Add(1).Add(2) 112 s2.Add(3).Add(4) 113 s3 := s1.Union(s2) 114 gtest.Assert(s3.Contains(1), true) 115 gtest.Assert(s3.Contains(2), true) 116 gtest.Assert(s3.Contains(3), true) 117 gtest.Assert(s3.Contains(4), true) 118 }) 119 } 120 121 func TestIntSet_Diff(t *testing.T) { 122 gtest.Case(t, func() { 123 s1 := gset.NewIntSet() 124 s2 := gset.NewIntSet() 125 s1.Add(1).Add(2).Add(3) 126 s2.Add(3).Add(4).Add(5) 127 s3 := s1.Diff(s2) 128 gtest.Assert(s3.Contains(1), true) 129 gtest.Assert(s3.Contains(2), true) 130 gtest.Assert(s3.Contains(3), false) 131 gtest.Assert(s3.Contains(4), false) 132 }) 133 } 134 135 func TestIntSet_Intersect(t *testing.T) { 136 gtest.Case(t, func() { 137 s1 := gset.NewIntSet() 138 s2 := gset.NewIntSet() 139 s1.Add(1).Add(2).Add(3) 140 s2.Add(3).Add(4).Add(5) 141 s3 := s1.Intersect(s2) 142 gtest.Assert(s3.Contains(1), false) 143 gtest.Assert(s3.Contains(2), false) 144 gtest.Assert(s3.Contains(3), true) 145 gtest.Assert(s3.Contains(4), false) 146 }) 147 } 148 149 func TestIntSet_Complement(t *testing.T) { 150 gtest.Case(t, func() { 151 s1 := gset.NewIntSet() 152 s2 := gset.NewIntSet() 153 s1.Add(1).Add(2).Add(3) 154 s2.Add(3).Add(4).Add(5) 155 s3 := s1.Complement(s2) 156 gtest.Assert(s3.Contains(1), false) 157 gtest.Assert(s3.Contains(2), false) 158 gtest.Assert(s3.Contains(4), true) 159 gtest.Assert(s3.Contains(5), true) 160 }) 161 } 162 163 func TestIntSet_Size(t *testing.T) { 164 gtest.Case(t, func() { 165 s1 := gset.NewIntSet(true) 166 s1.Add(1).Add(2).Add(3) 167 gtest.Assert(s1.Size(), 3) 168 169 }) 170 171 } 172 173 func TestIntSet_Merge(t *testing.T) { 174 gtest.Case(t, func() { 175 s1 := gset.NewIntSet() 176 s2 := gset.NewIntSet() 177 s1.Add(1).Add(2).Add(3) 178 s2.Add(3).Add(4).Add(5) 179 s3 := s1.Merge(s2) 180 gtest.Assert(s3.Contains(1), true) 181 gtest.Assert(s3.Contains(5), true) 182 gtest.Assert(s3.Contains(6), false) 183 }) 184 } 185 186 func TestIntSet_Join(t *testing.T) { 187 gtest.Case(t, func() { 188 s1 := gset.NewIntSet() 189 s1.Add(1).Add(2).Add(3) 190 s3 := s1.Join(",") 191 gtest.Assert(strings.Contains(s3, "3"), true) 192 193 }) 194 } 195 196 func TestIntSet_Sum(t *testing.T) { 197 gtest.Case(t, func() { 198 s1 := gset.NewIntSet() 199 s1.Add(1).Add(2).Add(3) 200 s2 := gset.NewIntSet() 201 s2.Add(5).Add(6).Add(7) 202 gtest.Assert(s2.Sum(), 18) 203 204 }) 205 206 } 207 208 func TestIntSet_Pop(t *testing.T) { 209 gtest.Case(t, func() { 210 s1 := gset.NewIntSet() 211 s1.Add(4).Add(2).Add(3) 212 gtest.AssertIN(s1.Pop(1), []int{4, 2, 3}) 213 gtest.AssertIN(s1.Pop(5), []int{4, 2, 3}) 214 gtest.Assert(s1.Size(), 3) 215 }) 216 }