github.com/gogf/gf@v1.16.9/container/garray/garray_z_unit_normal_any_array_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf. 6 7 // go test *.go 8 9 package garray_test 10 11 import ( 12 "github.com/gogf/gf/frame/g" 13 "github.com/gogf/gf/internal/json" 14 "testing" 15 "time" 16 17 "github.com/gogf/gf/container/garray" 18 "github.com/gogf/gf/test/gtest" 19 "github.com/gogf/gf/util/gconv" 20 ) 21 22 func Test_Array_Basic(t *testing.T) { 23 gtest.C(t, func(t *gtest.T) { 24 expect := []interface{}{0, 1, 2, 3} 25 array := garray.NewArrayFrom(expect) 26 array2 := garray.NewArrayFrom(expect) 27 array3 := garray.NewArrayFrom([]interface{}{}) 28 t.Assert(array.Slice(), expect) 29 t.Assert(array.Interfaces(), expect) 30 array.Set(0, 100) 31 32 v, ok := array.Get(0) 33 t.Assert(v, 100) 34 t.Assert(ok, true) 35 36 v, ok = array.Get(1) 37 t.Assert(v, 1) 38 t.Assert(ok, true) 39 40 t.Assert(array.Search(100), 0) 41 t.Assert(array3.Search(100), -1) 42 t.Assert(array.Contains(100), true) 43 44 v, ok = array.Remove(0) 45 t.Assert(v, 100) 46 t.Assert(ok, true) 47 48 v, ok = array.Remove(-1) 49 t.Assert(v, nil) 50 t.Assert(ok, false) 51 52 v, ok = array.Remove(100000) 53 t.Assert(v, nil) 54 t.Assert(ok, false) 55 56 v, ok = array2.Remove(3) 57 t.Assert(v, 3) 58 t.Assert(ok, true) 59 60 v, ok = array2.Remove(1) 61 t.Assert(v, 1) 62 t.Assert(ok, true) 63 64 t.Assert(array.Contains(100), false) 65 array.Append(4) 66 t.Assert(array.Len(), 4) 67 array.InsertBefore(0, 100) 68 array.InsertAfter(0, 200) 69 t.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 4}) 70 array.InsertBefore(5, 300) 71 array.InsertAfter(6, 400) 72 t.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 300, 4, 400}) 73 t.Assert(array.Clear().Len(), 0) 74 }) 75 } 76 77 func TestArray_Sort(t *testing.T) { 78 gtest.C(t, func(t *gtest.T) { 79 expect1 := []interface{}{0, 1, 2, 3} 80 expect2 := []interface{}{3, 2, 1, 0} 81 array := garray.NewArray() 82 for i := 3; i >= 0; i-- { 83 array.Append(i) 84 } 85 array.SortFunc(func(v1, v2 interface{}) bool { 86 return v1.(int) < v2.(int) 87 }) 88 t.Assert(array.Slice(), expect1) 89 array.SortFunc(func(v1, v2 interface{}) bool { 90 return v1.(int) > v2.(int) 91 }) 92 t.Assert(array.Slice(), expect2) 93 }) 94 } 95 96 func TestArray_Unique(t *testing.T) { 97 gtest.C(t, func(t *gtest.T) { 98 expect := []interface{}{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5} 99 array := garray.NewArrayFrom(expect) 100 t.Assert(array.Unique().Slice(), []interface{}{1, 2, 3, 4, 5}) 101 }) 102 } 103 104 func TestArray_PushAndPop(t *testing.T) { 105 gtest.C(t, func(t *gtest.T) { 106 expect := []interface{}{0, 1, 2, 3} 107 array := garray.NewArrayFrom(expect) 108 t.Assert(array.Slice(), expect) 109 110 v, ok := array.PopLeft() 111 t.Assert(v, 0) 112 t.Assert(ok, true) 113 114 v, ok = array.PopRight() 115 t.Assert(v, 3) 116 t.Assert(ok, true) 117 118 v, ok = array.PopRand() 119 t.AssertIN(v, []interface{}{1, 2}) 120 t.Assert(ok, true) 121 122 v, ok = array.PopRand() 123 t.AssertIN(v, []interface{}{1, 2}) 124 t.Assert(ok, true) 125 126 t.Assert(array.Len(), 0) 127 array.PushLeft(1).PushRight(2) 128 t.Assert(array.Slice(), []interface{}{1, 2}) 129 }) 130 } 131 132 func TestArray_PopRands(t *testing.T) { 133 gtest.C(t, func(t *gtest.T) { 134 a1 := []interface{}{100, 200, 300, 400, 500, 600} 135 array := garray.NewFromCopy(a1) 136 t.AssertIN(array.PopRands(2), []interface{}{100, 200, 300, 400, 500, 600}) 137 }) 138 } 139 140 func TestArray_PopLeft(t *testing.T) { 141 gtest.C(t, func(t *gtest.T) { 142 array := garray.NewFrom(g.Slice{1, 2, 3}) 143 v, ok := array.PopLeft() 144 t.Assert(v, 1) 145 t.Assert(ok, true) 146 t.Assert(array.Len(), 2) 147 v, ok = array.PopLeft() 148 t.Assert(v, 2) 149 t.Assert(ok, true) 150 t.Assert(array.Len(), 1) 151 v, ok = array.PopLeft() 152 t.Assert(v, 3) 153 t.Assert(ok, true) 154 t.Assert(array.Len(), 0) 155 }) 156 } 157 158 func TestArray_PopRight(t *testing.T) { 159 gtest.C(t, func(t *gtest.T) { 160 array := garray.NewFrom(g.Slice{1, 2, 3}) 161 162 v, ok := array.PopRight() 163 t.Assert(v, 3) 164 t.Assert(ok, true) 165 t.Assert(array.Len(), 2) 166 167 v, ok = array.PopRight() 168 t.Assert(v, 2) 169 t.Assert(ok, true) 170 t.Assert(array.Len(), 1) 171 172 v, ok = array.PopRight() 173 t.Assert(v, 1) 174 t.Assert(ok, true) 175 t.Assert(array.Len(), 0) 176 }) 177 } 178 179 func TestArray_PopLefts(t *testing.T) { 180 gtest.C(t, func(t *gtest.T) { 181 array := garray.NewFrom(g.Slice{1, 2, 3}) 182 t.Assert(array.PopLefts(2), g.Slice{1, 2}) 183 t.Assert(array.Len(), 1) 184 t.Assert(array.PopLefts(2), g.Slice{3}) 185 t.Assert(array.Len(), 0) 186 }) 187 } 188 189 func TestArray_PopRights(t *testing.T) { 190 gtest.C(t, func(t *gtest.T) { 191 array := garray.NewFrom(g.Slice{1, 2, 3}) 192 t.Assert(array.PopRights(2), g.Slice{2, 3}) 193 t.Assert(array.Len(), 1) 194 t.Assert(array.PopLefts(2), g.Slice{1}) 195 t.Assert(array.Len(), 0) 196 }) 197 } 198 199 func TestArray_PopLeftsAndPopRights(t *testing.T) { 200 gtest.C(t, func(t *gtest.T) { 201 array := garray.New() 202 v, ok := array.PopLeft() 203 t.Assert(v, nil) 204 t.Assert(ok, false) 205 t.Assert(array.PopLefts(10), nil) 206 207 v, ok = array.PopRight() 208 t.Assert(v, nil) 209 t.Assert(ok, false) 210 t.Assert(array.PopRights(10), nil) 211 212 v, ok = array.PopRand() 213 t.Assert(v, nil) 214 t.Assert(ok, false) 215 t.Assert(array.PopRands(10), nil) 216 }) 217 218 gtest.C(t, func(t *gtest.T) { 219 value1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 220 value2 := []interface{}{0, 1, 2, 3, 4, 5, 6} 221 array1 := garray.NewArrayFrom(value1) 222 array2 := garray.NewArrayFrom(value2) 223 t.Assert(array1.PopLefts(2), []interface{}{0, 1}) 224 t.Assert(array1.Slice(), []interface{}{2, 3, 4, 5, 6}) 225 t.Assert(array1.PopRights(2), []interface{}{5, 6}) 226 t.Assert(array1.Slice(), []interface{}{2, 3, 4}) 227 t.Assert(array1.PopRights(20), []interface{}{2, 3, 4}) 228 t.Assert(array1.Slice(), []interface{}{}) 229 t.Assert(array2.PopLefts(20), []interface{}{0, 1, 2, 3, 4, 5, 6}) 230 t.Assert(array2.Slice(), []interface{}{}) 231 }) 232 } 233 234 func TestArray_Range(t *testing.T) { 235 gtest.C(t, func(t *gtest.T) { 236 value1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 237 array1 := garray.NewArrayFrom(value1) 238 array2 := garray.NewArrayFrom(value1, true) 239 t.Assert(array1.Range(0, 1), []interface{}{0}) 240 t.Assert(array1.Range(1, 2), []interface{}{1}) 241 t.Assert(array1.Range(0, 2), []interface{}{0, 1}) 242 t.Assert(array1.Range(-1, 10), value1) 243 t.Assert(array1.Range(10, 2), nil) 244 t.Assert(array2.Range(1, 3), []interface{}{1, 2}) 245 }) 246 } 247 248 func TestArray_Merge(t *testing.T) { 249 gtest.C(t, func(t *gtest.T) { 250 func1 := func(v1, v2 interface{}) int { 251 if gconv.Int(v1) < gconv.Int(v2) { 252 return 0 253 } 254 return 1 255 } 256 257 i1 := []interface{}{0, 1, 2, 3} 258 i2 := []interface{}{4, 5, 6, 7} 259 array1 := garray.NewArrayFrom(i1) 260 array2 := garray.NewArrayFrom(i2) 261 t.Assert(array1.Merge(array2).Slice(), []interface{}{0, 1, 2, 3, 4, 5, 6, 7}) 262 263 //s1 := []string{"a", "b", "c", "d"} 264 s2 := []string{"e", "f"} 265 i3 := garray.NewIntArrayFrom([]int{1, 2, 3}) 266 i4 := garray.NewArrayFrom([]interface{}{3}) 267 s3 := garray.NewStrArrayFrom([]string{"g", "h"}) 268 s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) 269 s5 := garray.NewSortedStrArrayFrom(s2) 270 s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) 271 a1 := garray.NewArrayFrom(i1) 272 273 t.Assert(a1.Merge(s2).Len(), 6) 274 t.Assert(a1.Merge(i3).Len(), 9) 275 t.Assert(a1.Merge(i4).Len(), 10) 276 t.Assert(a1.Merge(s3).Len(), 12) 277 t.Assert(a1.Merge(s4).Len(), 14) 278 t.Assert(a1.Merge(s5).Len(), 16) 279 t.Assert(a1.Merge(s6).Len(), 19) 280 }) 281 } 282 283 func TestArray_Fill(t *testing.T) { 284 gtest.C(t, func(t *gtest.T) { 285 a1 := []interface{}{0} 286 a2 := []interface{}{0} 287 array1 := garray.NewArrayFrom(a1) 288 array2 := garray.NewArrayFrom(a2, true) 289 290 t.Assert(array1.Fill(1, 2, 100), nil) 291 t.Assert(array1.Slice(), []interface{}{0, 100, 100}) 292 293 t.Assert(array2.Fill(0, 2, 100), nil) 294 t.Assert(array2.Slice(), []interface{}{100, 100}) 295 296 t.AssertNE(array2.Fill(-1, 2, 100), nil) 297 t.Assert(array2.Slice(), []interface{}{100, 100}) 298 }) 299 } 300 301 func TestArray_Chunk(t *testing.T) { 302 gtest.C(t, func(t *gtest.T) { 303 a1 := []interface{}{1, 2, 3, 4, 5} 304 array1 := garray.NewArrayFrom(a1) 305 chunks := array1.Chunk(2) 306 t.Assert(len(chunks), 3) 307 t.Assert(chunks[0], []interface{}{1, 2}) 308 t.Assert(chunks[1], []interface{}{3, 4}) 309 t.Assert(chunks[2], []interface{}{5}) 310 t.Assert(array1.Chunk(0), nil) 311 }) 312 gtest.C(t, func(t *gtest.T) { 313 a1 := []interface{}{1, 2, 3, 4, 5} 314 array1 := garray.NewArrayFrom(a1) 315 chunks := array1.Chunk(3) 316 t.Assert(len(chunks), 2) 317 t.Assert(chunks[0], []interface{}{1, 2, 3}) 318 t.Assert(chunks[1], []interface{}{4, 5}) 319 t.Assert(array1.Chunk(0), nil) 320 }) 321 gtest.C(t, func(t *gtest.T) { 322 a1 := []interface{}{1, 2, 3, 4, 5, 6} 323 array1 := garray.NewArrayFrom(a1) 324 chunks := array1.Chunk(2) 325 t.Assert(len(chunks), 3) 326 t.Assert(chunks[0], []interface{}{1, 2}) 327 t.Assert(chunks[1], []interface{}{3, 4}) 328 t.Assert(chunks[2], []interface{}{5, 6}) 329 t.Assert(array1.Chunk(0), nil) 330 }) 331 gtest.C(t, func(t *gtest.T) { 332 a1 := []interface{}{1, 2, 3, 4, 5, 6} 333 array1 := garray.NewArrayFrom(a1) 334 chunks := array1.Chunk(3) 335 t.Assert(len(chunks), 2) 336 t.Assert(chunks[0], []interface{}{1, 2, 3}) 337 t.Assert(chunks[1], []interface{}{4, 5, 6}) 338 t.Assert(array1.Chunk(0), nil) 339 }) 340 } 341 342 func TestArray_Pad(t *testing.T) { 343 gtest.C(t, func(t *gtest.T) { 344 a1 := []interface{}{0} 345 array1 := garray.NewArrayFrom(a1) 346 t.Assert(array1.Pad(3, 1).Slice(), []interface{}{0, 1, 1}) 347 t.Assert(array1.Pad(-4, 1).Slice(), []interface{}{1, 0, 1, 1}) 348 t.Assert(array1.Pad(3, 1).Slice(), []interface{}{1, 0, 1, 1}) 349 }) 350 } 351 352 func TestArray_SubSlice(t *testing.T) { 353 gtest.C(t, func(t *gtest.T) { 354 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 355 array1 := garray.NewArrayFrom(a1) 356 array2 := garray.NewArrayFrom(a1, true) 357 t.Assert(array1.SubSlice(0, 2), []interface{}{0, 1}) 358 t.Assert(array1.SubSlice(2, 2), []interface{}{2, 3}) 359 t.Assert(array1.SubSlice(5, 8), []interface{}{5, 6}) 360 t.Assert(array1.SubSlice(9, 1), nil) 361 t.Assert(array1.SubSlice(-2, 2), []interface{}{5, 6}) 362 t.Assert(array1.SubSlice(-9, 2), nil) 363 t.Assert(array1.SubSlice(1, -2), nil) 364 t.Assert(array2.SubSlice(0, 2), []interface{}{0, 1}) 365 }) 366 } 367 368 func TestArray_Rand(t *testing.T) { 369 gtest.C(t, func(t *gtest.T) { 370 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 371 array1 := garray.NewArrayFrom(a1) 372 t.Assert(len(array1.Rands(2)), 2) 373 t.Assert(len(array1.Rands(10)), 10) 374 t.AssertIN(array1.Rands(1)[0], a1) 375 }) 376 377 gtest.C(t, func(t *gtest.T) { 378 s1 := []interface{}{"a", "b", "c", "d"} 379 a1 := garray.NewArrayFrom(s1) 380 i1, ok := a1.Rand() 381 t.Assert(ok, true) 382 t.Assert(a1.Contains(i1), true) 383 t.Assert(a1.Len(), 4) 384 }) 385 } 386 387 func TestArray_Shuffle(t *testing.T) { 388 gtest.C(t, func(t *gtest.T) { 389 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 390 array1 := garray.NewArrayFrom(a1) 391 t.Assert(array1.Shuffle().Len(), 7) 392 }) 393 } 394 395 func TestArray_Reverse(t *testing.T) { 396 gtest.C(t, func(t *gtest.T) { 397 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 398 array1 := garray.NewArrayFrom(a1) 399 t.Assert(array1.Reverse().Slice(), []interface{}{6, 5, 4, 3, 2, 1, 0}) 400 }) 401 } 402 403 func TestArray_Join(t *testing.T) { 404 gtest.C(t, func(t *gtest.T) { 405 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 406 array1 := garray.NewArrayFrom(a1) 407 t.Assert(array1.Join("."), `0.1.2.3.4.5.6`) 408 }) 409 410 gtest.C(t, func(t *gtest.T) { 411 a1 := []interface{}{0, 1, `"a"`, `\a`} 412 array1 := garray.NewArrayFrom(a1) 413 t.Assert(array1.Join("."), `0.1."a".\a`) 414 }) 415 } 416 417 func TestArray_String(t *testing.T) { 418 gtest.C(t, func(t *gtest.T) { 419 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 420 array1 := garray.NewArrayFrom(a1) 421 t.Assert(array1.String(), `[0,1,2,3,4,5,6]`) 422 }) 423 } 424 425 func TestArray_Replace(t *testing.T) { 426 gtest.C(t, func(t *gtest.T) { 427 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 428 a2 := []interface{}{"a", "b", "c"} 429 a3 := []interface{}{"m", "n", "p", "z", "x", "y", "d", "u"} 430 array1 := garray.NewArrayFrom(a1) 431 array2 := array1.Replace(a2) 432 t.Assert(array2.Len(), 7) 433 t.Assert(array2.Contains("b"), true) 434 t.Assert(array2.Contains(4), true) 435 t.Assert(array2.Contains("v"), false) 436 array3 := array1.Replace(a3) 437 t.Assert(array3.Len(), 7) 438 t.Assert(array3.Contains(4), false) 439 t.Assert(array3.Contains("p"), true) 440 t.Assert(array3.Contains("u"), false) 441 }) 442 } 443 444 func TestArray_SetArray(t *testing.T) { 445 gtest.C(t, func(t *gtest.T) { 446 a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} 447 a2 := []interface{}{"a", "b", "c"} 448 449 array1 := garray.NewArrayFrom(a1) 450 array1 = array1.SetArray(a2) 451 t.Assert(array1.Len(), 3) 452 t.Assert(array1.Contains("b"), true) 453 t.Assert(array1.Contains("5"), false) 454 }) 455 } 456 457 func TestArray_Sum(t *testing.T) { 458 gtest.C(t, func(t *gtest.T) { 459 a1 := []interface{}{0, 1, 2, 3} 460 a2 := []interface{}{"a", "b", "c"} 461 a3 := []interface{}{"a", "1", "2"} 462 463 array1 := garray.NewArrayFrom(a1) 464 array2 := garray.NewArrayFrom(a2) 465 array3 := garray.NewArrayFrom(a3) 466 467 t.Assert(array1.Sum(), 6) 468 t.Assert(array2.Sum(), 0) 469 t.Assert(array3.Sum(), 3) 470 471 }) 472 } 473 474 func TestArray_Clone(t *testing.T) { 475 gtest.C(t, func(t *gtest.T) { 476 a1 := []interface{}{0, 1, 2, 3} 477 array1 := garray.NewArrayFrom(a1) 478 array2 := array1.Clone() 479 480 t.Assert(array1.Len(), 4) 481 t.Assert(array2.Sum(), 6) 482 t.AssertEQ(array1, array2) 483 484 }) 485 } 486 487 func TestArray_CountValues(t *testing.T) { 488 gtest.C(t, func(t *gtest.T) { 489 a1 := []interface{}{"a", "b", "c", "d", "e", "d"} 490 array1 := garray.NewArrayFrom(a1) 491 array2 := array1.CountValues() 492 t.Assert(len(array2), 5) 493 t.Assert(array2["b"], 1) 494 t.Assert(array2["d"], 2) 495 }) 496 } 497 498 func TestArray_LockFunc(t *testing.T) { 499 gtest.C(t, func(t *gtest.T) { 500 s1 := []interface{}{"a", "b", "c", "d"} 501 a1 := garray.NewArrayFrom(s1, true) 502 503 ch1 := make(chan int64, 3) 504 ch2 := make(chan int64, 3) 505 //go1 506 go a1.LockFunc(func(n1 []interface{}) { //读写锁 507 time.Sleep(2 * time.Second) //暂停2秒 508 n1[2] = "g" 509 ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 510 }) 511 512 //go2 513 go func() { 514 time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行. 515 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 516 a1.Len() 517 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 518 }() 519 520 t1 := <-ch1 521 t2 := <-ch1 522 <-ch2 //等待go1完成 523 524 // 防止ci抖动,以豪秒为单位 525 t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。 526 t.Assert(a1.Contains("g"), true) 527 }) 528 } 529 530 func TestArray_RLockFunc(t *testing.T) { 531 gtest.C(t, func(t *gtest.T) { 532 s1 := []interface{}{"a", "b", "c", "d"} 533 a1 := garray.NewArrayFrom(s1, true) 534 535 ch1 := make(chan int64, 3) 536 ch2 := make(chan int64, 1) 537 //go1 538 go a1.RLockFunc(func(n1 []interface{}) { //读锁 539 time.Sleep(2 * time.Second) //暂停1秒 540 n1[2] = "g" 541 ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 542 }) 543 544 //go2 545 go func() { 546 time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行. 547 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 548 a1.Len() 549 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 550 }() 551 552 t1 := <-ch1 553 t2 := <-ch1 554 <-ch2 //等待go1完成 555 556 // 防止ci抖动,以豪秒为单位 557 t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。 558 t.Assert(a1.Contains("g"), true) 559 }) 560 } 561 562 func TestArray_Json(t *testing.T) { 563 // pointer 564 gtest.C(t, func(t *gtest.T) { 565 s1 := []interface{}{"a", "b", "d", "c"} 566 a1 := garray.NewArrayFrom(s1) 567 b1, err1 := json.Marshal(a1) 568 b2, err2 := json.Marshal(s1) 569 t.Assert(b1, b2) 570 t.Assert(err1, err2) 571 572 a2 := garray.New() 573 err2 = json.UnmarshalUseNumber(b2, &a2) 574 t.Assert(err2, nil) 575 t.Assert(a2.Slice(), s1) 576 577 var a3 garray.Array 578 err := json.UnmarshalUseNumber(b2, &a3) 579 t.Assert(err, nil) 580 t.Assert(a3.Slice(), s1) 581 }) 582 // value. 583 gtest.C(t, func(t *gtest.T) { 584 s1 := []interface{}{"a", "b", "d", "c"} 585 a1 := *garray.NewArrayFrom(s1) 586 b1, err1 := json.Marshal(a1) 587 b2, err2 := json.Marshal(s1) 588 t.Assert(b1, b2) 589 t.Assert(err1, err2) 590 591 a2 := garray.New() 592 err2 = json.UnmarshalUseNumber(b2, &a2) 593 t.Assert(err2, nil) 594 t.Assert(a2.Slice(), s1) 595 596 var a3 garray.Array 597 err := json.UnmarshalUseNumber(b2, &a3) 598 t.Assert(err, nil) 599 t.Assert(a3.Slice(), s1) 600 }) 601 // pointer 602 gtest.C(t, func(t *gtest.T) { 603 type User struct { 604 Name string 605 Scores *garray.Array 606 } 607 data := g.Map{ 608 "Name": "john", 609 "Scores": []int{99, 100, 98}, 610 } 611 b, err := json.Marshal(data) 612 t.Assert(err, nil) 613 614 user := new(User) 615 err = json.UnmarshalUseNumber(b, user) 616 t.Assert(err, nil) 617 t.Assert(user.Name, data["Name"]) 618 t.Assert(user.Scores, data["Scores"]) 619 }) 620 // value 621 gtest.C(t, func(t *gtest.T) { 622 type User struct { 623 Name string 624 Scores garray.Array 625 } 626 data := g.Map{ 627 "Name": "john", 628 "Scores": []int{99, 100, 98}, 629 } 630 b, err := json.Marshal(data) 631 t.Assert(err, nil) 632 633 user := new(User) 634 err = json.UnmarshalUseNumber(b, user) 635 t.Assert(err, nil) 636 t.Assert(user.Name, data["Name"]) 637 t.Assert(user.Scores, data["Scores"]) 638 }) 639 } 640 641 func TestArray_Iterator(t *testing.T) { 642 slice := g.Slice{"a", "b", "d", "c"} 643 array := garray.NewArrayFrom(slice) 644 gtest.C(t, func(t *gtest.T) { 645 array.Iterator(func(k int, v interface{}) bool { 646 t.Assert(v, slice[k]) 647 return true 648 }) 649 }) 650 gtest.C(t, func(t *gtest.T) { 651 array.IteratorAsc(func(k int, v interface{}) bool { 652 t.Assert(v, slice[k]) 653 return true 654 }) 655 }) 656 gtest.C(t, func(t *gtest.T) { 657 array.IteratorDesc(func(k int, v interface{}) bool { 658 t.Assert(v, slice[k]) 659 return true 660 }) 661 }) 662 gtest.C(t, func(t *gtest.T) { 663 index := 0 664 array.Iterator(func(k int, v interface{}) bool { 665 index++ 666 return false 667 }) 668 t.Assert(index, 1) 669 }) 670 gtest.C(t, func(t *gtest.T) { 671 index := 0 672 array.IteratorAsc(func(k int, v interface{}) bool { 673 index++ 674 return false 675 }) 676 t.Assert(index, 1) 677 }) 678 gtest.C(t, func(t *gtest.T) { 679 index := 0 680 array.IteratorDesc(func(k int, v interface{}) bool { 681 index++ 682 return false 683 }) 684 t.Assert(index, 1) 685 }) 686 } 687 688 func TestArray_RemoveValue(t *testing.T) { 689 slice := g.Slice{"a", "b", "d", "c"} 690 array := garray.NewArrayFrom(slice) 691 gtest.C(t, func(t *gtest.T) { 692 t.Assert(array.RemoveValue("e"), false) 693 t.Assert(array.RemoveValue("b"), true) 694 t.Assert(array.RemoveValue("a"), true) 695 t.Assert(array.RemoveValue("c"), true) 696 t.Assert(array.RemoveValue("f"), false) 697 }) 698 } 699 700 func TestArray_UnmarshalValue(t *testing.T) { 701 type V struct { 702 Name string 703 Array *garray.Array 704 } 705 // JSON 706 gtest.C(t, func(t *gtest.T) { 707 var v *V 708 err := gconv.Struct(g.Map{ 709 "name": "john", 710 "array": []byte(`[1,2,3]`), 711 }, &v) 712 t.Assert(err, nil) 713 t.Assert(v.Name, "john") 714 t.Assert(v.Array.Slice(), g.Slice{1, 2, 3}) 715 }) 716 // Map 717 gtest.C(t, func(t *gtest.T) { 718 var v *V 719 err := gconv.Struct(g.Map{ 720 "name": "john", 721 "array": g.Slice{1, 2, 3}, 722 }, &v) 723 t.Assert(err, nil) 724 t.Assert(v.Name, "john") 725 t.Assert(v.Array.Slice(), g.Slice{1, 2, 3}) 726 }) 727 } 728 729 func TestArray_FilterNil(t *testing.T) { 730 gtest.C(t, func(t *gtest.T) { 731 values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}} 732 array := garray.NewArrayFromCopy(values) 733 t.Assert(array.FilterNil().Slice(), values) 734 }) 735 gtest.C(t, func(t *gtest.T) { 736 array := garray.NewArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil}) 737 t.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4}) 738 }) 739 } 740 741 func TestArray_FilterEmpty(t *testing.T) { 742 gtest.C(t, func(t *gtest.T) { 743 array := garray.NewArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}) 744 t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4}) 745 }) 746 gtest.C(t, func(t *gtest.T) { 747 array := garray.NewArrayFrom(g.Slice{1, 2, 3, 4}) 748 t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4}) 749 }) 750 } 751 752 func TestArray_Walk(t *testing.T) { 753 gtest.C(t, func(t *gtest.T) { 754 array := garray.NewArrayFrom(g.Slice{"1", "2"}) 755 t.Assert(array.Walk(func(value interface{}) interface{} { 756 return "key-" + gconv.String(value) 757 }), g.Slice{"key-1", "key-2"}) 758 }) 759 }