Python operator.imul() Examples

The following are 30 code examples of operator.imul(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module operator , or try the search function .
Example #1
Source File: test_finfields.py    From mpyc with MIT License 6 votes vote down vote up
def test_operatorerrors(self):
        f2 = self.f2
        f2p = self.f2p
        f256 = self.f256
        f19 = self.f19
        self.assertRaises(TypeError, operator.add, f2(1), f2p(2))
        self.assertRaises(TypeError, operator.iadd, f2(1), f2p(2))
        self.assertRaises(TypeError, operator.sub, f2(1), f256(2))
        self.assertRaises(TypeError, operator.isub, f2(1), f256(2))
        self.assertRaises(TypeError, operator.mul, f2(1), f19(2))
        self.assertRaises(TypeError, operator.imul, f2(1), f19(2))
        self.assertRaises(TypeError, operator.truediv, f256(1), f19(2))
        self.assertRaises(TypeError, operator.itruediv, f256(1), f19(2))
        self.assertRaises(TypeError, operator.truediv, 3.14, f19(2))
        self.assertRaises(TypeError, operator.lshift, f2(1), f2(1))
        self.assertRaises(TypeError, operator.ilshift, f2(1), f2(1))
        self.assertRaises(TypeError, operator.lshift, 1, f2(1))
        self.assertRaises(TypeError, operator.rshift, f19(1), f19(1))
        self.assertRaises(TypeError, operator.irshift, f19(1), f19(1))
        self.assertRaises(TypeError, operator.irshift, f256(1), f256(1))
        self.assertRaises(TypeError, operator.pow, f2(1), f19(2))
        self.assertRaises(TypeError, operator.pow, f19(1), 3.14) 
Example #2
Source File: test_quantity.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_inplace_multiplication_with_autoconvert(self, input_tuple, expected):
        self.ureg.autoconvert_offset_to_baseunit = True
        (q1v, q1u), (q2v, q2u) = input_tuple
        # update input tuple with new values to have correct values on failure
        input_tuple = ((np.array([q1v]*2, dtype=np.float), q1u),
                       (np.array([q2v]*2, dtype=np.float), q2u))
        Q_ = self.Q_
        qin1, qin2 = input_tuple
        q1, q2 = Q_(*qin1), Q_(*qin2)
        q1_cp = copy.copy(q1)
        if expected == 'error':
            self.assertRaises(OffsetUnitCalculusError, op.imul, q1_cp, q2)
        else:
            expected = np.array([expected[0]]*2, dtype=np.float), expected[1]
            self.assertEqual(op.imul(q1_cp, q2).units, Q_(*expected).units)
            q1_cp = copy.copy(q1)
            self.assertQuantityAlmostEqual(op.imul(q1_cp, q2), Q_(*expected),
                                           atol=0.01) 
Example #3
Source File: test_quantity.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_inplace_multiplication(self, input_tuple, expected):
        self.ureg.autoconvert_offset_to_baseunit = False
        (q1v, q1u), (q2v, q2u) = input_tuple
        # update input tuple with new values to have correct values on failure
        input_tuple = ((np.array([q1v]*2, dtype=np.float), q1u),
                       (np.array([q2v]*2, dtype=np.float), q2u))
        Q_ = self.Q_
        qin1, qin2 = input_tuple
        q1, q2 = Q_(*qin1), Q_(*qin2)
        q1_cp = copy.copy(q1)
        if expected == 'error':
            self.assertRaises(OffsetUnitCalculusError, op.imul, q1_cp, q2)
        else:
            expected = np.array([expected[0]]*2, dtype=np.float), expected[1]
            self.assertEqual(op.imul(q1_cp, q2).units, Q_(*expected).units)
            q1_cp = copy.copy(q1)
            self.assertQuantityAlmostEqual(op.imul(q1_cp, q2), Q_(*expected),
                                           atol=0.01) 
Example #4
Source File: test_array.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_imul(self):
        a = array.array(self.typecode, self.example)
        b = a

        a *= 5
        self.assertTrue(a is b)
        self.assertEqual(
            a,
            array.array(self.typecode, 5*self.example)
        )

        a *= 0
        self.assertTrue(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a *= 1000
        self.assertTrue(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a *= -1
        self.assertTrue(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a = array.array(self.typecode, self.example)
        a *= -1
        self.assertEqual(a, array.array(self.typecode))

        if test_support.is_jython:
            self.assertRaises(TypeError, operator.imul, a, "bad")
        else:
            self.assertRaises(TypeError, a.__imul__, "bad") 
Example #5
Source File: test_array.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_imul(self):
        a = array.array(self.typecode, self.example)
        b = a

        a *= 5
        self.assert_(a is b)
        self.assertEqual(
            a,
            array.array(self.typecode, 5*self.example)
        )

        a *= 0
        self.assert_(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a *= 1000
        self.assert_(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a *= -1
        self.assert_(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a = array.array(self.typecode, self.example)
        a *= -1
        self.assertEqual(a, array.array(self.typecode))

        if test_support.is_jython:
            self.assertRaises(TypeError, operator.imul, a, "bad")
        else:
            self.assertRaises(TypeError, a.__imul__, "bad") 
Example #6
Source File: test_array.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_buffer(self):
        a = array.array(self.typecode, self.example)
        m = memoryview(a)
        expected = m.tobytes()
        self.assertEqual(a.tobytes(), expected)
        self.assertEqual(a.tobytes()[0], expected[0])
        # Resizing is forbidden when there are buffer exports.
        # For issue 4509, we also check after each error that
        # the array was not modified.
        self.assertRaises(BufferError, a.append, a[0])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.extend, a[0:1])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.remove, a[0])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.pop, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.fromlist, a.tolist())
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.frombytes, a.tobytes())
        self.assertEqual(m.tobytes(), expected)
        if self.typecode == 'u':
            self.assertRaises(BufferError, a.fromunicode, a.tounicode())
            self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.imul, a, 2)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.imul, a, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.delitem, a, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.delitem, a, slice(0, 1))
        self.assertEqual(m.tobytes(), expected) 
Example #7
Source File: objectproxy.py    From CrossHair with MIT License 5 votes vote down vote up
def __imul__(self, other):
        return operator.imul(self._wrapped(), other) 
Example #8
Source File: distributed.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _get_reduce_op(self, reduce_nodes):
        require(len(reduce_nodes) == 2)
        require(isinstance(reduce_nodes[0], ir.Assign))
        require(isinstance(reduce_nodes[1], ir.Assign))
        require(isinstance(reduce_nodes[0].value, ir.Expr))
        require(isinstance(reduce_nodes[1].value, ir.Var))
        rhs = reduce_nodes[0].value

        if rhs.op == 'inplace_binop':
            if rhs.fn in ('+=', operator.iadd):
                return Reduce_Type.Sum
            if rhs.fn in ('|=', operator.ior):
                return Reduce_Type.Or
            if rhs.fn in ('*=', operator.imul):
                return Reduce_Type.Prod

        if rhs.op == 'call':
            func = find_callname(self.state.func_ir, rhs, self.state.typemap)
            if func == ('min', 'builtins'):
                if isinstance(self.state.typemap[rhs.args[0].name], numba.core.typing.builtins.IndexValueType):
                    return Reduce_Type.Argmin
                return Reduce_Type.Min
            if func == ('max', 'builtins'):
                if isinstance(self.state.typemap[rhs.args[0].name], numba.core.typing.builtins.IndexValueType):
                    return Reduce_Type.Argmax
                return Reduce_Type.Max

        raise GuardException  # pragma: no cover 
Example #9
Source File: expr.py    From owasp-pysec with Apache License 2.0 5 votes vote down vote up
def __imul__(self, other):
        return Expression((self, other), operator.imul) 
Example #10
Source File: test_gfpx.py    From mpyc with MIT License 5 votes vote down vote up
def _test_errors(self, poly):
        self.assertRaises(ValueError, poly.from_terms, 'x**2')
        self.assertRaises(TypeError, poly, 0.1)
        self.assertRaises(TypeError, poly, gfpx.GFpX(257)(0))
        self.assertRaises(ValueError, poly, [poly.p])
        self.assertRaises(TypeError, operator.add, poly(0), 0.1)
        self.assertRaises(TypeError, operator.iadd, poly(0), 0.1)
        self.assertRaises(TypeError, operator.sub, poly(0), 0.1)
        self.assertRaises(TypeError, operator.sub, 0.1, poly(0))
        self.assertRaises(TypeError, operator.isub, poly(0), 0.1)
        self.assertRaises(TypeError, operator.mul, poly(0), 0.1)
        self.assertRaises(TypeError, operator.imul, poly(0), 0.1)
        self.assertRaises(TypeError, operator.lshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.lshift, 0.1, poly(0))
        self.assertRaises(TypeError, operator.ilshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.rshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.rshift, 0.1, poly(0))
        self.assertRaises(TypeError, operator.irshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.floordiv, poly(0), 0.1)
        self.assertRaises(TypeError, operator.floordiv, 0.1, poly(0))
        self.assertRaises(TypeError, operator.ifloordiv, poly(0), 0.1)
        self.assertRaises(TypeError, operator.mod, poly(0), 0.1)
        self.assertRaises(TypeError, operator.mod, 0.1, poly(0))
        self.assertRaises(TypeError, operator.imod, poly(0), 0.1)
        self.assertRaises(TypeError, divmod, poly(0), 0.1)
        self.assertRaises(TypeError, divmod, 0.1, poly(0))
        self.assertRaises(TypeError, operator.lt, poly(0), 0.1)
        self.assertRaises(TypeError, operator.lt, 0.1, poly(0))  # NB: tests >
        self.assertRaises(TypeError, operator.le, poly(0), 0.1)
        self.assertRaises(TypeError, operator.le, 0.1, poly(0))  # NB: tests <
        self.assertRaises(ZeroDivisionError, poly.invert, poly(283), poly(0))
        self.assertRaises(ZeroDivisionError, poly.invert, poly(283), poly(283))
        self.assertRaises(ZeroDivisionError, poly.mod, poly(283), poly(0))
        self.assertRaises(ZeroDivisionError, poly.divmod, poly(283), poly(0))
        self.assertRaises(ValueError, operator.pow, poly(3), -16) 
Example #11
Source File: quantity.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
def __imul__(self, other):
        if not isinstance(self._magnitude, ndarray):
            return self._mul_div(other, operator.mul)
        else:
            return self._imul_div(other, operator.imul) 
Example #12
Source File: test_unit.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_unitcontainer_arithmetic(self):
        x = UnitsContainer(meter=1)
        y = UnitsContainer(second=1)
        z = UnitsContainer(meter=1, second=-2)

        self._test_not_inplace(op.mul, x, y, UnitsContainer(meter=1, second=1))
        self._test_not_inplace(op.truediv, x, y, UnitsContainer(meter=1, second=-1))
        self._test_not_inplace(op.pow, z, 2, UnitsContainer(meter=2, second=-4))
        self._test_not_inplace(op.pow, z, -2, UnitsContainer(meter=-2, second=4))

        self._test_inplace(op.imul, x, y, UnitsContainer(meter=1, second=1))
        self._test_inplace(op.itruediv, x, y, UnitsContainer(meter=1, second=-1))
        self._test_inplace(op.ipow, z, 2, UnitsContainer(meter=2, second=-4))
        self._test_inplace(op.ipow, z, -2, UnitsContainer(meter=-2, second=4)) 
Example #13
Source File: test_issues.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_issue52(self):
        u1 = UnitRegistry()
        u2 = UnitRegistry()
        q1 = u1.meter
        q2 = u2.meter
        import operator as op
        for fun in (op.add, op.iadd,
                    op.sub, op.isub,
                    op.mul, op.imul,
                    op.floordiv, op.ifloordiv,
                    op.truediv, op.itruediv):
            self.assertRaises(ValueError, fun, q1, q2) 
Example #14
Source File: test_quantity.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _test_quantity_imul_idiv(self, unit, func):
        #func(op.imul, 10.0, '4.2*meter', '42*meter')
        func(op.imul, '4.2*meter', 10.0, '42*meter', unit)
        func(op.imul, '4.2*meter', '10*inch', '42*meter*inch', unit)
        #func(op.truediv, 42, '4.2*meter', '10/meter')
        func(op.itruediv, '4.2*meter', unit * 10.0, '0.42*meter', unit)
        func(op.itruediv, '4.2*meter', '10*inch', '0.42*meter/inch', unit) 
Example #15
Source File: test_array.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_buffer(self):
        a = array.array(self.typecode, self.example)
        m = memoryview(a)
        expected = m.tobytes()
        self.assertEqual(a.tobytes(), expected)
        self.assertEqual(a.tobytes()[0], expected[0])
        # Resizing is forbidden when there are buffer exports.
        # For issue 4509, we also check after each error that
        # the array was not modified.
        self.assertRaises(BufferError, a.append, a[0])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.extend, a[0:1])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.remove, a[0])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.pop, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.fromlist, a.tolist())
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.frombytes, a.tobytes())
        self.assertEqual(m.tobytes(), expected)
        if self.typecode == 'u':
            self.assertRaises(BufferError, a.fromunicode, a.tounicode())
            self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.imul, a, 2)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.imul, a, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.delitem, a, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.delitem, a, slice(0, 1))
        self.assertEqual(m.tobytes(), expected) 
