| 1 | # |
| 2 | # Copyright (C) 2019 EfficiOS Inc. |
| 3 | # |
| 4 | # This program is free software; you can redistribute it and/or |
| 5 | # modify it under the terms of the GNU General Public License |
| 6 | # as published by the Free Software Foundation; only version 2 |
| 7 | # of the License. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program; if not, write to the Free Software |
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | # |
| 18 | |
| 19 | from functools import partial, partialmethod |
| 20 | import operator |
| 21 | import unittest |
| 22 | import math |
| 23 | import copy |
| 24 | import itertools |
| 25 | import collections |
| 26 | import bt2 |
| 27 | from utils import get_default_trace_class |
| 28 | |
| 29 | |
| 30 | _COMP_BINOPS = (operator.eq, operator.ne) |
| 31 | |
| 32 | |
| 33 | # Create and return a stream with the field classes part of its stream packet |
| 34 | # context. |
| 35 | # |
| 36 | # The stream is part of a dummy trace created from trace class `tc`. |
| 37 | |
| 38 | |
| 39 | def _create_stream(tc, ctx_field_classes): |
| 40 | packet_context_fc = tc.create_structure_field_class() |
| 41 | for name, fc in ctx_field_classes: |
| 42 | packet_context_fc.append_member(name, fc) |
| 43 | |
| 44 | trace = tc() |
| 45 | stream_class = tc.create_stream_class( |
| 46 | packet_context_field_class=packet_context_fc, supports_packets=True |
| 47 | ) |
| 48 | |
| 49 | stream = trace.create_stream(stream_class) |
| 50 | return stream |
| 51 | |
| 52 | |
| 53 | # Create a field of the given field class. |
| 54 | # |
| 55 | # The field is part of a dummy stream, itself part of a dummy trace created |
| 56 | # from trace class `tc`. |
| 57 | |
| 58 | |
| 59 | def _create_field(tc, field_class): |
| 60 | field_name = 'field' |
| 61 | stream = _create_stream(tc, [(field_name, field_class)]) |
| 62 | packet = stream.create_packet() |
| 63 | return packet.context_field[field_name] |
| 64 | |
| 65 | |
| 66 | # Create a field of type string. |
| 67 | # |
| 68 | # The field is part of a dummy stream, itself part of a dummy trace created |
| 69 | # from trace class `tc`. It is made out of a dummy string field class. |
| 70 | |
| 71 | |
| 72 | def _create_string_field(tc): |
| 73 | field_name = 'string_field' |
| 74 | stream = _create_stream(tc, [(field_name, tc.create_string_field_class())]) |
| 75 | packet = stream.create_packet() |
| 76 | return packet.context_field[field_name] |
| 77 | |
| 78 | |
| 79 | # Create a field of type static array of ints. |
| 80 | # |
| 81 | # The field is part of a dummy stream, itself part of a dummy trace created |
| 82 | # from trace class `tc`. It is made out of a dummy static array field class, |
| 83 | # with a dummy integer field class as element class. |
| 84 | |
| 85 | |
| 86 | def _create_int_array_field(tc, length): |
| 87 | elem_fc = tc.create_signed_integer_field_class(32) |
| 88 | fc = tc.create_static_array_field_class(elem_fc, length) |
| 89 | field_name = 'int_array' |
| 90 | stream = _create_stream(tc, [(field_name, fc)]) |
| 91 | packet = stream.create_packet() |
| 92 | return packet.context_field[field_name] |
| 93 | |
| 94 | |
| 95 | # Create a field of type dynamic array of ints. |
| 96 | # |
| 97 | # The field is part of a dummy stream, itself part of a dummy trace created |
| 98 | # from trace class `tc`. It is made out of a dummy static array field class, |
| 99 | # with a dummy integer field class as element and length classes. |
| 100 | |
| 101 | |
| 102 | def _create_dynamic_array(tc): |
| 103 | elem_fc = tc.create_signed_integer_field_class(32) |
| 104 | len_fc = tc.create_signed_integer_field_class(32) |
| 105 | fc = tc.create_dynamic_array_field_class(elem_fc) |
| 106 | field_name = 'int_dyn_array' |
| 107 | stream = _create_stream(tc, [('thelength', len_fc), (field_name, fc)]) |
| 108 | packet = stream.create_packet() |
| 109 | packet.context_field[field_name].length = 3 |
| 110 | return packet.context_field[field_name] |
| 111 | |
| 112 | |
| 113 | # Create a field of type array of (empty) structures. |
| 114 | # |
| 115 | # The field is part of a dummy stream, itself part of a dummy trace created |
| 116 | # from trace class `tc`. It is made out of a dummy static array field class, |
| 117 | # with a dummy struct field class as element class. |
| 118 | |
| 119 | |
| 120 | def _create_struct_array_field(tc, length): |
| 121 | elem_fc = tc.create_structure_field_class() |
| 122 | fc = tc.create_static_array_field_class(elem_fc, length) |
| 123 | field_name = 'struct_array' |
| 124 | stream = _create_stream(tc, [(field_name, fc)]) |
| 125 | packet = stream.create_packet() |
| 126 | return packet.context_field[field_name] |
| 127 | |
| 128 | |
| 129 | # Base class for numeric field test cases. |
| 130 | # |
| 131 | # To be compatible with this base class, a derived class must, in its |
| 132 | # setUp() method: |
| 133 | # |
| 134 | # * Set `self._def` to a field object with an arbitrary value. |
| 135 | # * Set `self._def_value` to the equivalent value of `self._def`. |
| 136 | class _TestNumericField: |
| 137 | # Tries the binary operation `op`: |
| 138 | # |
| 139 | # 1. Between `self._def`, which is a field object, and `rhs`. |
| 140 | # 2. Between `self._def_value`, which is the raw value of |
| 141 | # `self._def`, and `rhs`. |
| 142 | # |
| 143 | # Returns the results of 1. and 2. |
| 144 | # |
| 145 | # If there's an exception while performing 1. or 2., asserts that |
| 146 | # both operations raised exceptions, that both exceptions have the |
| 147 | # same type, and returns `None` for both results. |
| 148 | def _binop(self, op, rhs): |
| 149 | type_rexc = None |
| 150 | type_rvexc = None |
| 151 | comp_value = rhs |
| 152 | |
| 153 | # try with field object |
| 154 | try: |
| 155 | r = op(self._def, rhs) |
| 156 | except Exception as e: |
| 157 | type_rexc = type(e) |
| 158 | |
| 159 | # try with value |
| 160 | try: |
| 161 | rv = op(self._def_value, comp_value) |
| 162 | except Exception as e: |
| 163 | type_rvexc = type(e) |
| 164 | |
| 165 | if type_rexc is not None or type_rvexc is not None: |
| 166 | # at least one of the operations raised an exception: in |
| 167 | # this case both operations should have raised the same |
| 168 | # type of exception (division by zero, bit shift with a |
| 169 | # floating point number operand, etc.) |
| 170 | self.assertIs(type_rexc, type_rvexc) |
| 171 | return None, None |
| 172 | |
| 173 | return r, rv |
| 174 | |
| 175 | # Tries the unary operation `op`: |
| 176 | # |
| 177 | # 1. On `self._def`, which is a field object. |
| 178 | # 2. On `self._def_value`, which is the value of `self._def`. |
| 179 | # |
| 180 | # Returns the results of 1. and 2. |
| 181 | # |
| 182 | # If there's an exception while performing 1. or 2., asserts that |
| 183 | # both operations raised exceptions, that both exceptions have the |
| 184 | # same type, and returns `None` for both results. |
| 185 | def _unaryop(self, op): |
| 186 | type_rexc = None |
| 187 | type_rvexc = None |
| 188 | |
| 189 | # try with field object |
| 190 | try: |
| 191 | r = op(self._def) |
| 192 | except Exception as e: |
| 193 | type_rexc = type(e) |
| 194 | |
| 195 | # try with value |
| 196 | try: |
| 197 | rv = op(self._def_value) |
| 198 | except Exception as e: |
| 199 | type_rvexc = type(e) |
| 200 | |
| 201 | if type_rexc is not None or type_rvexc is not None: |
| 202 | # at least one of the operations raised an exception: in |
| 203 | # this case both operations should have raised the same |
| 204 | # type of exception (division by zero, bit shift with a |
| 205 | # floating point number operand, etc.) |
| 206 | self.assertIs(type_rexc, type_rvexc) |
| 207 | return None, None |
| 208 | |
| 209 | return r, rv |
| 210 | |
| 211 | # Tests that the unary operation `op` gives results with the same |
| 212 | # type for both `self._def` and `self._def_value`. |
| 213 | def _test_unaryop_type(self, op): |
| 214 | r, rv = self._unaryop(op) |
| 215 | |
| 216 | if r is None: |
| 217 | return |
| 218 | |
| 219 | self.assertIsInstance(r, type(rv)) |
| 220 | |
| 221 | # Tests that the unary operation `op` gives results with the same |
| 222 | # value for both `self._def` and `self._def_value`. This uses the |
| 223 | # __eq__() operator of `self._def`. |
| 224 | def _test_unaryop_value(self, op): |
| 225 | r, rv = self._unaryop(op) |
| 226 | |
| 227 | if r is None: |
| 228 | return |
| 229 | |
| 230 | self.assertEqual(r, rv) |
| 231 | |
| 232 | # Tests that the unary operation `op`, when applied to `self._def`, |
| 233 | # does not change its underlying BT object address. |
| 234 | def _test_unaryop_addr_same(self, op): |
| 235 | addr_before = self._def.addr |
| 236 | self._unaryop(op) |
| 237 | self.assertEqual(self._def.addr, addr_before) |
| 238 | |
| 239 | # Tests that the unary operation `op`, when applied to `self._def`, |
| 240 | # does not change its value. |
| 241 | def _test_unaryop_value_same(self, op): |
| 242 | value_before = copy.copy(self._def_value) |
| 243 | self._unaryop(op) |
| 244 | self.assertEqual(self._def, value_before) |
| 245 | |
| 246 | # Tests that the binary operation `op` gives results with the same |
| 247 | # type for both `self._def` and `self._def_value`. |
| 248 | def _test_binop_type(self, op, rhs): |
| 249 | r, rv = self._binop(op, rhs) |
| 250 | |
| 251 | if r is None: |
| 252 | return |
| 253 | |
| 254 | if op in _COMP_BINOPS: |
| 255 | # __eq__() and __ne__() always return a 'bool' object |
| 256 | self.assertIsInstance(r, bool) |
| 257 | else: |
| 258 | self.assertIsInstance(r, type(rv)) |
| 259 | |
| 260 | # Tests that the binary operation `op` gives results with the same |
| 261 | # value for both `self._def` and `self._def_value`. This uses the |
| 262 | # __eq__() operator of `self._def`. |
| 263 | def _test_binop_value(self, op, rhs): |
| 264 | r, rv = self._binop(op, rhs) |
| 265 | |
| 266 | if r is None: |
| 267 | return |
| 268 | |
| 269 | self.assertEqual(r, rv) |
| 270 | |
| 271 | # Tests that the binary operation `op`, when applied to `self._def`, |
| 272 | # does not change its underlying BT object address. |
| 273 | def _test_binop_lhs_addr_same(self, op, rhs): |
| 274 | addr_before = self._def.addr |
| 275 | r, rv = self._binop(op, rhs) |
| 276 | self.assertEqual(self._def.addr, addr_before) |
| 277 | |
| 278 | # Tests that the binary operation `op`, when applied to `self._def`, |
| 279 | # does not change its value. |
| 280 | @unittest.skip('copy is not implemented') |
| 281 | def _test_binop_lhs_value_same(self, op, rhs): |
| 282 | value_before = copy.copy(self._def) |
| 283 | r, rv = self._binop(op, rhs) |
| 284 | self.assertEqual(self._def, value_before) |
| 285 | |
| 286 | # The methods below which take the `test_cb` and/or `op` parameters |
| 287 | # are meant to be used with one of the _test_binop_*() functions |
| 288 | # above as `test_cb` and a binary operator function as `op`. |
| 289 | # |
| 290 | # For example: |
| 291 | # |
| 292 | # self._test_binop_rhs_pos_int(self._test_binop_value, |
| 293 | # operator.add) |
| 294 | # |
| 295 | # This tests that a numeric field object added to a positive integer |
| 296 | # value gives a result with the expected value. |
| 297 | # |
| 298 | # `vint` and `vfloat` mean a signed integer value object and a real |
| 299 | # value object. |
| 300 | |
| 301 | def _test_binop_invalid_unknown(self, op): |
| 302 | if op in _COMP_BINOPS: |
| 303 | self.skipTest('not testing') |
| 304 | |
| 305 | class A: |
| 306 | pass |
| 307 | |
| 308 | with self.assertRaises(TypeError): |
| 309 | op(self._def, A()) |
| 310 | |
| 311 | def _test_binop_invalid_none(self, op): |
| 312 | if op in _COMP_BINOPS: |
| 313 | self.skipTest('not testing') |
| 314 | |
| 315 | with self.assertRaises(TypeError): |
| 316 | op(self._def, None) |
| 317 | |
| 318 | def _test_binop_rhs_false(self, test_cb, op): |
| 319 | test_cb(op, False) |
| 320 | |
| 321 | def _test_binop_rhs_true(self, test_cb, op): |
| 322 | test_cb(op, True) |
| 323 | |
| 324 | def _test_binop_rhs_pos_int(self, test_cb, op): |
| 325 | test_cb(op, 2) |
| 326 | |
| 327 | def _test_binop_rhs_neg_int(self, test_cb, op): |
| 328 | test_cb(op, -23) |
| 329 | |
| 330 | def _test_binop_rhs_zero_int(self, test_cb, op): |
| 331 | test_cb(op, 0) |
| 332 | |
| 333 | def _test_binop_rhs_pos_vint(self, test_cb, op): |
| 334 | test_cb(op, bt2.create_value(2)) |
| 335 | |
| 336 | def _test_binop_rhs_neg_vint(self, test_cb, op): |
| 337 | test_cb(op, bt2.create_value(-23)) |
| 338 | |
| 339 | def _test_binop_rhs_zero_vint(self, test_cb, op): |
| 340 | test_cb(op, bt2.create_value(0)) |
| 341 | |
| 342 | def _test_binop_rhs_pos_float(self, test_cb, op): |
| 343 | test_cb(op, 2.2) |
| 344 | |
| 345 | def _test_binop_rhs_neg_float(self, test_cb, op): |
| 346 | test_cb(op, -23.4) |
| 347 | |
| 348 | def _test_binop_rhs_zero_float(self, test_cb, op): |
| 349 | test_cb(op, 0.0) |
| 350 | |
| 351 | def _test_binop_rhs_pos_vfloat(self, test_cb, op): |
| 352 | test_cb(op, bt2.create_value(2.2)) |
| 353 | |
| 354 | def _test_binop_rhs_neg_vfloat(self, test_cb, op): |
| 355 | test_cb(op, bt2.create_value(-23.4)) |
| 356 | |
| 357 | def _test_binop_rhs_zero_vfloat(self, test_cb, op): |
| 358 | test_cb(op, bt2.create_value(0.0)) |
| 359 | |
| 360 | def _test_binop_rhs_complex(self, test_cb, op): |
| 361 | test_cb(op, -23 + 19j) |
| 362 | |
| 363 | def _test_binop_rhs_zero_complex(self, test_cb, op): |
| 364 | test_cb(op, 0j) |
| 365 | |
| 366 | def _test_binop_type_false(self, op): |
| 367 | self._test_binop_rhs_false(self._test_binop_type, op) |
| 368 | |
| 369 | def _test_binop_type_true(self, op): |
| 370 | self._test_binop_rhs_true(self._test_binop_type, op) |
| 371 | |
| 372 | def _test_binop_type_pos_int(self, op): |
| 373 | self._test_binop_rhs_pos_int(self._test_binop_type, op) |
| 374 | |
| 375 | def _test_binop_type_neg_int(self, op): |
| 376 | self._test_binop_rhs_neg_int(self._test_binop_type, op) |
| 377 | |
| 378 | def _test_binop_type_zero_int(self, op): |
| 379 | self._test_binop_rhs_zero_int(self._test_binop_type, op) |
| 380 | |
| 381 | def _test_binop_type_pos_vint(self, op): |
| 382 | self._test_binop_rhs_pos_vint(self._test_binop_type, op) |
| 383 | |
| 384 | def _test_binop_type_neg_vint(self, op): |
| 385 | self._test_binop_rhs_neg_vint(self._test_binop_type, op) |
| 386 | |
| 387 | def _test_binop_type_zero_vint(self, op): |
| 388 | self._test_binop_rhs_zero_vint(self._test_binop_type, op) |
| 389 | |
| 390 | def _test_binop_type_pos_float(self, op): |
| 391 | self._test_binop_rhs_pos_float(self._test_binop_type, op) |
| 392 | |
| 393 | def _test_binop_type_neg_float(self, op): |
| 394 | self._test_binop_rhs_neg_float(self._test_binop_type, op) |
| 395 | |
| 396 | def _test_binop_type_zero_float(self, op): |
| 397 | self._test_binop_rhs_zero_float(self._test_binop_type, op) |
| 398 | |
| 399 | def _test_binop_type_pos_vfloat(self, op): |
| 400 | self._test_binop_rhs_pos_vfloat(self._test_binop_type, op) |
| 401 | |
| 402 | def _test_binop_type_neg_vfloat(self, op): |
| 403 | self._test_binop_rhs_neg_vfloat(self._test_binop_type, op) |
| 404 | |
| 405 | def _test_binop_type_zero_vfloat(self, op): |
| 406 | self._test_binop_rhs_zero_vfloat(self._test_binop_type, op) |
| 407 | |
| 408 | def _test_binop_type_complex(self, op): |
| 409 | self._test_binop_rhs_complex(self._test_binop_type, op) |
| 410 | |
| 411 | def _test_binop_type_zero_complex(self, op): |
| 412 | self._test_binop_rhs_zero_complex(self._test_binop_type, op) |
| 413 | |
| 414 | def _test_binop_value_false(self, op): |
| 415 | self._test_binop_rhs_false(self._test_binop_value, op) |
| 416 | |
| 417 | def _test_binop_value_true(self, op): |
| 418 | self._test_binop_rhs_true(self._test_binop_value, op) |
| 419 | |
| 420 | def _test_binop_value_pos_int(self, op): |
| 421 | self._test_binop_rhs_pos_int(self._test_binop_value, op) |
| 422 | |
| 423 | def _test_binop_value_neg_int(self, op): |
| 424 | self._test_binop_rhs_neg_int(self._test_binop_value, op) |
| 425 | |
| 426 | def _test_binop_value_zero_int(self, op): |
| 427 | self._test_binop_rhs_zero_int(self._test_binop_value, op) |
| 428 | |
| 429 | def _test_binop_value_pos_vint(self, op): |
| 430 | self._test_binop_rhs_pos_vint(self._test_binop_value, op) |
| 431 | |
| 432 | def _test_binop_value_neg_vint(self, op): |
| 433 | self._test_binop_rhs_neg_vint(self._test_binop_value, op) |
| 434 | |
| 435 | def _test_binop_value_zero_vint(self, op): |
| 436 | self._test_binop_rhs_zero_vint(self._test_binop_value, op) |
| 437 | |
| 438 | def _test_binop_value_pos_float(self, op): |
| 439 | self._test_binop_rhs_pos_float(self._test_binop_value, op) |
| 440 | |
| 441 | def _test_binop_value_neg_float(self, op): |
| 442 | self._test_binop_rhs_neg_float(self._test_binop_value, op) |
| 443 | |
| 444 | def _test_binop_value_zero_float(self, op): |
| 445 | self._test_binop_rhs_zero_float(self._test_binop_value, op) |
| 446 | |
| 447 | def _test_binop_value_pos_vfloat(self, op): |
| 448 | self._test_binop_rhs_pos_vfloat(self._test_binop_value, op) |
| 449 | |
| 450 | def _test_binop_value_neg_vfloat(self, op): |
| 451 | self._test_binop_rhs_neg_vfloat(self._test_binop_value, op) |
| 452 | |
| 453 | def _test_binop_value_zero_vfloat(self, op): |
| 454 | self._test_binop_rhs_zero_vfloat(self._test_binop_value, op) |
| 455 | |
| 456 | def _test_binop_value_complex(self, op): |
| 457 | self._test_binop_rhs_complex(self._test_binop_value, op) |
| 458 | |
| 459 | def _test_binop_value_zero_complex(self, op): |
| 460 | self._test_binop_rhs_zero_complex(self._test_binop_value, op) |
| 461 | |
| 462 | def _test_binop_lhs_addr_same_false(self, op): |
| 463 | self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op) |
| 464 | |
| 465 | def _test_binop_lhs_addr_same_true(self, op): |
| 466 | self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op) |
| 467 | |
| 468 | def _test_binop_lhs_addr_same_pos_int(self, op): |
| 469 | self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op) |
| 470 | |
| 471 | def _test_binop_lhs_addr_same_neg_int(self, op): |
| 472 | self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op) |
| 473 | |
| 474 | def _test_binop_lhs_addr_same_zero_int(self, op): |
| 475 | self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op) |
| 476 | |
| 477 | def _test_binop_lhs_addr_same_pos_vint(self, op): |
| 478 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op) |
| 479 | |
| 480 | def _test_binop_lhs_addr_same_neg_vint(self, op): |
| 481 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op) |
| 482 | |
| 483 | def _test_binop_lhs_addr_same_zero_vint(self, op): |
| 484 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op) |
| 485 | |
| 486 | def _test_binop_lhs_addr_same_pos_float(self, op): |
| 487 | self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op) |
| 488 | |
| 489 | def _test_binop_lhs_addr_same_neg_float(self, op): |
| 490 | self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op) |
| 491 | |
| 492 | def _test_binop_lhs_addr_same_zero_float(self, op): |
| 493 | self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op) |
| 494 | |
| 495 | def _test_binop_lhs_addr_same_pos_vfloat(self, op): |
| 496 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op) |
| 497 | |
| 498 | def _test_binop_lhs_addr_same_neg_vfloat(self, op): |
| 499 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op) |
| 500 | |
| 501 | def _test_binop_lhs_addr_same_zero_vfloat(self, op): |
| 502 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op) |
| 503 | |
| 504 | def _test_binop_lhs_addr_same_complex(self, op): |
| 505 | self._test_binop_rhs_complex(self._test_binop_lhs_addr_same, op) |
| 506 | |
| 507 | def _test_binop_lhs_addr_same_zero_complex(self, op): |
| 508 | self._test_binop_rhs_zero_complex(self._test_binop_lhs_addr_same, op) |
| 509 | |
| 510 | def _test_binop_lhs_value_same_false(self, op): |
| 511 | self._test_binop_rhs_false(self._test_binop_lhs_value_same, op) |
| 512 | |
| 513 | def _test_binop_lhs_value_same_true(self, op): |
| 514 | self._test_binop_rhs_true(self._test_binop_lhs_value_same, op) |
| 515 | |
| 516 | def _test_binop_lhs_value_same_pos_int(self, op): |
| 517 | self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op) |
| 518 | |
| 519 | def _test_binop_lhs_value_same_neg_int(self, op): |
| 520 | self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op) |
| 521 | |
| 522 | def _test_binop_lhs_value_same_zero_int(self, op): |
| 523 | self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op) |
| 524 | |
| 525 | def _test_binop_lhs_value_same_pos_vint(self, op): |
| 526 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op) |
| 527 | |
| 528 | def _test_binop_lhs_value_same_neg_vint(self, op): |
| 529 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op) |
| 530 | |
| 531 | def _test_binop_lhs_value_same_zero_vint(self, op): |
| 532 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op) |
| 533 | |
| 534 | def _test_binop_lhs_value_same_pos_float(self, op): |
| 535 | self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op) |
| 536 | |
| 537 | def _test_binop_lhs_value_same_neg_float(self, op): |
| 538 | self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op) |
| 539 | |
| 540 | def _test_binop_lhs_value_same_zero_float(self, op): |
| 541 | self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op) |
| 542 | |
| 543 | def _test_binop_lhs_value_same_pos_vfloat(self, op): |
| 544 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op) |
| 545 | |
| 546 | def _test_binop_lhs_value_same_neg_vfloat(self, op): |
| 547 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op) |
| 548 | |
| 549 | def _test_binop_lhs_value_same_zero_vfloat(self, op): |
| 550 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op) |
| 551 | |
| 552 | def _test_binop_lhs_value_same_complex(self, op): |
| 553 | self._test_binop_rhs_complex(self._test_binop_lhs_value_same, op) |
| 554 | |
| 555 | def _test_binop_lhs_value_same_zero_complex(self, op): |
| 556 | self._test_binop_rhs_zero_complex(self._test_binop_lhs_value_same, op) |
| 557 | |
| 558 | def test_bool_op(self): |
| 559 | self.assertEqual(bool(self._def), bool(self._def_value)) |
| 560 | |
| 561 | def test_int_op(self): |
| 562 | self.assertEqual(int(self._def), int(self._def_value)) |
| 563 | |
| 564 | def test_float_op(self): |
| 565 | self.assertEqual(float(self._def), float(self._def_value)) |
| 566 | |
| 567 | def test_complex_op(self): |
| 568 | self.assertEqual(complex(self._def), complex(self._def_value)) |
| 569 | |
| 570 | def test_str_op(self): |
| 571 | self.assertEqual(str(self._def), str(self._def_value)) |
| 572 | |
| 573 | def test_eq_none(self): |
| 574 | # Ignore this lint error: |
| 575 | # E711 comparison to None should be 'if cond is None:' |
| 576 | # since this is what we want to test (even though not good practice). |
| 577 | self.assertFalse(self._def == None) # noqa: E711 |
| 578 | |
| 579 | def test_ne_none(self): |
| 580 | # Ignore this lint error: |
| 581 | # E711 comparison to None should be 'if cond is not None:' |
| 582 | # since this is what we want to test (even though not good practice). |
| 583 | self.assertTrue(self._def != None) # noqa: E711 |
| 584 | |
| 585 | |
| 586 | # This is a list of binary operators used for |
| 587 | # _inject_numeric_testing_methods(). |
| 588 | # |
| 589 | # Each entry is a pair of binary operator name (used as part of the |
| 590 | # created testing method's name) and operator function. |
| 591 | _BINOPS = ( |
| 592 | ('lt', operator.lt), |
| 593 | ('le', operator.le), |
| 594 | ('eq', operator.eq), |
| 595 | ('ne', operator.ne), |
| 596 | ('ge', operator.ge), |
| 597 | ('gt', operator.gt), |
| 598 | ('add', operator.add), |
| 599 | ('radd', lambda a, b: operator.add(b, a)), |
| 600 | ('and', operator.and_), |
| 601 | ('rand', lambda a, b: operator.and_(b, a)), |
| 602 | ('floordiv', operator.floordiv), |
| 603 | ('rfloordiv', lambda a, b: operator.floordiv(b, a)), |
| 604 | ('lshift', operator.lshift), |
| 605 | ('rlshift', lambda a, b: operator.lshift(b, a)), |
| 606 | ('mod', operator.mod), |
| 607 | ('rmod', lambda a, b: operator.mod(b, a)), |
| 608 | ('mul', operator.mul), |
| 609 | ('rmul', lambda a, b: operator.mul(b, a)), |
| 610 | ('or', operator.or_), |
| 611 | ('ror', lambda a, b: operator.or_(b, a)), |
| 612 | ('pow', operator.pow), |
| 613 | ('rpow', lambda a, b: operator.pow(b, a)), |
| 614 | ('rshift', operator.rshift), |
| 615 | ('rrshift', lambda a, b: operator.rshift(b, a)), |
| 616 | ('sub', operator.sub), |
| 617 | ('rsub', lambda a, b: operator.sub(b, a)), |
| 618 | ('truediv', operator.truediv), |
| 619 | ('rtruediv', lambda a, b: operator.truediv(b, a)), |
| 620 | ('xor', operator.xor), |
| 621 | ('rxor', lambda a, b: operator.xor(b, a)), |
| 622 | ) |
| 623 | |
| 624 | |
| 625 | # This is a list of unary operators used for |
| 626 | # _inject_numeric_testing_methods(). |
| 627 | # |
| 628 | # Each entry is a pair of unary operator name (used as part of the |
| 629 | # created testing method's name) and operator function. |
| 630 | _UNARYOPS = ( |
| 631 | ('neg', operator.neg), |
| 632 | ('pos', operator.pos), |
| 633 | ('abs', operator.abs), |
| 634 | ('invert', operator.invert), |
| 635 | ('round', round), |
| 636 | ('round_0', partial(round, ndigits=0)), |
| 637 | ('round_1', partial(round, ndigits=1)), |
| 638 | ('round_2', partial(round, ndigits=2)), |
| 639 | ('round_3', partial(round, ndigits=3)), |
| 640 | ('ceil', math.ceil), |
| 641 | ('floor', math.floor), |
| 642 | ('trunc', math.trunc), |
| 643 | ) |
| 644 | |
| 645 | |
| 646 | # This function injects a bunch of testing methods to a numeric |
| 647 | # field test case. |
| 648 | # |
| 649 | # It is meant to be used like this: |
| 650 | # |
| 651 | # _inject_numeric_testing_methods(MyNumericFieldTestCase) |
| 652 | # |
| 653 | # This function injects: |
| 654 | # |
| 655 | # * One testing method for each _TestNumericField._test_binop_*() |
| 656 | # method, for each binary operator in the _BINOPS tuple. |
| 657 | # |
| 658 | # * One testing method for each _TestNumericField._test_unaryop*() |
| 659 | # method, for each unary operator in the _UNARYOPS tuple. |
| 660 | def _inject_numeric_testing_methods(cls): |
| 661 | def test_binop_name(suffix): |
| 662 | return 'test_binop_{}_{}'.format(name, suffix) |
| 663 | |
| 664 | def test_unaryop_name(suffix): |
| 665 | return 'test_unaryop_{}_{}'.format(name, suffix) |
| 666 | |
| 667 | # inject testing methods for each binary operation |
| 668 | for name, binop in _BINOPS: |
| 669 | setattr( |
| 670 | cls, |
| 671 | test_binop_name('invalid_unknown'), |
| 672 | partialmethod(_TestNumericField._test_binop_invalid_unknown, op=binop), |
| 673 | ) |
| 674 | setattr( |
| 675 | cls, |
| 676 | test_binop_name('invalid_none'), |
| 677 | partialmethod(_TestNumericField._test_binop_invalid_none, op=binop), |
| 678 | ) |
| 679 | setattr( |
| 680 | cls, |
| 681 | test_binop_name('type_true'), |
| 682 | partialmethod(_TestNumericField._test_binop_type_true, op=binop), |
| 683 | ) |
| 684 | setattr( |
| 685 | cls, |
| 686 | test_binop_name('type_pos_int'), |
| 687 | partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop), |
| 688 | ) |
| 689 | setattr( |
| 690 | cls, |
| 691 | test_binop_name('type_pos_vint'), |
| 692 | partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop), |
| 693 | ) |
| 694 | setattr( |
| 695 | cls, |
| 696 | test_binop_name('value_true'), |
| 697 | partialmethod(_TestNumericField._test_binop_value_true, op=binop), |
| 698 | ) |
| 699 | setattr( |
| 700 | cls, |
| 701 | test_binop_name('value_pos_int'), |
| 702 | partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop), |
| 703 | ) |
| 704 | setattr( |
| 705 | cls, |
| 706 | test_binop_name('value_pos_vint'), |
| 707 | partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop), |
| 708 | ) |
| 709 | setattr( |
| 710 | cls, |
| 711 | test_binop_name('lhs_addr_same_true'), |
| 712 | partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop), |
| 713 | ) |
| 714 | setattr( |
| 715 | cls, |
| 716 | test_binop_name('lhs_addr_same_pos_int'), |
| 717 | partialmethod( |
| 718 | _TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop |
| 719 | ), |
| 720 | ) |
| 721 | setattr( |
| 722 | cls, |
| 723 | test_binop_name('lhs_addr_same_pos_vint'), |
| 724 | partialmethod( |
| 725 | _TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop |
| 726 | ), |
| 727 | ) |
| 728 | setattr( |
| 729 | cls, |
| 730 | test_binop_name('lhs_value_same_true'), |
| 731 | partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop), |
| 732 | ) |
| 733 | setattr( |
| 734 | cls, |
| 735 | test_binop_name('lhs_value_same_pos_int'), |
| 736 | partialmethod( |
| 737 | _TestNumericField._test_binop_lhs_value_same_pos_int, op=binop |
| 738 | ), |
| 739 | ) |
| 740 | setattr( |
| 741 | cls, |
| 742 | test_binop_name('lhs_value_same_pos_vint'), |
| 743 | partialmethod( |
| 744 | _TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop |
| 745 | ), |
| 746 | ) |
| 747 | setattr( |
| 748 | cls, |
| 749 | test_binop_name('type_neg_int'), |
| 750 | partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop), |
| 751 | ) |
| 752 | setattr( |
| 753 | cls, |
| 754 | test_binop_name('type_neg_vint'), |
| 755 | partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop), |
| 756 | ) |
| 757 | setattr( |
| 758 | cls, |
| 759 | test_binop_name('value_neg_int'), |
| 760 | partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop), |
| 761 | ) |
| 762 | setattr( |
| 763 | cls, |
| 764 | test_binop_name('value_neg_vint'), |
| 765 | partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop), |
| 766 | ) |
| 767 | setattr( |
| 768 | cls, |
| 769 | test_binop_name('lhs_addr_same_neg_int'), |
| 770 | partialmethod( |
| 771 | _TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop |
| 772 | ), |
| 773 | ) |
| 774 | setattr( |
| 775 | cls, |
| 776 | test_binop_name('lhs_addr_same_neg_vint'), |
| 777 | partialmethod( |
| 778 | _TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop |
| 779 | ), |
| 780 | ) |
| 781 | setattr( |
| 782 | cls, |
| 783 | test_binop_name('lhs_value_same_neg_int'), |
| 784 | partialmethod( |
| 785 | _TestNumericField._test_binop_lhs_value_same_neg_int, op=binop |
| 786 | ), |
| 787 | ) |
| 788 | setattr( |
| 789 | cls, |
| 790 | test_binop_name('lhs_value_same_neg_vint'), |
| 791 | partialmethod( |
| 792 | _TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop |
| 793 | ), |
| 794 | ) |
| 795 | setattr( |
| 796 | cls, |
| 797 | test_binop_name('type_false'), |
| 798 | partialmethod(_TestNumericField._test_binop_type_false, op=binop), |
| 799 | ) |
| 800 | setattr( |
| 801 | cls, |
| 802 | test_binop_name('type_zero_int'), |
| 803 | partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop), |
| 804 | ) |
| 805 | setattr( |
| 806 | cls, |
| 807 | test_binop_name('type_zero_vint'), |
| 808 | partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop), |
| 809 | ) |
| 810 | setattr( |
| 811 | cls, |
| 812 | test_binop_name('value_false'), |
| 813 | partialmethod(_TestNumericField._test_binop_value_false, op=binop), |
| 814 | ) |
| 815 | setattr( |
| 816 | cls, |
| 817 | test_binop_name('value_zero_int'), |
| 818 | partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop), |
| 819 | ) |
| 820 | setattr( |
| 821 | cls, |
| 822 | test_binop_name('value_zero_vint'), |
| 823 | partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop), |
| 824 | ) |
| 825 | setattr( |
| 826 | cls, |
| 827 | test_binop_name('lhs_addr_same_false'), |
| 828 | partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop), |
| 829 | ) |
| 830 | setattr( |
| 831 | cls, |
| 832 | test_binop_name('lhs_addr_same_zero_int'), |
| 833 | partialmethod( |
| 834 | _TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop |
| 835 | ), |
| 836 | ) |
| 837 | setattr( |
| 838 | cls, |
| 839 | test_binop_name('lhs_addr_same_zero_vint'), |
| 840 | partialmethod( |
| 841 | _TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop |
| 842 | ), |
| 843 | ) |
| 844 | setattr( |
| 845 | cls, |
| 846 | test_binop_name('lhs_value_same_false'), |
| 847 | partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop), |
| 848 | ) |
| 849 | setattr( |
| 850 | cls, |
| 851 | test_binop_name('lhs_value_same_zero_int'), |
| 852 | partialmethod( |
| 853 | _TestNumericField._test_binop_lhs_value_same_zero_int, op=binop |
| 854 | ), |
| 855 | ) |
| 856 | setattr( |
| 857 | cls, |
| 858 | test_binop_name('lhs_value_same_zero_vint'), |
| 859 | partialmethod( |
| 860 | _TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop |
| 861 | ), |
| 862 | ) |
| 863 | setattr( |
| 864 | cls, |
| 865 | test_binop_name('type_pos_float'), |
| 866 | partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop), |
| 867 | ) |
| 868 | setattr( |
| 869 | cls, |
| 870 | test_binop_name('type_neg_float'), |
| 871 | partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop), |
| 872 | ) |
| 873 | setattr( |
| 874 | cls, |
| 875 | test_binop_name('type_pos_vfloat'), |
| 876 | partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop), |
| 877 | ) |
| 878 | setattr( |
| 879 | cls, |
| 880 | test_binop_name('type_neg_vfloat'), |
| 881 | partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop), |
| 882 | ) |
| 883 | setattr( |
| 884 | cls, |
| 885 | test_binop_name('value_pos_float'), |
| 886 | partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop), |
| 887 | ) |
| 888 | setattr( |
| 889 | cls, |
| 890 | test_binop_name('value_neg_float'), |
| 891 | partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop), |
| 892 | ) |
| 893 | setattr( |
| 894 | cls, |
| 895 | test_binop_name('value_pos_vfloat'), |
| 896 | partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop), |
| 897 | ) |
| 898 | setattr( |
| 899 | cls, |
| 900 | test_binop_name('value_neg_vfloat'), |
| 901 | partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop), |
| 902 | ) |
| 903 | setattr( |
| 904 | cls, |
| 905 | test_binop_name('lhs_addr_same_pos_float'), |
| 906 | partialmethod( |
| 907 | _TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop |
| 908 | ), |
| 909 | ) |
| 910 | setattr( |
| 911 | cls, |
| 912 | test_binop_name('lhs_addr_same_neg_float'), |
| 913 | partialmethod( |
| 914 | _TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop |
| 915 | ), |
| 916 | ) |
| 917 | setattr( |
| 918 | cls, |
| 919 | test_binop_name('lhs_addr_same_pos_vfloat'), |
| 920 | partialmethod( |
| 921 | _TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop |
| 922 | ), |
| 923 | ) |
| 924 | setattr( |
| 925 | cls, |
| 926 | test_binop_name('lhs_addr_same_neg_vfloat'), |
| 927 | partialmethod( |
| 928 | _TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop |
| 929 | ), |
| 930 | ) |
| 931 | setattr( |
| 932 | cls, |
| 933 | test_binop_name('lhs_value_same_pos_float'), |
| 934 | partialmethod( |
| 935 | _TestNumericField._test_binop_lhs_value_same_pos_float, op=binop |
| 936 | ), |
| 937 | ) |
| 938 | setattr( |
| 939 | cls, |
| 940 | test_binop_name('lhs_value_same_neg_float'), |
| 941 | partialmethod( |
| 942 | _TestNumericField._test_binop_lhs_value_same_neg_float, op=binop |
| 943 | ), |
| 944 | ) |
| 945 | setattr( |
| 946 | cls, |
| 947 | test_binop_name('lhs_value_same_pos_vfloat'), |
| 948 | partialmethod( |
| 949 | _TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop |
| 950 | ), |
| 951 | ) |
| 952 | setattr( |
| 953 | cls, |
| 954 | test_binop_name('lhs_value_same_neg_vfloat'), |
| 955 | partialmethod( |
| 956 | _TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop |
| 957 | ), |
| 958 | ) |
| 959 | setattr( |
| 960 | cls, |
| 961 | test_binop_name('type_zero_float'), |
| 962 | partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop), |
| 963 | ) |
| 964 | setattr( |
| 965 | cls, |
| 966 | test_binop_name('type_zero_vfloat'), |
| 967 | partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop), |
| 968 | ) |
| 969 | setattr( |
| 970 | cls, |
| 971 | test_binop_name('value_zero_float'), |
| 972 | partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop), |
| 973 | ) |
| 974 | setattr( |
| 975 | cls, |
| 976 | test_binop_name('value_zero_vfloat'), |
| 977 | partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop), |
| 978 | ) |
| 979 | setattr( |
| 980 | cls, |
| 981 | test_binop_name('lhs_addr_same_zero_float'), |
| 982 | partialmethod( |
| 983 | _TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop |
| 984 | ), |
| 985 | ) |
| 986 | setattr( |
| 987 | cls, |
| 988 | test_binop_name('lhs_addr_same_zero_vfloat'), |
| 989 | partialmethod( |
| 990 | _TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop |
| 991 | ), |
| 992 | ) |
| 993 | setattr( |
| 994 | cls, |
| 995 | test_binop_name('lhs_value_same_zero_float'), |
| 996 | partialmethod( |
| 997 | _TestNumericField._test_binop_lhs_value_same_zero_float, op=binop |
| 998 | ), |
| 999 | ) |
| 1000 | setattr( |
| 1001 | cls, |
| 1002 | test_binop_name('lhs_value_same_zero_vfloat'), |
| 1003 | partialmethod( |
| 1004 | _TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop |
| 1005 | ), |
| 1006 | ) |
| 1007 | setattr( |
| 1008 | cls, |
| 1009 | test_binop_name('type_complex'), |
| 1010 | partialmethod(_TestNumericField._test_binop_type_complex, op=binop), |
| 1011 | ) |
| 1012 | setattr( |
| 1013 | cls, |
| 1014 | test_binop_name('type_zero_complex'), |
| 1015 | partialmethod(_TestNumericField._test_binop_type_zero_complex, op=binop), |
| 1016 | ) |
| 1017 | setattr( |
| 1018 | cls, |
| 1019 | test_binop_name('value_complex'), |
| 1020 | partialmethod(_TestNumericField._test_binop_value_complex, op=binop), |
| 1021 | ) |
| 1022 | setattr( |
| 1023 | cls, |
| 1024 | test_binop_name('value_zero_complex'), |
| 1025 | partialmethod(_TestNumericField._test_binop_value_zero_complex, op=binop), |
| 1026 | ) |
| 1027 | setattr( |
| 1028 | cls, |
| 1029 | test_binop_name('lhs_addr_same_complex'), |
| 1030 | partialmethod( |
| 1031 | _TestNumericField._test_binop_lhs_addr_same_complex, op=binop |
| 1032 | ), |
| 1033 | ) |
| 1034 | setattr( |
| 1035 | cls, |
| 1036 | test_binop_name('lhs_addr_same_zero_complex'), |
| 1037 | partialmethod( |
| 1038 | _TestNumericField._test_binop_lhs_addr_same_zero_complex, op=binop |
| 1039 | ), |
| 1040 | ) |
| 1041 | setattr( |
| 1042 | cls, |
| 1043 | test_binop_name('lhs_value_same_complex'), |
| 1044 | partialmethod( |
| 1045 | _TestNumericField._test_binop_lhs_value_same_complex, op=binop |
| 1046 | ), |
| 1047 | ) |
| 1048 | setattr( |
| 1049 | cls, |
| 1050 | test_binop_name('lhs_value_same_zero_complex'), |
| 1051 | partialmethod( |
| 1052 | _TestNumericField._test_binop_lhs_value_same_zero_complex, op=binop |
| 1053 | ), |
| 1054 | ) |
| 1055 | |
| 1056 | # inject testing methods for each unary operation |
| 1057 | for name, unaryop in _UNARYOPS: |
| 1058 | setattr( |
| 1059 | cls, |
| 1060 | test_unaryop_name('type'), |
| 1061 | partialmethod(_TestNumericField._test_unaryop_type, op=unaryop), |
| 1062 | ) |
| 1063 | setattr( |
| 1064 | cls, |
| 1065 | test_unaryop_name('value'), |
| 1066 | partialmethod(_TestNumericField._test_unaryop_value, op=unaryop), |
| 1067 | ) |
| 1068 | setattr( |
| 1069 | cls, |
| 1070 | test_unaryop_name('addr_same'), |
| 1071 | partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop), |
| 1072 | ) |
| 1073 | setattr( |
| 1074 | cls, |
| 1075 | test_unaryop_name('value_same'), |
| 1076 | partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop), |
| 1077 | ) |
| 1078 | |
| 1079 | |
| 1080 | class BoolFieldTestCase(_TestNumericField, unittest.TestCase): |
| 1081 | def _create_fc(self, tc): |
| 1082 | return tc.create_bool_field_class() |
| 1083 | |
| 1084 | def setUp(self): |
| 1085 | self._tc = get_default_trace_class() |
| 1086 | self._def = _create_field(self._tc, self._create_fc(self._tc)) |
| 1087 | self._def.value = True |
| 1088 | self._def_value = True |
| 1089 | self._def_new_value = False |
| 1090 | |
| 1091 | def test_assign_true(self): |
| 1092 | raw = True |
| 1093 | self._def.value = raw |
| 1094 | self.assertEqual(self._def, raw) |
| 1095 | |
| 1096 | def test_assign_false(self): |
| 1097 | raw = False |
| 1098 | self._def.value = raw |
| 1099 | self.assertEqual(self._def, raw) |
| 1100 | |
| 1101 | def test_assign_field_true(self): |
| 1102 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1103 | raw = True |
| 1104 | field.value = raw |
| 1105 | self._def.value = field |
| 1106 | self.assertEqual(self._def, raw) |
| 1107 | |
| 1108 | def test_assign_field_false(self): |
| 1109 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1110 | raw = False |
| 1111 | field.value = raw |
| 1112 | self._def.value = field |
| 1113 | self.assertEqual(self._def, raw) |
| 1114 | |
| 1115 | def test_assign_invalid_type(self): |
| 1116 | with self.assertRaises(TypeError): |
| 1117 | self._def.value = 17 |
| 1118 | |
| 1119 | def test_str_op(self): |
| 1120 | self.assertEqual(str(self._def), str(self._def_value)) |
| 1121 | |
| 1122 | |
| 1123 | _inject_numeric_testing_methods(BoolFieldTestCase) |
| 1124 | |
| 1125 | |
| 1126 | class _TestIntegerFieldCommon(_TestNumericField): |
| 1127 | def test_assign_true(self): |
| 1128 | raw = True |
| 1129 | self._def.value = raw |
| 1130 | self.assertEqual(self._def, raw) |
| 1131 | |
| 1132 | def test_assign_false(self): |
| 1133 | raw = False |
| 1134 | self._def.value = raw |
| 1135 | self.assertEqual(self._def, raw) |
| 1136 | |
| 1137 | def test_assign_pos_int(self): |
| 1138 | raw = 477 |
| 1139 | self._def.value = raw |
| 1140 | self.assertEqual(self._def, raw) |
| 1141 | |
| 1142 | def test_assign_neg_int(self): |
| 1143 | raw = -13 |
| 1144 | self._def.value = raw |
| 1145 | self.assertEqual(self._def, raw) |
| 1146 | |
| 1147 | def test_assign_int_field(self): |
| 1148 | raw = 999 |
| 1149 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1150 | field.value = raw |
| 1151 | self._def.value = field |
| 1152 | self.assertEqual(self._def, raw) |
| 1153 | |
| 1154 | def test_assign_invalid_type(self): |
| 1155 | with self.assertRaises(TypeError): |
| 1156 | self._def.value = 'yes' |
| 1157 | |
| 1158 | def test_assign_uint(self): |
| 1159 | uint_fc = self._tc.create_unsigned_integer_field_class(32) |
| 1160 | field = _create_field(self._tc, uint_fc) |
| 1161 | raw = 1777 |
| 1162 | field.value = 1777 |
| 1163 | self.assertEqual(field, raw) |
| 1164 | |
| 1165 | def test_assign_big_uint(self): |
| 1166 | uint_fc = self._tc.create_unsigned_integer_field_class(64) |
| 1167 | field = _create_field(self._tc, uint_fc) |
| 1168 | # Larger than the IEEE 754 double-precision exact representation of |
| 1169 | # integers. |
| 1170 | raw = (2 ** 53) + 1 |
| 1171 | field.value = (2 ** 53) + 1 |
| 1172 | self.assertEqual(field, raw) |
| 1173 | |
| 1174 | def test_assign_uint_invalid_neg(self): |
| 1175 | uint_fc = self._tc.create_unsigned_integer_field_class(32) |
| 1176 | field = _create_field(self._tc, uint_fc) |
| 1177 | |
| 1178 | with self.assertRaises(ValueError): |
| 1179 | field.value = -23 |
| 1180 | |
| 1181 | def test_str_op(self): |
| 1182 | self.assertEqual(str(self._def), str(self._def_value)) |
| 1183 | |
| 1184 | |
| 1185 | _inject_numeric_testing_methods(_TestIntegerFieldCommon) |
| 1186 | |
| 1187 | |
| 1188 | class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): |
| 1189 | def _create_fc(self, tc): |
| 1190 | return tc.create_signed_integer_field_class(25) |
| 1191 | |
| 1192 | def setUp(self): |
| 1193 | self._tc = get_default_trace_class() |
| 1194 | self._field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1195 | self._field.value = 17 |
| 1196 | self._def = _create_field(self._tc, self._create_fc(self._tc)) |
| 1197 | self._def.value = 17 |
| 1198 | self._def_value = 17 |
| 1199 | self._def_new_value = -101 |
| 1200 | |
| 1201 | |
| 1202 | class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): |
| 1203 | def _create_fc(self, tc): |
| 1204 | fc = tc.create_signed_enumeration_field_class(32) |
| 1205 | fc.add_mapping('something', bt2.SignedIntegerRangeSet([(17, 17)])) |
| 1206 | fc.add_mapping('speaker', bt2.SignedIntegerRangeSet([(12, 16)])) |
| 1207 | fc.add_mapping('can', bt2.SignedIntegerRangeSet([(18, 2540)])) |
| 1208 | fc.add_mapping( |
| 1209 | 'whole range', bt2.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)]) |
| 1210 | ) |
| 1211 | fc.add_mapping('zip', bt2.SignedIntegerRangeSet([(-45, 1001)])) |
| 1212 | return fc |
| 1213 | |
| 1214 | def setUp(self): |
| 1215 | self._tc = get_default_trace_class() |
| 1216 | self._field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1217 | self._def = _create_field(self._tc, self._create_fc(self._tc)) |
| 1218 | self._def.value = 17 |
| 1219 | self._def_value = 17 |
| 1220 | self._def_new_value = -101 |
| 1221 | |
| 1222 | def test_str_op(self): |
| 1223 | expected_string_found = False |
| 1224 | s = str(self._def) |
| 1225 | |
| 1226 | # Establish all permutations of the three expected matches since |
| 1227 | # the order in which mappings are enumerated is not explicitly part of |
| 1228 | # the API. |
| 1229 | for p in itertools.permutations(['whole range', 'something', 'zip']): |
| 1230 | candidate = '{} ({})'.format(self._def_value, ', '.join(p)) |
| 1231 | if candidate == s: |
| 1232 | expected_string_found = True |
| 1233 | break |
| 1234 | |
| 1235 | self.assertTrue(expected_string_found) |
| 1236 | |
| 1237 | def test_labels(self): |
| 1238 | self._field.value = 17 |
| 1239 | labels = sorted(self._field.labels) |
| 1240 | self.assertEqual(labels, ['something', 'whole range', 'zip']) |
| 1241 | |
| 1242 | |
| 1243 | class RealFieldTestCase(_TestNumericField, unittest.TestCase): |
| 1244 | def _create_fc(self, tc): |
| 1245 | return tc.create_real_field_class() |
| 1246 | |
| 1247 | def setUp(self): |
| 1248 | self._tc = get_default_trace_class() |
| 1249 | self._field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1250 | self._def = _create_field(self._tc, self._create_fc(self._tc)) |
| 1251 | self._def.value = 52.7 |
| 1252 | self._def_value = 52.7 |
| 1253 | self._def_new_value = -17.164857 |
| 1254 | |
| 1255 | def _test_invalid_op(self, cb): |
| 1256 | with self.assertRaises(TypeError): |
| 1257 | cb() |
| 1258 | |
| 1259 | def test_assign_true(self): |
| 1260 | self._def.value = True |
| 1261 | self.assertTrue(self._def) |
| 1262 | |
| 1263 | def test_assign_false(self): |
| 1264 | self._def.value = False |
| 1265 | self.assertFalse(self._def) |
| 1266 | |
| 1267 | def test_assign_pos_int(self): |
| 1268 | raw = 477 |
| 1269 | self._def.value = raw |
| 1270 | self.assertEqual(self._def, float(raw)) |
| 1271 | |
| 1272 | def test_assign_neg_int(self): |
| 1273 | raw = -13 |
| 1274 | self._def.value = raw |
| 1275 | self.assertEqual(self._def, float(raw)) |
| 1276 | |
| 1277 | def test_assign_int_field(self): |
| 1278 | int_fc = self._tc.create_signed_integer_field_class(32) |
| 1279 | int_field = _create_field(self._tc, int_fc) |
| 1280 | raw = 999 |
| 1281 | int_field.value = raw |
| 1282 | self._def.value = int_field |
| 1283 | self.assertEqual(self._def, float(raw)) |
| 1284 | |
| 1285 | def test_assign_float(self): |
| 1286 | raw = -19.23 |
| 1287 | self._def.value = raw |
| 1288 | self.assertEqual(self._def, raw) |
| 1289 | |
| 1290 | def test_assign_float_field(self): |
| 1291 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1292 | raw = 101.32 |
| 1293 | field.value = raw |
| 1294 | self._def.value = field |
| 1295 | self.assertEqual(self._def, raw) |
| 1296 | |
| 1297 | def test_assign_invalid_type(self): |
| 1298 | with self.assertRaises(TypeError): |
| 1299 | self._def.value = 'yes' |
| 1300 | |
| 1301 | def test_invalid_lshift(self): |
| 1302 | self._test_invalid_op(lambda: self._def << 23) |
| 1303 | |
| 1304 | def test_invalid_rshift(self): |
| 1305 | self._test_invalid_op(lambda: self._def >> 23) |
| 1306 | |
| 1307 | def test_invalid_and(self): |
| 1308 | self._test_invalid_op(lambda: self._def & 23) |
| 1309 | |
| 1310 | def test_invalid_or(self): |
| 1311 | self._test_invalid_op(lambda: self._def | 23) |
| 1312 | |
| 1313 | def test_invalid_xor(self): |
| 1314 | self._test_invalid_op(lambda: self._def ^ 23) |
| 1315 | |
| 1316 | def test_invalid_invert(self): |
| 1317 | self._test_invalid_op(lambda: ~self._def) |
| 1318 | |
| 1319 | def test_str_op(self): |
| 1320 | self.assertEqual(str(self._def), str(self._def_value)) |
| 1321 | |
| 1322 | |
| 1323 | _inject_numeric_testing_methods(RealFieldTestCase) |
| 1324 | |
| 1325 | |
| 1326 | class StringFieldTestCase(unittest.TestCase): |
| 1327 | def setUp(self): |
| 1328 | self._tc = get_default_trace_class() |
| 1329 | self._def_value = 'Hello, World!' |
| 1330 | self._def = _create_string_field(self._tc) |
| 1331 | self._def.value = self._def_value |
| 1332 | self._def_new_value = 'Yes!' |
| 1333 | |
| 1334 | def test_assign_int(self): |
| 1335 | with self.assertRaises(TypeError): |
| 1336 | self._def.value = 283 |
| 1337 | |
| 1338 | def test_assign_string_field(self): |
| 1339 | field = _create_string_field(self._tc) |
| 1340 | raw = 'zorg' |
| 1341 | field.value = raw |
| 1342 | self.assertEqual(field, raw) |
| 1343 | |
| 1344 | def test_eq(self): |
| 1345 | self.assertEqual(self._def, self._def_value) |
| 1346 | |
| 1347 | def test_not_eq(self): |
| 1348 | self.assertNotEqual(self._def, 23) |
| 1349 | |
| 1350 | def test_lt_vstring(self): |
| 1351 | s1 = _create_string_field(self._tc) |
| 1352 | s1.value = 'allo' |
| 1353 | s2 = _create_string_field(self._tc) |
| 1354 | s2.value = 'bateau' |
| 1355 | self.assertLess(s1, s2) |
| 1356 | |
| 1357 | def test_lt_string(self): |
| 1358 | s1 = _create_string_field(self._tc) |
| 1359 | s1.value = 'allo' |
| 1360 | self.assertLess(s1, 'bateau') |
| 1361 | |
| 1362 | def test_le_vstring(self): |
| 1363 | s1 = _create_string_field(self._tc) |
| 1364 | s1.value = 'allo' |
| 1365 | s2 = _create_string_field(self._tc) |
| 1366 | s2.value = 'bateau' |
| 1367 | self.assertLessEqual(s1, s2) |
| 1368 | |
| 1369 | def test_le_string(self): |
| 1370 | s1 = _create_string_field(self._tc) |
| 1371 | s1.value = 'allo' |
| 1372 | self.assertLessEqual(s1, 'bateau') |
| 1373 | |
| 1374 | def test_gt_vstring(self): |
| 1375 | s1 = _create_string_field(self._tc) |
| 1376 | s1.value = 'allo' |
| 1377 | s2 = _create_string_field(self._tc) |
| 1378 | s2.value = 'bateau' |
| 1379 | self.assertGreater(s2, s1) |
| 1380 | |
| 1381 | def test_gt_string(self): |
| 1382 | s1 = _create_string_field(self._tc) |
| 1383 | s1.value = 'allo' |
| 1384 | self.assertGreater('bateau', s1) |
| 1385 | |
| 1386 | def test_ge_vstring(self): |
| 1387 | s1 = _create_string_field(self._tc) |
| 1388 | s1.value = 'allo' |
| 1389 | s2 = _create_string_field(self._tc) |
| 1390 | s2.value = 'bateau' |
| 1391 | self.assertGreaterEqual(s2, s1) |
| 1392 | |
| 1393 | def test_ge_string(self): |
| 1394 | s1 = _create_string_field(self._tc) |
| 1395 | s1.value = 'allo' |
| 1396 | self.assertGreaterEqual('bateau', s1) |
| 1397 | |
| 1398 | def test_bool_op(self): |
| 1399 | self.assertEqual(bool(self._def), bool(self._def_value)) |
| 1400 | |
| 1401 | def test_str_op(self): |
| 1402 | self.assertEqual(str(self._def), str(self._def_value)) |
| 1403 | |
| 1404 | def test_len(self): |
| 1405 | self.assertEqual(len(self._def), len(self._def_value)) |
| 1406 | |
| 1407 | def test_getitem(self): |
| 1408 | self.assertEqual(self._def[5], self._def_value[5]) |
| 1409 | |
| 1410 | def test_append_str(self): |
| 1411 | to_append = 'meow meow meow' |
| 1412 | self._def += to_append |
| 1413 | self._def_value += to_append |
| 1414 | self.assertEqual(self._def, self._def_value) |
| 1415 | |
| 1416 | def test_append_string_field(self): |
| 1417 | field = _create_string_field(self._tc) |
| 1418 | to_append = 'meow meow meow' |
| 1419 | field.value = to_append |
| 1420 | self._def += field |
| 1421 | self._def_value += to_append |
| 1422 | self.assertEqual(self._def, self._def_value) |
| 1423 | |
| 1424 | |
| 1425 | class _TestArrayFieldCommon: |
| 1426 | def _modify_def(self): |
| 1427 | self._def[2] = 23 |
| 1428 | |
| 1429 | def test_bool_op_true(self): |
| 1430 | self.assertTrue(self._def) |
| 1431 | |
| 1432 | def test_len(self): |
| 1433 | self.assertEqual(len(self._def), 3) |
| 1434 | |
| 1435 | def test_length(self): |
| 1436 | self.assertEqual(self._def.length, 3) |
| 1437 | |
| 1438 | def test_getitem(self): |
| 1439 | field = self._def[1] |
| 1440 | self.assertIs(type(field), bt2._SignedIntegerField) |
| 1441 | self.assertEqual(field, 1847) |
| 1442 | |
| 1443 | def test_eq(self): |
| 1444 | field = _create_int_array_field(self._tc, 3) |
| 1445 | field[0] = 45 |
| 1446 | field[1] = 1847 |
| 1447 | field[2] = 1948754 |
| 1448 | self.assertEqual(self._def, field) |
| 1449 | |
| 1450 | def test_eq_invalid_type(self): |
| 1451 | self.assertNotEqual(self._def, 23) |
| 1452 | |
| 1453 | def test_eq_diff_len(self): |
| 1454 | field = _create_int_array_field(self._tc, 2) |
| 1455 | field[0] = 45 |
| 1456 | field[1] = 1847 |
| 1457 | self.assertNotEqual(self._def, field) |
| 1458 | |
| 1459 | def test_eq_diff_content_same_len(self): |
| 1460 | field = _create_int_array_field(self._tc, 3) |
| 1461 | field[0] = 45 |
| 1462 | field[1] = 1846 |
| 1463 | field[2] = 1948754 |
| 1464 | self.assertNotEqual(self._def, field) |
| 1465 | |
| 1466 | def test_eq_non_sequence_iterable(self): |
| 1467 | dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)]) |
| 1468 | field = _create_int_array_field(self._tc, 3) |
| 1469 | field[0] = 1 |
| 1470 | field[1] = 3 |
| 1471 | field[2] = 5 |
| 1472 | self.assertEqual(field, list(dct.keys())) |
| 1473 | self.assertNotEqual(field, dct) |
| 1474 | |
| 1475 | def test_setitem(self): |
| 1476 | self._def[2] = 24 |
| 1477 | self.assertEqual(self._def[2], 24) |
| 1478 | |
| 1479 | def test_setitem_int_field(self): |
| 1480 | int_fc = self._tc.create_signed_integer_field_class(32) |
| 1481 | int_field = _create_field(self._tc, int_fc) |
| 1482 | int_field.value = 19487 |
| 1483 | self._def[1] = int_field |
| 1484 | self.assertEqual(self._def[1], 19487) |
| 1485 | |
| 1486 | def test_setitem_non_basic_field(self): |
| 1487 | array_field = _create_struct_array_field(self._tc, 2) |
| 1488 | with self.assertRaises(TypeError): |
| 1489 | array_field[1] = 23 |
| 1490 | |
| 1491 | def test_setitem_none(self): |
| 1492 | with self.assertRaises(TypeError): |
| 1493 | self._def[1] = None |
| 1494 | |
| 1495 | def test_setitem_index_wrong_type(self): |
| 1496 | with self.assertRaises(TypeError): |
| 1497 | self._def['yes'] = 23 |
| 1498 | |
| 1499 | def test_setitem_index_neg(self): |
| 1500 | with self.assertRaises(IndexError): |
| 1501 | self._def[-2] = 23 |
| 1502 | |
| 1503 | def test_setitem_index_out_of_range(self): |
| 1504 | with self.assertRaises(IndexError): |
| 1505 | self._def[len(self._def)] = 134679 |
| 1506 | |
| 1507 | def test_iter(self): |
| 1508 | for field, value in zip(self._def, (45, 1847, 1948754)): |
| 1509 | self.assertEqual(field, value) |
| 1510 | |
| 1511 | def test_value_int_field(self): |
| 1512 | values = [45646, 145, 12145] |
| 1513 | self._def.value = values |
| 1514 | self.assertEqual(values, self._def) |
| 1515 | |
| 1516 | def test_value_check_sequence(self): |
| 1517 | values = 42 |
| 1518 | with self.assertRaises(TypeError): |
| 1519 | self._def.value = values |
| 1520 | |
| 1521 | def test_value_wrong_type_in_sequence(self): |
| 1522 | values = [32, 'hello', 11] |
| 1523 | with self.assertRaises(TypeError): |
| 1524 | self._def.value = values |
| 1525 | |
| 1526 | def test_value_complex_type(self): |
| 1527 | struct_fc = self._tc.create_structure_field_class() |
| 1528 | int_fc = self._tc.create_signed_integer_field_class(32) |
| 1529 | another_int_fc = self._tc.create_signed_integer_field_class(32) |
| 1530 | str_fc = self._tc.create_string_field_class() |
| 1531 | struct_fc.append_member(field_class=int_fc, name='an_int') |
| 1532 | struct_fc.append_member(field_class=str_fc, name='a_string') |
| 1533 | struct_fc.append_member(field_class=another_int_fc, name='another_int') |
| 1534 | array_fc = self._tc.create_static_array_field_class(struct_fc, 3) |
| 1535 | stream = _create_stream(self._tc, [('array_field', array_fc)]) |
| 1536 | values = [ |
| 1537 | {'an_int': 42, 'a_string': 'hello', 'another_int': 66}, |
| 1538 | {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488}, |
| 1539 | {'an_int': 156, 'a_string': 'or not', 'another_int': 4648}, |
| 1540 | ] |
| 1541 | |
| 1542 | array = stream.create_packet().context_field['array_field'] |
| 1543 | array.value = values |
| 1544 | self.assertEqual(values, array) |
| 1545 | values[0]['an_int'] = 'a string' |
| 1546 | with self.assertRaises(TypeError): |
| 1547 | array.value = values |
| 1548 | |
| 1549 | def test_str_op(self): |
| 1550 | s = str(self._def) |
| 1551 | expected_string = '[{}]'.format(', '.join([repr(v) for v in self._def_value])) |
| 1552 | self.assertEqual(expected_string, s) |
| 1553 | |
| 1554 | |
| 1555 | class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): |
| 1556 | def setUp(self): |
| 1557 | self._tc = get_default_trace_class() |
| 1558 | self._def = _create_int_array_field(self._tc, 3) |
| 1559 | self._def[0] = 45 |
| 1560 | self._def[1] = 1847 |
| 1561 | self._def[2] = 1948754 |
| 1562 | self._def_value = [45, 1847, 1948754] |
| 1563 | |
| 1564 | def test_value_wrong_len(self): |
| 1565 | values = [45, 1847] |
| 1566 | with self.assertRaises(ValueError): |
| 1567 | self._def.value = values |
| 1568 | |
| 1569 | |
| 1570 | class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): |
| 1571 | def setUp(self): |
| 1572 | self._tc = get_default_trace_class() |
| 1573 | self._def = _create_dynamic_array(self._tc) |
| 1574 | self._def[0] = 45 |
| 1575 | self._def[1] = 1847 |
| 1576 | self._def[2] = 1948754 |
| 1577 | self._def_value = [45, 1847, 1948754] |
| 1578 | |
| 1579 | def test_value_resize(self): |
| 1580 | new_values = [1, 2, 3, 4] |
| 1581 | self._def.value = new_values |
| 1582 | self.assertCountEqual(self._def, new_values) |
| 1583 | |
| 1584 | def test_set_length(self): |
| 1585 | self._def.length = 4 |
| 1586 | self._def[3] = 0 |
| 1587 | self.assertEqual(len(self._def), 4) |
| 1588 | |
| 1589 | def test_set_invalid_length(self): |
| 1590 | with self.assertRaises(TypeError): |
| 1591 | self._def.length = 'cheval' |
| 1592 | |
| 1593 | |
| 1594 | class StructureFieldTestCase(unittest.TestCase): |
| 1595 | def _create_fc(self, tc): |
| 1596 | fc = tc.create_structure_field_class() |
| 1597 | fc.append_member('A', self._fc0_fn()) |
| 1598 | fc.append_member('B', self._fc1_fn()) |
| 1599 | fc.append_member('C', self._fc2_fn()) |
| 1600 | fc.append_member('D', self._fc3_fn()) |
| 1601 | fc.append_member('E', self._fc4_fn()) |
| 1602 | fc5 = self._fc5_fn() |
| 1603 | fc5.append_member('F_1', self._fc5_inner_fn()) |
| 1604 | fc.append_member('F', fc5) |
| 1605 | return fc |
| 1606 | |
| 1607 | def setUp(self): |
| 1608 | self._tc = get_default_trace_class() |
| 1609 | self._fc0_fn = self._tc.create_signed_integer_field_class |
| 1610 | self._fc1_fn = self._tc.create_string_field_class |
| 1611 | self._fc2_fn = self._tc.create_real_field_class |
| 1612 | self._fc3_fn = self._tc.create_signed_integer_field_class |
| 1613 | self._fc4_fn = self._tc.create_structure_field_class |
| 1614 | self._fc5_fn = self._tc.create_structure_field_class |
| 1615 | self._fc5_inner_fn = self._tc.create_signed_integer_field_class |
| 1616 | |
| 1617 | self._fc = self._create_fc(self._tc) |
| 1618 | self._def = _create_field(self._tc, self._fc) |
| 1619 | self._def['A'] = -1872 |
| 1620 | self._def['B'] = 'salut' |
| 1621 | self._def['C'] = 17.5 |
| 1622 | self._def['D'] = 16497 |
| 1623 | self._def['E'] = {} |
| 1624 | self._def['F'] = {'F_1': 52} |
| 1625 | self._def_value = { |
| 1626 | 'A': -1872, |
| 1627 | 'B': 'salut', |
| 1628 | 'C': 17.5, |
| 1629 | 'D': 16497, |
| 1630 | 'E': {}, |
| 1631 | 'F': {'F_1': 52}, |
| 1632 | } |
| 1633 | |
| 1634 | def _modify_def(self): |
| 1635 | self._def['B'] = 'hola' |
| 1636 | |
| 1637 | def test_bool_op_true(self): |
| 1638 | self.assertTrue(self._def) |
| 1639 | |
| 1640 | def test_bool_op_false(self): |
| 1641 | field = self._def['E'] |
| 1642 | self.assertFalse(field) |
| 1643 | |
| 1644 | def test_len(self): |
| 1645 | self.assertEqual(len(self._def), len(self._def_value)) |
| 1646 | |
| 1647 | def test_getitem(self): |
| 1648 | field = self._def['A'] |
| 1649 | self.assertIs(type(field), bt2._SignedIntegerField) |
| 1650 | self.assertEqual(field, -1872) |
| 1651 | |
| 1652 | def test_member_at_index_out_of_bounds_after(self): |
| 1653 | with self.assertRaises(IndexError): |
| 1654 | self._def.member_at_index(len(self._def_value)) |
| 1655 | |
| 1656 | def test_eq(self): |
| 1657 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1658 | field['A'] = -1872 |
| 1659 | field['B'] = 'salut' |
| 1660 | field['C'] = 17.5 |
| 1661 | field['D'] = 16497 |
| 1662 | field['E'] = {} |
| 1663 | field['F'] = {'F_1': 52} |
| 1664 | self.assertEqual(self._def, field) |
| 1665 | |
| 1666 | def test_eq_invalid_type(self): |
| 1667 | self.assertNotEqual(self._def, 23) |
| 1668 | |
| 1669 | def test_eq_diff_len(self): |
| 1670 | fc = self._tc.create_structure_field_class() |
| 1671 | fc.append_member('A', self._fc0_fn()) |
| 1672 | fc.append_member('B', self._fc1_fn()) |
| 1673 | fc.append_member('C', self._fc2_fn()) |
| 1674 | |
| 1675 | field = _create_field(self._tc, fc) |
| 1676 | field['A'] = -1872 |
| 1677 | field['B'] = 'salut' |
| 1678 | field['C'] = 17.5 |
| 1679 | self.assertNotEqual(self._def, field) |
| 1680 | |
| 1681 | def test_eq_diff_keys(self): |
| 1682 | fc = self._tc.create_structure_field_class() |
| 1683 | fc.append_member('U', self._fc0_fn()) |
| 1684 | fc.append_member('V', self._fc1_fn()) |
| 1685 | fc.append_member('W', self._fc2_fn()) |
| 1686 | fc.append_member('X', self._fc3_fn()) |
| 1687 | fc.append_member('Y', self._fc4_fn()) |
| 1688 | fc.append_member('Z', self._fc5_fn()) |
| 1689 | field = _create_field(self._tc, fc) |
| 1690 | field['U'] = -1871 |
| 1691 | field['V'] = "gerry" |
| 1692 | field['W'] = 18.19 |
| 1693 | field['X'] = 16497 |
| 1694 | field['Y'] = {} |
| 1695 | field['Z'] = {} |
| 1696 | self.assertNotEqual(self._def, field) |
| 1697 | |
| 1698 | def test_eq_diff_content_same_len(self): |
| 1699 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1700 | field['A'] = -1872 |
| 1701 | field['B'] = 'salut' |
| 1702 | field['C'] = 17.4 |
| 1703 | field['D'] = 16497 |
| 1704 | field['E'] = {} |
| 1705 | field['F'] = {'F_1': 0} |
| 1706 | self.assertNotEqual(self._def, field) |
| 1707 | |
| 1708 | def test_eq_same_content_diff_keys(self): |
| 1709 | fc = self._tc.create_structure_field_class() |
| 1710 | fc.append_member('A', self._fc0_fn()) |
| 1711 | fc.append_member('B', self._fc1_fn()) |
| 1712 | fc.append_member('E', self._fc2_fn()) |
| 1713 | fc.append_member('D', self._fc3_fn()) |
| 1714 | fc.append_member('C', self._fc4_fn()) |
| 1715 | fc.append_member('F', self._fc5_fn()) |
| 1716 | field = _create_field(self._tc, fc) |
| 1717 | field['A'] = -1872 |
| 1718 | field['B'] = 'salut' |
| 1719 | field['E'] = 17.5 |
| 1720 | field['D'] = 16497 |
| 1721 | field['C'] = {} |
| 1722 | field['F'] = {} |
| 1723 | self.assertNotEqual(self._def, field) |
| 1724 | |
| 1725 | def test_setitem(self): |
| 1726 | self._def['C'] = -18.47 |
| 1727 | self.assertEqual(self._def['C'], -18.47) |
| 1728 | |
| 1729 | def test_setitem_int_field(self): |
| 1730 | int_fc = self._tc.create_signed_integer_field_class(32) |
| 1731 | int_field = _create_field(self._tc, int_fc) |
| 1732 | int_field.value = 19487 |
| 1733 | self._def['D'] = int_field |
| 1734 | self.assertEqual(self._def['D'], 19487) |
| 1735 | |
| 1736 | def test_setitem_non_basic_field(self): |
| 1737 | elem_fc = self._tc.create_structure_field_class() |
| 1738 | struct_fc = self._tc.create_structure_field_class() |
| 1739 | struct_fc.append_member('A', elem_fc) |
| 1740 | struct_field = _create_field(self._tc, struct_fc) |
| 1741 | |
| 1742 | # Will fail on access to .items() of the value |
| 1743 | with self.assertRaises(AttributeError): |
| 1744 | struct_field['A'] = 23 |
| 1745 | |
| 1746 | def test_setitem_none(self): |
| 1747 | with self.assertRaises(TypeError): |
| 1748 | self._def['C'] = None |
| 1749 | |
| 1750 | def test_setitem_key_wrong_type(self): |
| 1751 | with self.assertRaises(TypeError): |
| 1752 | self._def[3] = 23 |
| 1753 | |
| 1754 | def test_setitem_wrong_key(self): |
| 1755 | with self.assertRaises(KeyError): |
| 1756 | self._def['hi'] = 134679 |
| 1757 | |
| 1758 | def test_member_at_index(self): |
| 1759 | self.assertEqual(self._def.member_at_index(1), 'salut') |
| 1760 | |
| 1761 | def test_iter(self): |
| 1762 | orig_values = { |
| 1763 | 'A': -1872, |
| 1764 | 'B': 'salut', |
| 1765 | 'C': 17.5, |
| 1766 | 'D': 16497, |
| 1767 | 'E': {}, |
| 1768 | 'F': {'F_1': 52}, |
| 1769 | } |
| 1770 | |
| 1771 | for vkey, vval in self._def.items(): |
| 1772 | val = orig_values[vkey] |
| 1773 | self.assertEqual(vval, val) |
| 1774 | |
| 1775 | def test_value(self): |
| 1776 | orig_values = { |
| 1777 | 'A': -1872, |
| 1778 | 'B': 'salut', |
| 1779 | 'C': 17.5, |
| 1780 | 'D': 16497, |
| 1781 | 'E': {}, |
| 1782 | 'F': {'F_1': 52}, |
| 1783 | } |
| 1784 | self.assertEqual(self._def, orig_values) |
| 1785 | |
| 1786 | def test_set_value(self): |
| 1787 | int_fc = self._tc.create_signed_integer_field_class(32) |
| 1788 | another_int_fc = self._tc.create_signed_integer_field_class(32) |
| 1789 | str_fc = self._tc.create_string_field_class() |
| 1790 | struct_fc = self._tc.create_structure_field_class() |
| 1791 | struct_fc.append_member(field_class=int_fc, name='an_int') |
| 1792 | struct_fc.append_member(field_class=str_fc, name='a_string') |
| 1793 | struct_fc.append_member(field_class=another_int_fc, name='another_int') |
| 1794 | values = {'an_int': 42, 'a_string': 'hello', 'another_int': 66} |
| 1795 | |
| 1796 | struct = _create_field(self._tc, struct_fc) |
| 1797 | struct.value = values |
| 1798 | self.assertEqual(values, struct) |
| 1799 | |
| 1800 | bad_type_values = copy.deepcopy(values) |
| 1801 | bad_type_values['an_int'] = 'a string' |
| 1802 | with self.assertRaises(TypeError): |
| 1803 | struct.value = bad_type_values |
| 1804 | |
| 1805 | unknown_key_values = copy.deepcopy(values) |
| 1806 | unknown_key_values['unknown_key'] = 16546 |
| 1807 | with self.assertRaises(KeyError): |
| 1808 | struct.value = unknown_key_values |
| 1809 | |
| 1810 | def test_str_op(self): |
| 1811 | expected_string_found = False |
| 1812 | s = str(self._def) |
| 1813 | # Establish all permutations of the three expected matches since |
| 1814 | # the order in which mappings are enumerated is not explicitly part of |
| 1815 | # the API. |
| 1816 | for p in itertools.permutations([(k, v) for k, v in self._def.items()]): |
| 1817 | items = ['{}: {}'.format(repr(k), repr(v)) for k, v in p] |
| 1818 | candidate = '{{{}}}'.format(', '.join(items)) |
| 1819 | if candidate == s: |
| 1820 | expected_string_found = True |
| 1821 | break |
| 1822 | |
| 1823 | self.assertTrue(expected_string_found) |
| 1824 | |
| 1825 | |
| 1826 | class VariantFieldTestCase(unittest.TestCase): |
| 1827 | def _create_fc(self, tc): |
| 1828 | ft0 = tc.create_signed_integer_field_class(32) |
| 1829 | ft1 = tc.create_string_field_class() |
| 1830 | ft2 = tc.create_real_field_class() |
| 1831 | ft3 = tc.create_signed_integer_field_class(17) |
| 1832 | fc = tc.create_variant_field_class() |
| 1833 | fc.append_option('corner', ft0) |
| 1834 | fc.append_option('zoom', ft1) |
| 1835 | fc.append_option('mellotron', ft2) |
| 1836 | fc.append_option('giorgio', ft3) |
| 1837 | top_fc = tc.create_structure_field_class() |
| 1838 | top_fc.append_member('variant_field', fc) |
| 1839 | return top_fc |
| 1840 | |
| 1841 | def setUp(self): |
| 1842 | self._tc = get_default_trace_class() |
| 1843 | fld = _create_field(self._tc, self._create_fc(self._tc)) |
| 1844 | self._def = fld['variant_field'] |
| 1845 | |
| 1846 | def test_bool_op(self): |
| 1847 | self._def.selected_option_index = 2 |
| 1848 | self._def.value = -17.34 |
| 1849 | with self.assertRaises(NotImplementedError): |
| 1850 | bool(self._def) |
| 1851 | |
| 1852 | def test_selected_option_index(self): |
| 1853 | self._def.selected_option_index = 2 |
| 1854 | self.assertEqual(self._def.selected_option_index, 2) |
| 1855 | |
| 1856 | def test_selected_option(self): |
| 1857 | self._def.selected_option_index = 2 |
| 1858 | self._def.value = -17.34 |
| 1859 | self.assertEqual(self._def.selected_option, -17.34) |
| 1860 | |
| 1861 | self._def.selected_option_index = 3 |
| 1862 | self._def.value = 1921 |
| 1863 | self.assertEqual(self._def.selected_option, 1921) |
| 1864 | |
| 1865 | def test_eq(self): |
| 1866 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1867 | field = field['variant_field'] |
| 1868 | field.selected_option_index = 0 |
| 1869 | field.value = 1774 |
| 1870 | self._def.selected_option_index = 0 |
| 1871 | self._def.value = 1774 |
| 1872 | self.assertEqual(self._def, field) |
| 1873 | |
| 1874 | def test_eq_invalid_type(self): |
| 1875 | self._def.selected_option_index = 1 |
| 1876 | self._def.value = 'gerry' |
| 1877 | self.assertNotEqual(self._def, 23) |
| 1878 | |
| 1879 | def test_str_op_int(self): |
| 1880 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1881 | field = field['variant_field'] |
| 1882 | field.selected_option_index = 0 |
| 1883 | field.value = 1774 |
| 1884 | other_field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1885 | other_field = other_field['variant_field'] |
| 1886 | other_field.selected_option_index = 0 |
| 1887 | other_field.value = 1774 |
| 1888 | self.assertEqual(str(field), str(other_field)) |
| 1889 | |
| 1890 | def test_str_op_str(self): |
| 1891 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1892 | field = field['variant_field'] |
| 1893 | field.selected_option_index = 1 |
| 1894 | field.value = 'un beau grand bateau' |
| 1895 | other_field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1896 | other_field = other_field['variant_field'] |
| 1897 | other_field.selected_option_index = 1 |
| 1898 | other_field.value = 'un beau grand bateau' |
| 1899 | self.assertEqual(str(field), str(other_field)) |
| 1900 | |
| 1901 | def test_str_op_float(self): |
| 1902 | field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1903 | field = field['variant_field'] |
| 1904 | field.selected_option_index = 2 |
| 1905 | field.value = 14.4245 |
| 1906 | other_field = _create_field(self._tc, self._create_fc(self._tc)) |
| 1907 | other_field = other_field['variant_field'] |
| 1908 | other_field.selected_option_index = 2 |
| 1909 | other_field.value = 14.4245 |
| 1910 | self.assertEqual(str(field), str(other_field)) |