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