|
1 | 1 | from collections import defaultdict, deque
|
| 2 | +from copy import deepcopy |
2 | 3 | from dataclasses import dataclass, field
|
3 | 4 | from test.pycardano.util import check_two_way_cbor
|
4 | 5 | from typing import (
|
@@ -879,3 +880,105 @@ class ChildCodedClass(TestCodedClass):
|
879 | 880 | assert restored.value == "test"
|
880 | 881 | assert restored.numbers == [1, 2]
|
881 | 882 | assert restored.extra == "extra"
|
| 883 | + |
| 884 | + |
| 885 | +def test_ordered_set_deepcopy(): |
| 886 | + """Test the deepcopy implementation of OrderedSet.""" |
| 887 | + # Test basic deepcopy |
| 888 | + |
| 889 | + class MyOrderedSet(OrderedSet): |
| 890 | + pass |
| 891 | + |
| 892 | + s = MyOrderedSet([1, 2, 3], use_tag=True) |
| 893 | + s_copy = deepcopy(s) |
| 894 | + |
| 895 | + assert s == s_copy |
| 896 | + assert s is not s_copy |
| 897 | + assert s._use_tag == s_copy._use_tag |
| 898 | + |
| 899 | + # Test that modifications don't affect each other |
| 900 | + s_copy.append(4) |
| 901 | + assert 4 in s_copy |
| 902 | + assert 4 not in s |
| 903 | + |
| 904 | + # Test with complex objects |
| 905 | + class TestObj: |
| 906 | + def __init__(self, value): |
| 907 | + self.value = value |
| 908 | + |
| 909 | + def __str__(self): |
| 910 | + return f"TestObj({self.value})" |
| 911 | + |
| 912 | + obj1 = TestObj("a") |
| 913 | + obj2 = TestObj("b") |
| 914 | + |
| 915 | + s = MyOrderedSet([obj1, obj2], use_tag=False) |
| 916 | + s_copy = deepcopy(s) |
| 917 | + |
| 918 | + # Objects should be equal but not the same instances |
| 919 | + assert len(s) == len(s_copy) == 2 |
| 920 | + assert s[0].value == s_copy[0].value |
| 921 | + assert s[1].value == s_copy[1].value |
| 922 | + assert s[0] is not s_copy[0] |
| 923 | + assert s[1] is not s_copy[1] |
| 924 | + |
| 925 | + # Test that memodict works with shared references |
| 926 | + shared_obj = TestObj("shared") |
| 927 | + s = MyOrderedSet([shared_obj, shared_obj], use_tag=True) # Same object twice |
| 928 | + |
| 929 | + s_copy = deepcopy(s) |
| 930 | + # In the copy, both elements should be the same object (preserved reference) |
| 931 | + assert len(s_copy) == 1 |
| 932 | + assert s_copy[0] is not shared_obj |
| 933 | + |
| 934 | + |
| 935 | +def test_non_empty_ordered_set_deepcopy(): |
| 936 | + """Test the deepcopy implementation of NonEmptyOrderedSet.""" |
| 937 | + |
| 938 | + class MyNonEmptyOrderedSet(NonEmptyOrderedSet): |
| 939 | + pass |
| 940 | + |
| 941 | + # Test basic deepcopy |
| 942 | + s = MyNonEmptyOrderedSet([1, 2, 3], use_tag=True) |
| 943 | + s_copy = deepcopy(s) |
| 944 | + |
| 945 | + assert s == s_copy |
| 946 | + assert s is not s_copy |
| 947 | + assert s._use_tag == s_copy._use_tag |
| 948 | + |
| 949 | + # Test with nested lists |
| 950 | + nested_list = [[1, 2], [3, 4]] |
| 951 | + s = MyNonEmptyOrderedSet(nested_list, use_tag=False) |
| 952 | + s_copy = deepcopy(s) |
| 953 | + |
| 954 | + # Lists should be equal but not the same instances |
| 955 | + assert s[0] == s_copy[0] |
| 956 | + assert s[1] == s_copy[1] |
| 957 | + assert s[0] is not s_copy[0] |
| 958 | + assert s[1] is not s_copy[1] |
| 959 | + |
| 960 | + # Modifying the copy shouldn't affect the original |
| 961 | + s_copy[0].append(5) |
| 962 | + assert s[0] == [1, 2] |
| 963 | + assert s_copy[0] == [1, 2, 5] |
| 964 | + |
| 965 | + # Test complex nesting with CBORSerializable objects |
| 966 | + @dataclass |
| 967 | + class TestData(MapCBORSerializable): |
| 968 | + value: int = 0 |
| 969 | + |
| 970 | + obj1 = TestData(1) |
| 971 | + obj2 = TestData(2) |
| 972 | + s = MyNonEmptyOrderedSet([obj1, obj2], use_tag=True) |
| 973 | + s_copy = deepcopy(s) |
| 974 | + |
| 975 | + # Objects should be equal but not the same instances |
| 976 | + assert s[0].value == s_copy[0].value |
| 977 | + assert s[1].value == s_copy[1].value |
| 978 | + assert s[0] is not s_copy[0] |
| 979 | + assert s[1] is not s_copy[1] |
| 980 | + |
| 981 | + # Modifying the copy shouldn't affect the original |
| 982 | + s_copy[0].value = 100 |
| 983 | + assert s[0].value == 1 |
| 984 | + assert s_copy[0].value == 100 |
0 commit comments