Example #16
Source File: test_ndarray_elementwise_op.py    From cupy with MIT License 5 votes vote down vote up
def test_broadcasted_imul(self):
        self.check_array_broadcasted_op(operator.imul) 
Example #17
Source File: test_ndarray_elementwise_op.py    From cupy with MIT License 5 votes vote down vote up
def test_imul_array(self):
        self.check_array_array_op(operator.imul) 
Example #18
Source File: test_ndarray_elementwise_op.py    From cupy with MIT License 5 votes vote down vote up
def test_imul_scalar(self):
        self.check_array_scalar_op(operator.imul) 
Example #19
Source File: test_array.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_buffer(self):
        a = array.array(self.typecode, self.example)
        m = memoryview(a)
        expected = m.tobytes()
        self.assertEqual(a.tobytes(), expected)
        self.assertEqual(a.tobytes()[0], expected[0])
        # Resizing is forbidden when there are buffer exports.
        # For issue 4509, we also check after each error that
        # the array was not modified.
        self.assertRaises(BufferError, a.append, a[0])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.extend, a[0:1])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.remove, a[0])
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.pop, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.fromlist, a.tolist())
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, a.frombytes, a.tobytes())
        self.assertEqual(m.tobytes(), expected)
        if self.typecode == 'u':
            self.assertRaises(BufferError, a.fromunicode, a.tounicode())
            self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.imul, a, 2)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.imul, a, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.delitem, a, 0)
        self.assertEqual(m.tobytes(), expected)
        self.assertRaises(BufferError, operator.delitem, a, slice(0, 1))
        self.assertEqual(m.tobytes(), expected) 
