test_{field,value}.py: array test cases: add non-sequence equality tests
[babeltrace.git] / tests / bindings / python / bt2 / test_value.py
index 6f66930e93ca9c3e009fbd8434898fd48a79d30e..3521eb05b4fd37c02479734d750a7ee24fb491f7 100644 (file)
@@ -18,6 +18,7 @@
 
 from functools import partial, partialmethod
 import operator
+import collections
 import unittest
 import numbers
 import math
@@ -25,6 +26,9 @@ import copy
 import bt2
 
 
+# The value object classes explicitly do not implement the copy methods,
+# raising `NotImplementedError`, just in case we decide to implement
+# them someday.
 class _TestCopySimple:
     def test_copy(self):
         with self.assertRaises(NotImplementedError):
@@ -41,56 +45,90 @@ _COMP_BINOPS = (
 )
 
 
+# Base class for numeric value test cases.
+#
+# To be compatible with this base class, a derived class must, in its
+# setUp() method:
+#
+# * Set `self._def` to a value object with an arbitrary raw value.
+# * Set `self._def_value` to the equivalent raw value of `self._def`.
 class _TestNumericValue(_TestCopySimple):
+    # Tries the binary operation `op`:
+    #
+    # 1. Between `self._def`, which is a value object, and `rhs`.
+    # 2. Between `self._def_value`, which is the raw value of
+    #    `self._def`, and `rhs`.
+    #
+    # Returns the results of 1. and 2.
+    #
+    # If there's an exception while performing 1. or 2., asserts that
+    # both operations raised exceptions, that both exceptions have the
+    # same type, and returns `None` for both results.
     def _binop(self, op, rhs):
-        rexc = None
-        rvexc = None
+        type_rexc = None
+        type_rvexc = None
         comp_value = rhs
 
+        # try with value object
         try:
             r = op(self._def, rhs)
         except Exception as e:
-            rexc = e
+            type_rexc = type(e)
 
+        # try with raw value
         try:
             rv = op(self._def_value, comp_value)
         except Exception as e:
-            rvexc = e
+            type_rvexc = type(e)
 
-        if rexc is not None or rvexc is not None:
+        if type_rexc is not None or type_rvexc is not None:
             # at least one of the operations raised an exception: in
             # this case both operations should have raised the same
             # type of exception (division by zero, bit shift with a
             # floating point number operand, etc.)
-            self.assertIs(type(rexc), type(rvexc))
+            self.assertIs(type_rexc, type_rvexc)
             return None, None
 
         return r, rv
 
+    # Tries the unary operation `op`:
+    #
+    # 1. On `self._def`, which is a value object.
+    # 2. On `self._def_value`, which is the raw value of `self._def`.
+    #
+    # Returns the results of 1. and 2.
+    #
+    # If there's an exception while performing 1. or 2., asserts that
+    # both operations raised exceptions, that both exceptions have the
+    # same type, and returns `None` for both results.
     def _unaryop(self, op):
-        rexc = None
-        rvexc = None
+        type_rexc = None
+        type_rvexc = None
 
+        # try with value object
         try:
             r = op(self._def)
         except Exception as e:
-            rexc = e
+            type_rexc = type(e)
 
+        # try with raw value
         try:
             rv = op(self._def_value)
         except Exception as e:
-            rvexc = e
+            type_rvexc = type(e)
 
-        if rexc is not None or rvexc is not None:
+        if type_rexc is not None or type_rvexc is not None:
             # at least one of the operations raised an exception: in
             # this case both operations should have raised the same
             # type of exception (division by zero, bit shift with a
             # floating point number operand, etc.)
-            self.assertIs(type(rexc), type(rvexc))
+            self.assertIs(type_rexc, type_rvexc)
             return None, None
 
         return r, rv
 
+    # Tests that the unary operation `op` gives results with the same
+    # type for both `self._def` and `self._def_value`.
     def _test_unaryop_type(self, op):
         r, rv = self._unaryop(op)
 
@@ -99,6 +137,9 @@ class _TestNumericValue(_TestCopySimple):
 
         self.assertIsInstance(r, type(rv))
 
+    # Tests that the unary operation `op` gives results with the same
+    # value for both `self._def` and `self._def_value`. This uses the
+    # __eq__() operator of `self._def`.
     def _test_unaryop_value(self, op):
         r, rv = self._unaryop(op)
 
