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