github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bmap/comparable_test.go (about) 1 package bmap 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestComparableBMap(t *testing.T) { 9 compBMap := NewUnsafeComparableBMapByMap[int, int](map[int]int{1: 1, 2: 2, 3: 3}) 10 cp := compBMap.CloneToMap() 11 assert.Equal(t, true, compBMap.EqualByMap(cp)) 12 assert.Equal(t, false, compBMap.EqualByMap(map[int]int{1: 1, 3: 3})) 13 cpm := compBMap.CloneToBMap() 14 assert.Equal(t, true, compBMap.EqualByBMap(cpm)) 15 assert.Equal(t, false, compBMap.EqualByBMap(NewUnsafeAnyBMapByMap(map[int]int{1: 1, 3: 3}))) 16 17 assert.Equal(t, false, compBMap.Replace(1, 2, 3)) 18 assert.Equal(t, true, compBMap.Replace(1, 1, 3)) 19 v, ok := compBMap.Get(1) 20 assert.Equal(t, true, ok) 21 assert.Equal(t, 3, v) 22 } 23 24 func TestUnsafeComparableBMap_EqualByMap(t *testing.T) { 25 type fields struct { 26 UnsafeAnyBMap ComparableBMap[int, int] 27 } 28 type args struct { 29 m map[int]int 30 } 31 tests := []struct { 32 name string 33 fields fields 34 args args 35 want bool 36 }{ 37 { 38 name: "nil", 39 fields: fields{ 40 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](nil), 41 }, 42 args: args{ 43 m: nil, 44 }, 45 want: true, 46 }, 47 { 48 name: "nil", 49 fields: fields{ 50 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](nil), 51 }, 52 args: args{ 53 m: map[int]int{}, 54 }, 55 want: true, 56 }, 57 { 58 name: "nil", 59 fields: fields{ 60 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{}), 61 }, 62 args: args{ 63 m: nil, 64 }, 65 want: true, 66 }, 67 { 68 name: "", 69 fields: fields{ 70 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{1: 1}), 71 }, 72 args: args{ 73 m: nil, 74 }, 75 want: false, 76 }, 77 { 78 name: "", 79 fields: fields{ 80 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{}), 81 }, 82 args: args{ 83 m: map[int]int{1: 1}, 84 }, 85 want: false, 86 }, 87 { 88 name: "", 89 fields: fields{ 90 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{1: 1}), 91 }, 92 args: args{ 93 m: map[int]int{1: 1}, 94 }, 95 want: true, 96 }, 97 } 98 for _, tt := range tests { 99 t.Run(tt.name, func(t *testing.T) { 100 x := NewUnsafeComparableBMapByMap(tt.fields.UnsafeAnyBMap.ToMetaMap()) 101 assert.Equalf(t, tt.want, x.EqualByMap(tt.args.m), "EqualByMap(%v)", tt.args.m) 102 }) 103 } 104 } 105 106 func TestUnsafeComparableBMap_EqualByBMap(t *testing.T) { 107 type fields struct { 108 UnsafeAnyBMap ComparableBMap[int, int] 109 } 110 type args struct { 111 m map[int]int 112 } 113 tests := []struct { 114 name string 115 fields fields 116 args args 117 want bool 118 }{ 119 { 120 name: "nil", 121 fields: fields{ 122 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](nil), 123 }, 124 args: args{ 125 m: nil, 126 }, 127 want: true, 128 }, 129 { 130 name: "nil", 131 fields: fields{ 132 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](nil), 133 }, 134 args: args{ 135 m: map[int]int{}, 136 }, 137 want: true, 138 }, 139 { 140 name: "nil", 141 fields: fields{ 142 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{}), 143 }, 144 args: args{ 145 m: nil, 146 }, 147 want: true, 148 }, 149 { 150 name: "", 151 fields: fields{ 152 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{1: 1}), 153 }, 154 args: args{ 155 m: nil, 156 }, 157 want: false, 158 }, 159 { 160 name: "", 161 fields: fields{ 162 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{}), 163 }, 164 args: args{ 165 m: map[int]int{1: 1}, 166 }, 167 want: false, 168 }, 169 { 170 name: "", 171 fields: fields{ 172 UnsafeAnyBMap: NewUnsafeComparableBMapByMap[int, int](map[int]int{1: 1}), 173 }, 174 args: args{ 175 m: map[int]int{1: 1}, 176 }, 177 want: true, 178 }, 179 } 180 for _, tt := range tests { 181 t.Run(tt.name, func(t *testing.T) { 182 x := NewUnsafeComparableBMapByMap(tt.fields.UnsafeAnyBMap.ToMetaMap()) 183 assert.Equalf(t, tt.want, x.EqualByBMap(NewUnsafeComparableBMapByMap(tt.args.m)), "EqualByBMap(%v)", tt.args.m) 184 }) 185 } 186 } 187 188 func TestSafeComparableBMap_EqualByMap(t *testing.T) { 189 type fields struct { 190 SafeAnyBMap ComparableBMap[int, int] 191 } 192 type args struct { 193 m map[int]int 194 } 195 tests := []struct { 196 name string 197 fields fields 198 args args 199 want bool 200 }{ 201 { 202 name: "nil", 203 fields: fields{ 204 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](nil), 205 }, 206 args: args{ 207 m: nil, 208 }, 209 want: true, 210 }, 211 { 212 name: "nil", 213 fields: fields{ 214 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](nil), 215 }, 216 args: args{ 217 m: map[int]int{}, 218 }, 219 want: true, 220 }, 221 { 222 name: "nil", 223 fields: fields{ 224 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{}), 225 }, 226 args: args{ 227 m: nil, 228 }, 229 want: true, 230 }, 231 { 232 name: "", 233 fields: fields{ 234 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{1: 1}), 235 }, 236 args: args{ 237 m: nil, 238 }, 239 want: false, 240 }, 241 { 242 name: "", 243 fields: fields{ 244 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{}), 245 }, 246 args: args{ 247 m: map[int]int{1: 1}, 248 }, 249 want: false, 250 }, 251 { 252 name: "", 253 fields: fields{ 254 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{1: 1}), 255 }, 256 args: args{ 257 m: map[int]int{1: 1}, 258 }, 259 want: true, 260 }, 261 } 262 for _, tt := range tests { 263 t.Run(tt.name, func(t *testing.T) { 264 x := NewSafeComparableBMapByMap(tt.fields.SafeAnyBMap.ToMetaMap()) 265 assert.Equalf(t, tt.want, x.EqualByMap(tt.args.m), "EqualByMap(%v)", tt.args.m) 266 }) 267 } 268 } 269 270 func TestSafeComparableBMap_EqualByBMap(t *testing.T) { 271 type fields struct { 272 SafeAnyBMap ComparableBMap[int, int] 273 } 274 type args struct { 275 m map[int]int 276 } 277 tests := []struct { 278 name string 279 fields fields 280 args args 281 want bool 282 }{ 283 { 284 name: "nil", 285 fields: fields{ 286 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](nil), 287 }, 288 args: args{ 289 m: nil, 290 }, 291 want: true, 292 }, 293 { 294 name: "nil", 295 fields: fields{ 296 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](nil), 297 }, 298 args: args{ 299 m: map[int]int{}, 300 }, 301 want: true, 302 }, 303 { 304 name: "nil", 305 fields: fields{ 306 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{}), 307 }, 308 args: args{ 309 m: nil, 310 }, 311 want: true, 312 }, 313 { 314 name: "", 315 fields: fields{ 316 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{1: 1}), 317 }, 318 args: args{ 319 m: nil, 320 }, 321 want: false, 322 }, 323 { 324 name: "", 325 fields: fields{ 326 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{}), 327 }, 328 args: args{ 329 m: map[int]int{1: 1}, 330 }, 331 want: false, 332 }, 333 { 334 name: "", 335 fields: fields{ 336 SafeAnyBMap: NewSafeComparableBMapByMap[int, int](map[int]int{1: 1}), 337 }, 338 args: args{ 339 m: map[int]int{1: 1}, 340 }, 341 want: true, 342 }, 343 } 344 for _, tt := range tests { 345 t.Run(tt.name, func(t *testing.T) { 346 x := NewSafeComparableBMapByMap(tt.fields.SafeAnyBMap.ToMetaMap()) 347 assert.Equalf(t, tt.want, x.EqualByBMap(NewSafeComparableBMapByMap(tt.args.m)), "EqualByBMap(%v)", tt.args.m) 348 }) 349 } 350 }