@@ -107,16 +148,22 @@ class _TestNumericValue(_TestCopySimple):
 
         self.assertEqual(r, rv)
 
+    # Tests that the unary operation `op`, when applied to `self._def`,
+    # does not change its underlying BT object address.
     def _test_unaryop_addr_same(self, op):
         addr_before = self._def.addr
         self._unaryop(op)
         self.assertEqual(self._def.addr, addr_before)
 
+    # Tests that the unary operation `op`, when applied to `self._def`,
+    # does not change its value.
     def _test_unaryop_value_same(self, op):
         value_before = self._def.__class__(self._def)
         self._unaryop(op)
         self.assertEqual(self._def, value_before)
 
+    # Tests that the binary operation `op` gives results with the same
+    # type for both `self._def` and `self._def_value`.
     def _test_binop_type(self, op, rhs):
         r, rv = self._binop(op, rhs)
 
@@ -129,6 +176,9 @@ class _TestNumericValue(_TestCopySimple):
         else:
             self.assertIsInstance(r, type(rv))
 
+    # Tests that the binary operation `op` gives results with the same
+    # value for both `self._def` and `self._def_value`. This uses the
+    # __eq__() operator of `self._def`.
     def _test_binop_value(self, op, rhs):
         r, rv = self._binop(op, rhs)
 
@@ -137,25 +187,41 @@ class _TestNumericValue(_TestCopySimple):
 
         self.assertEqual(r, rv)
 
+    # Tests that the binary operation `op`, when applied to `self._def`,
+    # does not change its underlying BT object address.
     def _test_binop_lhs_addr_same(self, op, rhs):
         addr_before = self._def.addr
         r, rv = self._binop(op, rhs)
         self.assertEqual(self._def.addr, addr_before)
 
+    # Tests that the binary operation `op`, when applied to `self._def`,
+    # does not change its value.
     def _test_binop_lhs_value_same(self, op, rhs):
         value_before = self._def.__class__(self._def)
         r, rv = self._binop(op, rhs)
         self.assertEqual(self._def, value_before)
 
+    # The methods below which take the `test_cb` and `op` parameters
+    # are meant to be used with one of the _test_binop_*() functions
+    # above as `test_cb` and a binary operator function as `op`.
+    #
+    # For example:
+    #
+    #     self._test_binop_rhs_pos_int(self._test_binop_value,
+    #                                  operator.add)
+    #
+    # This tests that a numeric value object added to a positive integer
+    # raw value gives a result with the expected value.
+    #
+    # `vint` and `vfloat` mean a signed integer value object and a real
+    # value object.
+
     def _test_binop_invalid_unknown(self, op):
         if op in _COMP_BINOPS:
             self.skipTest('not testing')
 
-        class A:
-            pass
-
         with self.assertRaises(TypeError):
-            op(self._def, A())
+            op(self._def, object())
 
     def _test_binop_invalid_none(self, op):
         if op in _COMP_BINOPS:
@@ -197,6 +263,12 @@ class _TestNumericValue(_TestCopySimple):
     def _test_binop_rhs_zero_float(self, test_cb, op):
         test_cb(op, 0.0)
 
+    def _test_binop_rhs_complex(self, test_cb, op):
+        test_cb(op, -23+19j)
+
+    def _test_binop_rhs_zero_complex(self, test_cb, op):
+        test_cb(op, 0j)
+
     def _test_binop_rhs_pos_vfloat(self, test_cb, op):
         test_cb(op, bt2.create_value(2.2))
 
@@ -248,6 +320,12 @@ class _TestNumericValue(_TestCopySimple):
     def _test_binop_type_zero_vfloat(self, op):
         self._test_binop_rhs_zero_vfloat(self._test_binop_type, op)
 
+    def _test_binop_type_complex(self, op):
+        self._test_binop_rhs_complex(self._test_binop_type, op)
+
+    def _test_binop_type_zero_complex(self, op):
+        self._test_binop_rhs_zero_complex(self._test_binop_type, op)
+
     def _test_binop_value_false(self, op):
         self._test_binop_rhs_false(self._test_binop_value, op)
 
@@ -290,6 +368,12 @@ class _TestNumericValue(_TestCopySimple):
     def _test_binop_value_zero_vfloat(self, op):
         self._test_binop_rhs_zero_vfloat(self._test_binop_value, op)
 
