Debian Codegraph @

test/unit/test_memcache.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2016-2019 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import pytest
18 import logging
19
20 from pyocd.cache.memory import MemoryCache
21 from pyocd.debug.context import DebugContext
22 from pyocd.core import memory_map
23 from pyocd.utility import conversion
24 from pyocd.utility import mask
25
26 @pytest.fixture(scope='function')
27 def memcache(mockcore):
28 return MemoryCache(DebugContext(mockcore), mockcore)
29
30 class TestMemoryCache:
31 def test_1(self, mockcore, memcache):
32 memcache.write_memory_block8(0, [0x10, 0x12, 0x14, 0x16])
33 assert memcache.read_memory_block8(0, 4) == [0x10, 0x12, 0x14, 0x16]
34
35 def test_2(self, mockcore, memcache):
36 memcache.write_memory_block8(0, [0x10, 0x12, 0x14, 0x16])
37 assert memcache.read_memory_block8(0, 4) == [0x10, 0x12, 0x14, 0x16]
38 assert memcache.read_memory_block8(2, 4) == [0x14, 0x16, 0xff, 0xff]
39
40 def test_3(self, mockcore, memcache):
41 memcache.write_memory_block32(0, [0x10121416])
42 assert memcache.read_memory_block32(0, 1) == [0x10121416]
43 assert memcache.read_memory_block8(2, 4) == [0x12, 0x10, 0xff, 0xff]
44
45 def test_4(self, mockcore, memcache):
46 mockcore.write_memory_block8(0, [1, 2, 3, 4])
47 assert memcache.read_memory_block8(0, 8) == [1, 2, 3, 4, 0xff, 0xff, 0xff, 0xff]
48 assert memcache.read_memory_block8(4, 4) == [0xff] * 4
49 mockcore.write_memory_block8(10, [50, 51])
50 assert memcache.read_memory_block8(6, 6) == [0xff, 0xff, 0xff, 0xff, 50, 51]
51
52 def test_5(self, mockcore, memcache):
53 memcache.write_memory_block8(0, [1, 2])
54 memcache.write_memory_block8(4, [3, 4])
55 assert memcache.read_memory_block8(0, 8) == [1, 2, 0xff, 0xff, 3, 4, 0xff, 0xff]
56
57 def test_6_middle_cached(self, mockcore, memcache):
58 mockcore.write_memory_block8(0, [50, 51, 52, 53, 54, 55, 56, 57])
59 memcache.write_memory_block8(4, [3, 4])
60 assert memcache.read_memory_block8(0, 8) == [50, 51, 52, 53, 3, 4, 56, 57]
61
62 def test_7_odd_cached(self, mockcore, memcache):
63 mockcore.write_memory_block8(0, [50, 51, 52, 53, 54, 55, 56, 57])
64 memcache.write_memory_block8(1, [1])
65 memcache.write_memory_block8(3, [2])
66 memcache.write_memory_block8(5, [3])
67 memcache.write_memory_block8(7, [4])
68 assert memcache.read_memory_block8(0, 8) == [50, 1, 52, 2, 54, 3, 56, 4]
69
70 def test_8_no_overlap(self, mockcore, memcache):
71 memcache.write_memory_block8(0, [1, 2, 3, 4])
72 assert memcache.read_memory_block8(8, 4) == [0xff] * 4
73
74 def test_9_begin_overlap(self, mockcore, memcache):
75 memcache.write_memory_block8(4, range(8))
76 assert memcache.read_memory_block8(0, 8) == [0xff, 0xff, 0xff, 0xff, 0, 1, 2, 3]
77
78 def test_10_end_overlap(self, mockcore, memcache):
79 memcache.write_memory_block8(0, range(8))
80 assert memcache.read_memory_block8(4, 8) == [4, 5, 6, 7, 0xff, 0xff, 0xff, 0xff]
81
82 def test_11_full_overlap(self, mockcore, memcache):
83 memcache.write_memory_block8(0, range(8))
84 assert memcache.read_memory_block8(0, 8) == list(range(8))
85
86 def test_12_begin(self, mockcore, memcache):
87 memcache.write_memory_block8(8, [1, 2, 3, 4])
88 assert memcache.read_memory_block8(7, 1) == [0xff]
89 assert memcache.read_memory_block8(8, 1) == [1]
90
91 def test_13_end(self, mockcore, memcache):
92 memcache.write_memory_block8(0, [1, 2, 3, 4])
93 assert memcache.read_memory_block8(3, 1) == [4]
94 assert memcache.read_memory_block8(4, 1) == [0xff]
95
96 def test_14_write_begin_ragged_cached(self, mockcore, memcache):
97 memcache.write_memory_block8(4, [1, 2, 3, 4])
98 mockcore.write_memory_block8(8, [90, 91, 92, 93])
99 memcache.write_memory_block8(6, [55, 56, 57, 58])
100 assert memcache.read_memory_block8(4, 8) == [1, 2, 55, 56, 57, 58, 92, 93]
101
102 def test_15_write_end_ragged_cached(self, mockcore, memcache):
103 memcache.write_memory_block8(4, [1, 2, 3, 4])
104 mockcore.write_memory_block8(0, [90, 91, 92, 93])
105 memcache.write_memory_block8(2, [55, 56, 57, 58])
106 assert memcache.read_memory_block8(0, 8) == [90, 91, 55, 56, 57, 58, 3, 4]
107
108 def test_16_no_mem_region(self, mockcore, memcache):
109 assert memcache.read_memory_block8(0x30000000, 4) == [0x55] * 4
110 # Make sure we didn't cache anything.
111 assert memcache._cache.overlap(0x30000000, 0x30000004) == set()
112
113 def test_17_noncacheable_region_read(self, mockcore, memcache):
114 mockcore.write_memory_block8(0x20000410, [90, 91, 92, 93])
115 assert memcache.read_memory_block8(0x20000410, 4) == [90, 91, 92, 93]
116 # Make sure we didn't cache anything.
117 assert memcache._cache.overlap(0x20000410, 0x20000414) == set()
118
119 def test_18_noncacheable_region_write(self, mockcore, memcache):
120 memcache.write_memory_block8(0x20000410, [1, 2, 3, 4])
121 mockcore.write_memory_block8(0x20000410, [90, 91, 92, 93])
122 assert memcache.read_memory_block8(0x20000410, 4) == [90, 91, 92, 93]
123 # Make sure we didn't cache anything.
124 assert memcache._cache.overlap(0x20000410, 0x20000414) == set()
125
126 def test_19_write_into_cached(self, mockcore, memcache):
127 mockcore.write_memory_block8(4, [1, 2, 3, 4, 5, 6, 7, 8])
128 assert memcache.read_memory_block8(4, 8) == [1, 2, 3, 4, 5, 6, 7, 8]
129 memcache.write_memory_block8(6, [128, 129, 130, 131])
130 assert memcache.read_memory_block8(4, 8) == [1, 2, 128, 129, 130, 131, 7, 8]
131 assert len(list(memcache._cache.overlap(4, 12))[0].data) == 8
132
133 def test_20_empty_read(self, memcache):
134 assert memcache.read_memory_block8(128, 0) == []
135
136 def test_21_empty_write(self, memcache):
137 memcache.write_memory_block8(128, [])
138
139 # This test reproduces a bug where writes followed by reads will start
140 # accumulating and returning extra data.
141 def test_22_multi_write_read_size(self, memcache):
142 test_size = 128
143 for i in range(100):
144 data = [x for x in range(test_size)]
145 memcache.write_memory_block8(0, data)
146 block = memcache.read_memory_block8(0, test_size)
147 assert data == block
148
149 # Variant of test 22.
150 def test_23_multi_write_1_read_size(self, memcache):
151 test_size = 128
152 data = [x for x in range(test_size)]
153 for i in range(10):
154 memcache.write_memory_block8(0, data)
155 block = memcache.read_memory_block8(0, test_size)
156 assert data == block
157
158 # Variant of test 22.
159 def test_24_1_write_multi_read_size(self, memcache):
160 test_size = 128
161 data = [x for x in range(test_size)]
162 memcache.write_memory_block8(0, data)
163 for i in range(10):
164 block = memcache.read_memory_block8(0, test_size)
165 assert data == block
166
167 # Variant of test 22.
168 def test_25_multi_write_subrange_1_read_size(self, memcache):
169 test_size = 128
170 data = [x for x in range(test_size)]
171 memcache.write_memory_block8(0, data)
172 for i in range(10):
173 memcache.write_memory_block8(64, data[64:96])
174 block = memcache.read_memory_block8(0, test_size)
175 assert data == block
176
177 def test_26_read_subrange(self, memcache):
178 data = list((n % 256) for n in range(320))
179 memcache.write_memory_block8(0x20000000, data)
180 block = memcache.read_memory_block8(0x2000007e, 4)
181 assert block == data[0x7e:0x82]
182
183
184
185 # TODO test read32/16/8 with and without callbacks
186