| 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 numbers |
| 23 | import math |
| 24 | import copy |
| 25 | import bt2 |
| 26 | |
| 27 | |
| 28 | # The value object classes explicitly do not implement the copy methods, |
| 29 | # raising `NotImplementedError`, just in case we decide to implement |
| 30 | # them someday. |
| 31 | class _TestCopySimple: |
| 32 | def test_copy(self): |
| 33 | with self.assertRaises(NotImplementedError): |
| 34 | copy.copy(self._def) |
| 35 | |
| 36 | def test_deepcopy(self): |
| 37 | with self.assertRaises(NotImplementedError): |
| 38 | copy.deepcopy(self._def) |
| 39 | |
| 40 | |
| 41 | _COMP_BINOPS = ( |
| 42 | operator.eq, |
| 43 | operator.ne, |
| 44 | ) |
| 45 | |
| 46 | |
| 47 | # Base class for numeric value test cases. |
| 48 | # |
| 49 | # To be compatible with this base class, a derived class must, in its |
| 50 | # setUp() method: |
| 51 | # |
| 52 | # * Set `self._def` to a value object with an arbitrary raw value. |
| 53 | # * Set `self._def_value` to the equivalent raw value of `self._def`. |
| 54 | class _TestNumericValue(_TestCopySimple): |
| 55 | # Tries the binary operation `op`: |
| 56 | # |
| 57 | # 1. Between `self._def`, which is a value object, and `rhs`. |
| 58 | # 2. Between `self._def_value`, which is the raw value of |
| 59 | # `self._def`, and `rhs`. |
| 60 | # |
| 61 | # Returns the results of 1. and 2. |
| 62 | # |
| 63 | # If there's an exception while performing 1. or 2., asserts that |
| 64 | # both operations raised exceptions, that both exceptions have the |
| 65 | # same type, and returns `None` for both results. |
| 66 | def _binop(self, op, rhs): |
| 67 | type_rexc = None |
| 68 | type_rvexc = None |
| 69 | comp_value = rhs |
| 70 | |
| 71 | # try with value object |
| 72 | try: |
| 73 | r = op(self._def, rhs) |
| 74 | except Exception as e: |
| 75 | type_rexc = type(e) |
| 76 | |
| 77 | # try with raw value |
| 78 | try: |
| 79 | rv = op(self._def_value, comp_value) |
| 80 | except Exception as e: |
| 81 | type_rvexc = type(e) |
| 82 | |
| 83 | if type_rexc is not None or type_rvexc is not None: |
| 84 | # at least one of the operations raised an exception: in |
| 85 | # this case both operations should have raised the same |
| 86 | # type of exception (division by zero, bit shift with a |
| 87 | # floating point number operand, etc.) |
| 88 | self.assertIs(type_rexc, type_rvexc) |
| 89 | return None, None |
| 90 | |
| 91 | return r, rv |
| 92 | |
| 93 | # Tries the unary operation `op`: |
| 94 | # |
| 95 | # 1. On `self._def`, which is a value object. |
| 96 | # 2. On `self._def_value`, which is the raw value of `self._def`. |
| 97 | # |
| 98 | # Returns the results of 1. and 2. |
| 99 | # |
| 100 | # If there's an exception while performing 1. or 2., asserts that |
| 101 | # both operations raised exceptions, that both exceptions have the |
| 102 | # same type, and returns `None` for both results. |
| 103 | def _unaryop(self, op): |
| 104 | type_rexc = None |
| 105 | type_rvexc = None |
| 106 | |
| 107 | # try with value object |
| 108 | try: |
| 109 | r = op(self._def) |
| 110 | except Exception as e: |
| 111 | type_rexc = type(e) |
| 112 | |
| 113 | # try with raw value |
| 114 | try: |
| 115 | rv = op(self._def_value) |
| 116 | except Exception as e: |
| 117 | type_rvexc = type(e) |
| 118 | |
| 119 | if type_rexc is not None or type_rvexc is not None: |
| 120 | # at least one of the operations raised an exception: in |
| 121 | # this case both operations should have raised the same |
| 122 | # type of exception (division by zero, bit shift with a |
| 123 | # floating point number operand, etc.) |
| 124 | self.assertIs(type_rexc, type_rvexc) |
| 125 | return None, None |
| 126 | |
| 127 | return r, rv |
| 128 | |
| 129 | # Tests that the unary operation `op` gives results with the same |
| 130 | # type for both `self._def` and `self._def_value`. |
| 131 | def _test_unaryop_type(self, op): |
| 132 | r, rv = self._unaryop(op) |
| 133 | |
| 134 | if r is None: |
| 135 | return |
| 136 | |
| 137 | self.assertIsInstance(r, type(rv)) |
| 138 | |
| 139 | # Tests that the unary operation `op` gives results with the same |
| 140 | # value for both `self._def` and `self._def_value`. This uses the |
| 141 | # __eq__() operator of `self._def`. |
| 142 | def _test_unaryop_value(self, op): |
| 143 | r, rv = self._unaryop(op) |
| 144 | |
| 145 | if r is None: |
| 146 | return |
| 147 | |
| 148 | self.assertEqual(r, rv) |
| 149 | |
| 150 | # Tests that the unary operation `op`, when applied to `self._def`, |
| 151 | # does not change its underlying BT object address. |
| 152 | def _test_unaryop_addr_same(self, op): |
| 153 | addr_before = self._def.addr |
| 154 | self._unaryop(op) |
| 155 | self.assertEqual(self._def.addr, addr_before) |
| 156 | |
| 157 | # Tests that the unary operation `op`, when applied to `self._def`, |
| 158 | # does not change its value. |
| 159 | def _test_unaryop_value_same(self, op): |
| 160 | value_before = self._def.__class__(self._def) |
| 161 | self._unaryop(op) |
| 162 | self.assertEqual(self._def, value_before) |
| 163 | |
| 164 | # Tests that the binary operation `op` gives results with the same |
| 165 | # type for both `self._def` and `self._def_value`. |
| 166 | def _test_binop_type(self, op, rhs): |
| 167 | r, rv = self._binop(op, rhs) |
| 168 | |
| 169 | if r is None: |
| 170 | return |
| 171 | |
| 172 | if op in _COMP_BINOPS: |
| 173 | # __eq__() and __ne__() always return a 'bool' object |
| 174 | self.assertIsInstance(r, bool) |
| 175 | else: |
| 176 | self.assertIsInstance(r, type(rv)) |
| 177 | |
| 178 | # Tests that the binary operation `op` gives results with the same |
| 179 | # value for both `self._def` and `self._def_value`. This uses the |
| 180 | # __eq__() operator of `self._def`. |
| 181 | def _test_binop_value(self, op, rhs): |
| 182 | r, rv = self._binop(op, rhs) |
| 183 | |
| 184 | if r is None: |
| 185 | return |
| 186 | |
| 187 | self.assertEqual(r, rv) |
| 188 | |
| 189 | # Tests that the binary operation `op`, when applied to `self._def`, |
| 190 | # does not change its underlying BT object address. |
| 191 | def _test_binop_lhs_addr_same(self, op, rhs): |
| 192 | addr_before = self._def.addr |
| 193 | r, rv = self._binop(op, rhs) |
| 194 | self.assertEqual(self._def.addr, addr_before) |
| 195 | |
| 196 | # Tests that the binary operation `op`, when applied to `self._def`, |
| 197 | # does not change its value. |
| 198 | def _test_binop_lhs_value_same(self, op, rhs): |
| 199 | value_before = self._def.__class__(self._def) |
| 200 | r, rv = self._binop(op, rhs) |
| 201 | self.assertEqual(self._def, value_before) |
| 202 | |
| 203 | # The methods below which take the `test_cb` and `op` parameters |
| 204 | # are meant to be used with one of the _test_binop_*() functions |
| 205 | # above as `test_cb` and a binary operator function as `op`. |
| 206 | # |
| 207 | # For example: |
| 208 | # |
| 209 | # self._test_binop_rhs_pos_int(self._test_binop_value, |
| 210 | # operator.add) |
| 211 | # |
| 212 | # This tests that a numeric value object added to a positive integer |
| 213 | # raw value gives a result with the expected value. |
| 214 | # |
| 215 | # `vint` and `vfloat` mean a signed integer value object and a real |
| 216 | # value object. |
| 217 | |
| 218 | def _test_binop_invalid_unknown(self, op): |
| 219 | if op in _COMP_BINOPS: |
| 220 | self.skipTest('not testing') |
| 221 | |
| 222 | with self.assertRaises(TypeError): |
| 223 | op(self._def, object()) |
| 224 | |
| 225 | def _test_binop_invalid_none(self, op): |
| 226 | if op in _COMP_BINOPS: |
| 227 | self.skipTest('not testing') |
| 228 | |
| 229 | with self.assertRaises(TypeError): |
| 230 | op(self._def, None) |
| 231 | |
| 232 | def _test_binop_rhs_false(self, test_cb, op): |
| 233 | test_cb(op, False) |
| 234 | |
| 235 | def _test_binop_rhs_true(self, test_cb, op): |
| 236 | test_cb(op, True) |
| 237 | |
| 238 | def _test_binop_rhs_pos_int(self, test_cb, op): |
| 239 | test_cb(op, 2) |
| 240 | |
| 241 | def _test_binop_rhs_neg_int(self, test_cb, op): |
| 242 | test_cb(op, -23) |
| 243 | |
| 244 | def _test_binop_rhs_zero_int(self, test_cb, op): |
| 245 | test_cb(op, 0) |
| 246 | |
| 247 | def _test_binop_rhs_pos_vint(self, test_cb, op): |
| 248 | test_cb(op, bt2.create_value(2)) |
| 249 | |
| 250 | def _test_binop_rhs_neg_vint(self, test_cb, op): |
| 251 | test_cb(op, bt2.create_value(-23)) |
| 252 | |
| 253 | def _test_binop_rhs_zero_vint(self, test_cb, op): |
| 254 | test_cb(op, bt2.create_value(0)) |
| 255 | |
| 256 | def _test_binop_rhs_pos_float(self, test_cb, op): |
| 257 | test_cb(op, 2.2) |
| 258 | |
| 259 | def _test_binop_rhs_neg_float(self, test_cb, op): |
| 260 | test_cb(op, -23.4) |
| 261 | |
| 262 | def _test_binop_rhs_zero_float(self, test_cb, op): |
| 263 | test_cb(op, 0.0) |
| 264 | |
| 265 | def _test_binop_rhs_pos_vfloat(self, test_cb, op): |
| 266 | test_cb(op, bt2.create_value(2.2)) |
| 267 | |
| 268 | def _test_binop_rhs_neg_vfloat(self, test_cb, op): |
| 269 | test_cb(op, bt2.create_value(-23.4)) |
| 270 | |
| 271 | def _test_binop_rhs_zero_vfloat(self, test_cb, op): |
| 272 | test_cb(op, bt2.create_value(0.0)) |
| 273 | |
| 274 | def _test_binop_type_false(self, op): |
| 275 | self._test_binop_rhs_false(self._test_binop_type, op) |
| 276 | |
| 277 | def _test_binop_type_true(self, op): |
| 278 | self._test_binop_rhs_true(self._test_binop_type, op) |
| 279 | |
| 280 | def _test_binop_type_pos_int(self, op): |
| 281 | self._test_binop_rhs_pos_int(self._test_binop_type, op) |
| 282 | |
| 283 | def _test_binop_type_neg_int(self, op): |
| 284 | self._test_binop_rhs_neg_int(self._test_binop_type, op) |
| 285 | |
| 286 | def _test_binop_type_zero_int(self, op): |
| 287 | self._test_binop_rhs_zero_int(self._test_binop_type, op) |
| 288 | |
| 289 | def _test_binop_type_pos_vint(self, op): |
| 290 | self._test_binop_rhs_pos_vint(self._test_binop_type, op) |
| 291 | |
| 292 | def _test_binop_type_neg_vint(self, op): |
| 293 | self._test_binop_rhs_neg_vint(self._test_binop_type, op) |
| 294 | |
| 295 | def _test_binop_type_zero_vint(self, op): |
| 296 | self._test_binop_rhs_zero_vint(self._test_binop_type, op) |
| 297 | |
| 298 | def _test_binop_type_pos_float(self, op): |
| 299 | self._test_binop_rhs_pos_float(self._test_binop_type, op) |
| 300 | |
| 301 | def _test_binop_type_neg_float(self, op): |
| 302 | self._test_binop_rhs_neg_float(self._test_binop_type, op) |
| 303 | |
| 304 | def _test_binop_type_zero_float(self, op): |
| 305 | self._test_binop_rhs_zero_float(self._test_binop_type, op) |
| 306 | |
| 307 | def _test_binop_type_pos_vfloat(self, op): |
| 308 | self._test_binop_rhs_pos_vfloat(self._test_binop_type, op) |
| 309 | |
| 310 | def _test_binop_type_neg_vfloat(self, op): |
| 311 | self._test_binop_rhs_neg_vfloat(self._test_binop_type, op) |
| 312 | |
| 313 | def _test_binop_type_zero_vfloat(self, op): |
| 314 | self._test_binop_rhs_zero_vfloat(self._test_binop_type, op) |
| 315 | |
| 316 | def _test_binop_value_false(self, op): |
| 317 | self._test_binop_rhs_false(self._test_binop_value, op) |
| 318 | |
| 319 | def _test_binop_value_true(self, op): |
| 320 | self._test_binop_rhs_true(self._test_binop_value, op) |
| 321 | |
| 322 | def _test_binop_value_pos_int(self, op): |
| 323 | self._test_binop_rhs_pos_int(self._test_binop_value, op) |
| 324 | |
| 325 | def _test_binop_value_neg_int(self, op): |
| 326 | self._test_binop_rhs_neg_int(self._test_binop_value, op) |
| 327 | |
| 328 | def _test_binop_value_zero_int(self, op): |
| 329 | self._test_binop_rhs_zero_int(self._test_binop_value, op) |
| 330 | |
| 331 | def _test_binop_value_pos_vint(self, op): |
| 332 | self._test_binop_rhs_pos_vint(self._test_binop_value, op) |
| 333 | |
| 334 | def _test_binop_value_neg_vint(self, op): |
| 335 | self._test_binop_rhs_neg_vint(self._test_binop_value, op) |
| 336 | |
| 337 | def _test_binop_value_zero_vint(self, op): |
| 338 | self._test_binop_rhs_zero_vint(self._test_binop_value, op) |
| 339 | |
| 340 | def _test_binop_value_pos_float(self, op): |
| 341 | self._test_binop_rhs_pos_float(self._test_binop_value, op) |
| 342 | |
| 343 | def _test_binop_value_neg_float(self, op): |
| 344 | self._test_binop_rhs_neg_float(self._test_binop_value, op) |
| 345 | |
| 346 | def _test_binop_value_zero_float(self, op): |
| 347 | self._test_binop_rhs_zero_float(self._test_binop_value, op) |
| 348 | |
| 349 | def _test_binop_value_pos_vfloat(self, op): |
| 350 | self._test_binop_rhs_pos_vfloat(self._test_binop_value, op) |
| 351 | |
| 352 | def _test_binop_value_neg_vfloat(self, op): |
| 353 | self._test_binop_rhs_neg_vfloat(self._test_binop_value, op) |
| 354 | |
| 355 | def _test_binop_value_zero_vfloat(self, op): |
| 356 | self._test_binop_rhs_zero_vfloat(self._test_binop_value, op) |
| 357 | |
| 358 | def _test_binop_lhs_addr_same_false(self, op): |
| 359 | self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op) |
| 360 | |
| 361 | def _test_binop_lhs_addr_same_true(self, op): |
| 362 | self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op) |
| 363 | |
| 364 | def _test_binop_lhs_addr_same_pos_int(self, op): |
| 365 | self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op) |
| 366 | |
| 367 | def _test_binop_lhs_addr_same_neg_int(self, op): |
| 368 | self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op) |
| 369 | |
| 370 | def _test_binop_lhs_addr_same_zero_int(self, op): |
| 371 | self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op) |
| 372 | |
| 373 | def _test_binop_lhs_addr_same_pos_vint(self, op): |
| 374 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op) |
| 375 | |
| 376 | def _test_binop_lhs_addr_same_neg_vint(self, op): |
| 377 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op) |
| 378 | |
| 379 | def _test_binop_lhs_addr_same_zero_vint(self, op): |
| 380 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op) |
| 381 | |
| 382 | def _test_binop_lhs_addr_same_pos_float(self, op): |
| 383 | self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op) |
| 384 | |
| 385 | def _test_binop_lhs_addr_same_neg_float(self, op): |
| 386 | self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op) |
| 387 | |
| 388 | def _test_binop_lhs_addr_same_zero_float(self, op): |
| 389 | self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op) |
| 390 | |
| 391 | def _test_binop_lhs_addr_same_pos_vfloat(self, op): |
| 392 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op) |
| 393 | |
| 394 | def _test_binop_lhs_addr_same_neg_vfloat(self, op): |
| 395 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op) |
| 396 | |
| 397 | def _test_binop_lhs_addr_same_zero_vfloat(self, op): |
| 398 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op) |
| 399 | |
| 400 | def _test_binop_lhs_value_same_false(self, op): |
| 401 | self._test_binop_rhs_false(self._test_binop_lhs_value_same, op) |
| 402 | |
| 403 | def _test_binop_lhs_value_same_true(self, op): |
| 404 | self._test_binop_rhs_true(self._test_binop_lhs_value_same, op) |
| 405 | |
| 406 | def _test_binop_lhs_value_same_pos_int(self, op): |
| 407 | self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op) |
| 408 | |
| 409 | def _test_binop_lhs_value_same_neg_int(self, op): |
| 410 | self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op) |
| 411 | |
| 412 | def _test_binop_lhs_value_same_zero_int(self, op): |
| 413 | self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op) |
| 414 | |
| 415 | def _test_binop_lhs_value_same_pos_vint(self, op): |
| 416 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op) |
| 417 | |
| 418 | def _test_binop_lhs_value_same_neg_vint(self, op): |
| 419 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op) |
| 420 | |
| 421 | def _test_binop_lhs_value_same_zero_vint(self, op): |
| 422 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op) |
| 423 | |
| 424 | def _test_binop_lhs_value_same_pos_float(self, op): |
| 425 | self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op) |
| 426 | |
| 427 | def _test_binop_lhs_value_same_neg_float(self, op): |
| 428 | self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op) |
| 429 | |
| 430 | def _test_binop_lhs_value_same_zero_float(self, op): |
| 431 | self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op) |
| 432 | |
| 433 | def _test_binop_lhs_value_same_pos_vfloat(self, op): |
| 434 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op) |
| 435 | |
| 436 | def _test_binop_lhs_value_same_neg_vfloat(self, op): |
| 437 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op) |
| 438 | |
| 439 | def _test_binop_lhs_value_same_zero_vfloat(self, op): |
| 440 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op) |
| 441 | |
| 442 | def test_bool_op(self): |
| 443 | self.assertEqual(bool(self._def), bool(self._def_value)) |
| 444 | |
| 445 | def test_int_op(self): |
| 446 | self.assertEqual(int(self._def), int(self._def_value)) |
| 447 | |
| 448 | def test_float_op(self): |
| 449 | self.assertEqual(float(self._def), float(self._def_value)) |
| 450 | |
| 451 | def test_complex_op(self): |
| 452 | self.assertEqual(complex(self._def), complex(self._def_value)) |
| 453 | |
| 454 | def test_str_op(self): |
| 455 | self.assertEqual(str(self._def), str(self._def_value)) |
| 456 | |
| 457 | def test_eq_none(self): |
| 458 | self.assertFalse(self._def == None) |
| 459 | |
| 460 | def test_ne_none(self): |
| 461 | self.assertTrue(self._def != None) |
| 462 | |
| 463 | |
| 464 | # This is a list of binary operators used for |
| 465 | # _inject_numeric_testing_methods(). |
| 466 | # |
| 467 | # Each entry is a pair of binary operator name (used as part of the |
| 468 | # created testing method's name) and operator function. |
| 469 | _BINOPS = ( |
| 470 | ('lt', operator.lt), |
| 471 | ('le', operator.le), |
| 472 | ('eq', operator.eq), |
| 473 | ('ne', operator.ne), |
| 474 | ('ge', operator.ge), |
| 475 | ('gt', operator.gt), |
| 476 | ('add', operator.add), |
| 477 | ('radd', lambda a, b: operator.add(b, a)), |
| 478 | ('and', operator.and_), |
| 479 | ('rand', lambda a, b: operator.and_(b, a)), |
| 480 | ('floordiv', operator.floordiv), |
| 481 | ('rfloordiv', lambda a, b: operator.floordiv(b, a)), |
| 482 | ('lshift', operator.lshift), |
| 483 | ('rlshift', lambda a, b: operator.lshift(b, a)), |
| 484 | ('mod', operator.mod), |
| 485 | ('rmod', lambda a, b: operator.mod(b, a)), |
| 486 | ('mul', operator.mul), |
| 487 | ('rmul', lambda a, b: operator.mul(b, a)), |
| 488 | ('or', operator.or_), |
| 489 | ('ror', lambda a, b: operator.or_(b, a)), |
| 490 | ('pow', operator.pow), |
| 491 | ('rpow', lambda a, b: operator.pow(b, a)), |
| 492 | ('rshift', operator.rshift), |
| 493 | ('rrshift', lambda a, b: operator.rshift(b, a)), |
| 494 | ('sub', operator.sub), |
| 495 | ('rsub', lambda a, b: operator.sub(b, a)), |
| 496 | ('truediv', operator.truediv), |
| 497 | ('rtruediv', lambda a, b: operator.truediv(b, a)), |
| 498 | ('xor', operator.xor), |
| 499 | ('rxor', lambda a, b: operator.xor(b, a)), |
| 500 | ) |
| 501 | |
| 502 | |
| 503 | # This is a list of unary operators used for |
| 504 | # _inject_numeric_testing_methods(). |
| 505 | # |
| 506 | # Each entry is a pair of unary operator name (used as part of the |
| 507 | # created testing method's name) and operator function. |
| 508 | _UNARYOPS = ( |
| 509 | ('neg', operator.neg), |
| 510 | ('pos', operator.pos), |
| 511 | ('abs', operator.abs), |
| 512 | ('invert', operator.invert), |
| 513 | ('round', round), |
| 514 | ('round_0', partial(round, ndigits=0)), |
| 515 | ('round_1', partial(round, ndigits=1)), |
| 516 | ('round_2', partial(round, ndigits=2)), |
| 517 | ('round_3', partial(round, ndigits=3)), |
| 518 | ('ceil', math.ceil), |
| 519 | ('floor', math.floor), |
| 520 | ('trunc', math.trunc), |
| 521 | ) |
| 522 | |
| 523 | |
| 524 | # This function injects a bunch of testing methods to a numeric |
| 525 | # value test case. |
| 526 | # |
| 527 | # It is meant to be used like this: |
| 528 | # |
| 529 | # _inject_numeric_testing_methods(MyNumericValueTestCase) |
| 530 | # |
| 531 | # If `has_neg` is true, then the function injects testing methods which |
| 532 | # involve operations with a negative value. |
| 533 | # |
| 534 | # This function injects: |
| 535 | # |
| 536 | # * One testing method for each _TestNumericValue._test_binop_*() |
| 537 | # method, for each binary operator in the _BINOPS tuple. |
| 538 | # |
| 539 | # * One testing method for each _TestNumericValue._test_unaryop*() |
| 540 | # method, for each unary operator in the _UNARYOPS tuple. |
| 541 | def _inject_numeric_testing_methods(cls, has_neg=True): |
| 542 | def test_binop_name(suffix): |
| 543 | return 'test_binop_{}_{}'.format(name, suffix) |
| 544 | |
| 545 | def test_unaryop_name(suffix): |
| 546 | return 'test_unaryop_{}_{}'.format(name, suffix) |
| 547 | |
| 548 | # inject testing methods for each binary operation |
| 549 | for name, binop in _BINOPS: |
| 550 | setattr(cls, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue._test_binop_invalid_unknown, op=binop)) |
| 551 | setattr(cls, test_binop_name('invalid_none'), partialmethod(_TestNumericValue._test_binop_invalid_none, op=binop)) |
| 552 | setattr(cls, test_binop_name('type_true'), partialmethod(_TestNumericValue._test_binop_type_true, op=binop)) |
| 553 | setattr(cls, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue._test_binop_type_pos_int, op=binop)) |
| 554 | setattr(cls, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue._test_binop_type_pos_vint, op=binop)) |
| 555 | setattr(cls, test_binop_name('value_true'), partialmethod(_TestNumericValue._test_binop_value_true, op=binop)) |
| 556 | setattr(cls, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue._test_binop_value_pos_int, op=binop)) |
| 557 | setattr(cls, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue._test_binop_value_pos_vint, op=binop)) |
| 558 | setattr(cls, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_true, op=binop)) |
| 559 | setattr(cls, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_int, op=binop)) |
| 560 | setattr(cls, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_vint, op=binop)) |
| 561 | setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_true, op=binop)) |
| 562 | setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_int, op=binop)) |
| 563 | setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_vint, op=binop)) |
| 564 | |
| 565 | if has_neg: |
| 566 | setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue._test_binop_type_neg_int, op=binop)) |
| 567 | setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue._test_binop_type_neg_vint, op=binop)) |
| 568 | setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue._test_binop_value_neg_int, op=binop)) |
| 569 | setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue._test_binop_value_neg_vint, op=binop)) |
| 570 | setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_int, op=binop)) |
| 571 | setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vint, op=binop)) |
| 572 | setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_int, op=binop)) |
| 573 | setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vint, op=binop)) |
| 574 | |
| 575 | setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericValue._test_binop_type_false, op=binop)) |
| 576 | setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue._test_binop_type_zero_int, op=binop)) |
| 577 | setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue._test_binop_type_zero_vint, op=binop)) |
| 578 | setattr(cls, test_binop_name('value_false'), partialmethod(_TestNumericValue._test_binop_value_false, op=binop)) |
| 579 | setattr(cls, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue._test_binop_value_zero_int, op=binop)) |
| 580 | setattr(cls, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue._test_binop_value_zero_vint, op=binop)) |
| 581 | setattr(cls, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_false, op=binop)) |
| 582 | setattr(cls, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_int, op=binop)) |
| 583 | setattr(cls, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_vint, op=binop)) |
| 584 | setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_false, op=binop)) |
| 585 | setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_int, op=binop)) |
| 586 | setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vint, op=binop)) |
| 587 | |
| 588 | if has_neg: |
| 589 | setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue._test_binop_type_neg_float, op=binop)) |
| 590 | setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_type_neg_vfloat, op=binop)) |
| 591 | setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue._test_binop_value_neg_float, op=binop)) |
| 592 | setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_value_neg_vfloat, op=binop)) |
| 593 | setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_float, op=binop)) |
| 594 | setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vfloat, op=binop)) |
| 595 | setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_float, op=binop)) |
| 596 | setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vfloat, op=binop)) |
| 597 | |
| 598 | setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue._test_binop_type_pos_float, op=binop)) |
| 599 | setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_type_pos_vfloat, op=binop)) |
| 600 | setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue._test_binop_value_pos_float, op=binop)) |
| 601 | setattr(cls, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_value_pos_vfloat, op=binop)) |
| 602 | setattr(cls, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_float, op=binop)) |
| 603 | setattr(cls, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_vfloat, op=binop)) |
| 604 | setattr(cls, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_float, op=binop)) |
| 605 | setattr(cls, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_vfloat, op=binop)) |
| 606 | setattr(cls, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue._test_binop_type_zero_float, op=binop)) |
| 607 | setattr(cls, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_type_zero_vfloat, op=binop)) |
| 608 | setattr(cls, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue._test_binop_value_zero_float, op=binop)) |
| 609 | setattr(cls, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_value_zero_vfloat, op=binop)) |
| 610 | setattr(cls, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_float, op=binop)) |
| 611 | setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_vfloat, op=binop)) |
| 612 | setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_float, op=binop)) |
| 613 | setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vfloat, op=binop)) |
| 614 | |
| 615 | # inject testing methods for each unary operation |
| 616 | for name, unaryop in _UNARYOPS: |
| 617 | setattr(cls, test_unaryop_name('type'), partialmethod(_TestNumericValue._test_unaryop_type, op=unaryop)) |
| 618 | setattr(cls, test_unaryop_name('value'), partialmethod(_TestNumericValue._test_unaryop_value, op=unaryop)) |
| 619 | setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue._test_unaryop_addr_same, op=unaryop)) |
| 620 | setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericValue._test_unaryop_value_same, op=unaryop)) |
| 621 | |
| 622 | |
| 623 | class CreateValueFuncTestCase(unittest.TestCase): |
| 624 | def test_create_none(self): |
| 625 | v = bt2.create_value(None) |
| 626 | self.assertIsNone(v) |
| 627 | |
| 628 | def test_create_bool_false(self): |
| 629 | v = bt2.create_value(False) |
| 630 | self.assertIsInstance(v, bt2.BoolValue) |
| 631 | self.assertFalse(v) |
| 632 | |
| 633 | def test_create_bool_true(self): |
| 634 | v = bt2.create_value(True) |
| 635 | self.assertIsInstance(v, bt2.BoolValue) |
| 636 | self.assertTrue(v) |
| 637 | |
| 638 | def test_create_int_pos(self): |
| 639 | raw = 23 |
| 640 | v = bt2.create_value(raw) |
| 641 | self.assertIsInstance(v, bt2.SignedIntegerValue) |
| 642 | self.assertEqual(v, raw) |
| 643 | |
| 644 | def test_create_int_neg(self): |
| 645 | raw = -23 |
| 646 | v = bt2.create_value(raw) |
| 647 | self.assertIsInstance(v, bt2.SignedIntegerValue) |
| 648 | self.assertEqual(v, raw) |
| 649 | |
| 650 | def test_create_float_pos(self): |
| 651 | raw = 17.5 |
| 652 | v = bt2.create_value(raw) |
| 653 | self.assertIsInstance(v, bt2.RealValue) |
| 654 | self.assertEqual(v, raw) |
| 655 | |
| 656 | def test_create_float_neg(self): |
| 657 | raw = -17.5 |
| 658 | v = bt2.create_value(raw) |
| 659 | self.assertIsInstance(v, bt2.RealValue) |
| 660 | self.assertEqual(v, raw) |
| 661 | |
| 662 | def test_create_string(self): |
| 663 | raw = 'salut' |
| 664 | v = bt2.create_value(raw) |
| 665 | self.assertIsInstance(v, bt2.StringValue) |
| 666 | self.assertEqual(v, raw) |
| 667 | |
| 668 | def test_create_string_empty(self): |
| 669 | raw = '' |
| 670 | v = bt2.create_value(raw) |
| 671 | self.assertIsInstance(v, bt2.StringValue) |
| 672 | self.assertEqual(v, raw) |
| 673 | |
| 674 | def test_create_array_from_list(self): |
| 675 | raw = [1, 2, 3] |
| 676 | v = bt2.create_value(raw) |
| 677 | self.assertIsInstance(v, bt2.ArrayValue) |
| 678 | self.assertEqual(v, raw) |
| 679 | |
| 680 | def test_create_array_from_tuple(self): |
| 681 | raw = 4, 5, 6 |
| 682 | v = bt2.create_value(raw) |
| 683 | self.assertIsInstance(v, bt2.ArrayValue) |
| 684 | self.assertEqual(v, raw) |
| 685 | |
| 686 | def test_create_array_from_empty_list(self): |
| 687 | raw = [] |
| 688 | v = bt2.create_value(raw) |
| 689 | self.assertIsInstance(v, bt2.ArrayValue) |
| 690 | self.assertEqual(v, raw) |
| 691 | |
| 692 | def test_create_array_from_empty_tuple(self): |
| 693 | raw = () |
| 694 | v = bt2.create_value(raw) |
| 695 | self.assertIsInstance(v, bt2.ArrayValue) |
| 696 | self.assertEqual(v, raw) |
| 697 | |
| 698 | def test_create_map(self): |
| 699 | raw = {'salut': 23} |
| 700 | v = bt2.create_value(raw) |
| 701 | self.assertIsInstance(v, bt2.MapValue) |
| 702 | self.assertEqual(v, raw) |
| 703 | |
| 704 | def test_create_map_empty(self): |
| 705 | raw = {} |
| 706 | v = bt2.create_value(raw) |
| 707 | self.assertIsInstance(v, bt2.MapValue) |
| 708 | self.assertEqual(v, raw) |
| 709 | |
| 710 | def test_create_vfalse(self): |
| 711 | v = bt2.create_value(bt2.create_value(False)) |
| 712 | self.assertIsInstance(v, bt2.BoolValue) |
| 713 | self.assertFalse(v) |
| 714 | |
| 715 | def test_create_invalid(self): |
| 716 | class A: |
| 717 | pass |
| 718 | |
| 719 | a = A() |
| 720 | |
| 721 | with self.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm: |
| 722 | v = bt2.create_value(a) |
| 723 | |
| 724 | |
| 725 | class BoolValueTestCase(_TestCopySimple, unittest.TestCase): |
| 726 | def setUp(self): |
| 727 | self._f = bt2.BoolValue(False) |
| 728 | self._t = bt2.BoolValue(True) |
| 729 | self._def = self._f |
| 730 | self._def_value = False |
| 731 | self._def_new_value = True |
| 732 | |
| 733 | def tearDown(self): |
| 734 | del self._f |
| 735 | del self._t |
| 736 | del self._def |
| 737 | |
| 738 | def _assert_expecting_bool(self): |
| 739 | return self.assertRaisesRegex(TypeError, r"expecting a 'bool' object") |
| 740 | |
| 741 | def test_create_default(self): |
| 742 | b = bt2.BoolValue() |
| 743 | self.assertFalse(b) |
| 744 | |
| 745 | def test_create_false(self): |
| 746 | self.assertFalse(self._f) |
| 747 | |
| 748 | def test_create_true(self): |
| 749 | self.assertTrue(self._t) |
| 750 | |
| 751 | def test_create_from_vfalse(self): |
| 752 | b = bt2.BoolValue(self._f) |
| 753 | self.assertFalse(b) |
| 754 | |
| 755 | def test_create_from_vtrue(self): |
| 756 | b = bt2.BoolValue(self._t) |
| 757 | self.assertTrue(b) |
| 758 | |
| 759 | def test_create_from_int_non_zero(self): |
| 760 | with self.assertRaises(TypeError): |
| 761 | b = bt2.BoolValue(23) |
| 762 | |
| 763 | def test_create_from_int_zero(self): |
| 764 | with self.assertRaises(TypeError): |
| 765 | b = bt2.BoolValue(0) |
| 766 | |
| 767 | def test_assign_true(self): |
| 768 | b = bt2.BoolValue() |
| 769 | b.value = True |
| 770 | self.assertTrue(b) |
| 771 | |
| 772 | def test_assign_false(self): |
| 773 | b = bt2.BoolValue() |
| 774 | b.value = False |
| 775 | self.assertFalse(b) |
| 776 | |
| 777 | def test_assign_vtrue(self): |
| 778 | b = bt2.BoolValue() |
| 779 | b.value = self._t |
| 780 | self.assertTrue(b) |
| 781 | |
| 782 | def test_assign_vfalse(self): |
| 783 | b = bt2.BoolValue() |
| 784 | b.value = False |
| 785 | self.assertFalse(b) |
| 786 | |
| 787 | def test_assign_int(self): |
| 788 | with self.assertRaises(TypeError): |
| 789 | b = bt2.BoolValue() |
| 790 | b.value = 23 |
| 791 | |
| 792 | def test_bool_op(self): |
| 793 | self.assertEqual(bool(self._def), bool(self._def_value)) |
| 794 | |
| 795 | def test_str_op(self): |
| 796 | self.assertEqual(str(self._def), str(self._def_value)) |
| 797 | |
| 798 | def test_eq_none(self): |
| 799 | self.assertFalse(self._def == None) |
| 800 | |
| 801 | def test_ne_none(self): |
| 802 | self.assertTrue(self._def != None) |
| 803 | |
| 804 | def test_vfalse_eq_false(self): |
| 805 | self.assertEqual(self._f, False) |
| 806 | |
| 807 | def test_vfalse_ne_true(self): |
| 808 | self.assertNotEqual(self._f, True) |
| 809 | |
| 810 | def test_vtrue_eq_true(self): |
| 811 | self.assertEqual(self._t, True) |
| 812 | |
| 813 | def test_vtrue_ne_false(self): |
| 814 | self.assertNotEqual(self._t, False) |
| 815 | |
| 816 | |
| 817 | class _TestIntegerValue(_TestNumericValue): |
| 818 | def setUp(self): |
| 819 | self._pv = 23 |
| 820 | self._ip = self._CLS(self._pv) |
| 821 | self._def = self._ip |
| 822 | self._def_value = self._pv |
| 823 | self._def_new_value = 101 |
| 824 | |
| 825 | def tearDown(self): |
| 826 | del self._ip |
| 827 | del self._def |
| 828 | del self._def_value |
| 829 | |
| 830 | def _assert_expecting_int(self): |
| 831 | return self.assertRaisesRegex(TypeError, r'expecting an integral number object') |
| 832 | |
| 833 | def _assert_expecting_int64(self): |
| 834 | return self.assertRaisesRegex(ValueError, r"expecting a signed 64-bit integral value") |
| 835 | |
| 836 | def _assert_expecting_uint64(self): |
| 837 | return self.assertRaisesRegex(ValueError, r"expecting an unsigned 64-bit integral value") |
| 838 | |
| 839 | def test_create_default(self): |
| 840 | i = self._CLS() |
| 841 | self.assertEqual(i, 0) |
| 842 | |
| 843 | def test_create_pos(self): |
| 844 | self.assertEqual(self._ip, self._pv) |
| 845 | |
| 846 | def test_create_neg(self): |
| 847 | self.assertEqual(self._in, self._nv) |
| 848 | |
| 849 | def test_create_from_vint(self): |
| 850 | i = self._CLS(self._ip) |
| 851 | self.assertEqual(i, self._pv) |
| 852 | |
| 853 | def test_create_from_false(self): |
| 854 | i = self._CLS(False) |
| 855 | self.assertFalse(i) |
| 856 | |
| 857 | def test_create_from_true(self): |
| 858 | i = self._CLS(True) |
| 859 | self.assertTrue(i) |
| 860 | |
| 861 | def test_create_from_unknown(self): |
| 862 | class A: |
| 863 | pass |
| 864 | |
| 865 | with self._assert_expecting_int(): |
| 866 | i = self._CLS(A()) |
| 867 | |
| 868 | def test_create_from_varray(self): |
| 869 | with self._assert_expecting_int(): |
| 870 | i = self._CLS(bt2.ArrayValue()) |
| 871 | |
| 872 | def test_assign_true(self): |
| 873 | raw = True |
| 874 | self._def.value = raw |
| 875 | self.assertEqual(self._def, raw) |
| 876 | |
| 877 | def test_assign_false(self): |
| 878 | raw = False |
| 879 | self._def.value = raw |
| 880 | self.assertEqual(self._def, raw) |
| 881 | |
| 882 | def test_assign_pos_int(self): |
| 883 | raw = 477 |
| 884 | self._def.value = raw |
| 885 | self.assertEqual(self._def, raw) |
| 886 | |
| 887 | def test_assign_vint(self): |
| 888 | raw = 999 |
| 889 | self._def.value = bt2.create_value(raw) |
| 890 | self.assertEqual(self._def, raw) |
| 891 | |
| 892 | |
| 893 | class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase): |
| 894 | _CLS = bt2.SignedIntegerValue |
| 895 | |
| 896 | def setUp(self): |
| 897 | super().setUp() |
| 898 | self._nv = -52 |
| 899 | self._in = self._CLS(self._nv) |
| 900 | self._def_new_value = -101 |
| 901 | |
| 902 | def tearDown(self): |
| 903 | super().tearDown() |
| 904 | del self._in |
| 905 | |
| 906 | def test_create_neg(self): |
| 907 | self.assertEqual(self._in, self._nv) |
| 908 | |
| 909 | def test_create_pos_too_big(self): |
| 910 | with self._assert_expecting_int64(): |
| 911 | i = self._CLS(2 ** 63) |
| 912 | |
| 913 | def test_create_neg_too_big(self): |
| 914 | with self._assert_expecting_int64(): |
| 915 | i = self._CLS(-(2 ** 63) - 1) |
| 916 | |
| 917 | def test_assign_neg_int(self): |
| 918 | raw = -13 |
| 919 | self._def.value = raw |
| 920 | self.assertEqual(self._def, raw) |
| 921 | |
| 922 | def test_compare_big_int(self): |
| 923 | # Larger than the IEEE 754 double-precision exact representation of |
| 924 | # integers. |
| 925 | raw = (2**53) + 1 |
| 926 | v = bt2.create_value(raw) |
| 927 | self.assertEqual(v, raw) |
| 928 | |
| 929 | |
| 930 | _inject_numeric_testing_methods(SignedIntegerValueTestCase) |
| 931 | |
| 932 | |
| 933 | class UnsignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase): |
| 934 | _CLS = bt2.UnsignedIntegerValue |
| 935 | |
| 936 | def test_create_pos_too_big(self): |
| 937 | with self._assert_expecting_uint64(): |
| 938 | i = self._CLS(2 ** 64) |
| 939 | |
| 940 | def test_create_neg(self): |
| 941 | with self._assert_expecting_uint64(): |
| 942 | i = self._CLS(-1) |
| 943 | |
| 944 | |
| 945 | _inject_numeric_testing_methods(UnsignedIntegerValueTestCase, False) |
| 946 | |
| 947 | |
| 948 | class RealValueTestCase(_TestNumericValue, unittest.TestCase): |
| 949 | def setUp(self): |
| 950 | self._pv = 23.4 |
| 951 | self._nv = -52.7 |
| 952 | self._fp = bt2.RealValue(self._pv) |
| 953 | self._fn = bt2.RealValue(self._nv) |
| 954 | self._def = self._fp |
| 955 | self._def_value = self._pv |
| 956 | self._def_new_value = -101.88 |
| 957 | |
| 958 | def tearDown(self): |
| 959 | del self._fp |
| 960 | del self._fn |
| 961 | del self._def |
| 962 | del self._def_value |
| 963 | |
| 964 | def _assert_expecting_float(self): |
| 965 | return self.assertRaisesRegex(TypeError, r"expecting a real number object") |
| 966 | |
| 967 | def _test_invalid_op(self, cb): |
| 968 | with self.assertRaises(TypeError): |
| 969 | cb() |
| 970 | |
| 971 | def test_create_default(self): |
| 972 | f = bt2.RealValue() |
| 973 | self.assertEqual(f, 0.0) |
| 974 | |
| 975 | def test_create_pos(self): |
| 976 | self.assertEqual(self._fp, self._pv) |
| 977 | |
| 978 | def test_create_neg(self): |
| 979 | self.assertEqual(self._fn, self._nv) |
| 980 | |
| 981 | def test_create_from_vint(self): |
| 982 | f = bt2.RealValue(self._fp) |
| 983 | self.assertEqual(f, self._pv) |
| 984 | |
| 985 | def test_create_from_false(self): |
| 986 | f = bt2.RealValue(False) |
| 987 | self.assertFalse(f) |
| 988 | |
| 989 | def test_create_from_true(self): |
| 990 | f = bt2.RealValue(True) |
| 991 | self.assertTrue(f) |
| 992 | |
| 993 | def test_create_from_int(self): |
| 994 | raw = 17 |
| 995 | f = bt2.RealValue(raw) |
| 996 | self.assertEqual(f, float(raw)) |
| 997 | |
| 998 | def test_create_from_vint(self): |
| 999 | raw = 17 |
| 1000 | f = bt2.RealValue(bt2.create_value(raw)) |
| 1001 | self.assertEqual(f, float(raw)) |
| 1002 | |
| 1003 | def test_create_from_vfloat(self): |
| 1004 | raw = 17.17 |
| 1005 | f = bt2.RealValue(bt2.create_value(raw)) |
| 1006 | self.assertEqual(f, raw) |
| 1007 | |
| 1008 | def test_create_from_unknown(self): |
| 1009 | class A: |
| 1010 | pass |
| 1011 | |
| 1012 | with self._assert_expecting_float(): |
| 1013 | f = bt2.RealValue(A()) |
| 1014 | |
| 1015 | def test_create_from_varray(self): |
| 1016 | with self._assert_expecting_float(): |
| 1017 | f = bt2.RealValue(bt2.ArrayValue()) |
| 1018 | |
| 1019 | def test_assign_true(self): |
| 1020 | self._def.value = True |
| 1021 | self.assertTrue(self._def) |
| 1022 | |
| 1023 | def test_assign_false(self): |
| 1024 | self._def.value = False |
| 1025 | self.assertFalse(self._def) |
| 1026 | |
| 1027 | def test_assign_pos_int(self): |
| 1028 | raw = 477 |
| 1029 | self._def.value = raw |
| 1030 | self.assertEqual(self._def, float(raw)) |
| 1031 | |
| 1032 | def test_assign_neg_int(self): |
| 1033 | raw = -13 |
| 1034 | self._def.value = raw |
| 1035 | self.assertEqual(self._def, float(raw)) |
| 1036 | |
| 1037 | def test_assign_vint(self): |
| 1038 | raw = 999 |
| 1039 | self._def.value = bt2.create_value(raw) |
| 1040 | self.assertEqual(self._def, float(raw)) |
| 1041 | |
| 1042 | def test_assign_float(self): |
| 1043 | raw = -19.23 |
| 1044 | self._def.value = raw |
| 1045 | self.assertEqual(self._def, raw) |
| 1046 | |
| 1047 | def test_assign_vfloat(self): |
| 1048 | raw = 101.32 |
| 1049 | self._def.value = bt2.create_value(raw) |
| 1050 | self.assertEqual(self._def, raw) |
| 1051 | |
| 1052 | def test_invalid_lshift(self): |
| 1053 | self._test_invalid_op(lambda: self._def << 23) |
| 1054 | |
| 1055 | def test_invalid_rshift(self): |
| 1056 | self._test_invalid_op(lambda: self._def >> 23) |
| 1057 | |
| 1058 | def test_invalid_and(self): |
| 1059 | self._test_invalid_op(lambda: self._def & 23) |
| 1060 | |
| 1061 | def test_invalid_or(self): |
| 1062 | self._test_invalid_op(lambda: self._def | 23) |
| 1063 | |
| 1064 | def test_invalid_xor(self): |
| 1065 | self._test_invalid_op(lambda: self._def ^ 23) |
| 1066 | |
| 1067 | def test_invalid_invert(self): |
| 1068 | self._test_invalid_op(lambda: ~self._def) |
| 1069 | |
| 1070 | |
| 1071 | _inject_numeric_testing_methods(RealValueTestCase) |
| 1072 | |
| 1073 | |
| 1074 | class StringValueTestCase(_TestCopySimple, unittest.TestCase): |
| 1075 | def setUp(self): |
| 1076 | self._def_value = 'Hello, World!' |
| 1077 | self._def = bt2.StringValue(self._def_value) |
| 1078 | self._def_new_value = 'Yes!' |
| 1079 | |
| 1080 | def tearDown(self): |
| 1081 | del self._def |
| 1082 | |
| 1083 | def _assert_expecting_str(self): |
| 1084 | return self.assertRaises(TypeError) |
| 1085 | |
| 1086 | def test_create_default(self): |
| 1087 | s = bt2.StringValue() |
| 1088 | self.assertEqual(s, '') |
| 1089 | |
| 1090 | def test_create_from_str(self): |
| 1091 | raw = 'liberté' |
| 1092 | s = bt2.StringValue(raw) |
| 1093 | self.assertEqual(s, raw) |
| 1094 | |
| 1095 | def test_create_from_vstr(self): |
| 1096 | raw = 'liberté' |
| 1097 | s = bt2.StringValue(bt2.create_value(raw)) |
| 1098 | self.assertEqual(s, raw) |
| 1099 | |
| 1100 | def test_create_from_unknown(self): |
| 1101 | class A: |
| 1102 | pass |
| 1103 | |
| 1104 | with self._assert_expecting_str(): |
| 1105 | i = bt2.StringValue(A()) |
| 1106 | |
| 1107 | def test_create_from_varray(self): |
| 1108 | with self._assert_expecting_str(): |
| 1109 | i = bt2.StringValue(bt2.ArrayValue()) |
| 1110 | |
| 1111 | def test_assign_int(self): |
| 1112 | with self._assert_expecting_str(): |
| 1113 | self._def.value = 283 |
| 1114 | |
| 1115 | def test_assign_str(self): |
| 1116 | raw = 'zorg' |
| 1117 | self._def = raw |
| 1118 | self.assertEqual(self._def, raw) |
| 1119 | |
| 1120 | def test_assign_vstr(self): |
| 1121 | raw = 'zorg' |
| 1122 | self._def = bt2.create_value(raw) |
| 1123 | self.assertEqual(self._def, raw) |
| 1124 | |
| 1125 | def test_eq(self): |
| 1126 | self.assertEqual(self._def, self._def_value) |
| 1127 | |
| 1128 | def test_eq(self): |
| 1129 | self.assertNotEqual(self._def, 23) |
| 1130 | |
| 1131 | def test_lt_vstring(self): |
| 1132 | s1 = bt2.StringValue('allo') |
| 1133 | s2 = bt2.StringValue('bateau') |
| 1134 | self.assertLess(s1, s2) |
| 1135 | |
| 1136 | def test_lt_string(self): |
| 1137 | s1 = bt2.StringValue('allo') |
| 1138 | self.assertLess(s1, 'bateau') |
| 1139 | |
| 1140 | def test_le_vstring(self): |
| 1141 | s1 = bt2.StringValue('allo') |
| 1142 | s2 = bt2.StringValue('bateau') |
| 1143 | self.assertLessEqual(s1, s2) |
| 1144 | |
| 1145 | def test_le_string(self): |
| 1146 | s1 = bt2.StringValue('allo') |
| 1147 | self.assertLessEqual(s1, 'bateau') |
| 1148 | |
| 1149 | def test_gt_vstring(self): |
| 1150 | s1 = bt2.StringValue('allo') |
| 1151 | s2 = bt2.StringValue('bateau') |
| 1152 | self.assertGreater(s2, s1) |
| 1153 | |
| 1154 | def test_gt_string(self): |
| 1155 | s1 = bt2.StringValue('allo') |
| 1156 | self.assertGreater('bateau', s1) |
| 1157 | |
| 1158 | def test_ge_vstring(self): |
| 1159 | s1 = bt2.StringValue('allo') |
| 1160 | s2 = bt2.StringValue('bateau') |
| 1161 | self.assertGreaterEqual(s2, s1) |
| 1162 | |
| 1163 | def test_ge_string(self): |
| 1164 | s1 = bt2.StringValue('allo') |
| 1165 | self.assertGreaterEqual('bateau', s1) |
| 1166 | |
| 1167 | def test_bool_op(self): |
| 1168 | self.assertEqual(bool(self._def), bool(self._def_value)) |
| 1169 | |
| 1170 | def test_str_op(self): |
| 1171 | self.assertEqual(str(self._def), str(self._def_value)) |
| 1172 | |
| 1173 | def test_len(self): |
| 1174 | self.assertEqual(len(self._def), len(self._def_value)) |
| 1175 | |
| 1176 | def test_getitem(self): |
| 1177 | self.assertEqual(self._def[5], self._def_value[5]) |
| 1178 | |
| 1179 | def test_append_str(self): |
| 1180 | to_append = 'meow meow meow' |
| 1181 | self._def += to_append |
| 1182 | self._def_value += to_append |
| 1183 | self.assertEqual(self._def, self._def_value) |
| 1184 | |
| 1185 | def test_append_vstr(self): |
| 1186 | to_append = 'meow meow meow' |
| 1187 | self._def += bt2.create_value(to_append) |
| 1188 | self._def_value += to_append |
| 1189 | self.assertEqual(self._def, self._def_value) |
| 1190 | |
| 1191 | |
| 1192 | class ArrayValueTestCase(_TestCopySimple, unittest.TestCase): |
| 1193 | def setUp(self): |
| 1194 | self._def_value = [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes'] |
| 1195 | self._def = bt2.ArrayValue(copy.deepcopy(self._def_value)) |
| 1196 | |
| 1197 | def tearDown(self): |
| 1198 | del self._def |
| 1199 | |
| 1200 | def _modify_def(self): |
| 1201 | self._def[2] = 'xyz' |
| 1202 | |
| 1203 | def _assert_type_error(self): |
| 1204 | return self.assertRaises(TypeError) |
| 1205 | |
| 1206 | def test_create_default(self): |
| 1207 | a = bt2.ArrayValue() |
| 1208 | self.assertEqual(len(a), 0) |
| 1209 | |
| 1210 | def test_create_from_array(self): |
| 1211 | self.assertEqual(self._def, self._def_value) |
| 1212 | |
| 1213 | def test_create_from_tuple(self): |
| 1214 | t = 1, 2, False, None |
| 1215 | a = bt2.ArrayValue(t) |
| 1216 | self.assertEqual(a, t) |
| 1217 | |
| 1218 | def test_create_from_varray(self): |
| 1219 | va = bt2.ArrayValue(copy.deepcopy(self._def_value)) |
| 1220 | a = bt2.ArrayValue(va) |
| 1221 | self.assertEqual(va, a) |
| 1222 | |
| 1223 | def test_create_from_unknown(self): |
| 1224 | class A: |
| 1225 | pass |
| 1226 | |
| 1227 | with self._assert_type_error(): |
| 1228 | a = bt2.ArrayValue(A()) |
| 1229 | |
| 1230 | def test_bool_op_true(self): |
| 1231 | self.assertTrue(bool(self._def)) |
| 1232 | |
| 1233 | def test_bool_op_false(self): |
| 1234 | self.assertFalse(bool(bt2.ArrayValue())) |
| 1235 | |
| 1236 | def test_len(self): |
| 1237 | self.assertEqual(len(self._def), len(self._def_value)) |
| 1238 | |
| 1239 | def test_eq_int(self): |
| 1240 | self.assertNotEqual(self._def, 23) |
| 1241 | |
| 1242 | def test_eq_diff_len(self): |
| 1243 | a1 = bt2.create_value([1, 2, 3]) |
| 1244 | a2 = bt2.create_value([1, 2]) |
| 1245 | self.assertNotEqual(a1, a2) |
| 1246 | |
| 1247 | def test_eq_diff_content_same_len(self): |
| 1248 | a1 = bt2.create_value([1, 2, 3]) |
| 1249 | a2 = bt2.create_value([4, 5, 6]) |
| 1250 | self.assertNotEqual(a1, a2) |
| 1251 | |
| 1252 | def test_eq_same_content_same_len(self): |
| 1253 | raw = (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}]) |
| 1254 | a1 = bt2.ArrayValue(raw) |
| 1255 | a2 = bt2.ArrayValue(copy.deepcopy(raw)) |
| 1256 | self.assertEqual(a1, a2) |
| 1257 | |
| 1258 | def test_setitem_int(self): |
| 1259 | raw = 19 |
| 1260 | self._def[2] = raw |
| 1261 | self.assertEqual(self._def[2], raw) |
| 1262 | |
| 1263 | def test_setitem_vint(self): |
| 1264 | raw = 19 |
| 1265 | self._def[2] = bt2.create_value(raw) |
| 1266 | self.assertEqual(self._def[2], raw) |
| 1267 | |
| 1268 | def test_setitem_none(self): |
| 1269 | self._def[2] = None |
| 1270 | self.assertIsNone(self._def[2]) |
| 1271 | |
| 1272 | def test_setitem_index_wrong_type(self): |
| 1273 | with self._assert_type_error(): |
| 1274 | self._def['yes'] = 23 |
| 1275 | |
| 1276 | def test_setitem_index_neg(self): |
| 1277 | with self.assertRaises(IndexError): |
| 1278 | self._def[-2] = 23 |
| 1279 | |
| 1280 | def test_setitem_index_out_of_range(self): |
| 1281 | with self.assertRaises(IndexError): |
| 1282 | self._def[len(self._def)] = 23 |
| 1283 | |
| 1284 | def test_append_none(self): |
| 1285 | self._def.append(None) |
| 1286 | self.assertIsNone(self._def[len(self._def) - 1]) |
| 1287 | |
| 1288 | def test_append_int(self): |
| 1289 | raw = 145 |
| 1290 | self._def.append(raw) |
| 1291 | self.assertEqual(self._def[len(self._def) - 1], raw) |
| 1292 | |
| 1293 | def test_append_vint(self): |
| 1294 | raw = 145 |
| 1295 | self._def.append(bt2.create_value(raw)) |
| 1296 | self.assertEqual(self._def[len(self._def) - 1], raw) |
| 1297 | |
| 1298 | def test_append_unknown(self): |
| 1299 | class A: |
| 1300 | pass |
| 1301 | |
| 1302 | with self._assert_type_error(): |
| 1303 | self._def.append(A()) |
| 1304 | |
| 1305 | def test_iadd(self): |
| 1306 | raw = 4, 5, True |
| 1307 | self._def += raw |
| 1308 | self.assertEqual(self._def[len(self._def) - 3], raw[0]) |
| 1309 | self.assertEqual(self._def[len(self._def) - 2], raw[1]) |
| 1310 | self.assertEqual(self._def[len(self._def) - 1], raw[2]) |
| 1311 | |
| 1312 | def test_iadd_unknown(self): |
| 1313 | class A: |
| 1314 | pass |
| 1315 | |
| 1316 | with self._assert_type_error(): |
| 1317 | self._def += A() |
| 1318 | |
| 1319 | def test_iadd_list_unknown(self): |
| 1320 | class A: |
| 1321 | pass |
| 1322 | |
| 1323 | with self._assert_type_error(): |
| 1324 | self._def += [A()] |
| 1325 | |
| 1326 | def test_iter(self): |
| 1327 | for velem, elem in zip(self._def, self._def_value): |
| 1328 | self.assertEqual(velem, elem) |
| 1329 | |
| 1330 | |
| 1331 | class MapValueTestCase(_TestCopySimple, unittest.TestCase): |
| 1332 | def setUp(self): |
| 1333 | self._def_value = { |
| 1334 | 'none': None, |
| 1335 | 'false': False, |
| 1336 | 'true': True, |
| 1337 | 'neg-int': -23, |
| 1338 | 'zero': 0, |
| 1339 | 'pos-int': 42, |
| 1340 | 'neg-float': -42.4, |
| 1341 | 'pos-float': 23.17, |
| 1342 | 'str': 'yes' |
| 1343 | } |
| 1344 | self._def = bt2.MapValue(copy.deepcopy(self._def_value)) |
| 1345 | |
| 1346 | def tearDown(self): |
| 1347 | del self._def |
| 1348 | |
| 1349 | def _modify_def(self): |
| 1350 | self._def['zero'] = 1 |
| 1351 | |
| 1352 | def test_create_default(self): |
| 1353 | m = bt2.MapValue() |
| 1354 | self.assertEqual(len(m), 0) |
| 1355 | |
| 1356 | def test_create_from_dict(self): |
| 1357 | self.assertEqual(self._def, self._def_value) |
| 1358 | |
| 1359 | def test_create_from_vmap(self): |
| 1360 | vm = bt2.MapValue(copy.deepcopy(self._def_value)) |
| 1361 | m = bt2.MapValue(vm) |
| 1362 | self.assertEqual(vm, m) |
| 1363 | |
| 1364 | def test_create_from_unknown(self): |
| 1365 | class A: |
| 1366 | pass |
| 1367 | |
| 1368 | with self.assertRaises(AttributeError): |
| 1369 | m = bt2.MapValue(A()) |
| 1370 | |
| 1371 | def test_bool_op_true(self): |
| 1372 | self.assertTrue(bool(self._def)) |
| 1373 | |
| 1374 | def test_bool_op_false(self): |
| 1375 | self.assertFalse(bool(bt2.MapValue())) |
| 1376 | |
| 1377 | def test_len(self): |
| 1378 | self.assertEqual(len(self._def), len(self._def_value)) |
| 1379 | |
| 1380 | def test_eq_int(self): |
| 1381 | self.assertNotEqual(self._def, 23) |
| 1382 | |
| 1383 | def test_eq_diff_len(self): |
| 1384 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) |
| 1385 | a2 = bt2.create_value({'a': 1, 'b': 2}) |
| 1386 | self.assertNotEqual(a1, a2) |
| 1387 | |
| 1388 | def test_eq_diff_content_same_len(self): |
| 1389 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) |
| 1390 | a2 = bt2.create_value({'a': 4, 'b': 2, 'c': 3}) |
| 1391 | self.assertNotEqual(a1, a2) |
| 1392 | |
| 1393 | def test_eq_same_content_diff_keys(self): |
| 1394 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) |
| 1395 | a2 = bt2.create_value({'a': 1, 'k': 2, 'c': 3}) |
| 1396 | self.assertNotEqual(a1, a2) |
| 1397 | |
| 1398 | def test_eq_same_content_same_len(self): |
| 1399 | raw = { |
| 1400 | '3': 3, |
| 1401 | 'True': True, |
| 1402 | 'array': [1, 2.5, None, {'a': 17.6, 'b': None}] |
| 1403 | } |
| 1404 | a1 = bt2.MapValue(raw) |
| 1405 | a2 = bt2.MapValue(copy.deepcopy(raw)) |
| 1406 | self.assertEqual(a1, a2) |
| 1407 | self.assertEqual(a1, raw) |
| 1408 | |
| 1409 | def test_setitem_int(self): |
| 1410 | raw = 19 |
| 1411 | self._def['pos-int'] = raw |
| 1412 | self.assertEqual(self._def['pos-int'], raw) |
| 1413 | |
| 1414 | def test_setitem_vint(self): |
| 1415 | raw = 19 |
| 1416 | self._def['pos-int'] = bt2.create_value(raw) |
| 1417 | self.assertEqual(self._def['pos-int'], raw) |
| 1418 | |
| 1419 | def test_setitem_none(self): |
| 1420 | self._def['none'] = None |
| 1421 | self.assertIsNone(self._def['none']) |
| 1422 | |
| 1423 | def test_setitem_new_int(self): |
| 1424 | old_len = len(self._def) |
| 1425 | self._def['new-int'] = 23 |
| 1426 | self.assertEqual(self._def['new-int'], 23) |
| 1427 | self.assertEqual(len(self._def), old_len + 1) |
| 1428 | |
| 1429 | def test_setitem_index_wrong_type(self): |
| 1430 | with self.assertRaises(TypeError): |
| 1431 | self._def[18] = 23 |
| 1432 | |
| 1433 | def test_iter(self): |
| 1434 | for vkey, vval in self._def.items(): |
| 1435 | val = self._def_value[vkey] |
| 1436 | self.assertEqual(vval, val) |
| 1437 | |
| 1438 | def test_getitem_wrong_key(self): |
| 1439 | with self.assertRaises(KeyError): |
| 1440 | self._def['kilojoule'] |