Commit | Line | Data |
---|---|---|
9cf643d1 PP |
1 | from functools import partial, partialmethod |
2 | import operator | |
3 | import unittest | |
4 | import numbers | |
5 | import math | |
6 | import copy | |
7 | import bt2 | |
8 | ||
9 | ||
10 | class _TestCopySimple: | |
11 | def test_copy(self): | |
12 | cpy = copy.copy(self._def) | |
13 | self.assertIsNot(cpy, self._def) | |
14 | self.assertNotEqual(cpy.addr, self._def.addr) | |
15 | self.assertEqual(cpy, self._def) | |
16 | ||
17 | def test_deepcopy(self): | |
18 | cpy = copy.deepcopy(self._def) | |
19 | self.assertIsNot(cpy, self._def) | |
20 | self.assertNotEqual(cpy.addr, self._def.addr) | |
21 | self.assertEqual(cpy, self._def) | |
22 | ||
23 | ||
24 | _COMP_BINOPS = ( | |
25 | operator.eq, | |
26 | operator.ne, | |
27 | ) | |
28 | ||
29 | ||
30 | class _TestNumericField(_TestCopySimple): | |
31 | def _binop(self, op, rhs): | |
32 | rexc = None | |
33 | rvexc = None | |
34 | comp_value = rhs | |
35 | ||
36 | if isinstance(rhs, (bt2.fields._IntegerField, bt2.fields._FloatingPointNumberField)): | |
37 | comp_value = rhs.value | |
38 | ||
39 | try: | |
40 | r = op(self._def, rhs) | |
41 | except Exception as e: | |
42 | rexc = e | |
43 | ||
44 | try: | |
45 | rv = op(self._def_value, comp_value) | |
46 | except Exception as e: | |
47 | rvexc = e | |
48 | ||
49 | if rexc is not None or rvexc is not None: | |
50 | # at least one of the operations raised an exception: in | |
51 | # this case both operations should have raised the same | |
52 | # type of exception (division by zero, bit shift with a | |
53 | # floating point number operand, etc.) | |
54 | self.assertIs(type(rexc), type(rvexc)) | |
55 | return None, None | |
56 | ||
57 | return r, rv | |
58 | ||
59 | def _unaryop(self, op): | |
60 | rexc = None | |
61 | rvexc = None | |
62 | ||
63 | try: | |
64 | r = op(self._def) | |
65 | except Exception as e: | |
66 | rexc = e | |
67 | ||
68 | try: | |
69 | rv = op(self._def_value) | |
70 | except Exception as e: | |
71 | rvexc = e | |
72 | ||
73 | if rexc is not None or rvexc is not None: | |
74 | # at least one of the operations raised an exception: in | |
75 | # this case both operations should have raised the same | |
76 | # type of exception (division by zero, bit shift with a | |
77 | # floating point number operand, etc.) | |
78 | self.assertIs(type(rexc), type(rvexc)) | |
79 | return None, None | |
80 | ||
81 | return r, rv | |
82 | ||
83 | def _test_unaryop_type(self, op): | |
84 | r, rv = self._unaryop(op) | |
85 | ||
86 | if r is None: | |
87 | return | |
88 | ||
89 | self.assertIsInstance(r, type(rv)) | |
90 | ||
91 | def _test_unaryop_value(self, op): | |
92 | r, rv = self._unaryop(op) | |
93 | ||
94 | if r is None: | |
95 | return | |
96 | ||
97 | self.assertEqual(r, rv) | |
98 | ||
99 | def _test_unaryop_addr_same(self, op): | |
100 | addr_before = self._def.addr | |
101 | self._unaryop(op) | |
102 | self.assertEqual(self._def.addr, addr_before) | |
103 | ||
104 | def _test_unaryop_value_same(self, op): | |
105 | value_before = self._def.value | |
106 | self._unaryop(op) | |
107 | self.assertEqual(self._def.value, value_before) | |
108 | ||
109 | def _test_binop_type(self, op, rhs): | |
110 | r, rv = self._binop(op, rhs) | |
111 | ||
112 | if r is None: | |
113 | return | |
114 | ||
115 | if op in _COMP_BINOPS: | |
116 | # __eq__() and __ne__() always return a 'bool' object | |
117 | self.assertIsInstance(r, bool) | |
118 | else: | |
119 | self.assertIsInstance(r, type(rv)) | |
120 | ||
121 | def _test_binop_value(self, op, rhs): | |
122 | r, rv = self._binop(op, rhs) | |
123 | ||
124 | if r is None: | |
125 | return | |
126 | ||
127 | self.assertEqual(r, rv) | |
128 | ||
129 | def _test_binop_lhs_addr_same(self, op, rhs): | |
130 | addr_before = self._def.addr | |
131 | r, rv = self._binop(op, rhs) | |
132 | self.assertEqual(self._def.addr, addr_before) | |
133 | ||
134 | def _test_binop_lhs_value_same(self, op, rhs): | |
135 | value_before = self._def.value | |
136 | r, rv = self._binop(op, rhs) | |
137 | self.assertEqual(self._def.value, value_before) | |
138 | ||
139 | def _test_binop_invalid_unknown(self, op): | |
140 | if op in _COMP_BINOPS: | |
141 | self.skipTest('not testing') | |
142 | ||
143 | class A: | |
144 | pass | |
145 | ||
146 | with self.assertRaises(TypeError): | |
147 | op(self._def, A()) | |
148 | ||
149 | def _test_binop_invalid_none(self, op): | |
150 | if op in _COMP_BINOPS: | |
151 | self.skipTest('not testing') | |
152 | ||
153 | with self.assertRaises(TypeError): | |
154 | op(self._def, None) | |
155 | ||
156 | def _test_ibinop_value(self, op, rhs): | |
157 | r, rv = self._binop(op, rhs) | |
158 | ||
159 | if r is None: | |
160 | return | |
161 | ||
162 | # The inplace operators are special for field objects because | |
163 | # they do not return a new, immutable object like it's the case | |
164 | # for Python numbers. In Python, `a += 2`, where `a` is a number | |
165 | # object, assigns a new number object reference to `a`, dropping | |
166 | # the old reference. Since BT's field objects are mutable, we | |
167 | # modify their internal value with the inplace operators. This | |
168 | # means however that we can lose data in the process, for | |
169 | # example: | |
170 | # | |
171 | # int_value_obj += 3.3 | |
172 | # | |
173 | # Here, if `int_value_obj` is a Python `int` with the value 2, | |
174 | # it would be a `float` object after this, holding the value | |
175 | # 5.3. In our case, if `int_value_obj` is an integer field | |
176 | # object, 3.3 is converted to an `int` object (3) and added to | |
177 | # the current value of `int_value_obj`, so after this the value | |
178 | # of the object is 5. This does not compare to 5.3, which is | |
179 | # why we also use the `int()` type here. | |
180 | if isinstance(self._def, bt2.fields._IntegerField): | |
181 | rv = int(rv) | |
182 | ||
183 | self.assertEqual(r, rv) | |
184 | ||
185 | def _test_ibinop_type(self, op, rhs): | |
186 | r, rv = self._binop(op, rhs) | |
187 | ||
188 | if r is None: | |
189 | return | |
190 | ||
191 | self.assertIs(r, self._def) | |
192 | ||
193 | def _test_ibinop_invalid_unknown(self, op): | |
194 | class A: | |
195 | pass | |
196 | ||
197 | with self.assertRaises(TypeError): | |
198 | op(self._def, A()) | |
199 | ||
200 | def _test_ibinop_invalid_none(self, op): | |
201 | with self.assertRaises(TypeError): | |
202 | op(self._def, None) | |
203 | ||
204 | def _test_binop_rhs_false(self, test_cb, op): | |
205 | test_cb(op, False) | |
206 | ||
207 | def _test_binop_rhs_true(self, test_cb, op): | |
208 | test_cb(op, True) | |
209 | ||
210 | def _test_binop_rhs_pos_int(self, test_cb, op): | |
211 | test_cb(op, 2) | |
212 | ||
213 | def _test_binop_rhs_neg_int(self, test_cb, op): | |
214 | test_cb(op, -23) | |
215 | ||
216 | def _test_binop_rhs_zero_int(self, test_cb, op): | |
217 | test_cb(op, 0) | |
218 | ||
219 | def _test_binop_rhs_pos_vint(self, test_cb, op): | |
220 | test_cb(op, bt2.create_value(2)) | |
221 | ||
222 | def _test_binop_rhs_neg_vint(self, test_cb, op): | |
223 | test_cb(op, bt2.create_value(-23)) | |
224 | ||
225 | def _test_binop_rhs_zero_vint(self, test_cb, op): | |
226 | test_cb(op, bt2.create_value(0)) | |
227 | ||
228 | def _test_binop_rhs_pos_float(self, test_cb, op): | |
229 | test_cb(op, 2.2) | |
230 | ||
231 | def _test_binop_rhs_neg_float(self, test_cb, op): | |
232 | test_cb(op, -23.4) | |
233 | ||
234 | def _test_binop_rhs_zero_float(self, test_cb, op): | |
235 | test_cb(op, 0.0) | |
236 | ||
237 | def _test_binop_rhs_pos_vfloat(self, test_cb, op): | |
238 | test_cb(op, bt2.create_value(2.2)) | |
239 | ||
240 | def _test_binop_rhs_neg_vfloat(self, test_cb, op): | |
241 | test_cb(op, bt2.create_value(-23.4)) | |
242 | ||
243 | def _test_binop_rhs_zero_vfloat(self, test_cb, op): | |
244 | test_cb(op, bt2.create_value(0.0)) | |
245 | ||
246 | def _test_binop_type_false(self, op): | |
247 | self._test_binop_rhs_false(self._test_binop_type, op) | |
248 | ||
249 | def _test_binop_type_true(self, op): | |
250 | self._test_binop_rhs_true(self._test_binop_type, op) | |
251 | ||
252 | def _test_binop_type_pos_int(self, op): | |
253 | self._test_binop_rhs_pos_int(self._test_binop_type, op) | |
254 | ||
255 | def _test_binop_type_neg_int(self, op): | |
256 | self._test_binop_rhs_neg_int(self._test_binop_type, op) | |
257 | ||
258 | def _test_binop_type_zero_int(self, op): | |
259 | self._test_binop_rhs_zero_int(self._test_binop_type, op) | |
260 | ||
261 | def _test_binop_type_pos_vint(self, op): | |
262 | self._test_binop_rhs_pos_vint(self._test_binop_type, op) | |
263 | ||
264 | def _test_binop_type_neg_vint(self, op): | |
265 | self._test_binop_rhs_neg_vint(self._test_binop_type, op) | |
266 | ||
267 | def _test_binop_type_zero_vint(self, op): | |
268 | self._test_binop_rhs_zero_vint(self._test_binop_type, op) | |
269 | ||
270 | def _test_binop_type_pos_float(self, op): | |
271 | self._test_binop_rhs_pos_float(self._test_binop_type, op) | |
272 | ||
273 | def _test_binop_type_neg_float(self, op): | |
274 | self._test_binop_rhs_neg_float(self._test_binop_type, op) | |
275 | ||
276 | def _test_binop_type_zero_float(self, op): | |
277 | self._test_binop_rhs_zero_float(self._test_binop_type, op) | |
278 | ||
279 | def _test_binop_type_pos_vfloat(self, op): | |
280 | self._test_binop_rhs_pos_vfloat(self._test_binop_type, op) | |
281 | ||
282 | def _test_binop_type_neg_vfloat(self, op): | |
283 | self._test_binop_rhs_neg_vfloat(self._test_binop_type, op) | |
284 | ||
285 | def _test_binop_type_zero_vfloat(self, op): | |
286 | self._test_binop_rhs_zero_vfloat(self._test_binop_type, op) | |
287 | ||
288 | def _test_binop_value_false(self, op): | |
289 | self._test_binop_rhs_false(self._test_binop_value, op) | |
290 | ||
291 | def _test_binop_value_true(self, op): | |
292 | self._test_binop_rhs_true(self._test_binop_value, op) | |
293 | ||
294 | def _test_binop_value_pos_int(self, op): | |
295 | self._test_binop_rhs_pos_int(self._test_binop_value, op) | |
296 | ||
297 | def _test_binop_value_neg_int(self, op): | |
298 | self._test_binop_rhs_neg_int(self._test_binop_value, op) | |
299 | ||
300 | def _test_binop_value_zero_int(self, op): | |
301 | self._test_binop_rhs_zero_int(self._test_binop_value, op) | |
302 | ||
303 | def _test_binop_value_pos_vint(self, op): | |
304 | self._test_binop_rhs_pos_vint(self._test_binop_value, op) | |
305 | ||
306 | def _test_binop_value_neg_vint(self, op): | |
307 | self._test_binop_rhs_neg_vint(self._test_binop_value, op) | |
308 | ||
309 | def _test_binop_value_zero_vint(self, op): | |
310 | self._test_binop_rhs_zero_vint(self._test_binop_value, op) | |
311 | ||
312 | def _test_binop_value_pos_float(self, op): | |
313 | self._test_binop_rhs_pos_float(self._test_binop_value, op) | |
314 | ||
315 | def _test_binop_value_neg_float(self, op): | |
316 | self._test_binop_rhs_neg_float(self._test_binop_value, op) | |
317 | ||
318 | def _test_binop_value_zero_float(self, op): | |
319 | self._test_binop_rhs_zero_float(self._test_binop_value, op) | |
320 | ||
321 | def _test_binop_value_pos_vfloat(self, op): | |
322 | self._test_binop_rhs_pos_vfloat(self._test_binop_value, op) | |
323 | ||
324 | def _test_binop_value_neg_vfloat(self, op): | |
325 | self._test_binop_rhs_neg_vfloat(self._test_binop_value, op) | |
326 | ||
327 | def _test_binop_value_zero_vfloat(self, op): | |
328 | self._test_binop_rhs_zero_vfloat(self._test_binop_value, op) | |
329 | ||
330 | def _test_binop_lhs_addr_same_false(self, op): | |
331 | self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op) | |
332 | ||
333 | def _test_binop_lhs_addr_same_true(self, op): | |
334 | self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op) | |
335 | ||
336 | def _test_binop_lhs_addr_same_pos_int(self, op): | |
337 | self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op) | |
338 | ||
339 | def _test_binop_lhs_addr_same_neg_int(self, op): | |
340 | self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op) | |
341 | ||
342 | def _test_binop_lhs_addr_same_zero_int(self, op): | |
343 | self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op) | |
344 | ||
345 | def _test_binop_lhs_addr_same_pos_vint(self, op): | |
346 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op) | |
347 | ||
348 | def _test_binop_lhs_addr_same_neg_vint(self, op): | |
349 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op) | |
350 | ||
351 | def _test_binop_lhs_addr_same_zero_vint(self, op): | |
352 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op) | |
353 | ||
354 | def _test_binop_lhs_addr_same_pos_float(self, op): | |
355 | self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op) | |
356 | ||
357 | def _test_binop_lhs_addr_same_neg_float(self, op): | |
358 | self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op) | |
359 | ||
360 | def _test_binop_lhs_addr_same_zero_float(self, op): | |
361 | self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op) | |
362 | ||
363 | def _test_binop_lhs_addr_same_pos_vfloat(self, op): | |
364 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op) | |
365 | ||
366 | def _test_binop_lhs_addr_same_neg_vfloat(self, op): | |
367 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op) | |
368 | ||
369 | def _test_binop_lhs_addr_same_zero_vfloat(self, op): | |
370 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op) | |
371 | ||
372 | def _test_binop_lhs_value_same_false(self, op): | |
373 | self._test_binop_rhs_false(self._test_binop_lhs_value_same, op) | |
374 | ||
375 | def _test_binop_lhs_value_same_true(self, op): | |
376 | self._test_binop_rhs_true(self._test_binop_lhs_value_same, op) | |
377 | ||
378 | def _test_binop_lhs_value_same_pos_int(self, op): | |
379 | self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op) | |
380 | ||
381 | def _test_binop_lhs_value_same_neg_int(self, op): | |
382 | self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op) | |
383 | ||
384 | def _test_binop_lhs_value_same_zero_int(self, op): | |
385 | self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op) | |
386 | ||
387 | def _test_binop_lhs_value_same_pos_vint(self, op): | |
388 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op) | |
389 | ||
390 | def _test_binop_lhs_value_same_neg_vint(self, op): | |
391 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op) | |
392 | ||
393 | def _test_binop_lhs_value_same_zero_vint(self, op): | |
394 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op) | |
395 | ||
396 | def _test_binop_lhs_value_same_pos_float(self, op): | |
397 | self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op) | |
398 | ||
399 | def _test_binop_lhs_value_same_neg_float(self, op): | |
400 | self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op) | |
401 | ||
402 | def _test_binop_lhs_value_same_zero_float(self, op): | |
403 | self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op) | |
404 | ||
405 | def _test_binop_lhs_value_same_pos_vfloat(self, op): | |
406 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op) | |
407 | ||
408 | def _test_binop_lhs_value_same_neg_vfloat(self, op): | |
409 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op) | |
410 | ||
411 | def _test_binop_lhs_value_same_zero_vfloat(self, op): | |
412 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op) | |
413 | ||
414 | def _test_ibinop_type_false(self, op): | |
415 | self._test_binop_rhs_false(self._test_ibinop_type, op) | |
416 | ||
417 | def _test_ibinop_type_true(self, op): | |
418 | self._test_binop_rhs_true(self._test_ibinop_type, op) | |
419 | ||
420 | def _test_ibinop_type_pos_int(self, op): | |
421 | self._test_binop_rhs_pos_int(self._test_ibinop_type, op) | |
422 | ||
423 | def _test_ibinop_type_neg_int(self, op): | |
424 | self._test_binop_rhs_neg_int(self._test_ibinop_type, op) | |
425 | ||
426 | def _test_ibinop_type_zero_int(self, op): | |
427 | self._test_binop_rhs_zero_int(self._test_ibinop_type, op) | |
428 | ||
429 | def _test_ibinop_type_pos_vint(self, op): | |
430 | self._test_binop_rhs_pos_vint(self._test_ibinop_type, op) | |
431 | ||
432 | def _test_ibinop_type_neg_vint(self, op): | |
433 | self._test_binop_rhs_neg_vint(self._test_ibinop_type, op) | |
434 | ||
435 | def _test_ibinop_type_zero_vint(self, op): | |
436 | self._test_binop_rhs_zero_vint(self._test_ibinop_type, op) | |
437 | ||
438 | def _test_ibinop_type_pos_float(self, op): | |
439 | self._test_binop_rhs_pos_float(self._test_ibinop_type, op) | |
440 | ||
441 | def _test_ibinop_type_neg_float(self, op): | |
442 | self._test_binop_rhs_neg_float(self._test_ibinop_type, op) | |
443 | ||
444 | def _test_ibinop_type_zero_float(self, op): | |
445 | self._test_binop_rhs_zero_float(self._test_ibinop_type, op) | |
446 | ||
447 | def _test_ibinop_type_pos_vfloat(self, op): | |
448 | self._test_binop_rhs_pos_vfloat(self._test_ibinop_type, op) | |
449 | ||
450 | def _test_ibinop_type_neg_vfloat(self, op): | |
451 | self._test_binop_rhs_neg_vfloat(self._test_ibinop_type, op) | |
452 | ||
453 | def _test_ibinop_type_zero_vfloat(self, op): | |
454 | self._test_binop_rhs_zero_vfloat(self._test_ibinop_type, op) | |
455 | ||
456 | def _test_ibinop_value_false(self, op): | |
457 | self._test_binop_rhs_false(self._test_ibinop_value, op) | |
458 | ||
459 | def _test_ibinop_value_true(self, op): | |
460 | self._test_binop_rhs_true(self._test_ibinop_value, op) | |
461 | ||
462 | def _test_ibinop_value_pos_int(self, op): | |
463 | self._test_binop_rhs_pos_int(self._test_ibinop_value, op) | |
464 | ||
465 | def _test_ibinop_value_neg_int(self, op): | |
466 | self._test_binop_rhs_neg_int(self._test_ibinop_value, op) | |
467 | ||
468 | def _test_ibinop_value_zero_int(self, op): | |
469 | self._test_binop_rhs_zero_int(self._test_ibinop_value, op) | |
470 | ||
471 | def _test_ibinop_value_pos_vint(self, op): | |
472 | self._test_binop_rhs_pos_vint(self._test_ibinop_value, op) | |
473 | ||
474 | def _test_ibinop_value_neg_vint(self, op): | |
475 | self._test_binop_rhs_neg_vint(self._test_ibinop_value, op) | |
476 | ||
477 | def _test_ibinop_value_zero_vint(self, op): | |
478 | self._test_binop_rhs_zero_vint(self._test_ibinop_value, op) | |
479 | ||
480 | def _test_ibinop_value_pos_float(self, op): | |
481 | self._test_binop_rhs_pos_float(self._test_ibinop_value, op) | |
482 | ||
483 | def _test_ibinop_value_neg_float(self, op): | |
484 | self._test_binop_rhs_neg_float(self._test_ibinop_value, op) | |
485 | ||
486 | def _test_ibinop_value_zero_float(self, op): | |
487 | self._test_binop_rhs_zero_float(self._test_ibinop_value, op) | |
488 | ||
489 | def _test_ibinop_value_pos_vfloat(self, op): | |
490 | self._test_binop_rhs_pos_vfloat(self._test_ibinop_value, op) | |
491 | ||
492 | def _test_ibinop_value_neg_vfloat(self, op): | |
493 | self._test_binop_rhs_neg_vfloat(self._test_ibinop_value, op) | |
494 | ||
495 | def _test_ibinop_value_zero_vfloat(self, op): | |
496 | self._test_binop_rhs_zero_vfloat(self._test_ibinop_value, op) | |
497 | ||
498 | def test_bool_op(self): | |
499 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
500 | ||
501 | def test_int_op(self): | |
502 | self.assertEqual(int(self._def), int(self._def_value)) | |
503 | ||
504 | def test_float_op(self): | |
505 | self.assertEqual(float(self._def), float(self._def_value)) | |
506 | ||
507 | def test_complex_op(self): | |
508 | self.assertEqual(complex(self._def), complex(self._def_value)) | |
509 | ||
510 | def test_str_op(self): | |
511 | self.assertEqual(str(self._def), str(self._def_value)) | |
512 | ||
513 | def test_eq_none(self): | |
514 | self.assertFalse(self._def == None) | |
515 | ||
516 | def test_ne_none(self): | |
517 | self.assertTrue(self._def != None) | |
518 | ||
519 | ||
520 | _BINOPS = ( | |
521 | ('lt', operator.lt), | |
522 | ('le', operator.le), | |
523 | ('eq', operator.eq), | |
524 | ('ne', operator.ne), | |
525 | ('ge', operator.ge), | |
526 | ('gt', operator.gt), | |
527 | ('add', operator.add), | |
528 | ('radd', lambda a, b: operator.add(b, a)), | |
529 | ('and', operator.and_), | |
530 | ('rand', lambda a, b: operator.and_(b, a)), | |
531 | ('floordiv', operator.floordiv), | |
532 | ('rfloordiv', lambda a, b: operator.floordiv(b, a)), | |
533 | ('lshift', operator.lshift), | |
534 | ('rlshift', lambda a, b: operator.lshift(b, a)), | |
535 | ('mod', operator.mod), | |
536 | ('rmod', lambda a, b: operator.mod(b, a)), | |
537 | ('mul', operator.mul), | |
538 | ('rmul', lambda a, b: operator.mul(b, a)), | |
539 | ('or', operator.or_), | |
540 | ('ror', lambda a, b: operator.or_(b, a)), | |
541 | ('pow', operator.pow), | |
542 | ('rpow', lambda a, b: operator.pow(b, a)), | |
543 | ('rshift', operator.rshift), | |
544 | ('rrshift', lambda a, b: operator.rshift(b, a)), | |
545 | ('sub', operator.sub), | |
546 | ('rsub', lambda a, b: operator.sub(b, a)), | |
547 | ('truediv', operator.truediv), | |
548 | ('rtruediv', lambda a, b: operator.truediv(b, a)), | |
549 | ('xor', operator.xor), | |
550 | ('rxor', lambda a, b: operator.xor(b, a)), | |
551 | ) | |
552 | ||
553 | ||
554 | _IBINOPS = ( | |
555 | ('iadd', operator.iadd), | |
556 | ('iand', operator.iand), | |
557 | ('ifloordiv', operator.ifloordiv), | |
558 | ('ilshift', operator.ilshift), | |
559 | ('imod', operator.imod), | |
560 | ('imul', operator.imul), | |
561 | ('ior', operator.ior), | |
562 | ('ipow', operator.ipow), | |
563 | ('irshift', operator.irshift), | |
564 | ('isub', operator.isub), | |
565 | ('itruediv', operator.itruediv), | |
566 | ('ixor', operator.ixor), | |
567 | ) | |
568 | ||
569 | ||
570 | _UNARYOPS = ( | |
571 | ('neg', operator.neg), | |
572 | ('pos', operator.pos), | |
573 | ('abs', operator.abs), | |
574 | ('invert', operator.invert), | |
575 | ('round', round), | |
576 | ('round_0', partial(round, ndigits=0)), | |
577 | ('round_1', partial(round, ndigits=1)), | |
578 | ('round_2', partial(round, ndigits=2)), | |
579 | ('round_3', partial(round, ndigits=3)), | |
580 | ('ceil', math.ceil), | |
581 | ('floor', math.floor), | |
582 | ('trunc', math.trunc), | |
583 | ) | |
584 | ||
585 | ||
586 | def _inject_numeric_testing_methods(cls): | |
587 | def test_binop_name(suffix): | |
588 | return 'test_binop_{}_{}'.format(name, suffix) | |
589 | ||
590 | def test_ibinop_name(suffix): | |
591 | return 'test_ibinop_{}_{}'.format(name, suffix) | |
592 | ||
593 | def test_unaryop_name(suffix): | |
594 | return 'test_unaryop_{}_{}'.format(name, suffix) | |
595 | ||
596 | # inject testing methods for each binary operation | |
597 | for name, binop in _BINOPS: | |
598 | ||
599 | setattr(cls, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField._test_binop_invalid_unknown, op=binop)) | |
600 | setattr(cls, test_binop_name('invalid_none'), partialmethod(_TestNumericField._test_binop_invalid_none, op=binop)) | |
601 | setattr(cls, test_binop_name('type_true'), partialmethod(_TestNumericField._test_binop_type_true, op=binop)) | |
602 | setattr(cls, test_binop_name('type_pos_int'), partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop)) | |
603 | setattr(cls, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop)) | |
604 | setattr(cls, test_binop_name('value_true'), partialmethod(_TestNumericField._test_binop_value_true, op=binop)) | |
605 | setattr(cls, test_binop_name('value_pos_int'), partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop)) | |
606 | setattr(cls, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop)) | |
607 | setattr(cls, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop)) | |
608 | setattr(cls, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop)) | |
609 | setattr(cls, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop)) | |
610 | setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop)) | |
611 | setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_int, op=binop)) | |
612 | setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop)) | |
613 | setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop)) | |
614 | setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop)) | |
615 | setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop)) | |
616 | setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop)) | |
617 | setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop)) | |
618 | setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop)) | |
619 | setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_int, op=binop)) | |
620 | setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop)) | |
621 | setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericField._test_binop_type_false, op=binop)) | |
622 | setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop)) | |
623 | setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop)) | |
624 | setattr(cls, test_binop_name('value_false'), partialmethod(_TestNumericField._test_binop_value_false, op=binop)) | |
625 | setattr(cls, test_binop_name('value_zero_int'), partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop)) | |
626 | setattr(cls, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop)) | |
627 | setattr(cls, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop)) | |
628 | setattr(cls, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop)) | |
629 | setattr(cls, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop)) | |
630 | setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop)) | |
631 | setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_int, op=binop)) | |
632 | setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop)) | |
633 | setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop)) | |
634 | setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop)) | |
635 | setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop)) | |
636 | setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop)) | |
637 | setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop)) | |
638 | setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop)) | |
639 | setattr(cls, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop)) | |
640 | setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop)) | |
641 | setattr(cls, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop)) | |
642 | setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop)) | |
643 | setattr(cls, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop)) | |
644 | setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop)) | |
645 | setattr(cls, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_float, op=binop)) | |
646 | setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_float, op=binop)) | |
647 | setattr(cls, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop)) | |
648 | setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop)) | |
649 | setattr(cls, test_binop_name('type_zero_float'), partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop)) | |
650 | setattr(cls, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop)) | |
651 | setattr(cls, test_binop_name('value_zero_float'), partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop)) | |
652 | setattr(cls, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop)) | |
653 | setattr(cls, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop)) | |
654 | setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop)) | |
655 | setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_float, op=binop)) | |
656 | setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop)) | |
657 | ||
658 | # inject testing methods for each unary operation | |
659 | for name, unaryop in _UNARYOPS: | |
660 | setattr(cls, test_unaryop_name('type'), partialmethod(_TestNumericField._test_unaryop_type, op=unaryop)) | |
661 | setattr(cls, test_unaryop_name('value'), partialmethod(_TestNumericField._test_unaryop_value, op=unaryop)) | |
662 | setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop)) | |
663 | setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop)) | |
664 | ||
665 | # inject testing methods for each inplace binary operation | |
666 | for name, ibinop in _IBINOPS: | |
667 | setattr(cls, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField._test_ibinop_invalid_unknown, op=ibinop)) | |
668 | setattr(cls, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField._test_ibinop_invalid_none, op=ibinop)) | |
669 | setattr(cls, test_ibinop_name('type_true'), partialmethod(_TestNumericField._test_ibinop_type_true, op=ibinop)) | |
670 | setattr(cls, test_ibinop_name('value_true'), partialmethod(_TestNumericField._test_ibinop_value_true, op=ibinop)) | |
671 | setattr(cls, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField._test_ibinop_type_pos_int, op=ibinop)) | |
672 | setattr(cls, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField._test_ibinop_type_pos_vint, op=ibinop)) | |
673 | setattr(cls, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField._test_ibinop_value_pos_int, op=ibinop)) | |
674 | setattr(cls, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField._test_ibinop_value_pos_vint, op=ibinop)) | |
675 | setattr(cls, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField._test_ibinop_type_neg_int, op=ibinop)) | |
676 | setattr(cls, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField._test_ibinop_type_neg_vint, op=ibinop)) | |
677 | setattr(cls, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField._test_ibinop_value_neg_int, op=ibinop)) | |
678 | setattr(cls, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField._test_ibinop_value_neg_vint, op=ibinop)) | |
679 | setattr(cls, test_ibinop_name('type_false'), partialmethod(_TestNumericField._test_ibinop_type_false, op=ibinop)) | |
680 | setattr(cls, test_ibinop_name('value_false'), partialmethod(_TestNumericField._test_ibinop_value_false, op=ibinop)) | |
681 | setattr(cls, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField._test_ibinop_type_zero_int, op=ibinop)) | |
682 | setattr(cls, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField._test_ibinop_type_zero_vint, op=ibinop)) | |
683 | setattr(cls, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField._test_ibinop_value_zero_int, op=ibinop)) | |
684 | setattr(cls, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField._test_ibinop_value_zero_vint, op=ibinop)) | |
685 | setattr(cls, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField._test_ibinop_type_pos_float, op=ibinop)) | |
686 | setattr(cls, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField._test_ibinop_type_neg_float, op=ibinop)) | |
687 | setattr(cls, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_pos_vfloat, op=ibinop)) | |
688 | setattr(cls, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_neg_vfloat, op=ibinop)) | |
689 | setattr(cls, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField._test_ibinop_value_pos_float, op=ibinop)) | |
690 | setattr(cls, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField._test_ibinop_value_neg_float, op=ibinop)) | |
691 | setattr(cls, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_pos_vfloat, op=ibinop)) | |
692 | setattr(cls, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_neg_vfloat, op=ibinop)) | |
693 | setattr(cls, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField._test_ibinop_type_zero_float, op=ibinop)) | |
694 | setattr(cls, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_zero_vfloat, op=ibinop)) | |
695 | setattr(cls, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField._test_ibinop_value_zero_float, op=ibinop)) | |
696 | setattr(cls, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_zero_vfloat, op=ibinop)) | |
697 | ||
698 | ||
699 | class _TestIntegerFieldCommon(_TestNumericField): | |
700 | def test_assign_true(self): | |
701 | raw = True | |
702 | self._def.value = raw | |
703 | self.assertEqual(self._def, raw) | |
704 | self.assertEqual(self._def.value, raw) | |
705 | ||
706 | def test_assign_false(self): | |
707 | raw = False | |
708 | self._def.value = raw | |
709 | self.assertEqual(self._def, raw) | |
710 | self.assertEqual(self._def.value, raw) | |
711 | ||
712 | def test_assign_pos_int(self): | |
713 | raw = 477 | |
714 | self._def.value = raw | |
715 | self.assertEqual(self._def, raw) | |
716 | self.assertEqual(self._def.value, raw) | |
717 | ||
718 | def test_assign_neg_int(self): | |
719 | raw = -13 | |
720 | self._def.value = raw | |
721 | self.assertEqual(self._def, raw) | |
722 | self.assertEqual(self._def.value, raw) | |
723 | ||
724 | def test_assign_int_field(self): | |
725 | raw = 999 | |
726 | field = self._ft() | |
727 | field.value = raw | |
728 | self._def.value = field | |
729 | self.assertEqual(self._def, raw) | |
730 | self.assertEqual(self._def.value, raw) | |
731 | ||
732 | def test_assign_float(self): | |
733 | raw = 123.456 | |
734 | self._def.value = raw | |
735 | self.assertEqual(self._def, int(raw)) | |
736 | self.assertEqual(self._def.value, int(raw)) | |
737 | ||
738 | def test_assign_invalid_type(self): | |
739 | with self.assertRaises(TypeError): | |
740 | self._def.value = 'yes' | |
741 | ||
742 | def test_assign_uint(self): | |
743 | ft = bt2.IntegerFieldType(size=32, is_signed=False) | |
744 | field = ft() | |
745 | raw = 1777 | |
746 | field.value = 1777 | |
747 | self.assertEqual(field, raw) | |
748 | self.assertEqual(field.value, raw) | |
749 | ||
750 | def test_assign_uint_invalid_neg(self): | |
751 | ft = bt2.IntegerFieldType(size=32, is_signed=False) | |
752 | field = ft() | |
753 | ||
754 | with self.assertRaises(ValueError): | |
755 | field.value = -23 | |
756 | ||
757 | ||
758 | _inject_numeric_testing_methods(_TestIntegerFieldCommon) | |
759 | ||
760 | ||
761 | class IntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): | |
762 | def setUp(self): | |
763 | self._ft = bt2.IntegerFieldType(25, is_signed=True) | |
764 | self._field = self._ft() | |
765 | self._def = self._ft() | |
766 | self._def.value = 17 | |
767 | self._def_value = 17 | |
768 | self._def_new_value = -101 | |
769 | ||
770 | ||
771 | class EnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): | |
772 | def setUp(self): | |
773 | self._ft = bt2.EnumerationFieldType(size=32, is_signed=True) | |
774 | self._ft.append_mapping('whole range', -(2 ** 31), (2 ** 31) - 1) | |
775 | self._def = self._ft() | |
776 | self._def.value = 17 | |
777 | self._def_value = 17 | |
778 | self._def_new_value = -101 | |
779 | ||
780 | ||
781 | class FloatingPointNumberFieldTestCase(_TestNumericField, unittest.TestCase): | |
782 | def setUp(self): | |
783 | self._ft = bt2.FloatingPointNumberFieldType() | |
784 | self._field = self._ft() | |
785 | self._def = self._ft() | |
786 | self._def.value = 52.7 | |
787 | self._def_value = 52.7 | |
788 | self._def_new_value = -17.164857 | |
789 | ||
790 | def _test_invalid_op(self, cb): | |
791 | with self.assertRaises(TypeError): | |
792 | cb() | |
793 | ||
794 | def test_assign_true(self): | |
795 | self._def.value = True | |
796 | self.assertTrue(self._def) | |
797 | self.assertTrue(self._def.value) | |
798 | ||
799 | def test_assign_false(self): | |
800 | self._def.value = False | |
801 | self.assertFalse(self._def) | |
802 | self.assertFalse(self._def.value) | |
803 | ||
804 | def test_assign_pos_int(self): | |
805 | raw = 477 | |
806 | self._def.value = raw | |
807 | self.assertEqual(self._def, float(raw)) | |
808 | self.assertEqual(self._def.value, float(raw)) | |
809 | ||
810 | def test_assign_neg_int(self): | |
811 | raw = -13 | |
812 | self._def.value = raw | |
813 | self.assertEqual(self._def, float(raw)) | |
814 | self.assertEqual(self._def.value, float(raw)) | |
815 | ||
816 | def test_assign_int_field(self): | |
817 | ft = bt2.IntegerFieldType(32) | |
818 | field = ft() | |
819 | raw = 999 | |
820 | field.value = raw | |
821 | self._def.value = field | |
822 | self.assertEqual(self._def, float(raw)) | |
823 | self.assertEqual(self._def.value, float(raw)) | |
824 | ||
825 | def test_assign_float(self): | |
826 | raw = -19.23 | |
827 | self._def.value = raw | |
828 | self.assertEqual(self._def, raw) | |
829 | self.assertEqual(self._def.value, raw) | |
830 | ||
831 | def test_assign_float_field(self): | |
832 | ft = bt2.FloatingPointNumberFieldType(32) | |
833 | field = ft() | |
834 | raw = 101.32 | |
835 | field.value = raw | |
836 | self._def.value = field | |
837 | self.assertEqual(self._def, raw) | |
838 | self.assertEqual(self._def.value, raw) | |
839 | ||
840 | def test_assign_invalid_type(self): | |
841 | with self.assertRaises(TypeError): | |
842 | self._def.value = 'yes' | |
843 | ||
844 | def test_invalid_lshift(self): | |
845 | self._test_invalid_op(lambda: self._def << 23) | |
846 | ||
847 | def test_invalid_rshift(self): | |
848 | self._test_invalid_op(lambda: self._def >> 23) | |
849 | ||
850 | def test_invalid_and(self): | |
851 | self._test_invalid_op(lambda: self._def & 23) | |
852 | ||
853 | def test_invalid_or(self): | |
854 | self._test_invalid_op(lambda: self._def | 23) | |
855 | ||
856 | def test_invalid_xor(self): | |
857 | self._test_invalid_op(lambda: self._def ^ 23) | |
858 | ||
859 | def test_invalid_invert(self): | |
860 | self._test_invalid_op(lambda: ~self._def) | |
861 | ||
862 | ||
863 | _inject_numeric_testing_methods(FloatingPointNumberFieldTestCase) | |
864 | ||
865 | ||
866 | class StringFieldTestCase(_TestCopySimple, unittest.TestCase): | |
867 | def setUp(self): | |
868 | self._ft = bt2.StringFieldType() | |
869 | self._def_value = 'Hello, World!' | |
870 | self._def = self._ft() | |
871 | self._def.value = self._def_value | |
872 | self._def_new_value = 'Yes!' | |
873 | ||
874 | def test_assign_int(self): | |
875 | with self.assertRaises(TypeError): | |
876 | self._def.value = 283 | |
877 | ||
878 | def test_assign_str(self): | |
879 | raw = 'zorg' | |
880 | self._def = raw | |
881 | self.assertEqual(self._def, raw) | |
882 | ||
883 | def test_assign_string_field(self): | |
884 | ft = bt2.StringFieldType() | |
885 | field = ft() | |
886 | raw = 'zorg' | |
887 | field.value = raw | |
888 | self.assertEqual(field, raw) | |
889 | ||
890 | def test_eq(self): | |
891 | self.assertEqual(self._def, self._def_value) | |
892 | ||
893 | def test_eq(self): | |
894 | self.assertNotEqual(self._def, 23) | |
895 | ||
896 | def test_lt_vstring(self): | |
897 | s1 = self._ft() | |
898 | s1.value = 'allo' | |
899 | s2 = self._ft() | |
900 | s2.value = 'bateau' | |
901 | self.assertLess(s1, s2) | |
902 | ||
903 | def test_lt_string(self): | |
904 | s1 = self._ft() | |
905 | s1.value = 'allo' | |
906 | self.assertLess(s1, 'bateau') | |
907 | ||
908 | def test_le_vstring(self): | |
909 | s1 = self._ft() | |
910 | s1.value = 'allo' | |
911 | s2 = self._ft() | |
912 | s2.value = 'bateau' | |
913 | self.assertLessEqual(s1, s2) | |
914 | ||
915 | def test_le_string(self): | |
916 | s1 = self._ft() | |
917 | s1.value = 'allo' | |
918 | self.assertLessEqual(s1, 'bateau') | |
919 | ||
920 | def test_gt_vstring(self): | |
921 | s1 = self._ft() | |
922 | s1.value = 'allo' | |
923 | s2 = self._ft() | |
924 | s2.value = 'bateau' | |
925 | self.assertGreater(s2, s1) | |
926 | ||
927 | def test_gt_string(self): | |
928 | s1 = self._ft() | |
929 | s1.value = 'allo' | |
930 | self.assertGreater('bateau', s1) | |
931 | ||
932 | def test_ge_vstring(self): | |
933 | s1 = self._ft() | |
934 | s1.value = 'allo' | |
935 | s2 = self._ft() | |
936 | s2.value = 'bateau' | |
937 | self.assertGreaterEqual(s2, s1) | |
938 | ||
939 | def test_ge_string(self): | |
940 | s1 = self._ft() | |
941 | s1.value = 'allo' | |
942 | self.assertGreaterEqual('bateau', s1) | |
943 | ||
944 | def test_bool_op(self): | |
945 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
946 | ||
947 | def test_str_op(self): | |
948 | self.assertEqual(str(self._def), str(self._def_value)) | |
949 | ||
950 | def test_len(self): | |
951 | self.assertEqual(len(self._def), len(self._def_value)) | |
952 | ||
953 | def test_getitem(self): | |
954 | self.assertEqual(self._def[5], self._def_value[5]) | |
955 | ||
956 | def test_append_str(self): | |
957 | to_append = 'meow meow meow' | |
958 | self._def += to_append | |
959 | self._def_value += to_append | |
960 | self.assertEqual(self._def, self._def_value) | |
961 | ||
962 | def test_append_string_field(self): | |
963 | ft = bt2.StringFieldType() | |
964 | field = ft() | |
965 | to_append = 'meow meow meow' | |
966 | field.value = to_append | |
967 | self._def += field | |
968 | self._def_value += to_append | |
969 | self.assertEqual(self._def, self._def_value) | |
970 | ||
971 | ||
972 | class _TestArraySequenceFieldCommon(_TestCopySimple): | |
973 | def _modify_def(self): | |
974 | self._def[2] = 23 | |
975 | ||
976 | def test_bool_op_true(self): | |
977 | self.assertTrue(self._def) | |
978 | ||
979 | def test_len(self): | |
980 | self.assertEqual(len(self._def), 3) | |
981 | ||
982 | def test_getitem(self): | |
983 | field = self._def[1] | |
984 | self.assertIs(type(field), bt2.fields._IntegerField) | |
985 | self.assertEqual(field, 1847) | |
986 | ||
987 | def test_eq(self): | |
988 | ft = bt2.ArrayFieldType(self._elem_ft, 3) | |
989 | field = ft() | |
990 | field[0] = 45 | |
991 | field[1] = 1847 | |
992 | field[2] = 1948754 | |
993 | self.assertEqual(self._def, field) | |
994 | ||
995 | def test_eq_invalid_type(self): | |
996 | self.assertNotEqual(self._def, 23) | |
997 | ||
998 | def test_eq_diff_len(self): | |
999 | ft = bt2.ArrayFieldType(self._elem_ft, 2) | |
1000 | field = ft() | |
1001 | field[0] = 45 | |
1002 | field[1] = 1847 | |
1003 | self.assertNotEqual(self._def, field) | |
1004 | ||
1005 | def test_eq_diff_content_same_len(self): | |
1006 | ft = bt2.ArrayFieldType(self._elem_ft, 3) | |
1007 | field = ft() | |
1008 | field[0] = 45 | |
1009 | field[1] = 1846 | |
1010 | field[2] = 1948754 | |
1011 | self.assertNotEqual(self._def, field) | |
1012 | ||
1013 | def test_setitem(self): | |
1014 | self._def[2] = 24 | |
1015 | self.assertEqual(self._def[2], 24) | |
1016 | ||
1017 | def test_setitem_int_field(self): | |
1018 | int_field = self._elem_ft() | |
1019 | int_field.value = 19487 | |
1020 | self._def[1] = int_field | |
1021 | self.assertEqual(self._def[1], 19487) | |
1022 | ||
1023 | def test_setitem_non_basic_field(self): | |
1024 | elem_ft = bt2.StructureFieldType() | |
1025 | array_ft = bt2.ArrayFieldType(elem_ft, 3) | |
1026 | elem_field = elem_ft() | |
1027 | array_field = array_ft() | |
1028 | ||
1029 | with self.assertRaises(TypeError): | |
1030 | array_field[1] = 23 | |
1031 | ||
1032 | def test_setitem_none(self): | |
1033 | with self.assertRaises(TypeError): | |
1034 | self._def[1] = None | |
1035 | ||
1036 | def test_setitem_index_wrong_type(self): | |
1037 | with self.assertRaises(TypeError): | |
1038 | self._def['yes'] = 23 | |
1039 | ||
1040 | def test_setitem_index_neg(self): | |
1041 | with self.assertRaises(IndexError): | |
1042 | self._def[-2] = 23 | |
1043 | ||
1044 | def test_setitem_index_out_of_range(self): | |
1045 | with self.assertRaises(IndexError): | |
1046 | self._def[len(self._def)] = 134679 | |
1047 | ||
1048 | def test_iter(self): | |
1049 | for field, value in zip(self._def, (45, 1847, 1948754)): | |
1050 | self.assertEqual(field, value) | |
1051 | ||
1052 | ||
1053 | class ArrayFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase): | |
1054 | def setUp(self): | |
1055 | self._elem_ft = bt2.IntegerFieldType(32) | |
1056 | self._ft = bt2.ArrayFieldType(self._elem_ft, 3) | |
1057 | self._def = self._ft() | |
1058 | self._def[0] = 45 | |
1059 | self._def[1] = 1847 | |
1060 | self._def[2] = 1948754 | |
1061 | ||
1062 | ||
1063 | class SequenceFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase): | |
1064 | def setUp(self): | |
1065 | self._elem_ft = bt2.IntegerFieldType(32) | |
1066 | self._ft = bt2.SequenceFieldType(self._elem_ft, 'the.length') | |
1067 | self._def = self._ft() | |
1068 | self._length_field = self._elem_ft(3) | |
1069 | self._def.length_field = self._length_field | |
1070 | self._def[0] = 45 | |
1071 | self._def[1] = 1847 | |
1072 | self._def[2] = 1948754 | |
1073 | ||
1074 | ||
1075 | class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): | |
1076 | def setUp(self): | |
1077 | self._ft0 = bt2.IntegerFieldType(32, is_signed=True) | |
1078 | self._ft1 = bt2.StringFieldType() | |
1079 | self._ft2 = bt2.FloatingPointNumberFieldType() | |
1080 | self._ft3 = bt2.IntegerFieldType(17) | |
1081 | self._ft = bt2.StructureFieldType() | |
1082 | self._ft.append_field('A', self._ft0) | |
1083 | self._ft.append_field('B', self._ft1) | |
1084 | self._ft.append_field('C', self._ft2) | |
1085 | self._ft.append_field('D', self._ft3) | |
1086 | self._def = self._ft() | |
1087 | self._def['A'] = -1872 | |
1088 | self._def['B'] = 'salut' | |
1089 | self._def['C'] = 17.5 | |
1090 | self._def['D'] = 16497 | |
1091 | ||
1092 | def _modify_def(self): | |
1093 | self._def['B'] = 'hola' | |
1094 | ||
1095 | def test_bool_op_true(self): | |
1096 | self.assertTrue(self._def) | |
1097 | ||
1098 | def test_bool_op_false(self): | |
1099 | ft = bt2.StructureFieldType() | |
1100 | field = ft() | |
1101 | self.assertFalse(field) | |
1102 | ||
1103 | def test_len(self): | |
1104 | self.assertEqual(len(self._def), 4) | |
1105 | ||
1106 | def test_getitem(self): | |
1107 | field = self._def['A'] | |
1108 | self.assertIs(type(field), bt2.fields._IntegerField) | |
1109 | self.assertEqual(field, -1872) | |
1110 | ||
1111 | def test_eq(self): | |
1112 | ft = bt2.StructureFieldType() | |
1113 | ft.append_field('A', self._ft0) | |
1114 | ft.append_field('B', self._ft1) | |
1115 | ft.append_field('C', self._ft2) | |
1116 | ft.append_field('D', self._ft3) | |
1117 | field = ft() | |
1118 | field['A'] = -1872 | |
1119 | field['B'] = 'salut' | |
1120 | field['C'] = 17.5 | |
1121 | field['D'] = 16497 | |
1122 | self.assertEqual(self._def, field) | |
1123 | ||
1124 | def test_eq_invalid_type(self): | |
1125 | self.assertNotEqual(self._def, 23) | |
1126 | ||
1127 | def test_eq_diff_len(self): | |
1128 | ft = bt2.StructureFieldType() | |
1129 | ft.append_field('A', self._ft0) | |
1130 | ft.append_field('B', self._ft1) | |
1131 | ft.append_field('C', self._ft2) | |
1132 | field = ft() | |
1133 | field['A'] = -1872 | |
1134 | field['B'] = 'salut' | |
1135 | field['C'] = 17.5 | |
1136 | self.assertNotEqual(self._def, field) | |
1137 | ||
1138 | def test_eq_diff_content_same_len(self): | |
1139 | ft = bt2.StructureFieldType() | |
1140 | ft.append_field('A', self._ft0) | |
1141 | ft.append_field('B', self._ft1) | |
1142 | ft.append_field('C', self._ft2) | |
1143 | ft.append_field('D', self._ft3) | |
1144 | field = ft() | |
1145 | field['A'] = -1872 | |
1146 | field['B'] = 'salut' | |
1147 | field['C'] = 17.4 | |
1148 | field['D'] = 16497 | |
1149 | self.assertNotEqual(self._def, field) | |
1150 | ||
1151 | def test_eq_same_content_diff_keys(self): | |
1152 | ft = bt2.StructureFieldType() | |
1153 | ft.append_field('A', self._ft0) | |
1154 | ft.append_field('B', self._ft1) | |
1155 | ft.append_field('E', self._ft2) | |
1156 | ft.append_field('D', self._ft3) | |
1157 | field = ft() | |
1158 | field['A'] = -1872 | |
1159 | field['B'] = 'salut' | |
1160 | field['E'] = 17.4 | |
1161 | field['D'] = 16497 | |
1162 | self.assertNotEqual(self._def, field) | |
1163 | ||
1164 | def test_setitem(self): | |
1165 | self._def['C'] = -18.47 | |
1166 | self.assertEqual(self._def['C'], -18.47) | |
1167 | ||
1168 | def test_setitem_int_field(self): | |
1169 | int_ft = bt2.IntegerFieldType(16) | |
1170 | int_field = int_ft() | |
1171 | int_field.value = 19487 | |
1172 | self._def['D'] = int_field | |
1173 | self.assertEqual(self._def['D'], 19487) | |
1174 | ||
1175 | def test_setitem_non_basic_field(self): | |
1176 | elem_ft = bt2.StructureFieldType() | |
1177 | elem_field = elem_ft() | |
1178 | struct_ft = bt2.StructureFieldType() | |
1179 | struct_ft.append_field('A', elem_ft) | |
1180 | struct_field = struct_ft() | |
1181 | ||
1182 | with self.assertRaises(TypeError): | |
1183 | struct_field['A'] = 23 | |
1184 | ||
1185 | def test_setitem_none(self): | |
1186 | with self.assertRaises(TypeError): | |
1187 | self._def['C'] = None | |
1188 | ||
1189 | def test_setitem_key_wrong_type(self): | |
1190 | with self.assertRaises(TypeError): | |
1191 | self._def[3] = 23 | |
1192 | ||
1193 | def test_setitem_wrong_key(self): | |
1194 | with self.assertRaises(KeyError): | |
1195 | self._def['hi'] = 134679 | |
1196 | ||
1197 | def test_at_index(self): | |
1198 | self.assertEqual(self._def.at_index(1), 'salut') | |
1199 | ||
1200 | def test_iter(self): | |
1201 | orig_values = { | |
1202 | 'A': -1872, | |
1203 | 'B': 'salut', | |
1204 | 'C': 17.5, | |
1205 | 'D': 16497, | |
1206 | } | |
1207 | ||
1208 | for vkey, vval in self._def.items(): | |
1209 | val = orig_values[vkey] | |
1210 | self.assertEqual(vval, val) |