Example #20
Source File: test_base.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_inplace_dense(self):
        a = np.ones((3, 4))
        b = self.spmatrix(a)

        x = a.copy()
        y = a.copy()
        x += a
        y += b
        assert_array_equal(x, y)

        x = a.copy()
        y = a.copy()
        x -= a
        y -= b
        assert_array_equal(x, y)

        # This is matrix product, from __rmul__
        assert_raises(ValueError, operator.imul, x, b)
        x = a.copy()
        y = a.copy()
        x = x.dot(a.T)
        y *= b.T
        assert_array_equal(x, y)

        # Matrix (non-elementwise) floor division is not defined
        assert_raises(TypeError, operator.ifloordiv, x, b) 
Example #21
Source File: test_array.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_imul(self):
        a = array.array(self.typecode, self.example)
        b = a

        a *= 5
        self.assertTrue(a is b)
        self.assertEqual(
            a,
            array.array(self.typecode, 5*self.example)
        )

        a *= 0
        self.assertTrue(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a *= 1000
        self.assertTrue(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a *= -1
        self.assertTrue(a is b)
        self.assertEqual(a, array.array(self.typecode))

        a = array.array(self.typecode, self.example)
        a *= -1
        self.assertEqual(a, array.array(self.typecode))

        if test_support.is_jython:
            self.assertRaises(TypeError, operator.imul, a, "bad")
        else:
            self.assertRaises(TypeError, a.__imul__, "bad") 
Example #22
Source File: test_cycler.py    From cycler with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_failures():
    c1 = cycler(c='rgb')
    c2 = cycler(c=c1)
    pytest.raises(ValueError, add, c1, c2)
    pytest.raises(ValueError, iadd, c1, c2)
    pytest.raises(ValueError, mul, c1, c2)
    pytest.raises(ValueError, imul, c1, c2)
    pytest.raises(TypeError, iadd, c2, 'aardvark')
    pytest.raises(TypeError, imul, c2, 'aardvark')

    c3 = cycler(ec=c1)

    pytest.raises(ValueError, cycler, c=c2+c3) 
Example #23
Source File: test_Signal.py    From myhdl with GNU Lesser General Public License v2.1 5 votes vote down vote up
def testIMul(self):
        self.augmentedAssignCheck(operator.imul, imax=maxint)  # XXX doesn't work for long i??? 
Example #24
Source File: test_intbv.py    From myhdl with GNU Lesser General Public License v2.1 5 votes vote down vote up
def testIMul(self):
        self.augmentedAssignCheck(operator.imul, imax=maxint)  # XXX doesn't work for long i??? 
Example #25
Source File: test_operator.py    From CTFCrackTools-V2 with GNU General Public License v3.0 4 votes vote down vote up
def test_inplace(self):
        class C(object):
            def __iadd__     (self, other): return "iadd"
            def __iand__     (self, other): return "iand"
            def __idiv__     (self, other): return "idiv"
            def __ifloordiv__(self, other): return "ifloordiv"
            def __ilshift__  (self, other): return "ilshift"
            def __imod__     (self, other): return "imod"
            def __imul__     (self, other): return "imul"
            def __ior__      (self, other): return "ior"
            def __ipow__     (self, other): return "ipow"
            def __irshift__  (self, other): return "irshift"
            def __isub__     (self, other): return "isub"
            def __itruediv__ (self, other): return "itruediv"
            def __ixor__     (self, other): return "ixor"
            def __getitem__(self, other): return 5  # so that C is a sequence
        c = C()
        self.assertEqual(operator.iadd     (c, 5), "iadd")
        self.assertEqual(operator.iand     (c, 5), "iand")
        self.assertEqual(operator.idiv     (c, 5), "idiv")
        self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
        self.assertEqual(operator.ilshift  (c, 5), "ilshift")
        self.assertEqual(operator.imod     (c, 5), "imod")
        self.assertEqual(operator.imul     (c, 5), "imul")
        self.assertEqual(operator.ior      (c, 5), "ior")
        self.assertEqual(operator.ipow     (c, 5), "ipow")
        self.assertEqual(operator.irshift  (c, 5), "irshift")
        self.assertEqual(operator.isub     (c, 5), "isub")
        self.assertEqual(operator.itruediv (c, 5), "itruediv")
        self.assertEqual(operator.ixor     (c, 5), "ixor")
        self.assertEqual(operator.iconcat  (c, c), "iadd")
        self.assertEqual(operator.irepeat  (c, 5), "imul")
        self.assertEqual(operator.__iadd__     (c, 5), "iadd")
        self.assertEqual(operator.__iand__     (c, 5), "iand")
        self.assertEqual(operator.__idiv__     (c, 5), "idiv")
        self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
        self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
        self.assertEqual(operator.__imod__     (c, 5), "imod")
        self.assertEqual(operator.__imul__     (c, 5), "imul")
        self.assertEqual(operator.__ior__      (c, 5), "ior")
        self.assertEqual(operator.__ipow__     (c, 5), "ipow")
        self.assertEqual(operator.__irshift__  (c, 5), "irshift")
        self.assertEqual(operator.__isub__     (c, 5), "isub")
        self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
        self.assertEqual(operator.__ixor__     (c, 5), "ixor")
        self.assertEqual(operator.__iconcat__  (c, c), "iadd")
        self.assertEqual(operator.__irepeat__  (c, 5), "imul") 
Example #26
Source File: test_array.py    From CTFCrackTools-V2 with GNU General Public License v3.0 4 votes vote down vote up
def test_resize_forbidden(self):
        # Test that array resizing is forbidden with buffer exports (Jython addition).
        # Test adapted from corresponding one in test_bytes.
        # We can't resize an array when there are buffer exports, even
        # if it wouldn't reallocate the underlying array.
        # Furthermore, no destructive changes to the buffer may be applied
        # before raising the error.
        a = array.array(self.typecode, self.example)
        def resize(n):
            "n = -1 -> Smaller, 0 -> the same, or 1 -> larger."
            a[1:-1] = array.array(self.typecode, self.example[1-n:-1])

        v = memoryview(a)
        orig = a[:]

        self.assertRaises(BufferError, resize, -1)
        self.assertEqual(a, orig)
        #self.assertRaises(BufferError, resize, 0)
        #self.assertEqual(a, orig)
        self.assertRaises(BufferError, resize, 1)
        self.assertEqual(a, orig)

        # Other operations implying resize
        self.assertRaises(BufferError, a.pop, 0)
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.remove, a[1])
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.append, self.outside)
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.insert, 1, self.outside)
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.extend, self.example)
        self.assertEqual(a, orig)

        def iadd(x):
            x += array.array(self.typecode, self.biggerexample)
        self.assertRaises(BufferError, iadd, a)
        self.assertEqual(a, orig)

        def imul(x):
            x *= 3
        self.assertRaises(BufferError, imul, a)
        self.assertEqual(a, orig)

        def delitem():
            del a[1]
        self.assertRaises(BufferError, delitem)
        self.assertEqual(a, orig)

        # deleting a non-contiguous slice
        def delslice():
            del a[1:-1:2]
        self.assertRaises(BufferError, delslice)
        self.assertEqual(a, orig)

        # Show that releasing v releases the array for size change
        v.release()
        a.pop() 
