-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_encoder_fix.py
More file actions
68 lines (58 loc) · 2.15 KB
/
test_encoder_fix.py
File metadata and controls
68 lines (58 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
"""Test the updated encoder implementation."""
import sys
sys.path.insert(0, "src")
from link_notation_objects_codec import encode, decode
# Test 1: Simple self-reference
print("Test 1: Simple self-reference")
obj = {}
obj["self"] = obj
encoded = encode(obj)
print(f" Encoded: {encoded}")
print(f" Lines: {len(encoded.split(chr(10)))}")
decoded = decode(encoded)
print(f" Decoded correctly: {decoded is decoded.get('self')}")
print()
# Test 2: Mutual reference dicts
print("Test 2: Mutual reference dicts")
dict1 = {"name": "dict1"}
dict2 = {"name": "dict2"}
dict1["other"] = dict2
dict2["other"] = dict1
encoded = encode(dict1)
print(f" Encoded:\n{encoded}")
print(f" Lines: {len(encoded.split(chr(10)))}")
decoded = decode(encoded)
print(f" Decoded has 'name': {'name' in decoded}")
print(f" Decoded has 'other': {'other' in decoded}")
if "other" in decoded and "other" in decoded["other"]:
print(f" Circular ref works: {decoded['other']['other'] is decoded}")
print()
# Test 3: List with multiple references to same object
print("Test 3: List with multiple references to same object")
shared = {"shared": "value"}
lst = [shared, shared, shared]
encoded = encode(lst)
print(f" Encoded:\n{encoded}")
print(f" Lines: {len(encoded.split(chr(10)))}")
decoded = decode(encoded)
print(f" Decoded type: {type(decoded)}")
print(f" Length: {len(decoded)}")
if len(decoded) == 3:
print(f" All three are same object: {decoded[0] is decoded[1] is decoded[2]}")
print()
# Test 4: Complex circular structure
print("Test 4: Complex circular structure")
root = {"name": "root", "children": []}
child1 = {"name": "child1", "parent": root}
child2 = {"name": "child2", "parent": root}
root["children"].extend([child1, child2])
encoded = encode(root)
print(f" Encoded:\n{encoded}")
print(f" Lines: {len(encoded.split(chr(10)))}")
decoded = decode(encoded)
print(f" Decoded has 'children': {'children' in decoded}")
if "children" in decoded and len(decoded["children"]) > 0:
print(f" Children count: {len(decoded['children'])}")
if "parent" in decoded["children"][0]:
print(f" Circular ref works: {decoded['children'][0]['parent'] is decoded}")