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