Example #27
Source File: pspace_test.py    From odl with Mozilla Public License 2.0 4 votes vote down vote up
def test_operators(odl_arithmetic_op):
    # Test of the operators `+`, `-`, etc work as expected by numpy
    op = odl_arithmetic_op

    space = odl.rn(3)
    pspace = odl.ProductSpace(space, 2)

    # Interactions with scalars

    for scalar in [-31.2, -1, 0, 1, 2.13]:

        # Left op
        x_arr, x = noise_elements(pspace)
        if scalar == 0 and op in [operator.truediv, operator.itruediv]:
            # Check for correct zero division behaviour
            with pytest.raises(ZeroDivisionError):
                y = op(x, scalar)
        else:
            y_arr = op(x_arr, scalar)
            y = op(x, scalar)

            assert all_almost_equal([x, y], [x_arr, y_arr])

        # Right op
        x_arr, x = noise_elements(pspace)

        y_arr = op(scalar, x_arr)
        y = op(scalar, x)

        assert all_almost_equal([x, y], [x_arr, y_arr])

    # Verify that the statement z=op(x, y) gives equivalent results to NumPy
    x_arr, x = noise_elements(space, 1)
    y_arr, y = noise_elements(pspace, 1)

    # non-aliased left
    if op in [operator.iadd, operator.isub, operator.itruediv, operator.imul]:
        # Check for correct error since in-place op is not possible here
        with pytest.raises(TypeError):
            z = op(x, y)
    else:
        z_arr = op(x_arr, y_arr)
        z = op(x, y)

        assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr])

    # non-aliased right
    z_arr = op(y_arr, x_arr)
    z = op(y, x)

    assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr])

    # aliased operation
    z_arr = op(y_arr, y_arr)
    z = op(y, y)

    assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr]) 