+    def _test_binop_value_complex(self, op):
+        self._test_binop_rhs_complex(self._test_binop_value, op)
+
+    def _test_binop_value_zero_complex(self, op):
+        self._test_binop_rhs_zero_complex(self._test_binop_value, op)
+
     def _test_binop_lhs_addr_same_false(self, op):
         self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op)
 
@@ -332,6 +416,12 @@ class _TestNumericValue(_TestCopySimple):
     def _test_binop_lhs_addr_same_zero_vfloat(self, op):
         self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op)
 
+    def _test_binop_lhs_addr_same_complex(self, op):
+        self._test_binop_rhs_complex(self._test_binop_lhs_addr_same, op)
+
+    def _test_binop_lhs_addr_same_zero_complex(self, op):
+        self._test_binop_rhs_zero_complex(self._test_binop_lhs_addr_same, op)
+
     def _test_binop_lhs_value_same_false(self, op):
         self._test_binop_rhs_false(self._test_binop_lhs_value_same, op)
 
@@ -374,6 +464,12 @@ class _TestNumericValue(_TestCopySimple):
     def _test_binop_lhs_value_same_zero_vfloat(self, op):
         self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op)
 
+    def _test_binop_lhs_value_same_complex(self, op):
+        self._test_binop_rhs_complex(self._test_binop_lhs_value_same, op)
+
+    def _test_binop_lhs_value_same_zero_complex(self, op):
+        self._test_binop_rhs_zero_complex(self._test_binop_lhs_value_same, op)
+
     def test_bool_op(self):
         self.assertEqual(bool(self._def), bool(self._def_value))
 
@@ -396,6 +492,11 @@ class _TestNumericValue(_TestCopySimple):
         self.assertTrue(self._def != None)
 
 
+# This is a list of binary operators used for
+# _inject_numeric_testing_methods().
+#
+# Each entry is a pair of binary operator name (used as part of the
+# created testing method's name) and operator function.
 _BINOPS = (
     ('lt', operator.lt),
     ('le', operator.le),
@@ -430,6 +531,11 @@ _BINOPS = (
 )
 
 
+# This is a list of unary operators used for
+# _inject_numeric_testing_methods().
+#
+# Each entry is a pair of unary operator name (used as part of the
+# created testing method's name) and operator function.
 _UNARYOPS = (
     ('neg', operator.neg),
     ('pos', operator.pos),
@@ -446,7 +552,21 @@ _UNARYOPS = (
 )
 
 
-def _inject_numeric_testing_methods(cls, has_neg=True):
+# This function injects a bunch of testing methods to a numeric
+# value test case.
+#
+# It is meant to be used like this:
+#
+#     _inject_numeric_testing_methods(MyNumericValueTestCase)
+#
+# This function injects:
+#
+# * One testing method for each _TestNumericValue._test_binop_*()
+#   method, for each binary operator in the _BINOPS tuple.
+#
+# * One testing method for each _TestNumericValue._test_unaryop*()
+#   method, for each unary operator in the _UNARYOPS tuple.
+def _inject_numeric_testing_methods(cls):
     def test_binop_name(suffix):
         return 'test_binop_{}_{}'.format(name, suffix)
 
@@ -469,17 +589,14 @@ def _inject_numeric_testing_methods(cls, has_neg=True):
         setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_true, op=binop))
         setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_int, op=binop))
         setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_vint, op=binop))
-
-        if has_neg:
-            setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue._test_binop_type_neg_int, op=binop))
-            setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue._test_binop_type_neg_vint, op=binop))
-            setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue._test_binop_value_neg_int, op=binop))
-            setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue._test_binop_value_neg_vint, op=binop))
-            setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_int, op=binop))
-            setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vint, op=binop))
-            setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_int, op=binop))
-            setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vint, op=binop))
-
+        setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue._test_binop_type_neg_int, op=binop))
+        setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue._test_binop_type_neg_vint, op=binop))
+        setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue._test_binop_value_neg_int, op=binop))
+        setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue._test_binop_value_neg_vint, op=binop))
+        setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_int, op=binop))
+        setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vint, op=binop))
+        setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_int, op=binop))
+        setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vint, op=binop))
         setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericValue._test_binop_type_false, op=binop))
         setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue._test_binop_type_zero_int, op=binop))
         setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue._test_binop_type_zero_vint, op=binop))
