github.com/gogf/gf/v2@v2.7.4/container/glist/glist_z_unit_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 package glist 8 9 import ( 10 "container/list" 11 "testing" 12 13 "github.com/gogf/gf/v2/internal/json" 14 "github.com/gogf/gf/v2/test/gtest" 15 "github.com/gogf/gf/v2/util/gconv" 16 ) 17 18 func checkListLen(t *gtest.T, l *List, len int) bool { 19 if n := l.Len(); n != len { 20 t.Errorf("l.Len() = %d, want %d", n, len) 21 return false 22 } 23 return true 24 } 25 26 func checkListPointers(t *gtest.T, l *List, es []*Element) { 27 if !checkListLen(t, l, len(es)) { 28 return 29 } 30 l.RLockFunc(func(list *list.List) { 31 for i, e := 0, l.list.Front(); i < list.Len(); i, e = i+1, e.Next() { 32 if e.Prev() != es[i].Prev() { 33 t.Errorf("list[%d].Prev = %p, want %p", i, e.Prev(), es[i].Prev()) 34 } 35 if e.Next() != es[i].Next() { 36 t.Errorf("list[%d].Next = %p, want %p", i, e.Next(), es[i].Next()) 37 } 38 } 39 }) 40 } 41 42 func TestVar(t *testing.T) { 43 var l List 44 l.PushFront(1) 45 l.PushFront(2) 46 if v := l.PopBack(); v != 1 { 47 t.Errorf("EXPECT %v, GOT %v", 1, v) 48 } else { 49 // fmt.Println(v) 50 } 51 if v := l.PopBack(); v != 2 { 52 t.Errorf("EXPECT %v, GOT %v", 2, v) 53 } else { 54 // fmt.Println(v) 55 } 56 if v := l.PopBack(); v != nil { 57 t.Errorf("EXPECT %v, GOT %v", nil, v) 58 } else { 59 // fmt.Println(v) 60 } 61 l.PushBack(1) 62 l.PushBack(2) 63 if v := l.PopFront(); v != 1 { 64 t.Errorf("EXPECT %v, GOT %v", 1, v) 65 } else { 66 // fmt.Println(v) 67 } 68 if v := l.PopFront(); v != 2 { 69 t.Errorf("EXPECT %v, GOT %v", 2, v) 70 } else { 71 // fmt.Println(v) 72 } 73 if v := l.PopFront(); v != nil { 74 t.Errorf("EXPECT %v, GOT %v", nil, v) 75 } else { 76 // fmt.Println(v) 77 } 78 } 79 80 func TestBasic(t *testing.T) { 81 l := New() 82 l.PushFront(1) 83 l.PushFront(2) 84 if v := l.PopBack(); v != 1 { 85 t.Errorf("EXPECT %v, GOT %v", 1, v) 86 } else { 87 // fmt.Println(v) 88 } 89 if v := l.PopBack(); v != 2 { 90 t.Errorf("EXPECT %v, GOT %v", 2, v) 91 } else { 92 // fmt.Println(v) 93 } 94 if v := l.PopBack(); v != nil { 95 t.Errorf("EXPECT %v, GOT %v", nil, v) 96 } else { 97 // fmt.Println(v) 98 } 99 l.PushBack(1) 100 l.PushBack(2) 101 if v := l.PopFront(); v != 1 { 102 t.Errorf("EXPECT %v, GOT %v", 1, v) 103 } else { 104 // fmt.Println(v) 105 } 106 if v := l.PopFront(); v != 2 { 107 t.Errorf("EXPECT %v, GOT %v", 2, v) 108 } else { 109 // fmt.Println(v) 110 } 111 if v := l.PopFront(); v != nil { 112 t.Errorf("EXPECT %v, GOT %v", nil, v) 113 } else { 114 // fmt.Println(v) 115 } 116 } 117 118 func TestList(t *testing.T) { 119 gtest.C(t, func(t *gtest.T) { 120 l := New() 121 checkListPointers(t, l, []*Element{}) 122 123 // Single element list 124 e := l.PushFront("a") 125 checkListPointers(t, l, []*Element{e}) 126 l.MoveToFront(e) 127 checkListPointers(t, l, []*Element{e}) 128 l.MoveToBack(e) 129 checkListPointers(t, l, []*Element{e}) 130 l.Remove(e) 131 checkListPointers(t, l, []*Element{}) 132 133 // Bigger list 134 e2 := l.PushFront(2) 135 e1 := l.PushFront(1) 136 e3 := l.PushBack(3) 137 e4 := l.PushBack("banana") 138 checkListPointers(t, l, []*Element{e1, e2, e3, e4}) 139 140 l.Remove(e2) 141 checkListPointers(t, l, []*Element{e1, e3, e4}) 142 143 l.MoveToFront(e3) // move from middle 144 checkListPointers(t, l, []*Element{e3, e1, e4}) 145 146 l.MoveToFront(e1) 147 l.MoveToBack(e3) // move from middle 148 checkListPointers(t, l, []*Element{e1, e4, e3}) 149 150 l.MoveToFront(e3) // move from back 151 checkListPointers(t, l, []*Element{e3, e1, e4}) 152 l.MoveToFront(e3) // should be no-op 153 checkListPointers(t, l, []*Element{e3, e1, e4}) 154 155 l.MoveToBack(e3) // move from front 156 checkListPointers(t, l, []*Element{e1, e4, e3}) 157 l.MoveToBack(e3) // should be no-op 158 checkListPointers(t, l, []*Element{e1, e4, e3}) 159 160 e2 = l.InsertBefore(e1, 2) // insert before front 161 checkListPointers(t, l, []*Element{e2, e1, e4, e3}) 162 l.Remove(e2) 163 e2 = l.InsertBefore(e4, 2) // insert before middle 164 checkListPointers(t, l, []*Element{e1, e2, e4, e3}) 165 l.Remove(e2) 166 e2 = l.InsertBefore(e3, 2) // insert before back 167 checkListPointers(t, l, []*Element{e1, e4, e2, e3}) 168 l.Remove(e2) 169 170 e2 = l.InsertAfter(e1, 2) // insert after front 171 checkListPointers(t, l, []*Element{e1, e2, e4, e3}) 172 l.Remove(e2) 173 e2 = l.InsertAfter(e4, 2) // insert after middle 174 checkListPointers(t, l, []*Element{e1, e4, e2, e3}) 175 l.Remove(e2) 176 e2 = l.InsertAfter(e3, 2) // insert after back 177 checkListPointers(t, l, []*Element{e1, e4, e3, e2}) 178 l.Remove(e2) 179 180 // Check standard iteration. 181 sum := 0 182 for e := l.Front(); e != nil; e = e.Next() { 183 if i, ok := e.Value.(int); ok { 184 sum += i 185 } 186 } 187 if sum != 4 { 188 t.Errorf("sum over l = %d, want 4", sum) 189 } 190 191 // Clear all elements by iterating 192 var next *Element 193 for e := l.Front(); e != nil; e = next { 194 next = e.Next() 195 l.Remove(e) 196 } 197 checkListPointers(t, l, []*Element{}) 198 }) 199 } 200 201 func checkList(t *gtest.T, l *List, es []interface{}) { 202 if !checkListLen(t, l, len(es)) { 203 return 204 } 205 206 i := 0 207 for e := l.Front(); e != nil; e = e.Next() { 208 209 switch e.Value.(type) { 210 case int: 211 if le := e.Value.(int); le != es[i] { 212 t.Errorf("elt[%d].Value() = %v, want %v", i, le, es[i]) 213 } 214 // default string 215 default: 216 if le := e.Value.(string); le != es[i] { 217 t.Errorf("elt[%v].Value() = %v, want %v", i, le, es[i]) 218 } 219 } 220 221 i++ 222 } 223 224 // for e := l.Front(); e != nil; e = e.Next() { 225 // le := e.Value.(int) 226 // if le != es[i] { 227 // t.Errorf("elt[%d].Value() = %v, want %v", i, le, es[i]) 228 // } 229 // i++ 230 // } 231 } 232 233 func TestExtending(t *testing.T) { 234 gtest.C(t, func(t *gtest.T) { 235 l1 := New() 236 l2 := New() 237 238 l1.PushBack(1) 239 l1.PushBack(2) 240 l1.PushBack(3) 241 242 l2.PushBack(4) 243 l2.PushBack(5) 244 245 l3 := New() 246 l3.PushBackList(l1) 247 checkList(t, l3, []interface{}{1, 2, 3}) 248 l3.PushBackList(l2) 249 checkList(t, l3, []interface{}{1, 2, 3, 4, 5}) 250 251 l3 = New() 252 l3.PushFrontList(l2) 253 checkList(t, l3, []interface{}{4, 5}) 254 l3.PushFrontList(l1) 255 checkList(t, l3, []interface{}{1, 2, 3, 4, 5}) 256 257 checkList(t, l1, []interface{}{1, 2, 3}) 258 checkList(t, l2, []interface{}{4, 5}) 259 260 l3 = New() 261 l3.PushBackList(l1) 262 checkList(t, l3, []interface{}{1, 2, 3}) 263 l3.PushBackList(l3) 264 checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3}) 265 266 l3 = New() 267 l3.PushFrontList(l1) 268 checkList(t, l3, []interface{}{1, 2, 3}) 269 l3.PushFrontList(l3) 270 checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3}) 271 272 l3 = New() 273 l1.PushBackList(l3) 274 checkList(t, l1, []interface{}{1, 2, 3}) 275 l1.PushFrontList(l3) 276 checkList(t, l1, []interface{}{1, 2, 3}) 277 }) 278 } 279 280 func TestRemove(t *testing.T) { 281 gtest.C(t, func(t *gtest.T) { 282 l := New() 283 e1 := l.PushBack(1) 284 e2 := l.PushBack(2) 285 checkListPointers(t, l, []*Element{e1, e2}) 286 // e := l.Front() 287 // l.Remove(e) 288 // checkListPointers(t, l, []*Element{e2}) 289 // l.Remove(e) 290 // checkListPointers(t, l, []*Element{e2}) 291 }) 292 } 293 294 func Test_Issue4103(t *testing.T) { 295 l1 := New() 296 l1.PushBack(1) 297 l1.PushBack(2) 298 299 l2 := New() 300 l2.PushBack(3) 301 l2.PushBack(4) 302 303 e := l1.Front() 304 l2.Remove(e) // l2 should not change because e is not an element of l2 305 if n := l2.Len(); n != 2 { 306 t.Errorf("l2.Len() = %d, want 2", n) 307 } 308 309 l1.InsertBefore(e, 8) 310 if n := l1.Len(); n != 3 { 311 t.Errorf("l1.Len() = %d, want 3", n) 312 } 313 } 314 315 func Test_Issue6349(t *testing.T) { 316 l := New() 317 l.PushBack(1) 318 l.PushBack(2) 319 320 e := l.Front() 321 l.Remove(e) 322 if e.Value != 1 { 323 t.Errorf("e.value = %d, want 1", e.Value) 324 } 325 // if e.Next() != nil { 326 // t.Errorf("e.Next() != nil") 327 // } 328 // if e.Prev() != nil { 329 // t.Errorf("e.Prev() != nil") 330 // } 331 } 332 333 func TestMove(t *testing.T) { 334 gtest.C(t, func(t *gtest.T) { 335 l := New() 336 e1 := l.PushBack(1) 337 e2 := l.PushBack(2) 338 e3 := l.PushBack(3) 339 e4 := l.PushBack(4) 340 341 l.MoveAfter(e3, e3) 342 checkListPointers(t, l, []*Element{e1, e2, e3, e4}) 343 l.MoveBefore(e2, e2) 344 checkListPointers(t, l, []*Element{e1, e2, e3, e4}) 345 346 l.MoveAfter(e3, e2) 347 checkListPointers(t, l, []*Element{e1, e2, e3, e4}) 348 l.MoveBefore(e2, e3) 349 checkListPointers(t, l, []*Element{e1, e2, e3, e4}) 350 351 l.MoveBefore(e2, e4) 352 checkListPointers(t, l, []*Element{e1, e3, e2, e4}) 353 e2, e3 = e3, e2 354 355 l.MoveBefore(e4, e1) 356 checkListPointers(t, l, []*Element{e4, e1, e2, e3}) 357 e1, e2, e3, e4 = e4, e1, e2, e3 358 359 l.MoveAfter(e4, e1) 360 checkListPointers(t, l, []*Element{e1, e4, e2, e3}) 361 e2, e3, e4 = e4, e2, e3 362 363 l.MoveAfter(e2, e3) 364 checkListPointers(t, l, []*Element{e1, e3, e2, e4}) 365 e2, e3 = e3, e2 366 }) 367 } 368 369 // Test PushFront, PushBack, PushFrontList, PushBackList with uninitialized List 370 func TestZeroList(t *testing.T) { 371 gtest.C(t, func(t *gtest.T) { 372 var l1 = New() 373 l1.PushFront(1) 374 checkList(t, l1, []interface{}{1}) 375 376 var l2 = New() 377 l2.PushBack(1) 378 checkList(t, l2, []interface{}{1}) 379 380 var l3 = New() 381 l3.PushFrontList(l1) 382 checkList(t, l3, []interface{}{1}) 383 384 var l4 = New() 385 l4.PushBackList(l2) 386 checkList(t, l4, []interface{}{1}) 387 }) 388 } 389 390 // Test that a list l is not modified when calling InsertBefore with a mark that is not an element of l. 391 func TestInsertBeforeUnknownMark(t *testing.T) { 392 gtest.C(t, func(t *gtest.T) { 393 l := New() 394 l.PushBack(1) 395 l.PushBack(2) 396 l.PushBack(3) 397 l.InsertBefore(new(Element), 1) 398 checkList(t, l, []interface{}{1, 2, 3}) 399 }) 400 } 401 402 // Test that a list l is not modified when calling InsertAfter with a mark that is not an element of l. 403 func TestInsertAfterUnknownMark(t *testing.T) { 404 gtest.C(t, func(t *gtest.T) { 405 l := New() 406 l.PushBack(1) 407 l.PushBack(2) 408 l.PushBack(3) 409 l.InsertAfter(new(Element), 1) 410 checkList(t, l, []interface{}{1, 2, 3}) 411 }) 412 } 413 414 // Test that a list l is not modified when calling MoveAfter or MoveBefore with a mark that is not an element of l. 415 func TestMoveUnknownMark(t *testing.T) { 416 gtest.C(t, func(t *gtest.T) { 417 l1 := New() 418 e1 := l1.PushBack(1) 419 420 l2 := New() 421 e2 := l2.PushBack(2) 422 423 l1.MoveAfter(e1, e2) 424 checkList(t, l1, []interface{}{1}) 425 checkList(t, l2, []interface{}{2}) 426 427 l1.MoveBefore(e1, e2) 428 checkList(t, l1, []interface{}{1}) 429 checkList(t, l2, []interface{}{2}) 430 }) 431 } 432 433 func TestList_RemoveAll(t *testing.T) { 434 gtest.C(t, func(t *gtest.T) { 435 l := New() 436 l.PushBack(1) 437 l.RemoveAll() 438 checkList(t, l, []interface{}{}) 439 l.PushBack(2) 440 checkList(t, l, []interface{}{2}) 441 }) 442 } 443 444 func TestList_PushFronts(t *testing.T) { 445 gtest.C(t, func(t *gtest.T) { 446 l := New() 447 a1 := []interface{}{1, 2} 448 l.PushFronts(a1) 449 checkList(t, l, []interface{}{2, 1}) 450 a1 = []interface{}{3, 4, 5} 451 l.PushFronts(a1) 452 checkList(t, l, []interface{}{5, 4, 3, 2, 1}) 453 }) 454 } 455 456 func TestList_PushBacks(t *testing.T) { 457 gtest.C(t, func(t *gtest.T) { 458 l := New() 459 a1 := []interface{}{1, 2} 460 l.PushBacks(a1) 461 checkList(t, l, []interface{}{1, 2}) 462 a1 = []interface{}{3, 4, 5} 463 l.PushBacks(a1) 464 checkList(t, l, []interface{}{1, 2, 3, 4, 5}) 465 }) 466 } 467 468 func TestList_PopBacks(t *testing.T) { 469 gtest.C(t, func(t *gtest.T) { 470 l := New() 471 a1 := []interface{}{1, 2, 3, 4} 472 a2 := []interface{}{"a", "c", "b", "e"} 473 l.PushFronts(a1) 474 i1 := l.PopBacks(2) 475 t.Assert(i1, []interface{}{1, 2}) 476 477 l.PushBacks(a2) // 4.3,a,c,b,e 478 i1 = l.PopBacks(3) 479 t.Assert(i1, []interface{}{"e", "b", "c"}) 480 }) 481 } 482 483 func TestList_PopFronts(t *testing.T) { 484 gtest.C(t, func(t *gtest.T) { 485 l := New() 486 a1 := []interface{}{1, 2, 3, 4} 487 l.PushFronts(a1) 488 i1 := l.PopFronts(2) 489 t.Assert(i1, []interface{}{4, 3}) 490 t.Assert(l.Len(), 2) 491 }) 492 } 493 494 func TestList_PopBackAll(t *testing.T) { 495 gtest.C(t, func(t *gtest.T) { 496 l := New() 497 a1 := []interface{}{1, 2, 3, 4} 498 l.PushFronts(a1) 499 i1 := l.PopBackAll() 500 t.Assert(i1, []interface{}{1, 2, 3, 4}) 501 t.Assert(l.Len(), 0) 502 }) 503 } 504 505 func TestList_PopFrontAll(t *testing.T) { 506 gtest.C(t, func(t *gtest.T) { 507 l := New() 508 a1 := []interface{}{1, 2, 3, 4} 509 l.PushFronts(a1) 510 i1 := l.PopFrontAll() 511 t.Assert(i1, []interface{}{4, 3, 2, 1}) 512 t.Assert(l.Len(), 0) 513 }) 514 } 515 516 func TestList_FrontAll(t *testing.T) { 517 gtest.C(t, func(t *gtest.T) { 518 l := New() 519 a1 := []interface{}{1, 2, 3, 4} 520 l.PushFronts(a1) 521 i1 := l.FrontAll() 522 t.Assert(i1, []interface{}{4, 3, 2, 1}) 523 t.Assert(l.Len(), 4) 524 }) 525 } 526 527 func TestList_BackAll(t *testing.T) { 528 gtest.C(t, func(t *gtest.T) { 529 l := New() 530 a1 := []interface{}{1, 2, 3, 4} 531 l.PushFronts(a1) 532 i1 := l.BackAll() 533 t.Assert(i1, []interface{}{1, 2, 3, 4}) 534 t.Assert(l.Len(), 4) 535 }) 536 } 537 538 func TestList_FrontValue(t *testing.T) { 539 gtest.C(t, func(t *gtest.T) { 540 l := New() 541 l2 := New() 542 a1 := []interface{}{1, 2, 3, 4} 543 l.PushFronts(a1) 544 i1 := l.FrontValue() 545 t.Assert(gconv.Int(i1), 4) 546 t.Assert(l.Len(), 4) 547 548 i1 = l2.FrontValue() 549 t.Assert(i1, nil) 550 }) 551 } 552 553 func TestList_BackValue(t *testing.T) { 554 gtest.C(t, func(t *gtest.T) { 555 l := New() 556 l2 := New() 557 a1 := []interface{}{1, 2, 3, 4} 558 l.PushFronts(a1) 559 i1 := l.BackValue() 560 t.Assert(gconv.Int(i1), 1) 561 t.Assert(l.Len(), 4) 562 563 i1 = l2.FrontValue() 564 t.Assert(i1, nil) 565 }) 566 } 567 568 func TestList_Back(t *testing.T) { 569 gtest.C(t, func(t *gtest.T) { 570 l := New() 571 a1 := []interface{}{1, 2, 3, 4} 572 l.PushFronts(a1) 573 e1 := l.Back() 574 t.Assert(e1.Value, 1) 575 t.Assert(l.Len(), 4) 576 }) 577 } 578 579 func TestList_Size(t *testing.T) { 580 gtest.C(t, func(t *gtest.T) { 581 l := New() 582 a1 := []interface{}{1, 2, 3, 4} 583 l.PushFronts(a1) 584 t.Assert(l.Size(), 4) 585 l.PopFront() 586 t.Assert(l.Size(), 3) 587 }) 588 } 589 590 func TestList_Removes(t *testing.T) { 591 gtest.C(t, func(t *gtest.T) { 592 l := New() 593 a1 := []interface{}{1, 2, 3, 4} 594 l.PushFronts(a1) 595 e1 := l.Back() 596 l.Removes([]*Element{e1}) 597 t.Assert(l.Len(), 3) 598 599 e2 := l.Back() 600 l.Removes([]*Element{e2}) 601 t.Assert(l.Len(), 2) 602 checkList(t, l, []interface{}{4, 3}) 603 }) 604 } 605 606 func TestList_Pop(t *testing.T) { 607 gtest.C(t, func(t *gtest.T) { 608 l := NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) 609 610 t.Assert(l.PopBack(), 9) 611 t.Assert(l.PopBacks(2), []interface{}{8, 7}) 612 t.Assert(l.PopFront(), 1) 613 t.Assert(l.PopFronts(2), []interface{}{2, 3}) 614 }) 615 } 616 617 func TestList_Clear(t *testing.T) { 618 gtest.C(t, func(t *gtest.T) { 619 l := New() 620 a1 := []interface{}{1, 2, 3, 4} 621 l.PushFronts(a1) 622 l.Clear() 623 t.Assert(l.Len(), 0) 624 }) 625 } 626 627 func TestList_IteratorAsc(t *testing.T) { 628 gtest.C(t, func(t *gtest.T) { 629 l := New() 630 a1 := []interface{}{1, 2, 5, 6, 3, 4} 631 l.PushFronts(a1) 632 e1 := l.Back() 633 fun1 := func(e *Element) bool { 634 return gconv.Int(e1.Value) > 2 635 } 636 checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1}) 637 l.IteratorAsc(fun1) 638 checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1}) 639 }) 640 } 641 642 func TestList_IteratorDesc(t *testing.T) { 643 gtest.C(t, func(t *gtest.T) { 644 l := New() 645 a1 := []interface{}{1, 2, 3, 4} 646 l.PushFronts(a1) 647 e1 := l.Back() 648 fun1 := func(e *Element) bool { 649 return gconv.Int(e1.Value) > 6 650 } 651 l.IteratorDesc(fun1) 652 t.Assert(l.Len(), 4) 653 checkList(t, l, []interface{}{4, 3, 2, 1}) 654 }) 655 } 656 657 func TestList_Iterator(t *testing.T) { 658 gtest.C(t, func(t *gtest.T) { 659 l := New() 660 a1 := []interface{}{"a", "b", "c", "d", "e"} 661 l.PushFronts(a1) 662 e1 := l.Back() 663 fun1 := func(e *Element) bool { 664 return gconv.String(e1.Value) > "c" 665 } 666 checkList(t, l, []interface{}{"e", "d", "c", "b", "a"}) 667 l.Iterator(fun1) 668 checkList(t, l, []interface{}{"e", "d", "c", "b", "a"}) 669 }) 670 } 671 672 func TestList_Join(t *testing.T) { 673 gtest.C(t, func(t *gtest.T) { 674 l := NewFrom([]interface{}{1, 2, "a", `"b"`, `\c`}) 675 t.Assert(l.Join(","), `1,2,a,"b",\c`) 676 t.Assert(l.Join("."), `1.2.a."b".\c`) 677 }) 678 } 679 680 func TestList_String(t *testing.T) { 681 gtest.C(t, func(t *gtest.T) { 682 l := NewFrom([]interface{}{1, 2, "a", `"b"`, `\c`}) 683 t.Assert(l.String(), `[1,2,a,"b",\c]`) 684 }) 685 } 686 687 func TestList_Json(t *testing.T) { 688 // Marshal 689 gtest.C(t, func(t *gtest.T) { 690 a := []interface{}{"a", "b", "c"} 691 l := New() 692 l.PushBacks(a) 693 b1, err1 := json.Marshal(l) 694 b2, err2 := json.Marshal(a) 695 t.Assert(err1, err2) 696 t.Assert(b1, b2) 697 }) 698 // Unmarshal 699 gtest.C(t, func(t *gtest.T) { 700 a := []interface{}{"a", "b", "c"} 701 l := New() 702 b, err := json.Marshal(a) 703 t.AssertNil(err) 704 705 err = json.UnmarshalUseNumber(b, l) 706 t.AssertNil(err) 707 t.Assert(l.FrontAll(), a) 708 }) 709 gtest.C(t, func(t *gtest.T) { 710 var l List 711 a := []interface{}{"a", "b", "c"} 712 b, err := json.Marshal(a) 713 t.AssertNil(err) 714 715 err = json.UnmarshalUseNumber(b, &l) 716 t.AssertNil(err) 717 t.Assert(l.FrontAll(), a) 718 }) 719 } 720 721 func TestList_UnmarshalValue(t *testing.T) { 722 type TList struct { 723 Name string 724 List *List 725 } 726 // JSON 727 gtest.C(t, func(t *gtest.T) { 728 var tlist *TList 729 err := gconv.Struct(map[string]interface{}{ 730 "name": "john", 731 "list": []byte(`[1,2,3]`), 732 }, &tlist) 733 t.AssertNil(err) 734 t.Assert(tlist.Name, "john") 735 t.Assert(tlist.List.FrontAll(), []interface{}{1, 2, 3}) 736 }) 737 // Map 738 gtest.C(t, func(t *gtest.T) { 739 var tlist *TList 740 err := gconv.Struct(map[string]interface{}{ 741 "name": "john", 742 "list": []interface{}{1, 2, 3}, 743 }, &tlist) 744 t.AssertNil(err) 745 t.Assert(tlist.Name, "john") 746 t.Assert(tlist.List.FrontAll(), []interface{}{1, 2, 3}) 747 }) 748 } 749 750 func TestList_DeepCopy(t *testing.T) { 751 gtest.C(t, func(t *gtest.T) { 752 l := NewFrom([]interface{}{1, 2, "a", `"b"`, `\c`}) 753 copyList := l.DeepCopy() 754 cl := copyList.(*List) 755 cl.PopBack() 756 t.AssertNE(l.Size(), cl.Size()) 757 }) 758 }