Example #28
Source File: test_array.py    From CTFCrackTools with GNU General Public License v3.0 4 votes vote down vote up
def test_resize_forbidden(self):
        # Test that array resizing is forbidden with buffer exports (Jython addition).
        # Test adapted from corresponding one in test_bytes.
        # We can't resize an array when there are buffer exports, even
        # if it wouldn't reallocate the underlying array.
        # Furthermore, no destructive changes to the buffer may be applied
        # before raising the error.
        a = array.array(self.typecode, self.example)
        def resize(n):
            "n = -1 -> Smaller, 0 -> the same, or 1 -> larger."
            a[1:-1] = array.array(self.typecode, self.example[1-n:-1])

        v = memoryview(a)
        orig = a[:]

        self.assertRaises(BufferError, resize, -1)
        self.assertEqual(a, orig)
        #self.assertRaises(BufferError, resize, 0)
        #self.assertEqual(a, orig)
        self.assertRaises(BufferError, resize, 1)
        self.assertEqual(a, orig)

        # Other operations implying resize
        self.assertRaises(BufferError, a.pop, 0)
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.remove, a[1])
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.append, self.outside)
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.insert, 1, self.outside)
        self.assertEqual(a, orig)
        self.assertRaises(BufferError, a.extend, self.example)
        self.assertEqual(a, orig)

        def iadd(x):
            x += array.array(self.typecode, self.biggerexample)
        self.assertRaises(BufferError, iadd, a)
        self.assertEqual(a, orig)

        def imul(x):
            x *= 3
        self.assertRaises(BufferError, imul, a)
        self.assertEqual(a, orig)

        def delitem():
            del a[1]
        self.assertRaises(BufferError, delitem)
        self.assertEqual(a, orig)

        # deleting a non-contiguous slice
        def delslice():
            del a[1:-1:2]
        self.assertRaises(BufferError, delslice)
        self.assertEqual(a, orig)

        # Show that releasing v releases the array for size change
        v.release()
        a.pop() 
