|
7 | 7 | from dml import expr
|
8 | 8 | from dml import traits
|
9 | 9 | from dml import logging
|
| 10 | +from contextlib import contextmanager |
10 | 11 |
|
11 | 12 | import unittest
|
12 | 13 |
|
@@ -104,29 +105,33 @@ def bitfields():
|
104 | 105 | self.assert_eq(TInt(8, False, members={}), TInt(8, False, members={}))
|
105 | 106 | self.assert_eq(bitfields(), bitfields())
|
106 | 107 |
|
107 |
| - t = bitfields() |
108 |
| - |
| 108 | + @contextmanager |
109 | 109 | def neq_bitfields_testcase():
|
110 |
| - nonlocal t |
111 |
| - self.assert_neq(bitfields(), t) |
112 | 110 | t = bitfields()
|
| 111 | + yield t |
| 112 | + self.assert_neq(bitfields(), t) |
113 | 113 |
|
114 | 114 | # Presence of members matter
|
115 |
| - del t.members['a'] |
116 |
| - neq_bitfields_testcase() |
| 115 | + with neq_bitfields_testcase() as t: |
| 116 | + del t.members['a'] |
| 117 | + |
117 | 118 | # type of members matters
|
118 |
| - t.members['a'][0].signed = True |
119 |
| - neq_bitfields_testcase() |
| 119 | + with neq_bitfields_testcase() as t: |
| 120 | + t.members['a'][0].signed = True |
| 121 | + |
120 | 122 | # msb/lsb of members matters
|
121 |
| - t.members['a'] = (TInt(8, False), 12, 5) |
122 |
| - neq_bitfields_testcase() |
| 123 | + with neq_bitfields_testcase() as t: |
| 124 | + (mt, msb, lsb) = t.members['a'] |
| 125 | + t.members['a'] = (mt, msb - 1, lsb - 1) |
| 126 | + |
123 | 127 | # order matters
|
124 |
| - t.members = dict(reversed(t.members.items())) |
125 |
| - neq_bitfields_testcase() |
| 128 | + with neq_bitfields_testcase() as t: |
| 129 | + t.members = dict(reversed(t.members.items())) |
| 130 | + |
126 | 131 | # names matter
|
127 |
| - t.members['c'] = t.members['b'] |
128 |
| - del t.members['b'] |
129 |
| - neq_bitfields_testcase() |
| 132 | + with neq_bitfields_testcase() as t: |
| 133 | + t.members['c'] = t.members['b'] |
| 134 | + del t.members['b'] |
130 | 135 |
|
131 | 136 | def test_TEndianInt(self):
|
132 | 137 | self.assert_eq(TEndianInt(8, False, 'big-endian'),
|
@@ -158,37 +163,34 @@ def test_TArray(self):
|
158 | 163 | b.size = a.size
|
159 | 164 | self.assert_eq(a, b)
|
160 | 165 |
|
161 |
| - a = self.mkTArray(4) |
162 |
| - b = self.mkTArray(4) |
163 |
| - |
| 166 | + @contextmanager |
164 | 167 | def array_testcase(res):
|
165 |
| - nonlocal a, b |
| 168 | + a = self.mkTArray(4) |
| 169 | + b = self.mkTArray(4) |
| 170 | + yield (a, b) |
166 | 171 | if res:
|
167 | 172 | self.assert_eq(a, b)
|
168 | 173 | else:
|
169 | 174 | self.assert_neq(a, b)
|
170 | 175 |
|
171 |
| - a = self.mkTArray(4) |
172 |
| - b = self.mkTArray(4) |
173 |
| - |
174 | 176 | # Base type matters
|
175 |
| - a.base.signed = True |
176 |
| - array_testcase(False) |
| 177 | + with array_testcase(False) as (a, _): |
| 178 | + a.base.signed = True |
177 | 179 |
|
178 | 180 | # Constness: constness of base type and array are normalized
|
179 |
| - a.const = True |
180 |
| - array_testcase(False) |
181 |
| - a.const = True |
182 |
| - b.const = True |
183 |
| - array_testcase(True) |
184 |
| - a.base.const = True |
185 |
| - b.const = True |
186 |
| - array_testcase(True) |
187 |
| - a.base = self.mkTArray(4) |
188 |
| - b.base = self.mkTArray(4) |
189 |
| - a.base.base.const = True |
190 |
| - b.const = True |
191 |
| - array_testcase(True) |
| 181 | + with array_testcase(False) as (a, _): |
| 182 | + a.const = True |
| 183 | + with array_testcase(True) as (a, b): |
| 184 | + a.const = True |
| 185 | + b.const = True |
| 186 | + with array_testcase(True) as (a, b): |
| 187 | + a.base.const = True |
| 188 | + b.const = True |
| 189 | + with array_testcase(True) as (a, b): |
| 190 | + a.base = self.mkTArray(4) |
| 191 | + b.base = self.mkTArray(4) |
| 192 | + a.base.base.const = True |
| 193 | + b.const = True |
192 | 194 |
|
193 | 195 | # Pointers are not equivalent to arrays
|
194 | 196 | self.assert_neq(self.mkTArray(4), TPtr(TInt(32, False)))
|
|
0 commit comments