|
11 | 11 | y.extend(x) |
12 | 12 | assert y == [2, 1, 2, 3, 1, 2, 3] |
13 | 13 |
|
| 14 | +a = [] |
| 15 | +a.extend((1,2,3,4)) |
| 16 | +assert a == [1, 2, 3, 4] |
| 17 | + |
| 18 | +a.extend('abcdefg') |
| 19 | +assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g'] |
| 20 | + |
| 21 | +a.extend(range(10)) |
| 22 | +assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 23 | + |
| 24 | +a = [] |
| 25 | +a.extend({1,2,3,4}) |
| 26 | +assert a == [1, 2, 3, 4] |
| 27 | + |
| 28 | +a.extend({'a': 1, 'b': 2, 'z': 51}) |
| 29 | +assert a == [1, 2, 3, 4, 'a', 'b', 'z'] |
| 30 | + |
| 31 | +class Iter: |
| 32 | + def __iter__(self): |
| 33 | + yield 12 |
| 34 | + yield 28 |
| 35 | + |
| 36 | +a.extend(Iter()) |
| 37 | +assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28] |
| 38 | + |
| 39 | +a.extend(bytes(b'hello world')) |
| 40 | +assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] |
| 41 | + |
| 42 | +class Next: |
| 43 | + def __next__(self): |
| 44 | + yield 12 |
| 45 | + yield 28 |
| 46 | + |
| 47 | +assert_raises(TypeError, lambda: [].extend(3)) |
| 48 | +assert_raises(TypeError, lambda: [].extend(slice(0, 10, 1))) |
| 49 | + |
14 | 50 | assert x * 0 == [], "list __mul__ by 0 failed" |
15 | 51 | assert x * -1 == [], "list __mul__ by -1 failed" |
16 | 52 | assert x * 2 == [1, 2, 3, 1, 2, 3], "list __mul__ by 2 failed" |
@@ -532,3 +568,54 @@ def bad_iter_assign(): |
532 | 568 | assert [float('inf'), float('inf')] >= [float('inf'), float('inf')] |
533 | 569 | assert not [float('inf'), float('inf')] < [float('inf'), float('inf')] |
534 | 570 | assert not [float('inf'), float('inf')] > [float('inf'), float('inf')] |
| 571 | + |
| 572 | +# list __iadd__ |
| 573 | +a = [] |
| 574 | +a += [1, 2, 3] |
| 575 | +assert a == [1, 2, 3] |
| 576 | + |
| 577 | +a = [] |
| 578 | +a += (1,2,3,4) |
| 579 | +assert a == [1, 2, 3, 4] |
| 580 | + |
| 581 | +a += 'abcdefg' |
| 582 | +assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g'] |
| 583 | + |
| 584 | +a += range(10) |
| 585 | +assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 586 | + |
| 587 | +a = [] |
| 588 | +a += {1,2,3,4} |
| 589 | +assert a == [1, 2, 3, 4] |
| 590 | + |
| 591 | +a += {'a': 1, 'b': 2, 'z': 51} |
| 592 | +assert a == [1, 2, 3, 4, 'a', 'b', 'z'] |
| 593 | + |
| 594 | +class Iter: |
| 595 | + def __iter__(self): |
| 596 | + yield 12 |
| 597 | + yield 28 |
| 598 | + |
| 599 | +a += Iter() |
| 600 | +assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28] |
| 601 | + |
| 602 | +a += bytes(b'hello world') |
| 603 | +assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] |
| 604 | + |
| 605 | +class Next: |
| 606 | + def __next__(self): |
| 607 | + yield 12 |
| 608 | + yield 28 |
| 609 | + |
| 610 | +def iadd_int(): |
| 611 | + a = [] |
| 612 | + a += 3 |
| 613 | + |
| 614 | +def iadd_slice(): |
| 615 | + a = [] |
| 616 | + a += slice(0, 10, 1) |
| 617 | + |
| 618 | +assert_raises(TypeError, iadd_int) |
| 619 | +assert_raises(TypeError, iadd_slice) |
| 620 | + |
| 621 | + |
0 commit comments