Example #29
Source File: test_operator.py    From CTFCrackTools with GNU General Public License v3.0 4 votes vote down vote up
def test_inplace(self):
        class C(object):
            def __iadd__     (self, other): return "iadd"
            def __iand__     (self, other): return "iand"
            def __idiv__     (self, other): return "idiv"
            def __ifloordiv__(self, other): return "ifloordiv"
            def __ilshift__  (self, other): return "ilshift"
            def __imod__     (self, other): return "imod"
            def __imul__     (self, other): return "imul"
            def __ior__      (self, other): return "ior"
            def __ipow__     (self, other): return "ipow"
            def __irshift__  (self, other): return "irshift"
            def __isub__     (self, other): return "isub"
            def __itruediv__ (self, other): return "itruediv"
            def __ixor__     (self, other): return "ixor"
            def __getitem__(self, other): return 5  # so that C is a sequence
        c = C()
        self.assertEqual(operator.iadd     (c, 5), "iadd")
        self.assertEqual(operator.iand     (c, 5), "iand")
        self.assertEqual(operator.idiv     (c, 5), "idiv")
        self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
        self.assertEqual(operator.ilshift  (c, 5), "ilshift")
        self.assertEqual(operator.imod     (c, 5), "imod")
        self.assertEqual(operator.imul     (c, 5), "imul")
        self.assertEqual(operator.ior      (c, 5), "ior")
        self.assertEqual(operator.ipow     (c, 5), "ipow")
        self.assertEqual(operator.irshift  (c, 5), "irshift")
        self.assertEqual(operator.isub     (c, 5), "isub")
        self.assertEqual(operator.itruediv (c, 5), "itruediv")
        self.assertEqual(operator.ixor     (c, 5), "ixor")
        self.assertEqual(operator.iconcat  (c, c), "iadd")
        self.assertEqual(operator.irepeat  (c, 5), "imul")
        self.assertEqual(operator.__iadd__     (c, 5), "iadd")
        self.assertEqual(operator.__iand__     (c, 5), "iand")
        self.assertEqual(operator.__idiv__     (c, 5), "idiv")
        self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
        self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
        self.assertEqual(operator.__imod__     (c, 5), "imod")
        self.assertEqual(operator.__imul__     (c, 5), "imul")
        self.assertEqual(operator.__ior__      (c, 5), "ior")
        self.assertEqual(operator.__ipow__     (c, 5), "ipow")
        self.assertEqual(operator.__irshift__  (c, 5), "irshift")
        self.assertEqual(operator.__isub__     (c, 5), "isub")
        self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
        self.assertEqual(operator.__ixor__     (c, 5), "ixor")
        self.assertEqual(operator.__iconcat__  (c, c), "iadd")
        self.assertEqual(operator.__irepeat__  (c, 5), "imul") 
