|
| 1 | + |
| 2 | +import testutils |
| 3 | + |
| 4 | +def test_dunion_ior0(): |
| 5 | + a={1:2,2:3} |
| 6 | + b={3:4,5:6} |
| 7 | + a|=b |
| 8 | + |
| 9 | + assert a == {1:2,2:3,3:4,5:6}, f"wrong value assigned {a=}" |
| 10 | + assert b == {3:4,5:6}, f"right hand side modified, {b=}" |
| 11 | + |
| 12 | +def test_dunion_or0(): |
| 13 | + a={1:2,2:3} |
| 14 | + b={3:4,5:6} |
| 15 | + c=a|b |
| 16 | + |
| 17 | + assert a == {1:2,2:3}, f"left hand side of non-assignment operator modified {a=}" |
| 18 | + assert b == {3:4,5:6}, f"right hand side of non-assignment operator modified, {b=}" |
| 19 | + assert c == {1:2,2:3, 3:4, 5:6}, f"unexpected result of dict union {c=}" |
| 20 | + |
| 21 | + |
| 22 | +def test_dunion_or1(): |
| 23 | + a={1:2,2:3} |
| 24 | + b={3:4,5:6} |
| 25 | + c=a.__or__(b) |
| 26 | + |
| 27 | + assert a == {1:2,2:3}, f"left hand side of non-assignment operator modified {a=}" |
| 28 | + assert b == {3:4,5:6}, f"right hand side of non-assignment operator modified, {b=}" |
| 29 | + assert c == {1:2,2:3, 3:4, 5:6}, f"unexpected result of dict union {c=}" |
| 30 | + |
| 31 | + |
| 32 | +def test_dunion_ror0(): |
| 33 | + a={1:2,2:3} |
| 34 | + b={3:4,5:6} |
| 35 | + c=b.__ror__(a) |
| 36 | + |
| 37 | + assert a == {1:2,2:3}, f"left hand side of non-assignment operator modified {a=}" |
| 38 | + assert b == {3:4,5:6}, f"right hand side of non-assignment operator modified, {b=}" |
| 39 | + assert c == {1:2,2:3, 3:4, 5:6}, f"unexpected result of dict union {c=}" |
| 40 | + |
| 41 | + |
| 42 | +def test_dunion_other_types(): |
| 43 | + def perf_test_or(other_obj): |
| 44 | + d={1:2} |
| 45 | + try: |
| 46 | + d.__or__(other_obj) |
| 47 | + except: |
| 48 | + return True |
| 49 | + return False |
| 50 | + |
| 51 | + def perf_test_ior(other_obj): |
| 52 | + d={1:2} |
| 53 | + try: |
| 54 | + d.__ior__(other_obj) |
| 55 | + except: |
| 56 | + return True |
| 57 | + return False |
| 58 | + |
| 59 | + def perf_test_ror(other_obj): |
| 60 | + d={1:2} |
| 61 | + try: |
| 62 | + d.__ror__(other_obj) |
| 63 | + except: |
| 64 | + return True |
| 65 | + return False |
| 66 | + |
| 67 | + test_fct={'__or__':perf_test_or, '__ror__':perf_test_ror, '__ior__':perf_test_ior} |
| 68 | + others=['FooBar', 42, [36], set([19]), ['aa'], None] |
| 69 | + for tfn,tf in test_fct.items(): |
| 70 | + for other in others: |
| 71 | + assert tf(other), f"Failed: dict {tfn}, accepted {other}" |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +testutils.skip_if_unsupported(3,9,test_dunion_ior0) |
| 77 | +testutils.skip_if_unsupported(3,9,test_dunion_or0) |
| 78 | +testutils.skip_if_unsupported(3,9,test_dunion_or1) |
| 79 | +testutils.skip_if_unsupported(3,9,test_dunion_ror0) |
| 80 | +testutils.skip_if_unsupported(3,9,test_dunion_other_types) |
| 81 | + |
| 82 | + |
| 83 | + |
0 commit comments