@@ -492,17 +609,14 @@ def _inject_numeric_testing_methods(cls, has_neg=True):
         setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_false, op=binop))
         setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_int, op=binop))
         setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vint, op=binop))
-
-        if has_neg:
-            setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue._test_binop_type_neg_float, op=binop))
-            setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_type_neg_vfloat, op=binop))
-            setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue._test_binop_value_neg_float, op=binop))
-            setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_value_neg_vfloat, op=binop))
-            setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_float, op=binop))
-            setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vfloat, op=binop))
-            setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_float, op=binop))
-            setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vfloat, op=binop))
-
+        setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue._test_binop_type_neg_float, op=binop))
+        setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_type_neg_vfloat, op=binop))
+        setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue._test_binop_value_neg_float, op=binop))
+        setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_value_neg_vfloat, op=binop))
+        setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_float, op=binop))
+        setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vfloat, op=binop))
+        setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_float, op=binop))
+        setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vfloat, op=binop))
         setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue._test_binop_type_pos_float, op=binop))
         setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_type_pos_vfloat, op=binop))
         setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue._test_binop_value_pos_float, op=binop))
@@ -519,6 +633,14 @@ def _inject_numeric_testing_methods(cls, has_neg=True):
         setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_vfloat, op=binop))
         setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_float, op=binop))
         setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vfloat, op=binop))
+        setattr(cls, test_binop_name('type_complex'), partialmethod(_TestNumericValue._test_binop_type_complex, op=binop))
+        setattr(cls, test_binop_name('type_zero_complex'), partialmethod(_TestNumericValue._test_binop_type_zero_complex, op=binop))
+        setattr(cls, test_binop_name('value_complex'), partialmethod(_TestNumericValue._test_binop_value_complex, op=binop))
+        setattr(cls, test_binop_name('value_zero_complex'), partialmethod(_TestNumericValue._test_binop_value_zero_complex, op=binop))
+        setattr(cls, test_binop_name('lhs_addr_same_complex'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_complex, op=binop))
+        setattr(cls, test_binop_name('lhs_addr_same_zero_complex'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_complex, op=binop))
+        setattr(cls, test_binop_name('lhs_value_same_complex'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_complex, op=binop))
+        setattr(cls, test_binop_name('lhs_value_same_zero_complex'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_complex, op=binop))
 
     # inject testing methods for each unary operation
     for name, unaryop in _UNARYOPS:
@@ -630,7 +752,7 @@ class CreateValueFuncTestCase(unittest.TestCase):
             v = bt2.create_value(a)
 
 
-class BoolValueTestCase(_TestCopySimple, unittest.TestCase):
+class BoolValueTestCase(_TestNumericValue, unittest.TestCase):
     def setUp(self):
         self._f = bt2.BoolValue(False)
         self._t = bt2.BoolValue(True)
@@ -722,6 +844,9 @@ class BoolValueTestCase(_TestCopySimple, unittest.TestCase):
         self.assertNotEqual(self._t, False)
 
 
+_inject_numeric_testing_methods(BoolValueTestCase)
+
+
 class _TestIntegerValue(_TestNumericValue):
     def setUp(self):
         self._pv = 23
@@ -850,7 +975,7 @@ class UnsignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase):
             i = self._CLS(-1)
 
 
-_inject_numeric_testing_methods(UnsignedIntegerValueTestCase, False)
+_inject_numeric_testing_methods(UnsignedIntegerValueTestCase)
 
 
 class RealValueTestCase(_TestNumericValue, unittest.TestCase):
@@ -1163,6 +1288,12 @@ class ArrayValueTestCase(_TestCopySimple, unittest.TestCase):
         a2 = bt2.ArrayValue(copy.deepcopy(raw))
         self.assertEqual(a1, a2)
 
+    def test_eq_non_sequence_iterable(self):
+        dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)])
+        a = bt2.ArrayValue((1, 3, 5))
+        self.assertEqual(a, list(dct.keys()))
+        self.assertNotEqual(a, dct)
+
     def test_setitem_int(self):
         raw = 19
         self._def[2] = raw
This page took 0.030309 seconds and 4 git commands to generate.