Example #30
Source File: test_operator.py    From medicare-demo with Apache License 2.0 4 votes vote down vote up
def test_inplace(self):
        class C(object):
            def __iadd__     (self, other): return "iadd"
            def __iand__     (self, other): return "iand"
            def __idiv__     (self, other): return "idiv"
            def __ifloordiv__(self, other): return "ifloordiv"
            def __ilshift__  (self, other): return "ilshift"
            def __imod__     (self, other): return "imod"
            def __imul__     (self, other): return "imul"
            def __ior__      (self, other): return "ior"
            def __ipow__     (self, other): return "ipow"
            def __irshift__  (self, other): return "irshift"
            def __isub__     (self, other): return "isub"
            def __itruediv__ (self, other): return "itruediv"
            def __ixor__     (self, other): return "ixor"
            def __getitem__(self, other): return 5  # so that C is a sequence
        c = C()
        self.assertEqual(operator.iadd     (c, 5), "iadd")
        self.assertEqual(operator.iand     (c, 5), "iand")
        self.assertEqual(operator.idiv     (c, 5), "idiv")
        self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
        self.assertEqual(operator.ilshift  (c, 5), "ilshift")
        self.assertEqual(operator.imod     (c, 5), "imod")
        self.assertEqual(operator.imul     (c, 5), "imul")
        self.assertEqual(operator.ior      (c, 5), "ior")
        self.assertEqual(operator.ipow     (c, 5), "ipow")
        self.assertEqual(operator.irshift  (c, 5), "irshift")
        self.assertEqual(operator.isub     (c, 5), "isub")
        self.assertEqual(operator.itruediv (c, 5), "itruediv")
        self.assertEqual(operator.ixor     (c, 5), "ixor")
        self.assertEqual(operator.iconcat  (c, c), "iadd")
        self.assertEqual(operator.irepeat  (c, 5), "imul")
        self.assertEqual(operator.__iadd__     (c, 5), "iadd")
        self.assertEqual(operator.__iand__     (c, 5), "iand")
        self.assertEqual(operator.__idiv__     (c, 5), "idiv")
        self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
        self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
        self.assertEqual(operator.__imod__     (c, 5), "imod")
        self.assertEqual(operator.__imul__     (c, 5), "imul")
        self.assertEqual(operator.__ior__      (c, 5), "ior")
        self.assertEqual(operator.__ipow__     (c, 5), "ipow")
        self.assertEqual(operator.__irshift__  (c, 5), "irshift")
        self.assertEqual(operator.__isub__     (c, 5), "isub")
        self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
        self.assertEqual(operator.__ixor__     (c, 5), "ixor")
        self.assertEqual(operator.__iconcat__  (c, c), "iadd")
        self.assertEqual(operator.__irepeat__  (c, 5), "imul")