-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathmemory_dict_test.py
174 lines (146 loc) · 5.16 KB
/
memory_dict_test.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import pytest
from starkware.cairo.lang.vm.memory_dict import (
InconsistentMemoryError,
MemoryDict,
UnknownMemoryError,
)
from starkware.cairo.lang.vm.relocatable import RelocatableValue
def test_memory_dict_items():
memory = MemoryDict()
assert list(memory.items()) == []
memory = MemoryDict({1: 2, 3: 4, 5: 6})
assert list(memory.items()) == [(1, 2), (3, 4), (5, 6)]
def test_memory_dict_serialize():
memory = MemoryDict({1: 2, 3: 4, 5: 6})
expected_serialized = bytes(
[
1,
0,
0,
0,
0,
0,
0,
0,
2,
0,
0,
3,
0,
0,
0,
0,
0,
0,
0,
4,
0,
0,
5,
0,
0,
0,
0,
0,
0,
0,
6,
0,
0,
]
)
serialized = memory.serialize(3)
assert expected_serialized == serialized
assert MemoryDict.deserialize(serialized, 3) == memory
def test_memory_dict_getitem():
memory = MemoryDict({11: 12})
with pytest.raises(UnknownMemoryError):
memory[12]
def test_memory_dict_check_element():
memory = MemoryDict()
with pytest.raises(KeyError, match="must be an int"):
memory["not a number"] = 12 # type: ignore
with pytest.raises(KeyError, match="must be nonnegative"):
memory[-12] = 13
with pytest.raises(ValueError, match="must be nonnegative"):
memory[12] = -13
with pytest.raises(ValueError, match="The offset of a relocatable value must be nonnegative"):
memory[RelocatableValue(segment_index=10, offset=-2)] = 13
# A value may have a negative offset.
memory[13] = RelocatableValue(segment_index=10, offset=-2)
def test_memory_dict_get():
DEFAULT = 12345
memory = MemoryDict({14: 15})
assert memory.get(14, DEFAULT) == 15
assert memory.get(1234, DEFAULT) == DEFAULT
assert memory.get(-10, DEFAULT) == DEFAULT
# Attempting to read address with a negative offset is ok, it simply returns None.
assert memory.get(RelocatableValue(segment_index=10, offset=-2)) is None
def test_memory_dict_setdefault():
memory = MemoryDict({14: 15})
memory.setdefault(14, 0)
assert memory[14] == 15
memory.setdefault(123, 456)
assert memory[123] == 456
with pytest.raises(ValueError, match="must be an int"):
memory.setdefault(10, "default")
with pytest.raises(KeyError, match="must be nonnegative"):
memory.setdefault(-10, 123)
with pytest.raises(ValueError, match="The offset of a relocatable value must be nonnegative"):
memory[RelocatableValue(segment_index=10, offset=-2)] = 13
def test_memory_dict_in():
memory = MemoryDict({1: 2, 3: 4})
assert 1 in memory
assert 2 not in memory
# Test that `in` doesn't add the value to the dict.
assert 2 not in memory
def test_memory_dict_multiple_values():
memory = MemoryDict({5: 10})
memory[5] = 10
memory[5] = 10
with pytest.raises(InconsistentMemoryError):
memory[5] = 11
def test_segment_relocations():
memory = MemoryDict()
temp_segment = RelocatableValue(segment_index=-1, offset=0)
memory[5] = temp_segment + 2
assert memory[5] == RelocatableValue(segment_index=-1, offset=2)
relocation_target = RelocatableValue(segment_index=4, offset=25)
memory.add_relocation_rule(src_ptr=temp_segment, dest_ptr=relocation_target)
assert memory[5] == relocation_target + 2
memory[temp_segment + 3] = 17
memory.relocate_memory()
assert memory.data == {
5: relocation_target + 2,
relocation_target + 3: 17,
}
def test_segment_relocation_failures():
memory = MemoryDict()
relocation_target = RelocatableValue(segment_index=4, offset=25)
with pytest.raises(AssertionError, match="src_ptr.segment_index must be < 0, src_ptr=1:2."):
memory.add_relocation_rule(
src_ptr=RelocatableValue(segment_index=1, offset=2), dest_ptr=relocation_target
)
with pytest.raises(AssertionError, match="src_ptr.offset must be 0, src_ptr=-3:2."):
memory.add_relocation_rule(
src_ptr=RelocatableValue(segment_index=-3, offset=2), dest_ptr=relocation_target
)
memory.add_relocation_rule(
src_ptr=RelocatableValue(segment_index=-3, offset=0), dest_ptr=relocation_target
)
with pytest.raises(
AssertionError, match="The segment with index -3 already has a relocation rule."
):
memory.add_relocation_rule(
src_ptr=RelocatableValue(segment_index=-3, offset=0), dest_ptr=relocation_target
)
relocation_target = RelocatableValue(segment_index=100, offset=0)
src_ptr = RelocatableValue(segment_index=-6, offset=0)
memory[src_ptr] = 1
memory.add_relocation_rule(src_ptr=src_ptr, dest_ptr=relocation_target)
memory[relocation_target] = 2
with pytest.raises(
InconsistentMemoryError,
match=f"Inconsistent memory assignment at address {relocation_target}. 1 != 2.",
):
memory.relocate_memory()