| 1 | from functools import partial, partialmethod |
| 2 | import operator |
| 3 | import unittest |
| 4 | import numbers |
| 5 | import math |
| 6 | import copy |
| 7 | import itertools |
| 8 | import bt2 |
| 9 | |
| 10 | |
| 11 | class _TestCopySimple: |
| 12 | def test_copy(self): |
| 13 | cpy = copy.copy(self._def) |
| 14 | self.assertIsNot(cpy, self._def) |
| 15 | self.assertNotEqual(cpy.addr, self._def.addr) |
| 16 | self.assertEqual(cpy, self._def) |
| 17 | |
| 18 | def test_deepcopy(self): |
| 19 | cpy = copy.deepcopy(self._def) |
| 20 | self.assertIsNot(cpy, self._def) |
| 21 | self.assertNotEqual(cpy.addr, self._def.addr) |
| 22 | self.assertEqual(cpy, self._def) |
| 23 | |
| 24 | |
| 25 | _COMP_BINOPS = ( |
| 26 | operator.eq, |
| 27 | operator.ne, |
| 28 | ) |
| 29 | |
| 30 | |
| 31 | class _TestNumericField(_TestCopySimple): |
| 32 | def _binop(self, op, rhs): |
| 33 | rexc = None |
| 34 | rvexc = None |
| 35 | comp_value = rhs |
| 36 | |
| 37 | if isinstance(rhs, (bt2.fields._IntegerField, bt2.fields._FloatingPointNumberField)): |
| 38 | comp_value = copy.copy(rhs) |
| 39 | |
| 40 | try: |
| 41 | r = op(self._def, rhs) |
| 42 | except Exception as e: |
| 43 | rexc = e |
| 44 | |
| 45 | try: |
| 46 | rv = op(self._def_value, comp_value) |
| 47 | except Exception as e: |
| 48 | rvexc = e |
| 49 | |
| 50 | if rexc is not None or rvexc is not None: |
| 51 | # at least one of the operations raised an exception: in |
| 52 | # this case both operations should have raised the same |
| 53 | # type of exception (division by zero, bit shift with a |
| 54 | # floating point number operand, etc.) |
| 55 | self.assertIs(type(rexc), type(rvexc)) |
| 56 | return None, None |
| 57 | |
| 58 | return r, rv |
| 59 | |
| 60 | def _unaryop(self, op): |
| 61 | rexc = None |
| 62 | rvexc = None |
| 63 | |
| 64 | try: |
| 65 | r = op(self._def) |
| 66 | except Exception as e: |
| 67 | rexc = e |
| 68 | |
| 69 | try: |
| 70 | rv = op(self._def_value) |
| 71 | except Exception as e: |
| 72 | rvexc = e |
| 73 | |
| 74 | if rexc is not None or rvexc is not None: |
| 75 | # at least one of the operations raised an exception: in |
| 76 | # this case both operations should have raised the same |
| 77 | # type of exception (division by zero, bit shift with a |
| 78 | # floating point number operand, etc.) |
| 79 | self.assertIs(type(rexc), type(rvexc)) |
| 80 | return None, None |
| 81 | |
| 82 | return r, rv |
| 83 | |
| 84 | def _test_unaryop_type(self, op): |
| 85 | r, rv = self._unaryop(op) |
| 86 | |
| 87 | if r is None: |
| 88 | return |
| 89 | |
| 90 | self.assertIsInstance(r, type(rv)) |
| 91 | |
| 92 | def _test_unaryop_value(self, op): |
| 93 | r, rv = self._unaryop(op) |
| 94 | |
| 95 | if r is None: |
| 96 | return |
| 97 | |
| 98 | self.assertEqual(r, rv) |
| 99 | |
| 100 | def _test_unaryop_addr_same(self, op): |
| 101 | addr_before = self._def.addr |
| 102 | self._unaryop(op) |
| 103 | self.assertEqual(self._def.addr, addr_before) |
| 104 | |
| 105 | def _test_unaryop_value_same(self, op): |
| 106 | value_before = copy.copy(self._def_value) |
| 107 | self._unaryop(op) |
| 108 | self.assertEqual(self._def, value_before) |
| 109 | |
| 110 | def _test_binop_type(self, op, rhs): |
| 111 | r, rv = self._binop(op, rhs) |
| 112 | |
| 113 | if r is None: |
| 114 | return |
| 115 | |
| 116 | if op in _COMP_BINOPS: |
| 117 | # __eq__() and __ne__() always return a 'bool' object |
| 118 | self.assertIsInstance(r, bool) |
| 119 | else: |
| 120 | self.assertIsInstance(r, type(rv)) |
| 121 | |
| 122 | def _test_binop_value(self, op, rhs): |
| 123 | r, rv = self._binop(op, rhs) |
| 124 | |
| 125 | if r is None: |
| 126 | return |
| 127 | |
| 128 | self.assertEqual(r, rv) |
| 129 | |
| 130 | def _test_binop_lhs_addr_same(self, op, rhs): |
| 131 | addr_before = self._def.addr |
| 132 | r, rv = self._binop(op, rhs) |
| 133 | self.assertEqual(self._def.addr, addr_before) |
| 134 | |
| 135 | def _test_binop_lhs_value_same(self, op, rhs): |
| 136 | value_before = copy.copy(self._def) |
| 137 | r, rv = self._binop(op, rhs) |
| 138 | self.assertEqual(self._def, value_before) |
| 139 | |
| 140 | def _test_binop_invalid_unknown(self, op): |
| 141 | if op in _COMP_BINOPS: |
| 142 | self.skipTest('not testing') |
| 143 | |
| 144 | class A: |
| 145 | pass |
| 146 | |
| 147 | with self.assertRaises(TypeError): |
| 148 | op(self._def, A()) |
| 149 | |
| 150 | def _test_binop_invalid_none(self, op): |
| 151 | if op in _COMP_BINOPS: |
| 152 | self.skipTest('not testing') |
| 153 | |
| 154 | with self.assertRaises(TypeError): |
| 155 | op(self._def, None) |
| 156 | |
| 157 | def _test_ibinop_value(self, op, rhs): |
| 158 | r, rv = self._binop(op, rhs) |
| 159 | |
| 160 | if r is None: |
| 161 | return |
| 162 | |
| 163 | # The inplace operators are special for field objects because |
| 164 | # they do not return a new, immutable object like it's the case |
| 165 | # for Python numbers. In Python, `a += 2`, where `a` is a number |
| 166 | # object, assigns a new number object reference to `a`, dropping |
| 167 | # the old reference. Since BT's field objects are mutable, we |
| 168 | # modify their internal value with the inplace operators. This |
| 169 | # means however that we can lose data in the process, for |
| 170 | # example: |
| 171 | # |
| 172 | # int_value_obj += 3.3 |
| 173 | # |
| 174 | # Here, if `int_value_obj` is a Python `int` with the value 2, |
| 175 | # it would be a `float` object after this, holding the value |
| 176 | # 5.3. In our case, if `int_value_obj` is an integer field |
| 177 | # object, 3.3 is converted to an `int` object (3) and added to |
| 178 | # the current value of `int_value_obj`, so after this the value |
| 179 | # of the object is 5. This does not compare to 5.3, which is |
| 180 | # why we also use the `int()` type here. |
| 181 | if isinstance(self._def, bt2.fields._IntegerField): |
| 182 | rv = int(rv) |
| 183 | |
| 184 | self.assertEqual(r, rv) |
| 185 | |
| 186 | def _test_ibinop_type(self, op, rhs): |
| 187 | r, rv = self._binop(op, rhs) |
| 188 | |
| 189 | if r is None: |
| 190 | return |
| 191 | |
| 192 | self.assertIs(r, self._def) |
| 193 | |
| 194 | def _test_ibinop_invalid_unknown(self, op): |
| 195 | class A: |
| 196 | pass |
| 197 | |
| 198 | with self.assertRaises(TypeError): |
| 199 | op(self._def, A()) |
| 200 | |
| 201 | def _test_ibinop_invalid_none(self, op): |
| 202 | with self.assertRaises(TypeError): |
| 203 | op(self._def, None) |
| 204 | |
| 205 | def _test_binop_rhs_false(self, test_cb, op): |
| 206 | test_cb(op, False) |
| 207 | |
| 208 | def _test_binop_rhs_true(self, test_cb, op): |
| 209 | test_cb(op, True) |
| 210 | |
| 211 | def _test_binop_rhs_pos_int(self, test_cb, op): |
| 212 | test_cb(op, 2) |
| 213 | |
| 214 | def _test_binop_rhs_neg_int(self, test_cb, op): |
| 215 | test_cb(op, -23) |
| 216 | |
| 217 | def _test_binop_rhs_zero_int(self, test_cb, op): |
| 218 | test_cb(op, 0) |
| 219 | |
| 220 | def _test_binop_rhs_pos_vint(self, test_cb, op): |
| 221 | test_cb(op, bt2.create_value(2)) |
| 222 | |
| 223 | def _test_binop_rhs_neg_vint(self, test_cb, op): |
| 224 | test_cb(op, bt2.create_value(-23)) |
| 225 | |
| 226 | def _test_binop_rhs_zero_vint(self, test_cb, op): |
| 227 | test_cb(op, bt2.create_value(0)) |
| 228 | |
| 229 | def _test_binop_rhs_pos_float(self, test_cb, op): |
| 230 | test_cb(op, 2.2) |
| 231 | |
| 232 | def _test_binop_rhs_neg_float(self, test_cb, op): |
| 233 | test_cb(op, -23.4) |
| 234 | |
| 235 | def _test_binop_rhs_zero_float(self, test_cb, op): |
| 236 | test_cb(op, 0.0) |
| 237 | |
| 238 | def _test_binop_rhs_pos_vfloat(self, test_cb, op): |
| 239 | test_cb(op, bt2.create_value(2.2)) |
| 240 | |
| 241 | def _test_binop_rhs_neg_vfloat(self, test_cb, op): |
| 242 | test_cb(op, bt2.create_value(-23.4)) |
| 243 | |
| 244 | def _test_binop_rhs_zero_vfloat(self, test_cb, op): |
| 245 | test_cb(op, bt2.create_value(0.0)) |
| 246 | |
| 247 | def _test_binop_type_false(self, op): |
| 248 | self._test_binop_rhs_false(self._test_binop_type, op) |
| 249 | |
| 250 | def _test_binop_type_true(self, op): |
| 251 | self._test_binop_rhs_true(self._test_binop_type, op) |
| 252 | |
| 253 | def _test_binop_type_pos_int(self, op): |
| 254 | self._test_binop_rhs_pos_int(self._test_binop_type, op) |
| 255 | |
| 256 | def _test_binop_type_neg_int(self, op): |
| 257 | self._test_binop_rhs_neg_int(self._test_binop_type, op) |
| 258 | |
| 259 | def _test_binop_type_zero_int(self, op): |
| 260 | self._test_binop_rhs_zero_int(self._test_binop_type, op) |
| 261 | |
| 262 | def _test_binop_type_pos_vint(self, op): |
| 263 | self._test_binop_rhs_pos_vint(self._test_binop_type, op) |
| 264 | |
| 265 | def _test_binop_type_neg_vint(self, op): |
| 266 | self._test_binop_rhs_neg_vint(self._test_binop_type, op) |
| 267 | |
| 268 | def _test_binop_type_zero_vint(self, op): |
| 269 | self._test_binop_rhs_zero_vint(self._test_binop_type, op) |
| 270 | |
| 271 | def _test_binop_type_pos_float(self, op): |
| 272 | self._test_binop_rhs_pos_float(self._test_binop_type, op) |
| 273 | |
| 274 | def _test_binop_type_neg_float(self, op): |
| 275 | self._test_binop_rhs_neg_float(self._test_binop_type, op) |
| 276 | |
| 277 | def _test_binop_type_zero_float(self, op): |
| 278 | self._test_binop_rhs_zero_float(self._test_binop_type, op) |
| 279 | |
| 280 | def _test_binop_type_pos_vfloat(self, op): |
| 281 | self._test_binop_rhs_pos_vfloat(self._test_binop_type, op) |
| 282 | |
| 283 | def _test_binop_type_neg_vfloat(self, op): |
| 284 | self._test_binop_rhs_neg_vfloat(self._test_binop_type, op) |
| 285 | |
| 286 | def _test_binop_type_zero_vfloat(self, op): |
| 287 | self._test_binop_rhs_zero_vfloat(self._test_binop_type, op) |
| 288 | |
| 289 | def _test_binop_value_false(self, op): |
| 290 | self._test_binop_rhs_false(self._test_binop_value, op) |
| 291 | |
| 292 | def _test_binop_value_true(self, op): |
| 293 | self._test_binop_rhs_true(self._test_binop_value, op) |
| 294 | |
| 295 | def _test_binop_value_pos_int(self, op): |
| 296 | self._test_binop_rhs_pos_int(self._test_binop_value, op) |
| 297 | |
| 298 | def _test_binop_value_neg_int(self, op): |
| 299 | self._test_binop_rhs_neg_int(self._test_binop_value, op) |
| 300 | |
| 301 | def _test_binop_value_zero_int(self, op): |
| 302 | self._test_binop_rhs_zero_int(self._test_binop_value, op) |
| 303 | |
| 304 | def _test_binop_value_pos_vint(self, op): |
| 305 | self._test_binop_rhs_pos_vint(self._test_binop_value, op) |
| 306 | |
| 307 | def _test_binop_value_neg_vint(self, op): |
| 308 | self._test_binop_rhs_neg_vint(self._test_binop_value, op) |
| 309 | |
| 310 | def _test_binop_value_zero_vint(self, op): |
| 311 | self._test_binop_rhs_zero_vint(self._test_binop_value, op) |
| 312 | |
| 313 | def _test_binop_value_pos_float(self, op): |
| 314 | self._test_binop_rhs_pos_float(self._test_binop_value, op) |
| 315 | |
| 316 | def _test_binop_value_neg_float(self, op): |
| 317 | self._test_binop_rhs_neg_float(self._test_binop_value, op) |
| 318 | |
| 319 | def _test_binop_value_zero_float(self, op): |
| 320 | self._test_binop_rhs_zero_float(self._test_binop_value, op) |
| 321 | |
| 322 | def _test_binop_value_pos_vfloat(self, op): |
| 323 | self._test_binop_rhs_pos_vfloat(self._test_binop_value, op) |
| 324 | |
| 325 | def _test_binop_value_neg_vfloat(self, op): |
| 326 | self._test_binop_rhs_neg_vfloat(self._test_binop_value, op) |
| 327 | |
| 328 | def _test_binop_value_zero_vfloat(self, op): |
| 329 | self._test_binop_rhs_zero_vfloat(self._test_binop_value, op) |
| 330 | |
| 331 | def _test_binop_lhs_addr_same_false(self, op): |
| 332 | self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op) |
| 333 | |
| 334 | def _test_binop_lhs_addr_same_true(self, op): |
| 335 | self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op) |
| 336 | |
| 337 | def _test_binop_lhs_addr_same_pos_int(self, op): |
| 338 | self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op) |
| 339 | |
| 340 | def _test_binop_lhs_addr_same_neg_int(self, op): |
| 341 | self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op) |
| 342 | |
| 343 | def _test_binop_lhs_addr_same_zero_int(self, op): |
| 344 | self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op) |
| 345 | |
| 346 | def _test_binop_lhs_addr_same_pos_vint(self, op): |
| 347 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op) |
| 348 | |
| 349 | def _test_binop_lhs_addr_same_neg_vint(self, op): |
| 350 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op) |
| 351 | |
| 352 | def _test_binop_lhs_addr_same_zero_vint(self, op): |
| 353 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op) |
| 354 | |
| 355 | def _test_binop_lhs_addr_same_pos_float(self, op): |
| 356 | self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op) |
| 357 | |
| 358 | def _test_binop_lhs_addr_same_neg_float(self, op): |
| 359 | self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op) |
| 360 | |
| 361 | def _test_binop_lhs_addr_same_zero_float(self, op): |
| 362 | self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op) |
| 363 | |
| 364 | def _test_binop_lhs_addr_same_pos_vfloat(self, op): |
| 365 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op) |
| 366 | |
| 367 | def _test_binop_lhs_addr_same_neg_vfloat(self, op): |
| 368 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op) |
| 369 | |
| 370 | def _test_binop_lhs_addr_same_zero_vfloat(self, op): |
| 371 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op) |
| 372 | |
| 373 | def _test_binop_lhs_value_same_false(self, op): |
| 374 | self._test_binop_rhs_false(self._test_binop_lhs_value_same, op) |
| 375 | |
| 376 | def _test_binop_lhs_value_same_true(self, op): |
| 377 | self._test_binop_rhs_true(self._test_binop_lhs_value_same, op) |
| 378 | |
| 379 | def _test_binop_lhs_value_same_pos_int(self, op): |
| 380 | self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op) |
| 381 | |
| 382 | def _test_binop_lhs_value_same_neg_int(self, op): |
| 383 | self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op) |
| 384 | |
| 385 | def _test_binop_lhs_value_same_zero_int(self, op): |
| 386 | self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op) |
| 387 | |
| 388 | def _test_binop_lhs_value_same_pos_vint(self, op): |
| 389 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op) |
| 390 | |
| 391 | def _test_binop_lhs_value_same_neg_vint(self, op): |
| 392 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op) |
| 393 | |
| 394 | def _test_binop_lhs_value_same_zero_vint(self, op): |
| 395 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op) |
| 396 | |
| 397 | def _test_binop_lhs_value_same_pos_float(self, op): |
| 398 | self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op) |
| 399 | |
| 400 | def _test_binop_lhs_value_same_neg_float(self, op): |
| 401 | self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op) |
| 402 | |
| 403 | def _test_binop_lhs_value_same_zero_float(self, op): |
| 404 | self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op) |
| 405 | |
| 406 | def _test_binop_lhs_value_same_pos_vfloat(self, op): |
| 407 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op) |
| 408 | |
| 409 | def _test_binop_lhs_value_same_neg_vfloat(self, op): |
| 410 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op) |
| 411 | |
| 412 | def _test_binop_lhs_value_same_zero_vfloat(self, op): |
| 413 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op) |
| 414 | |
| 415 | def _test_ibinop_type_false(self, op): |
| 416 | self._test_binop_rhs_false(self._test_ibinop_type, op) |
| 417 | |
| 418 | def _test_ibinop_type_true(self, op): |
| 419 | self._test_binop_rhs_true(self._test_ibinop_type, op) |
| 420 | |
| 421 | def _test_ibinop_type_pos_int(self, op): |
| 422 | self._test_binop_rhs_pos_int(self._test_ibinop_type, op) |
| 423 | |
| 424 | def _test_ibinop_type_neg_int(self, op): |
| 425 | self._test_binop_rhs_neg_int(self._test_ibinop_type, op) |
| 426 | |
| 427 | def _test_ibinop_type_zero_int(self, op): |
| 428 | self._test_binop_rhs_zero_int(self._test_ibinop_type, op) |
| 429 | |
| 430 | def _test_ibinop_type_pos_vint(self, op): |
| 431 | self._test_binop_rhs_pos_vint(self._test_ibinop_type, op) |
| 432 | |
| 433 | def _test_ibinop_type_neg_vint(self, op): |
| 434 | self._test_binop_rhs_neg_vint(self._test_ibinop_type, op) |
| 435 | |
| 436 | def _test_ibinop_type_zero_vint(self, op): |
| 437 | self._test_binop_rhs_zero_vint(self._test_ibinop_type, op) |
| 438 | |
| 439 | def _test_ibinop_type_pos_float(self, op): |
| 440 | self._test_binop_rhs_pos_float(self._test_ibinop_type, op) |
| 441 | |
| 442 | def _test_ibinop_type_neg_float(self, op): |
| 443 | self._test_binop_rhs_neg_float(self._test_ibinop_type, op) |
| 444 | |
| 445 | def _test_ibinop_type_zero_float(self, op): |
| 446 | self._test_binop_rhs_zero_float(self._test_ibinop_type, op) |
| 447 | |
| 448 | def _test_ibinop_type_pos_vfloat(self, op): |
| 449 | self._test_binop_rhs_pos_vfloat(self._test_ibinop_type, op) |
| 450 | |
| 451 | def _test_ibinop_type_neg_vfloat(self, op): |
| 452 | self._test_binop_rhs_neg_vfloat(self._test_ibinop_type, op) |
| 453 | |
| 454 | def _test_ibinop_type_zero_vfloat(self, op): |
| 455 | self._test_binop_rhs_zero_vfloat(self._test_ibinop_type, op) |
| 456 | |
| 457 | def _test_ibinop_value_false(self, op): |
| 458 | self._test_binop_rhs_false(self._test_ibinop_value, op) |
| 459 | |
| 460 | def _test_ibinop_value_true(self, op): |
| 461 | self._test_binop_rhs_true(self._test_ibinop_value, op) |
| 462 | |
| 463 | def _test_ibinop_value_pos_int(self, op): |
| 464 | self._test_binop_rhs_pos_int(self._test_ibinop_value, op) |
| 465 | |
| 466 | def _test_ibinop_value_neg_int(self, op): |
| 467 | self._test_binop_rhs_neg_int(self._test_ibinop_value, op) |
| 468 | |
| 469 | def _test_ibinop_value_zero_int(self, op): |
| 470 | self._test_binop_rhs_zero_int(self._test_ibinop_value, op) |
| 471 | |
| 472 | def _test_ibinop_value_pos_vint(self, op): |
| 473 | self._test_binop_rhs_pos_vint(self._test_ibinop_value, op) |
| 474 | |
| 475 | def _test_ibinop_value_neg_vint(self, op): |
| 476 | self._test_binop_rhs_neg_vint(self._test_ibinop_value, op) |
| 477 | |
| 478 | def _test_ibinop_value_zero_vint(self, op): |
| 479 | self._test_binop_rhs_zero_vint(self._test_ibinop_value, op) |
| 480 | |
| 481 | def _test_ibinop_value_pos_float(self, op): |
| 482 | self._test_binop_rhs_pos_float(self._test_ibinop_value, op) |
| 483 | |
| 484 | def _test_ibinop_value_neg_float(self, op): |
| 485 | self._test_binop_rhs_neg_float(self._test_ibinop_value, op) |
| 486 | |
| 487 | def _test_ibinop_value_zero_float(self, op): |
| 488 | self._test_binop_rhs_zero_float(self._test_ibinop_value, op) |
| 489 | |
| 490 | def _test_ibinop_value_pos_vfloat(self, op): |
| 491 | self._test_binop_rhs_pos_vfloat(self._test_ibinop_value, op) |
| 492 | |
| 493 | def _test_ibinop_value_neg_vfloat(self, op): |
| 494 | self._test_binop_rhs_neg_vfloat(self._test_ibinop_value, op) |
| 495 | |
| 496 | def _test_ibinop_value_zero_vfloat(self, op): |
| 497 | self._test_binop_rhs_zero_vfloat(self._test_ibinop_value, op) |
| 498 | |
| 499 | def test_bool_op(self): |
| 500 | self.assertEqual(bool(self._def), bool(self._def_value)) |
| 501 | |
| 502 | def test_int_op(self): |
| 503 | self.assertEqual(int(self._def), int(self._def_value)) |
| 504 | |
| 505 | def test_float_op(self): |
| 506 | self.assertEqual(float(self._def), float(self._def_value)) |
| 507 | |
| 508 | def test_complex_op(self): |
| 509 | self.assertEqual(complex(self._def), complex(self._def_value)) |
| 510 | |
| 511 | def test_str_op(self): |
| 512 | self.assertEqual(str(self._def), str(self._def_value)) |
| 513 | |
| 514 | def test_eq_none(self): |
| 515 | self.assertFalse(self._def == None) |
| 516 | |
| 517 | def test_ne_none(self): |
| 518 | self.assertTrue(self._def != None) |
| 519 | |
| 520 | def test_is_set(self): |
| 521 | raw = self._def_value |
| 522 | field = self._fc() |
| 523 | self.assertFalse(field.is_set) |
| 524 | field.value = raw |
| 525 | self.assertTrue(field.is_set) |
| 526 | |
| 527 | def test_reset(self): |
| 528 | raw = self._def_value |
| 529 | field = self._fc() |
| 530 | field.value = raw |
| 531 | self.assertTrue(field.is_set) |
| 532 | field.reset() |
| 533 | self.assertFalse(field.is_set) |
| 534 | other = self._fc() |
| 535 | self.assertEqual(other, field) |
| 536 | |
| 537 | |
| 538 | _BINOPS = ( |
| 539 | ('lt', operator.lt), |
| 540 | ('le', operator.le), |
| 541 | ('eq', operator.eq), |
| 542 | ('ne', operator.ne), |
| 543 | ('ge', operator.ge), |
| 544 | ('gt', operator.gt), |
| 545 | ('add', operator.add), |
| 546 | ('radd', lambda a, b: operator.add(b, a)), |
| 547 | ('and', operator.and_), |
| 548 | ('rand', lambda a, b: operator.and_(b, a)), |
| 549 | ('floordiv', operator.floordiv), |
| 550 | ('rfloordiv', lambda a, b: operator.floordiv(b, a)), |
| 551 | ('lshift', operator.lshift), |
| 552 | ('rlshift', lambda a, b: operator.lshift(b, a)), |
| 553 | ('mod', operator.mod), |
| 554 | ('rmod', lambda a, b: operator.mod(b, a)), |
| 555 | ('mul', operator.mul), |
| 556 | ('rmul', lambda a, b: operator.mul(b, a)), |
| 557 | ('or', operator.or_), |
| 558 | ('ror', lambda a, b: operator.or_(b, a)), |
| 559 | ('pow', operator.pow), |
| 560 | ('rpow', lambda a, b: operator.pow(b, a)), |
| 561 | ('rshift', operator.rshift), |
| 562 | ('rrshift', lambda a, b: operator.rshift(b, a)), |
| 563 | ('sub', operator.sub), |
| 564 | ('rsub', lambda a, b: operator.sub(b, a)), |
| 565 | ('truediv', operator.truediv), |
| 566 | ('rtruediv', lambda a, b: operator.truediv(b, a)), |
| 567 | ('xor', operator.xor), |
| 568 | ('rxor', lambda a, b: operator.xor(b, a)), |
| 569 | ) |
| 570 | |
| 571 | |
| 572 | _IBINOPS = ( |
| 573 | ('iadd', operator.iadd), |
| 574 | ('iand', operator.iand), |
| 575 | ('ifloordiv', operator.ifloordiv), |
| 576 | ('ilshift', operator.ilshift), |
| 577 | ('imod', operator.imod), |
| 578 | ('imul', operator.imul), |
| 579 | ('ior', operator.ior), |
| 580 | ('ipow', operator.ipow), |
| 581 | ('irshift', operator.irshift), |
| 582 | ('isub', operator.isub), |
| 583 | ('itruediv', operator.itruediv), |
| 584 | ('ixor', operator.ixor), |
| 585 | ) |
| 586 | |
| 587 | |
| 588 | _UNARYOPS = ( |
| 589 | ('neg', operator.neg), |
| 590 | ('pos', operator.pos), |
| 591 | ('abs', operator.abs), |
| 592 | ('invert', operator.invert), |
| 593 | ('round', round), |
| 594 | ('round_0', partial(round, ndigits=0)), |
| 595 | ('round_1', partial(round, ndigits=1)), |
| 596 | ('round_2', partial(round, ndigits=2)), |
| 597 | ('round_3', partial(round, ndigits=3)), |
| 598 | ('ceil', math.ceil), |
| 599 | ('floor', math.floor), |
| 600 | ('trunc', math.trunc), |
| 601 | ) |
| 602 | |
| 603 | |
| 604 | def _inject_numeric_testing_methods(cls): |
| 605 | def test_binop_name(suffix): |
| 606 | return 'test_binop_{}_{}'.format(name, suffix) |
| 607 | |
| 608 | def test_ibinop_name(suffix): |
| 609 | return 'test_ibinop_{}_{}'.format(name, suffix) |
| 610 | |
| 611 | def test_unaryop_name(suffix): |
| 612 | return 'test_unaryop_{}_{}'.format(name, suffix) |
| 613 | |
| 614 | # inject testing methods for each binary operation |
| 615 | for name, binop in _BINOPS: |
| 616 | setattr(cls, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField._test_binop_invalid_unknown, op=binop)) |
| 617 | setattr(cls, test_binop_name('invalid_none'), partialmethod(_TestNumericField._test_binop_invalid_none, op=binop)) |
| 618 | setattr(cls, test_binop_name('type_true'), partialmethod(_TestNumericField._test_binop_type_true, op=binop)) |
| 619 | setattr(cls, test_binop_name('type_pos_int'), partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop)) |
| 620 | setattr(cls, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop)) |
| 621 | setattr(cls, test_binop_name('value_true'), partialmethod(_TestNumericField._test_binop_value_true, op=binop)) |
| 622 | setattr(cls, test_binop_name('value_pos_int'), partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop)) |
| 623 | setattr(cls, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop)) |
| 624 | setattr(cls, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop)) |
| 625 | setattr(cls, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop)) |
| 626 | setattr(cls, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop)) |
| 627 | setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop)) |
| 628 | setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_int, op=binop)) |
| 629 | setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop)) |
| 630 | setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop)) |
| 631 | setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop)) |
| 632 | setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop)) |
| 633 | setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop)) |
| 634 | setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop)) |
| 635 | setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop)) |
| 636 | setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_int, op=binop)) |
| 637 | setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop)) |
| 638 | setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericField._test_binop_type_false, op=binop)) |
| 639 | setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop)) |
| 640 | setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop)) |
| 641 | setattr(cls, test_binop_name('value_false'), partialmethod(_TestNumericField._test_binop_value_false, op=binop)) |
| 642 | setattr(cls, test_binop_name('value_zero_int'), partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop)) |
| 643 | setattr(cls, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop)) |
| 644 | setattr(cls, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop)) |
| 645 | setattr(cls, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop)) |
| 646 | setattr(cls, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop)) |
| 647 | setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop)) |
| 648 | setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_int, op=binop)) |
| 649 | setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop)) |
| 650 | setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop)) |
| 651 | setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop)) |
| 652 | setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop)) |
| 653 | setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop)) |
| 654 | setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop)) |
| 655 | setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop)) |
| 656 | setattr(cls, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop)) |
| 657 | setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop)) |
| 658 | setattr(cls, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop)) |
| 659 | setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop)) |
| 660 | setattr(cls, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop)) |
| 661 | setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop)) |
| 662 | setattr(cls, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_float, op=binop)) |
| 663 | setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_float, op=binop)) |
| 664 | setattr(cls, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop)) |
| 665 | setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop)) |
| 666 | setattr(cls, test_binop_name('type_zero_float'), partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop)) |
| 667 | setattr(cls, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop)) |
| 668 | setattr(cls, test_binop_name('value_zero_float'), partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop)) |
| 669 | setattr(cls, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop)) |
| 670 | setattr(cls, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop)) |
| 671 | setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop)) |
| 672 | setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_float, op=binop)) |
| 673 | setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop)) |
| 674 | |
| 675 | # inject testing methods for each unary operation |
| 676 | for name, unaryop in _UNARYOPS: |
| 677 | setattr(cls, test_unaryop_name('type'), partialmethod(_TestNumericField._test_unaryop_type, op=unaryop)) |
| 678 | setattr(cls, test_unaryop_name('value'), partialmethod(_TestNumericField._test_unaryop_value, op=unaryop)) |
| 679 | setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop)) |
| 680 | setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop)) |
| 681 | |
| 682 | # inject testing methods for each inplace binary operation |
| 683 | for name, ibinop in _IBINOPS: |
| 684 | setattr(cls, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField._test_ibinop_invalid_unknown, op=ibinop)) |
| 685 | setattr(cls, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField._test_ibinop_invalid_none, op=ibinop)) |
| 686 | setattr(cls, test_ibinop_name('type_true'), partialmethod(_TestNumericField._test_ibinop_type_true, op=ibinop)) |
| 687 | setattr(cls, test_ibinop_name('value_true'), partialmethod(_TestNumericField._test_ibinop_value_true, op=ibinop)) |
| 688 | setattr(cls, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField._test_ibinop_type_pos_int, op=ibinop)) |
| 689 | setattr(cls, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField._test_ibinop_type_pos_vint, op=ibinop)) |
| 690 | setattr(cls, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField._test_ibinop_value_pos_int, op=ibinop)) |
| 691 | setattr(cls, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField._test_ibinop_value_pos_vint, op=ibinop)) |
| 692 | setattr(cls, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField._test_ibinop_type_neg_int, op=ibinop)) |
| 693 | setattr(cls, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField._test_ibinop_type_neg_vint, op=ibinop)) |
| 694 | setattr(cls, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField._test_ibinop_value_neg_int, op=ibinop)) |
| 695 | setattr(cls, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField._test_ibinop_value_neg_vint, op=ibinop)) |
| 696 | setattr(cls, test_ibinop_name('type_false'), partialmethod(_TestNumericField._test_ibinop_type_false, op=ibinop)) |
| 697 | setattr(cls, test_ibinop_name('value_false'), partialmethod(_TestNumericField._test_ibinop_value_false, op=ibinop)) |
| 698 | setattr(cls, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField._test_ibinop_type_zero_int, op=ibinop)) |
| 699 | setattr(cls, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField._test_ibinop_type_zero_vint, op=ibinop)) |
| 700 | setattr(cls, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField._test_ibinop_value_zero_int, op=ibinop)) |
| 701 | setattr(cls, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField._test_ibinop_value_zero_vint, op=ibinop)) |
| 702 | setattr(cls, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField._test_ibinop_type_pos_float, op=ibinop)) |
| 703 | setattr(cls, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField._test_ibinop_type_neg_float, op=ibinop)) |
| 704 | setattr(cls, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_pos_vfloat, op=ibinop)) |
| 705 | setattr(cls, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_neg_vfloat, op=ibinop)) |
| 706 | setattr(cls, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField._test_ibinop_value_pos_float, op=ibinop)) |
| 707 | setattr(cls, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField._test_ibinop_value_neg_float, op=ibinop)) |
| 708 | setattr(cls, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_pos_vfloat, op=ibinop)) |
| 709 | setattr(cls, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_neg_vfloat, op=ibinop)) |
| 710 | setattr(cls, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField._test_ibinop_type_zero_float, op=ibinop)) |
| 711 | setattr(cls, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_zero_vfloat, op=ibinop)) |
| 712 | setattr(cls, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField._test_ibinop_value_zero_float, op=ibinop)) |
| 713 | setattr(cls, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_zero_vfloat, op=ibinop)) |
| 714 | |
| 715 | |
| 716 | class _TestIntegerFieldCommon(_TestNumericField): |
| 717 | def test_assign_true(self): |
| 718 | raw = True |
| 719 | self._def.value = raw |
| 720 | self.assertEqual(self._def, raw) |
| 721 | |
| 722 | def test_assign_false(self): |
| 723 | raw = False |
| 724 | self._def.value = raw |
| 725 | self.assertEqual(self._def, raw) |
| 726 | |
| 727 | def test_assign_pos_int(self): |
| 728 | raw = 477 |
| 729 | self._def.value = raw |
| 730 | self.assertEqual(self._def, raw) |
| 731 | |
| 732 | def test_assign_neg_int(self): |
| 733 | raw = -13 |
| 734 | self._def.value = raw |
| 735 | self.assertEqual(self._def, raw) |
| 736 | |
| 737 | def test_assign_int_field(self): |
| 738 | raw = 999 |
| 739 | field = self._fc() |
| 740 | field.value = raw |
| 741 | self._def.value = field |
| 742 | self.assertEqual(self._def, raw) |
| 743 | |
| 744 | def test_assign_float(self): |
| 745 | raw = 123.456 |
| 746 | self._def.value = raw |
| 747 | self.assertEqual(self._def, int(raw)) |
| 748 | |
| 749 | def test_assign_invalid_type(self): |
| 750 | with self.assertRaises(TypeError): |
| 751 | self._def.value = 'yes' |
| 752 | |
| 753 | def test_assign_uint(self): |
| 754 | fc = bt2.IntegerFieldClass(size=32, is_signed=False) |
| 755 | field = fc() |
| 756 | raw = 1777 |
| 757 | field.value = 1777 |
| 758 | self.assertEqual(field, raw) |
| 759 | |
| 760 | def test_assign_uint_invalid_neg(self): |
| 761 | fc = bt2.IntegerFieldClass(size=32, is_signed=False) |
| 762 | field = fc() |
| 763 | |
| 764 | with self.assertRaises(ValueError): |
| 765 | field.value = -23 |
| 766 | |
| 767 | def test_str_op(self): |
| 768 | self.assertEqual(str(self._def), str(self._def_value)) |
| 769 | |
| 770 | def test_str_op_unset(self): |
| 771 | self.assertEqual(str(self._fc()), 'Unset') |
| 772 | |
| 773 | |
| 774 | _inject_numeric_testing_methods(_TestIntegerFieldCommon) |
| 775 | |
| 776 | |
| 777 | @unittest.skip("this is broken") |
| 778 | class IntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): |
| 779 | def setUp(self): |
| 780 | self._fc = bt2.IntegerFieldClass(25, is_signed=True) |
| 781 | self._field = self._fc() |
| 782 | self._def = self._fc() |
| 783 | self._def.value = 17 |
| 784 | self._def_value = 17 |
| 785 | self._def_new_value = -101 |
| 786 | |
| 787 | def tearDown(self): |
| 788 | del self._fc |
| 789 | del self._field |
| 790 | del self._def |
| 791 | |
| 792 | |
| 793 | @unittest.skip("this is broken") |
| 794 | class EnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): |
| 795 | def setUp(self): |
| 796 | self._fc = bt2.EnumerationFieldClass(size=32, is_signed=True) |
| 797 | self._fc.add_mapping('whole range', -(2 ** 31), (2 ** 31) - 1) |
| 798 | self._fc.add_mapping('something', 17) |
| 799 | self._fc.add_mapping('speaker', 12, 16) |
| 800 | self._fc.add_mapping('can', 18, 2540) |
| 801 | self._fc.add_mapping('zip', -45, 1001) |
| 802 | self._def = self._fc() |
| 803 | self._def.value = 17 |
| 804 | self._def_value = 17 |
| 805 | self._def_new_value = -101 |
| 806 | |
| 807 | def tearDown(self): |
| 808 | del self._fc |
| 809 | del self._def |
| 810 | |
| 811 | def test_mappings(self): |
| 812 | mappings = ( |
| 813 | ('whole range', -(2 ** 31), (2 ** 31) - 1), |
| 814 | ('something', 17, 17), |
| 815 | ('zip', -45, 1001), |
| 816 | ) |
| 817 | |
| 818 | total = 0 |
| 819 | index_set = set() |
| 820 | |
| 821 | for fm in self._def.mappings: |
| 822 | total += 1 |
| 823 | for index, mapping in enumerate(mappings): |
| 824 | if fm.name == mapping[0] and fm.lower == mapping[1] and fm.upper == mapping[2]: |
| 825 | index_set.add(index) |
| 826 | |
| 827 | self.assertEqual(total, 3) |
| 828 | self.assertTrue(0 in index_set and 1 in index_set and 2 in index_set) |
| 829 | |
| 830 | def test_str_op(self): |
| 831 | expected_string_found = False |
| 832 | s = str(self._def) |
| 833 | |
| 834 | # Establish all permutations of the three expected matches since |
| 835 | # the order in which mappings are enumerated is not explicitly part of |
| 836 | # the API. |
| 837 | for p in itertools.permutations(["'whole range'", "'something'", |
| 838 | "'zip'"]): |
| 839 | candidate = '{} ({})'.format(self._def_value, ', '.join(p)) |
| 840 | if candidate == s: |
| 841 | expected_string_found = True |
| 842 | break |
| 843 | |
| 844 | self.assertTrue(expected_string_found) |
| 845 | |
| 846 | def test_str_op_unset(self): |
| 847 | self.assertEqual(str(self._fc()), 'Unset') |
| 848 | |
| 849 | |
| 850 | @unittest.skip("this is broken") |
| 851 | class FloatingPointNumberFieldTestCase(_TestNumericField, unittest.TestCase): |
| 852 | def setUp(self): |
| 853 | self._fc = bt2.FloatingPointNumberFieldClass() |
| 854 | self._field = self._fc() |
| 855 | self._def = self._fc() |
| 856 | self._def.value = 52.7 |
| 857 | self._def_value = 52.7 |
| 858 | self._def_new_value = -17.164857 |
| 859 | |
| 860 | def tearDown(self): |
| 861 | del self._fc |
| 862 | del self._field |
| 863 | del self._def |
| 864 | |
| 865 | def _test_invalid_op(self, cb): |
| 866 | with self.assertRaises(TypeError): |
| 867 | cb() |
| 868 | |
| 869 | def test_assign_true(self): |
| 870 | self._def.value = True |
| 871 | self.assertTrue(self._def) |
| 872 | |
| 873 | def test_assign_false(self): |
| 874 | self._def.value = False |
| 875 | self.assertFalse(self._def) |
| 876 | |
| 877 | def test_assign_pos_int(self): |
| 878 | raw = 477 |
| 879 | self._def.value = raw |
| 880 | self.assertEqual(self._def, float(raw)) |
| 881 | |
| 882 | def test_assign_neg_int(self): |
| 883 | raw = -13 |
| 884 | self._def.value = raw |
| 885 | self.assertEqual(self._def, float(raw)) |
| 886 | |
| 887 | def test_assign_int_field(self): |
| 888 | fc = bt2.IntegerFieldClass(32) |
| 889 | field = fc() |
| 890 | raw = 999 |
| 891 | field.value = raw |
| 892 | self._def.value = field |
| 893 | self.assertEqual(self._def, float(raw)) |
| 894 | |
| 895 | def test_assign_float(self): |
| 896 | raw = -19.23 |
| 897 | self._def.value = raw |
| 898 | self.assertEqual(self._def, raw) |
| 899 | |
| 900 | def test_assign_float_field(self): |
| 901 | fc = bt2.FloatingPointNumberFieldClass(32) |
| 902 | field = fc() |
| 903 | raw = 101.32 |
| 904 | field.value = raw |
| 905 | self._def.value = field |
| 906 | self.assertEqual(self._def, raw) |
| 907 | |
| 908 | def test_assign_invalid_type(self): |
| 909 | with self.assertRaises(TypeError): |
| 910 | self._def.value = 'yes' |
| 911 | |
| 912 | def test_invalid_lshift(self): |
| 913 | self._test_invalid_op(lambda: self._def << 23) |
| 914 | |
| 915 | def test_invalid_rshift(self): |
| 916 | self._test_invalid_op(lambda: self._def >> 23) |
| 917 | |
| 918 | def test_invalid_and(self): |
| 919 | self._test_invalid_op(lambda: self._def & 23) |
| 920 | |
| 921 | def test_invalid_or(self): |
| 922 | self._test_invalid_op(lambda: self._def | 23) |
| 923 | |
| 924 | def test_invalid_xor(self): |
| 925 | self._test_invalid_op(lambda: self._def ^ 23) |
| 926 | |
| 927 | def test_invalid_invert(self): |
| 928 | self._test_invalid_op(lambda: ~self._def) |
| 929 | |
| 930 | def test_str_op(self): |
| 931 | self.assertEqual(str(self._def), str(self._def_value)) |
| 932 | |
| 933 | def test_str_op_unset(self): |
| 934 | self.assertEqual(str(self._fc()), 'Unset') |
| 935 | |
| 936 | _inject_numeric_testing_methods(FloatingPointNumberFieldTestCase) |
| 937 | |
| 938 | |
| 939 | @unittest.skip("this is broken") |
| 940 | class StringFieldTestCase(_TestCopySimple, unittest.TestCase): |
| 941 | def setUp(self): |
| 942 | self._fc = bt2.StringFieldClass() |
| 943 | self._def_value = 'Hello, World!' |
| 944 | self._def = self._fc() |
| 945 | self._def.value = self._def_value |
| 946 | self._def_new_value = 'Yes!' |
| 947 | |
| 948 | def tearDown(self): |
| 949 | del self._fc |
| 950 | del self._def |
| 951 | |
| 952 | def test_assign_int(self): |
| 953 | with self.assertRaises(TypeError): |
| 954 | self._def.value = 283 |
| 955 | |
| 956 | def test_assign_string_field(self): |
| 957 | fc = bt2.StringFieldClass() |
| 958 | field = fc() |
| 959 | raw = 'zorg' |
| 960 | field.value = raw |
| 961 | self.assertEqual(field, raw) |
| 962 | |
| 963 | def test_eq(self): |
| 964 | self.assertEqual(self._def, self._def_value) |
| 965 | |
| 966 | def test_eq(self): |
| 967 | self.assertNotEqual(self._def, 23) |
| 968 | |
| 969 | def test_lt_vstring(self): |
| 970 | s1 = self._fc() |
| 971 | s1.value = 'allo' |
| 972 | s2 = self._fc() |
| 973 | s2.value = 'bateau' |
| 974 | self.assertLess(s1, s2) |
| 975 | |
| 976 | def test_lt_string(self): |
| 977 | s1 = self._fc() |
| 978 | s1.value = 'allo' |
| 979 | self.assertLess(s1, 'bateau') |
| 980 | |
| 981 | def test_le_vstring(self): |
| 982 | s1 = self._fc() |
| 983 | s1.value = 'allo' |
| 984 | s2 = self._fc() |
| 985 | s2.value = 'bateau' |
| 986 | self.assertLessEqual(s1, s2) |
| 987 | |
| 988 | def test_le_string(self): |
| 989 | s1 = self._fc() |
| 990 | s1.value = 'allo' |
| 991 | self.assertLessEqual(s1, 'bateau') |
| 992 | |
| 993 | def test_gt_vstring(self): |
| 994 | s1 = self._fc() |
| 995 | s1.value = 'allo' |
| 996 | s2 = self._fc() |
| 997 | s2.value = 'bateau' |
| 998 | self.assertGreater(s2, s1) |
| 999 | |
| 1000 | def test_gt_string(self): |
| 1001 | s1 = self._fc() |
| 1002 | s1.value = 'allo' |
| 1003 | self.assertGreater('bateau', s1) |
| 1004 | |
| 1005 | def test_ge_vstring(self): |
| 1006 | s1 = self._fc() |
| 1007 | s1.value = 'allo' |
| 1008 | s2 = self._fc() |
| 1009 | s2.value = 'bateau' |
| 1010 | self.assertGreaterEqual(s2, s1) |
| 1011 | |
| 1012 | def test_ge_string(self): |
| 1013 | s1 = self._fc() |
| 1014 | s1.value = 'allo' |
| 1015 | self.assertGreaterEqual('bateau', s1) |
| 1016 | |
| 1017 | def test_bool_op(self): |
| 1018 | self.assertEqual(bool(self._def), bool(self._def_value)) |
| 1019 | |
| 1020 | def test_str_op(self): |
| 1021 | self.assertEqual(str(self._def), str(self._def_value)) |
| 1022 | |
| 1023 | def test_str_op_unset(self): |
| 1024 | self.assertEqual(str(self._fc()), 'Unset') |
| 1025 | |
| 1026 | def test_len(self): |
| 1027 | self.assertEqual(len(self._def), len(self._def_value)) |
| 1028 | |
| 1029 | def test_getitem(self): |
| 1030 | self.assertEqual(self._def[5], self._def_value[5]) |
| 1031 | |
| 1032 | def test_append_str(self): |
| 1033 | to_append = 'meow meow meow' |
| 1034 | self._def += to_append |
| 1035 | self._def_value += to_append |
| 1036 | self.assertEqual(self._def, self._def_value) |
| 1037 | |
| 1038 | def test_append_string_field(self): |
| 1039 | fc = bt2.StringFieldClass() |
| 1040 | field = fc() |
| 1041 | to_append = 'meow meow meow' |
| 1042 | field.value = to_append |
| 1043 | self._def += field |
| 1044 | self._def_value += to_append |
| 1045 | self.assertEqual(self._def, self._def_value) |
| 1046 | |
| 1047 | def test_is_set(self): |
| 1048 | raw = self._def_value |
| 1049 | field = self._fc() |
| 1050 | self.assertFalse(field.is_set) |
| 1051 | field.value = raw |
| 1052 | self.assertTrue(field.is_set) |
| 1053 | |
| 1054 | def test_reset(self): |
| 1055 | raw = self._def_value |
| 1056 | field = self._fc() |
| 1057 | field.value = raw |
| 1058 | self.assertTrue(field.is_set) |
| 1059 | field.reset() |
| 1060 | self.assertFalse(field.is_set) |
| 1061 | other = self._fc() |
| 1062 | self.assertEqual(other, field) |
| 1063 | |
| 1064 | |
| 1065 | class _TestArraySequenceFieldCommon(_TestCopySimple): |
| 1066 | def _modify_def(self): |
| 1067 | self._def[2] = 23 |
| 1068 | |
| 1069 | def test_bool_op_true(self): |
| 1070 | self.assertTrue(self._def) |
| 1071 | |
| 1072 | def test_len(self): |
| 1073 | self.assertEqual(len(self._def), 3) |
| 1074 | |
| 1075 | def test_getitem(self): |
| 1076 | field = self._def[1] |
| 1077 | self.assertIs(type(field), bt2.fields._IntegerField) |
| 1078 | self.assertEqual(field, 1847) |
| 1079 | |
| 1080 | def test_eq(self): |
| 1081 | fc = bt2.ArrayFieldClass(self._elem_fc, 3) |
| 1082 | field = fc() |
| 1083 | field[0] = 45 |
| 1084 | field[1] = 1847 |
| 1085 | field[2] = 1948754 |
| 1086 | self.assertEqual(self._def, field) |
| 1087 | |
| 1088 | def test_eq_invalid_type(self): |
| 1089 | self.assertNotEqual(self._def, 23) |
| 1090 | |
| 1091 | def test_eq_diff_len(self): |
| 1092 | fc = bt2.ArrayFieldClass(self._elem_fc, 2) |
| 1093 | field = fc() |
| 1094 | field[0] = 45 |
| 1095 | field[1] = 1847 |
| 1096 | self.assertNotEqual(self._def, field) |
| 1097 | |
| 1098 | def test_eq_diff_content_same_len(self): |
| 1099 | fc = bt2.ArrayFieldClass(self._elem_fc, 3) |
| 1100 | field = fc() |
| 1101 | field[0] = 45 |
| 1102 | field[1] = 1846 |
| 1103 | field[2] = 1948754 |
| 1104 | self.assertNotEqual(self._def, field) |
| 1105 | |
| 1106 | def test_setitem(self): |
| 1107 | self._def[2] = 24 |
| 1108 | self.assertEqual(self._def[2], 24) |
| 1109 | |
| 1110 | def test_setitem_int_field(self): |
| 1111 | int_field = self._elem_fc() |
| 1112 | int_field.value = 19487 |
| 1113 | self._def[1] = int_field |
| 1114 | self.assertEqual(self._def[1], 19487) |
| 1115 | |
| 1116 | def test_setitem_non_basic_field(self): |
| 1117 | elem_fc = bt2.StructureFieldClass() |
| 1118 | array_fc = bt2.ArrayFieldClass(elem_fc, 3) |
| 1119 | elem_field = elem_fc() |
| 1120 | array_field = array_fc() |
| 1121 | |
| 1122 | with self.assertRaises(TypeError): |
| 1123 | array_field[1] = 23 |
| 1124 | |
| 1125 | def test_setitem_none(self): |
| 1126 | with self.assertRaises(TypeError): |
| 1127 | self._def[1] = None |
| 1128 | |
| 1129 | def test_setitem_index_wrong_type(self): |
| 1130 | with self.assertRaises(TypeError): |
| 1131 | self._def['yes'] = 23 |
| 1132 | |
| 1133 | def test_setitem_index_neg(self): |
| 1134 | with self.assertRaises(IndexError): |
| 1135 | self._def[-2] = 23 |
| 1136 | |
| 1137 | def test_setitem_index_out_of_range(self): |
| 1138 | with self.assertRaises(IndexError): |
| 1139 | self._def[len(self._def)] = 134679 |
| 1140 | |
| 1141 | def test_iter(self): |
| 1142 | for field, value in zip(self._def, (45, 1847, 1948754)): |
| 1143 | self.assertEqual(field, value) |
| 1144 | |
| 1145 | def test_value_int_field(self): |
| 1146 | values = [45646, 145, 12145] |
| 1147 | self._def.value = values |
| 1148 | self.assertEqual(values, self._def) |
| 1149 | |
| 1150 | def test_value_unset(self): |
| 1151 | values = [45646, None, 12145] |
| 1152 | self._def.value = values |
| 1153 | self.assertFalse(self._def[1].is_set) |
| 1154 | |
| 1155 | def test_value_rollback(self): |
| 1156 | values = [45, 1847, 1948754] |
| 1157 | # value is out of range, should not affect those we set previously |
| 1158 | with self.assertRaises(bt2.Error): |
| 1159 | self._def[2].value = 2**60 |
| 1160 | self.assertEqual(values, self._def) |
| 1161 | |
| 1162 | def test_value_check_sequence(self): |
| 1163 | values = 42 |
| 1164 | with self.assertRaises(TypeError): |
| 1165 | self._def.value = values |
| 1166 | |
| 1167 | def test_value_wrong_type_in_sequence(self): |
| 1168 | values = [32, 'hello', 11] |
| 1169 | with self.assertRaises(TypeError): |
| 1170 | self._def.value = values |
| 1171 | |
| 1172 | def test_value_complex_type(self): |
| 1173 | struct_fc = bt2.StructureFieldClass() |
| 1174 | int_fc = bt2.IntegerFieldClass(32) |
| 1175 | str_fc = bt2.StringFieldClass() |
| 1176 | struct_fc.append_field(field_class=int_fc, name='an_int') |
| 1177 | struct_fc.append_field(field_class=str_fc, name='a_string') |
| 1178 | struct_fc.append_field(field_class=int_fc, name='another_int') |
| 1179 | array_fc = bt2.ArrayFieldClass(struct_fc, 3) |
| 1180 | values = [ |
| 1181 | { |
| 1182 | 'an_int': 42, |
| 1183 | 'a_string': 'hello', |
| 1184 | 'another_int': 66 |
| 1185 | }, |
| 1186 | { |
| 1187 | 'an_int': 1, |
| 1188 | 'a_string': 'goodbye', |
| 1189 | 'another_int': 488 |
| 1190 | }, |
| 1191 | { |
| 1192 | 'an_int': 156, |
| 1193 | 'a_string': 'or not', |
| 1194 | 'another_int': 4648 |
| 1195 | }, |
| 1196 | ] |
| 1197 | |
| 1198 | array = array_fc() |
| 1199 | array.value = values |
| 1200 | self.assertEqual(values, array) |
| 1201 | values[0]['an_int'] = 'a string' |
| 1202 | with self.assertRaises(TypeError): |
| 1203 | array.value = values |
| 1204 | |
| 1205 | def test_is_set(self): |
| 1206 | raw = self._def_value |
| 1207 | field = self._fc() |
| 1208 | self.assertFalse(field.is_set) |
| 1209 | field.value = raw |
| 1210 | self.assertTrue(field.is_set) |
| 1211 | |
| 1212 | def test_reset(self): |
| 1213 | raw = self._def_value |
| 1214 | field = self._fc() |
| 1215 | field.value = raw |
| 1216 | self.assertTrue(field.is_set) |
| 1217 | field.reset() |
| 1218 | self.assertFalse(field.is_set) |
| 1219 | other = self._fc() |
| 1220 | self.assertEqual(other, field) |
| 1221 | |
| 1222 | def test_str_op(self): |
| 1223 | s = str(self._def) |
| 1224 | expected_string = '[{}]'.format(', '.join( |
| 1225 | [repr(v) for v in self._def_value])) |
| 1226 | self.assertEqual(expected_string, s) |
| 1227 | |
| 1228 | def test_str_op_unset(self): |
| 1229 | self.assertEqual(str(self._fc()), 'Unset') |
| 1230 | |
| 1231 | |
| 1232 | @unittest.skip("this is broken") |
| 1233 | class ArrayFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase): |
| 1234 | def setUp(self): |
| 1235 | self._elem_fc = bt2.IntegerFieldClass(32) |
| 1236 | self._fc = bt2.ArrayFieldClass(self._elem_fc, 3) |
| 1237 | self._def = self._fc() |
| 1238 | self._def[0] = 45 |
| 1239 | self._def[1] = 1847 |
| 1240 | self._def[2] = 1948754 |
| 1241 | self._def_value = [45, 1847, 1948754] |
| 1242 | |
| 1243 | def tearDown(self): |
| 1244 | del self._elem_fc |
| 1245 | del self._fc |
| 1246 | del self._def |
| 1247 | |
| 1248 | def test_value_wrong_len(self): |
| 1249 | values = [45, 1847] |
| 1250 | with self.assertRaises(ValueError): |
| 1251 | self._def.value = values |
| 1252 | |
| 1253 | |
| 1254 | @unittest.skip("this is broken") |
| 1255 | class SequenceFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase): |
| 1256 | def setUp(self): |
| 1257 | self._elem_fc = bt2.IntegerFieldClass(32) |
| 1258 | self._fc = bt2.SequenceFieldClass(self._elem_fc, 'the.length') |
| 1259 | self._def = self._fc() |
| 1260 | self._length_field = self._elem_fc(3) |
| 1261 | self._def.length_field = self._length_field |
| 1262 | self._def[0] = 45 |
| 1263 | self._def[1] = 1847 |
| 1264 | self._def[2] = 1948754 |
| 1265 | self._def_value = [45, 1847, 1948754] |
| 1266 | |
| 1267 | def tearDown(self): |
| 1268 | del self._elem_fc |
| 1269 | del self._fc |
| 1270 | del self._def |
| 1271 | del self._length_field |
| 1272 | |
| 1273 | def test_value_resize(self): |
| 1274 | new_values = [1, 2, 3, 4] |
| 1275 | self._def.value = new_values |
| 1276 | self.assertCountEqual(self._def, new_values) |
| 1277 | |
| 1278 | def test_value_resize_rollback(self): |
| 1279 | with self.assertRaises(TypeError): |
| 1280 | self._def.value = [1, 2, 3, 'unexpected string'] |
| 1281 | self.assertEqual(self._def, self._def_value) |
| 1282 | |
| 1283 | self._def.reset() |
| 1284 | with self.assertRaises(TypeError): |
| 1285 | self._def.value = [1, 2, 3, 'unexpected string'] |
| 1286 | self.assertFalse(self._def.is_set) |
| 1287 | |
| 1288 | |
| 1289 | @unittest.skip("this is broken") |
| 1290 | class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): |
| 1291 | def setUp(self): |
| 1292 | self._fc0 = bt2.IntegerFieldClass(32, is_signed=True) |
| 1293 | self._fc1 = bt2.StringFieldClass() |
| 1294 | self._fc2 = bt2.FloatingPointNumberFieldClass() |
| 1295 | self._fc3 = bt2.IntegerFieldClass(17) |
| 1296 | self._fc = bt2.StructureFieldClass() |
| 1297 | self._fc.append_field('A', self._fc0) |
| 1298 | self._fc.append_field('B', self._fc1) |
| 1299 | self._fc.append_field('C', self._fc2) |
| 1300 | self._fc.append_field('D', self._fc3) |
| 1301 | self._def = self._fc() |
| 1302 | self._def['A'] = -1872 |
| 1303 | self._def['B'] = 'salut' |
| 1304 | self._def['C'] = 17.5 |
| 1305 | self._def['D'] = 16497 |
| 1306 | self._def_value = { |
| 1307 | 'A': -1872, |
| 1308 | 'B': 'salut', |
| 1309 | 'C': 17.5, |
| 1310 | 'D': 16497 |
| 1311 | } |
| 1312 | |
| 1313 | def tearDown(self): |
| 1314 | del self._fc0 |
| 1315 | del self._fc1 |
| 1316 | del self._fc2 |
| 1317 | del self._fc3 |
| 1318 | del self._fc |
| 1319 | del self._def |
| 1320 | |
| 1321 | def _modify_def(self): |
| 1322 | self._def['B'] = 'hola' |
| 1323 | |
| 1324 | def test_bool_op_true(self): |
| 1325 | self.assertTrue(self._def) |
| 1326 | |
| 1327 | def test_bool_op_false(self): |
| 1328 | fc = bt2.StructureFieldClass() |
| 1329 | field = fc() |
| 1330 | self.assertFalse(field) |
| 1331 | |
| 1332 | def test_len(self): |
| 1333 | self.assertEqual(len(self._def), 4) |
| 1334 | |
| 1335 | def test_getitem(self): |
| 1336 | field = self._def['A'] |
| 1337 | self.assertIs(type(field), bt2.fields._IntegerField) |
| 1338 | self.assertEqual(field, -1872) |
| 1339 | |
| 1340 | def test_at_index_out_of_bounds_after(self): |
| 1341 | with self.assertRaises(IndexError): |
| 1342 | self._def.at_index(len(self._fc)) |
| 1343 | |
| 1344 | def test_eq(self): |
| 1345 | fc = bt2.StructureFieldClass() |
| 1346 | fc.append_field('A', self._fc0) |
| 1347 | fc.append_field('B', self._fc1) |
| 1348 | fc.append_field('C', self._fc2) |
| 1349 | fc.append_field('D', self._fc3) |
| 1350 | field = fc() |
| 1351 | field['A'] = -1872 |
| 1352 | field['B'] = 'salut' |
| 1353 | field['C'] = 17.5 |
| 1354 | field['D'] = 16497 |
| 1355 | self.assertEqual(self._def, field) |
| 1356 | |
| 1357 | def test_eq_invalid_type(self): |
| 1358 | self.assertNotEqual(self._def, 23) |
| 1359 | |
| 1360 | def test_eq_diff_len(self): |
| 1361 | fc = bt2.StructureFieldClass() |
| 1362 | fc.append_field('A', self._fc0) |
| 1363 | fc.append_field('B', self._fc1) |
| 1364 | fc.append_field('C', self._fc2) |
| 1365 | field = fc() |
| 1366 | field['A'] = -1872 |
| 1367 | field['B'] = 'salut' |
| 1368 | field['C'] = 17.5 |
| 1369 | self.assertNotEqual(self._def, field) |
| 1370 | |
| 1371 | def test_eq_diff_content_same_len(self): |
| 1372 | fc = bt2.StructureFieldClass() |
| 1373 | fc.append_field('A', self._fc0) |
| 1374 | fc.append_field('B', self._fc1) |
| 1375 | fc.append_field('C', self._fc2) |
| 1376 | fc.append_field('D', self._fc3) |
| 1377 | field = fc() |
| 1378 | field['A'] = -1872 |
| 1379 | field['B'] = 'salut' |
| 1380 | field['C'] = 17.4 |
| 1381 | field['D'] = 16497 |
| 1382 | self.assertNotEqual(self._def, field) |
| 1383 | |
| 1384 | def test_eq_same_content_diff_keys(self): |
| 1385 | fc = bt2.StructureFieldClass() |
| 1386 | fc.append_field('A', self._fc0) |
| 1387 | fc.append_field('B', self._fc1) |
| 1388 | fc.append_field('E', self._fc2) |
| 1389 | fc.append_field('D', self._fc3) |
| 1390 | field = fc() |
| 1391 | field['A'] = -1872 |
| 1392 | field['B'] = 'salut' |
| 1393 | field['E'] = 17.4 |
| 1394 | field['D'] = 16497 |
| 1395 | self.assertNotEqual(self._def, field) |
| 1396 | |
| 1397 | def test_setitem(self): |
| 1398 | self._def['C'] = -18.47 |
| 1399 | self.assertEqual(self._def['C'], -18.47) |
| 1400 | |
| 1401 | def test_setitem_int_field(self): |
| 1402 | int_fc = bt2.IntegerFieldClass(16) |
| 1403 | int_field = int_fc() |
| 1404 | int_field.value = 19487 |
| 1405 | self._def['D'] = int_field |
| 1406 | self.assertEqual(self._def['D'], 19487) |
| 1407 | |
| 1408 | def test_setitem_non_basic_field(self): |
| 1409 | elem_fc = bt2.StructureFieldClass() |
| 1410 | elem_field = elem_fc() |
| 1411 | struct_fc = bt2.StructureFieldClass() |
| 1412 | struct_fc.append_field('A', elem_fc) |
| 1413 | struct_field = struct_fc() |
| 1414 | |
| 1415 | # Will fail on access to .items() of the value |
| 1416 | with self.assertRaises(AttributeError): |
| 1417 | struct_field['A'] = 23 |
| 1418 | |
| 1419 | def test_setitem_none(self): |
| 1420 | with self.assertRaises(TypeError): |
| 1421 | self._def['C'] = None |
| 1422 | |
| 1423 | def test_setitem_key_wrong_type(self): |
| 1424 | with self.assertRaises(TypeError): |
| 1425 | self._def[3] = 23 |
| 1426 | |
| 1427 | def test_setitem_wrong_key(self): |
| 1428 | with self.assertRaises(KeyError): |
| 1429 | self._def['hi'] = 134679 |
| 1430 | |
| 1431 | def test_at_index(self): |
| 1432 | self.assertEqual(self._def.at_index(1), 'salut') |
| 1433 | |
| 1434 | def test_iter(self): |
| 1435 | orig_values = { |
| 1436 | 'A': -1872, |
| 1437 | 'B': 'salut', |
| 1438 | 'C': 17.5, |
| 1439 | 'D': 16497, |
| 1440 | } |
| 1441 | |
| 1442 | for vkey, vval in self._def.items(): |
| 1443 | val = orig_values[vkey] |
| 1444 | self.assertEqual(vval, val) |
| 1445 | |
| 1446 | def test_value(self): |
| 1447 | orig_values = { |
| 1448 | 'A': -1872, |
| 1449 | 'B': 'salut', |
| 1450 | 'C': 17.5, |
| 1451 | 'D': 16497, |
| 1452 | } |
| 1453 | self.assertEqual(self._def, orig_values) |
| 1454 | |
| 1455 | def test_set_value(self): |
| 1456 | int_fc = bt2.IntegerFieldClass(32) |
| 1457 | str_fc = bt2.StringFieldClass() |
| 1458 | struct_fc = bt2.StructureFieldClass() |
| 1459 | struct_fc.append_field(field_class=int_fc, name='an_int') |
| 1460 | struct_fc.append_field(field_class=str_fc, name='a_string') |
| 1461 | struct_fc.append_field(field_class=int_fc, name='another_int') |
| 1462 | values = { |
| 1463 | 'an_int': 42, |
| 1464 | 'a_string': 'hello', |
| 1465 | 'another_int': 66 |
| 1466 | } |
| 1467 | |
| 1468 | struct = struct_fc() |
| 1469 | struct.value = values |
| 1470 | self.assertEqual(values, struct) |
| 1471 | |
| 1472 | bad_type_values = copy.deepcopy(values) |
| 1473 | bad_type_values['an_int'] = 'a string' |
| 1474 | with self.assertRaises(TypeError): |
| 1475 | struct.value = bad_type_values |
| 1476 | |
| 1477 | unknown_key_values = copy.deepcopy(values) |
| 1478 | unknown_key_values['unknown_key'] = 16546 |
| 1479 | with self.assertRaises(KeyError): |
| 1480 | struct.value = unknown_key_values |
| 1481 | |
| 1482 | def test_value_rollback(self): |
| 1483 | int_fc = bt2.IntegerFieldClass(32) |
| 1484 | str_fc = bt2.StringFieldClass() |
| 1485 | struct_fc = bt2.StructureFieldClass() |
| 1486 | struct_fc.append_field(field_class=int_fc, name='an_int') |
| 1487 | struct_fc.append_field(field_class=str_fc, name='a_string') |
| 1488 | struct_fc.append_field(field_class=int_fc, name='another_int') |
| 1489 | values = { |
| 1490 | 'an_int': 42, |
| 1491 | 'a_string': 'hello', |
| 1492 | 'another_int': 66 |
| 1493 | } |
| 1494 | |
| 1495 | def test_is_set(self): |
| 1496 | values = { |
| 1497 | 'an_int': 42, |
| 1498 | 'a_string': 'hello', |
| 1499 | 'another_int': 66 |
| 1500 | } |
| 1501 | |
| 1502 | int_fc = bt2.IntegerFieldClass(32) |
| 1503 | str_fc = bt2.StringFieldClass() |
| 1504 | struct_fc = bt2.StructureFieldClass() |
| 1505 | struct_fc.append_field(field_class=int_fc, name='an_int') |
| 1506 | struct_fc.append_field(field_class=str_fc, name='a_string') |
| 1507 | struct_fc.append_field(field_class=int_fc, name='another_int') |
| 1508 | |
| 1509 | struct = struct_fc() |
| 1510 | self.assertFalse(struct.is_set) |
| 1511 | struct.value = values |
| 1512 | self.assertTrue(struct.is_set) |
| 1513 | |
| 1514 | struct = struct_fc() |
| 1515 | struct['an_int'].value = 42 |
| 1516 | self.assertFalse(struct.is_set) |
| 1517 | |
| 1518 | def test_reset(self): |
| 1519 | values = { |
| 1520 | 'an_int': 42, |
| 1521 | 'a_string': 'hello', |
| 1522 | 'another_int': 66 |
| 1523 | } |
| 1524 | |
| 1525 | int_fc = bt2.IntegerFieldClass(32) |
| 1526 | str_fc = bt2.StringFieldClass() |
| 1527 | struct_fc = bt2.StructureFieldClass() |
| 1528 | struct_fc.append_field(field_class=int_fc, name='an_int') |
| 1529 | struct_fc.append_field(field_class=str_fc, name='a_string') |
| 1530 | struct_fc.append_field(field_class=int_fc, name='another_int') |
| 1531 | |
| 1532 | struct = struct_fc() |
| 1533 | struct.value = values |
| 1534 | self.assertTrue(struct.is_set) |
| 1535 | struct.reset() |
| 1536 | self.assertEqual(struct_fc(), struct) |
| 1537 | |
| 1538 | def test_str_op(self): |
| 1539 | expected_string_found = False |
| 1540 | s = str(self._def) |
| 1541 | # Establish all permutations of the three expected matches since |
| 1542 | # the order in which mappings are enumerated is not explicitly part of |
| 1543 | # the API. |
| 1544 | for p in itertools.permutations([(k, v) for k, v in self._def.items()]): |
| 1545 | items = ['{}: {}'.format(repr(k), repr(v)) for k, v in p] |
| 1546 | candidate = '{{{}}}'.format(', '.join(items)) |
| 1547 | if candidate == s: |
| 1548 | expected_string_found = True |
| 1549 | break |
| 1550 | |
| 1551 | self.assertTrue(expected_string_found) |
| 1552 | |
| 1553 | def test_str_op_unset(self): |
| 1554 | self.assertEqual(str(self._fc()), 'Unset') |
| 1555 | |
| 1556 | |
| 1557 | @unittest.skip("this is broken") |
| 1558 | class VariantFieldTestCase(_TestCopySimple, unittest.TestCase): |
| 1559 | def setUp(self): |
| 1560 | self._tag_fc = bt2.EnumerationFieldClass(size=32) |
| 1561 | self._tag_fc.add_mapping('corner', 23) |
| 1562 | self._tag_fc.add_mapping('zoom', 17, 20) |
| 1563 | self._tag_fc.add_mapping('mellotron', 1001) |
| 1564 | self._tag_fc.add_mapping('giorgio', 2000, 3000) |
| 1565 | self._fc0 = bt2.IntegerFieldClass(32, is_signed=True) |
| 1566 | self._fc1 = bt2.StringFieldClass() |
| 1567 | self._fc2 = bt2.FloatingPointNumberFieldClass() |
| 1568 | self._fc3 = bt2.IntegerFieldClass(17) |
| 1569 | self._fc = bt2.VariantFieldClass('salut', self._tag_fc) |
| 1570 | self._fc.append_field('corner', self._fc0) |
| 1571 | self._fc.append_field('zoom', self._fc1) |
| 1572 | self._fc.append_field('mellotron', self._fc2) |
| 1573 | self._fc.append_field('giorgio', self._fc3) |
| 1574 | self._def = self._fc() |
| 1575 | |
| 1576 | def tearDown(self): |
| 1577 | del self._tag_fc |
| 1578 | del self._fc0 |
| 1579 | del self._fc1 |
| 1580 | del self._fc2 |
| 1581 | del self._fc3 |
| 1582 | del self._fc |
| 1583 | del self._def |
| 1584 | |
| 1585 | def test_bool_op_true(self): |
| 1586 | tag_field = self._tag_fc(1001) |
| 1587 | self._def.field(tag_field).value = -17.34 |
| 1588 | self.assertTrue(self._def) |
| 1589 | |
| 1590 | def test_bool_op_false(self): |
| 1591 | self.assertFalse(self._def) |
| 1592 | |
| 1593 | def test_tag_field_none(self): |
| 1594 | self.assertIsNone(self._def.tag_field) |
| 1595 | |
| 1596 | def test_tag_field(self): |
| 1597 | tag_field = self._tag_fc(2800) |
| 1598 | self._def.field(tag_field).value = 1847 |
| 1599 | self.assertEqual(self._def.tag_field, tag_field) |
| 1600 | self.assertEqual(self._def.tag_field.addr, tag_field.addr) |
| 1601 | |
| 1602 | def test_selected_field_none(self): |
| 1603 | self.assertIsNone(self._def.selected_field) |
| 1604 | |
| 1605 | def test_selected_field(self): |
| 1606 | var_field1 = self._fc() |
| 1607 | tag_field1 = self._tag_fc(1001) |
| 1608 | var_field1.field(tag_field1).value = -17.34 |
| 1609 | self.assertEqual(var_field1.field(), -17.34) |
| 1610 | self.assertEqual(var_field1.selected_field, -17.34) |
| 1611 | var_field2 = self._fc() |
| 1612 | tag_field2 = self._tag_fc(2500) |
| 1613 | var_field2.field(tag_field2).value = 1921 |
| 1614 | self.assertEqual(var_field2.field(), 1921) |
| 1615 | self.assertEqual(var_field2.selected_field, 1921) |
| 1616 | |
| 1617 | def test_eq(self): |
| 1618 | tag_fc = bt2.EnumerationFieldClass(size=32) |
| 1619 | tag_fc.add_mapping('corner', 23) |
| 1620 | tag_fc.add_mapping('zoom', 17, 20) |
| 1621 | tag_fc.add_mapping('mellotron', 1001) |
| 1622 | tag_fc.add_mapping('giorgio', 2000, 3000) |
| 1623 | fc0 = bt2.IntegerFieldClass(32, is_signed=True) |
| 1624 | fc1 = bt2.StringFieldClass() |
| 1625 | fc2 = bt2.FloatingPointNumberFieldClass() |
| 1626 | fc3 = bt2.IntegerFieldClass(17) |
| 1627 | fc = bt2.VariantFieldClass('salut', tag_fc) |
| 1628 | fc.append_field('corner', fc0) |
| 1629 | fc.append_field('zoom', fc1) |
| 1630 | fc.append_field('mellotron', fc2) |
| 1631 | fc.append_field('giorgio', fc3) |
| 1632 | field = fc() |
| 1633 | field_tag = tag_fc(23) |
| 1634 | def_tag = self._tag_fc(23) |
| 1635 | field.field(field_tag).value = 1774 |
| 1636 | self._def.field(def_tag).value = 1774 |
| 1637 | self.assertEqual(self._def, field) |
| 1638 | |
| 1639 | def test_eq_invalid_type(self): |
| 1640 | self.assertNotEqual(self._def, 23) |
| 1641 | |
| 1642 | def test_is_set(self): |
| 1643 | self.assertFalse(self._def.is_set) |
| 1644 | tag_field = self._tag_fc(2800) |
| 1645 | self._def.field(tag_field).value = 684 |
| 1646 | self.assertTrue(self._def.is_set) |
| 1647 | |
| 1648 | def test_reset(self): |
| 1649 | tag_field = self._tag_fc(2800) |
| 1650 | self._def.field(tag_field).value = 684 |
| 1651 | self._def.reset() |
| 1652 | self.assertFalse(self._def.is_set) |
| 1653 | self.assertIsNone(self._def.selected_field) |
| 1654 | self.assertIsNone(self._def.tag_field) |
| 1655 | |
| 1656 | def test_str_op_int(self): |
| 1657 | v = self._fc() |
| 1658 | v.field(self._tag_fc(23)).value = 42 |
| 1659 | f = self._fc0(42) |
| 1660 | self.assertEqual(str(f), str(v)) |
| 1661 | |
| 1662 | def test_str_op_str(self): |
| 1663 | v = self._fc() |
| 1664 | v.field(self._tag_fc(18)).value = 'some test string' |
| 1665 | f = self._fc1('some test string') |
| 1666 | self.assertEqual(str(f), str(v)) |
| 1667 | |
| 1668 | def test_str_op_flt(self): |
| 1669 | v = self._fc() |
| 1670 | v.field(self._tag_fc(1001)).value = 14.4245 |
| 1671 | f = self._fc2(14.4245) |
| 1672 | self.assertEqual(str(f), str(v)) |
| 1673 | |
| 1674 | def test_str_op_unset(self): |
| 1675 | self.assertEqual(str(self._fc()), 'Unset') |