Exemple #1
0
    def test_it(self):
        class foo(object):
            def __init__(self, **kwargs):
                for attr, val in kwargs.items():
                    setattr(self, attr, val)
        obj = foo()
        d = self.kls(obj)
        with pytest.raises(KeyError):
            operator.__getitem__(d, 'x')
        with pytest.raises(KeyError):
            operator.__delitem__(d, 'x')
        assert 'x' not in d
        d['x'] = 1
        assert d['x'] == 1
        assert 'x' in d
        assert ['x'] == list(x for x in d if not x.startswith("__"))
        del d['x']
        assert 'x' not in d
        with pytest.raises(KeyError):
            operator.__delitem__(d, 'x')
        with pytest.raises(KeyError):
            operator.__getitem__(d, 'x')

        # Finally, verify that immutable attribute errors are handled correctly.
        d = self.kls(object())
        with pytest.raises(KeyError):
            operator.__setitem__(d, 'x', 1)
        with pytest.raises(KeyError):
            operator.__delitem__(d, 'x')
Exemple #2
0
    def test_it(self):
        class foo(object):
            def __init__(self, **kwargs):
                for attr, val in kwargs.items():
                    setattr(self, attr, val)
        obj = foo()
        d = self.kls(obj)
        with pytest.raises(KeyError):
            operator.__getitem__(d, 'x')
        with pytest.raises(KeyError):
            operator.__delitem__(d, 'x')
        assert 'x' not in d
        d['x'] = 1
        assert d['x'] == 1
        assert 'x' in d
        assert ['x'] == list(x for x in d if not x.startswith("__"))
        del d['x']
        assert 'x' not in d
        with pytest.raises(KeyError):
            operator.__delitem__(d, 'x')
        with pytest.raises(KeyError):
            operator.__getitem__(d, 'x')

        # Finally, verify that immutable attribute errors are handled correctly.
        d = self.kls(object())
        with pytest.raises(KeyError):
            operator.__setitem__(d, 'x', 1)
        with pytest.raises(KeyError):
            operator.__delitem__(d, 'x')
Exemple #3
0
    def test_sanity(self):
        """Performs a set of simple sanity checks on most operators."""

        #__abs__
        self.assertEqual(operator.__abs__(0), 0)
        self.assertEqual(operator.__abs__(1), 1)
        self.assertEqual(operator.__abs__(-1), 1)
        self.assertEqual(operator.__abs__(0.0), 0.0)
        self.assertEqual(operator.__abs__(1.1), 1.1)
        self.assertEqual(operator.__abs__(-1.1), 1.1)
        self.assertEqual(operator.__abs__(big(0)), big(0))
        self.assertEqual(operator.__abs__(big(1)), big(1))
        self.assertEqual(operator.__abs__(-big(1)), big(1))

        #__neg__
        self.assertEqual(operator.__neg__(0), 0)
        self.assertEqual(operator.__neg__(1), -1)
        self.assertEqual(operator.__neg__(-1), 1)
        self.assertEqual(operator.__neg__(0.0), 0.0)
        self.assertEqual(operator.__neg__(1.1), -1.1)
        self.assertEqual(operator.__neg__(-1.1), 1.1)
        self.assertEqual(operator.__neg__(big(0)), big(0))
        self.assertEqual(operator.__neg__(big(1)), -big(1))
        self.assertEqual(operator.__neg__(-big(1)), big(1))

        #__pos__
        self.assertEqual(operator.__pos__(0), 0)
        self.assertEqual(operator.__pos__(1), 1)
        self.assertEqual(operator.__pos__(-1), -1)
        self.assertEqual(operator.__pos__(0.0), 0.0)
        self.assertEqual(operator.__pos__(1.1), 1.1)
        self.assertEqual(operator.__pos__(-1.1), -1.1)
        self.assertEqual(operator.__pos__(big(0)), big(0))
        self.assertEqual(operator.__pos__(big(1)), big(1))
        self.assertEqual(operator.__pos__(-big(1)), -big(1))

        #__add__
        self.assertEqual(operator.__add__(0, 0), 0)
        self.assertEqual(operator.__add__(1, 2), 3)
        self.assertEqual(operator.__add__(-1, 2), 1)
        self.assertEqual(operator.__add__(0.0, 0.0), 0.0)
        self.assertEqual(operator.__add__(1.1, 2.1), 3.2)
        self.assertEqual(operator.__add__(-1.1, 2.1), 1.0)
        self.assertEqual(operator.__add__(big(0), big(0)), big(0))
        self.assertEqual(operator.__add__(big(1), big(2)), big(3))
        self.assertEqual(operator.__add__(-big(1), big(2)), big(1))

        #__sub__
        self.assertEqual(operator.__sub__(0, 0), 0)
        self.assertEqual(operator.__sub__(1, 2), -1)
        self.assertEqual(operator.__sub__(-1, 2), -3)
        self.assertEqual(operator.__sub__(0.0, 0.0), 0.0)
        self.assertEqual(operator.__sub__(1.1, 2.1), -1.0)
        self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2)
        self.assertEqual(operator.__sub__(big(0), big(0)), big(0))
        self.assertEqual(operator.__sub__(big(1), big(2)), -big(1))
        self.assertEqual(operator.__sub__(-big(1), big(2)), -big(3))

        #__mul__
        self.assertEqual(operator.__mul__(0, 0), 0)
        self.assertEqual(operator.__mul__(1, 2), 2)
        self.assertEqual(operator.__mul__(-1, 2), -2)
        self.assertEqual(operator.__mul__(0.0, 0.0), 0.0)
        self.assertEqual(operator.__mul__(2.0, 3.0), 6.0)
        self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0)
        self.assertEqual(operator.__mul__(big(0), big(0)), big(0))
        self.assertEqual(operator.__mul__(big(1), big(2)), big(2))
        self.assertEqual(operator.__mul__(-big(1), big(2)), -big(2))

        #__div__
        self.assertEqual(operator.__div__(0, 1), 0)
        self.assertEqual(operator.__div__(4, 2), 2)
        self.assertEqual(operator.__div__(-1, 2), -1)
        self.assertEqual(operator.__div__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__div__(4.0, 2.0), 2.0)
        self.assertEqual(operator.__div__(-4.0, 2.0), -2.0)
        self.assertEqual(operator.__div__(big(0), big(1)), big(0))
        self.assertEqual(operator.__div__(big(4), big(2)), big(2))
        self.assertEqual(operator.__div__(-big(4), big(2)), -big(2))

        #__floordiv__
        self.assertEqual(operator.__floordiv__(0, 1), 0)
        self.assertEqual(operator.__floordiv__(4, 2), 2)
        self.assertEqual(operator.__floordiv__(-1, 2), -1)
        self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0)
        self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0)
        self.assertEqual(operator.__floordiv__(big(0), big(1)), big(0))
        self.assertEqual(operator.__floordiv__(big(4), big(2)), big(2))
        self.assertEqual(operator.__floordiv__(-big(4), big(2)), -big(2))

        #__truediv__
        self.assertEqual(operator.__truediv__(0, 1), 0)
        self.assertEqual(operator.__truediv__(4, 2), 2)
        self.assertEqual(operator.__truediv__(-1, 2), -0.5)
        self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0)
        self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5)
        self.assertEqual(operator.__truediv__(big(0), big(1)), big(0))
        self.assertEqual(operator.__truediv__(big(4), big(2)), big(2))
        self.assertEqual(operator.__truediv__(-big(4), big(2)), -big(2))

        #__mod__
        self.assertEqual(operator.__mod__(0, 1), 0)
        self.assertEqual(operator.__mod__(4, 2), 0)
        self.assertEqual(operator.__mod__(-1, 2), 1)
        self.assertEqual(operator.__mod__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__mod__(4.0, 2.0), 0.0)
        self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0)
        self.assertEqual(operator.__mod__(big(0), big(1)), big(0))
        self.assertEqual(operator.__mod__(big(4), big(2)), big(0))
        self.assertEqual(operator.__mod__(-big(4), big(2)), big(0))

        #__inv__
        self.assertEqual(operator.__inv__(0), -1)
        self.assertEqual(operator.__inv__(1), -2)
        self.assertEqual(operator.__inv__(-1), 0)
        self.assertEqual(operator.__inv__(big(0)), -big(1))
        self.assertEqual(operator.__inv__(big(1)), -big(2))
        self.assertEqual(operator.__inv__(-big(1)), big(0))

        #__invert__
        self.assertEqual(operator.__invert__(0), -1)
        self.assertEqual(operator.__invert__(1), -2)
        self.assertEqual(operator.__invert__(-1), 0)
        self.assertEqual(operator.__invert__(big(0)), -big(1))
        self.assertEqual(operator.__invert__(big(1)), -big(2))
        self.assertEqual(operator.__invert__(-big(1)), big(0))

        #__lshift__
        self.assertEqual(operator.__lshift__(0, 1), 0)
        self.assertEqual(operator.__lshift__(1, 1), 2)
        self.assertEqual(operator.__lshift__(-1, 1), -2)
        self.assertEqual(operator.__lshift__(big(0), 1), big(0))
        self.assertEqual(operator.__lshift__(big(1), 1), big(2))
        self.assertEqual(operator.__lshift__(-big(1), 1), -big(2))

        #__rshift__
        self.assertEqual(operator.__rshift__(1, 1), 0)
        self.assertEqual(operator.__rshift__(2, 1), 1)
        self.assertEqual(operator.__rshift__(-1, 1), -1)
        self.assertEqual(operator.__rshift__(big(1), 1), big(0))
        self.assertEqual(operator.__rshift__(big(2), 1), big(1))
        self.assertEqual(operator.__rshift__(-big(1), 1), -big(1))

        #__not__
        self.assertEqual(operator.__not__(0), 1)
        self.assertEqual(operator.__not__(1), 0)
        self.assertEqual(operator.__not__(-1), 0)
        self.assertEqual(operator.__not__(big(0)), 1)
        self.assertEqual(operator.__not__(big(1)), 0)
        self.assertEqual(operator.__not__(-big(1)), 0)

        #__and__
        self.assertEqual(operator.__and__(0, 0), 0)
        self.assertEqual(operator.__and__(1, 1), 1)
        self.assertEqual(operator.__and__(0, 1), 0)
        self.assertEqual(operator.__and__(1, 0), 0)

        #__xor__
        self.assertEqual(operator.__xor__(0, 0), 0)
        self.assertEqual(operator.__xor__(1, 1), 0)
        self.assertEqual(operator.__xor__(0, 1), 1)
        self.assertEqual(operator.__xor__(1, 0), 1)

        #__or__
        self.assertEqual(operator.__or__(0, 0), 0)
        self.assertEqual(operator.__or__(1, 1), 1)
        self.assertEqual(operator.__or__(0, 1), 1)
        self.assertEqual(operator.__or__(1, 0), 1)

        #__concat__
        self.assertEqual(operator.__concat__([0], [1]), [0, 1])
        self.assertEqual(operator.__concat__([2], [1]), [2, 1])
        self.assertEqual(operator.__concat__([-1], [1]), [-1, 1])

        #__contains__
        self.assertTrue(operator.__contains__("abc", "c"))
        self.assertTrue(not operator.__contains__("abc", "d"))
        self.assertTrue(operator.__contains__("abc", ""))
        self.assertTrue(not operator.__contains__("", "c"))
        self.assertTrue(operator.__contains__([1, 2, 3], 1))
        self.assertTrue(not operator.__contains__([1, 2, 3], 4))

        #__getitem__
        self.assertEqual(operator.__getitem__("abc", 2), "c")
        self.assertRaises(IndexError, operator.__getitem__, "abc", 3)
        self.assertEqual(operator.__getitem__([1, 2, 3], 2), 3)
        self.assertRaises(IndexError, operator.__getitem__, [1, 2, 3], 3)

        #__setitem__
        self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d")
        t_list = [1, 2, 3]
        operator.__setitem__(t_list, 2, 4)
        self.assertEqual(t_list, [1, 2, 4])
        self.assertRaises(IndexError, operator.__setitem__, [1, 2, 3], 4, 9)

        #__delitem__
        #UNIMPLEMENTED
        #self.assertRaises(TypeError, operator.__delitem__, "abc", 2)
        t_list = [1, 2, 3]
        operator.__delitem__(t_list, 2)
        self.assertEqual(t_list, [1, 2])
        self.assertRaises(IndexError, operator.__delitem__, [1, 2, 3], 4)

        #__repeat__
        self.assertEqual(operator.__repeat__("abc", 2), "abcabc")
        self.assertEqual(operator.__repeat__("", 2), "")
        self.assertEqual(operator.__repeat__([1, 2, 3], 2), [1, 2, 3, 1, 2, 3])

        #__getslice__
        self.assertEqual(operator.__getslice__("abc", 1, 2), "b")
        self.assertEqual(operator.__getslice__("abc", 0, 3), "abc")
        self.assertEqual(operator.__getslice__("", 0, 0), "")
        self.assertEqual(operator.__getslice__([1, 2, 3], 1, 2), [2])
        self.assertEqual(operator.__getslice__([1, 2, 3], 0, 3), [1, 2, 3])
        self.assertEqual(operator.__getslice__([], 0, 0), [])

        #__delslice__
        t_list = [1, 2, 3]
        operator.__delslice__(t_list, 1, 2)
        self.assertEqual(t_list, [1, 3])

        t_list = [1, 2, 3]
        operator.__delslice__(t_list, 0, 3)
        self.assertEqual(t_list, [])

        t_list = [1, 2, 3]
        operator.__delslice__(t_list, 0, 0)
        self.assertEqual(t_list, [1, 2, 3])

        #__setslice__
        t_list = [1, 2, 3]
        operator.__setslice__(t_list, 1, 2, [9])
        self.assertEqual(t_list, [1, 9, 3])

        t_list = [1, 2, 3]
        operator.__setslice__(t_list, 0, 3, [9, 8])
        self.assertEqual(t_list, [9, 8])

        t_list = [1, 2, 3]
        operator.__setslice__(t_list, 0, 0, [9])
        self.assertEqual(t_list, [9, 1, 2, 3])
Exemple #4
0
def test_sanity():
    '''
    Performs a set of simple sanity checks on most operators.
    '''
    
    #__abs__
    AreEqual(operator.__abs__(0), 0)
    AreEqual(operator.__abs__(1), 1)
    AreEqual(operator.__abs__(-1), 1)
    AreEqual(operator.__abs__(0.0), 0.0)
    AreEqual(operator.__abs__(1.1), 1.1)
    AreEqual(operator.__abs__(-1.1), 1.1)
    AreEqual(operator.__abs__(0L), 0L)
    AreEqual(operator.__abs__(1L), 1L)
    AreEqual(operator.__abs__(-1L), 1L)
    
    #__neg__
    AreEqual(operator.__neg__(0), 0)
    AreEqual(operator.__neg__(1), -1)
    AreEqual(operator.__neg__(-1), 1)
    AreEqual(operator.__neg__(0.0), 0.0)
    AreEqual(operator.__neg__(1.1), -1.1)
    AreEqual(operator.__neg__(-1.1), 1.1)
    AreEqual(operator.__neg__(0L), 0L)
    AreEqual(operator.__neg__(1L), -1L)
    AreEqual(operator.__neg__(-1L), 1L)
    
    #__pos__
    AreEqual(operator.__pos__(0), 0)
    AreEqual(operator.__pos__(1), 1)
    AreEqual(operator.__pos__(-1), -1)
    AreEqual(operator.__pos__(0.0), 0.0)
    AreEqual(operator.__pos__(1.1), 1.1)
    AreEqual(operator.__pos__(-1.1), -1.1)
    AreEqual(operator.__pos__(0L), 0L)
    AreEqual(operator.__pos__(1L), 1L)
    AreEqual(operator.__pos__(-1L), -1L)
    
    #__add__
    AreEqual(operator.__add__(0, 0), 0)
    AreEqual(operator.__add__(1, 2), 3)
    AreEqual(operator.__add__(-1, 2), 1)
    AreEqual(operator.__add__(0.0, 0.0), 0.0)
    AreEqual(operator.__add__(1.1, 2.1), 3.2)
    AreEqual(operator.__add__(-1.1, 2.1), 1.0)
    AreEqual(operator.__add__(0L, 0L), 0L)
    AreEqual(operator.__add__(1L, 2L), 3L)
    AreEqual(operator.__add__(-1L, 2L), 1L)
    
    #__sub__
    AreEqual(operator.__sub__(0, 0), 0)
    AreEqual(operator.__sub__(1, 2), -1)
    AreEqual(operator.__sub__(-1, 2), -3)
    AreEqual(operator.__sub__(0.0, 0.0), 0.0)
    AreEqual(operator.__sub__(1.1, 2.1), -1.0)
    AreEqual(operator.__sub__(-1.1, 2.1), -3.2)
    AreEqual(operator.__sub__(0L, 0L), 0L)
    AreEqual(operator.__sub__(1L, 2L), -1L)
    AreEqual(operator.__sub__(-1L, 2L), -3L)
    
    #__mul__
    AreEqual(operator.__mul__(0, 0), 0)
    AreEqual(operator.__mul__(1, 2), 2)
    AreEqual(operator.__mul__(-1, 2), -2)
    AreEqual(operator.__mul__(0.0, 0.0), 0.0)
    AreEqual(operator.__mul__(2.0, 3.0), 6.0)
    AreEqual(operator.__mul__(-2.0, 3.0), -6.0)
    AreEqual(operator.__mul__(0L, 0L), 0L)
    AreEqual(operator.__mul__(1L, 2L), 2L)
    AreEqual(operator.__mul__(-1L, 2L), -2L)
    
    #__div__
    AreEqual(operator.__div__(0, 1), 0)
    AreEqual(operator.__div__(4, 2), 2)
    AreEqual(operator.__div__(-1, 2), -1)
    AreEqual(operator.__div__(0.0, 1.0), 0.0)
    AreEqual(operator.__div__(4.0, 2.0), 2.0)
    AreEqual(operator.__div__(-4.0, 2.0), -2.0)
    AreEqual(operator.__div__(0L, 1L), 0L)
    AreEqual(operator.__div__(4L, 2L), 2L)
    AreEqual(operator.__div__(-4L, 2L), -2L)
    
    #__floordiv__
    AreEqual(operator.__floordiv__(0, 1), 0)
    AreEqual(operator.__floordiv__(4, 2), 2)
    AreEqual(operator.__floordiv__(-1, 2), -1)
    AreEqual(operator.__floordiv__(0.0, 1.0), 0.0)
    AreEqual(operator.__floordiv__(4.0, 2.0), 2.0)
    AreEqual(operator.__floordiv__(-4.0, 2.0), -2.0)
    AreEqual(operator.__floordiv__(0L, 1L), 0L)
    AreEqual(operator.__floordiv__(4L, 2L), 2L)
    AreEqual(operator.__floordiv__(-4L, 2L), -2L)

    #__truediv__
    AreEqual(operator.__truediv__(0, 1), 0)
    AreEqual(operator.__truediv__(4, 2), 2)
    AreEqual(operator.__truediv__(-1, 2), -0.5)
    AreEqual(operator.__truediv__(0.0, 1.0), 0.0)
    AreEqual(operator.__truediv__(4.0, 2.0), 2.0)
    AreEqual(operator.__truediv__(-1.0, 2.0), -0.5)
    AreEqual(operator.__truediv__(0L, 1L), 0L)
    AreEqual(operator.__truediv__(4L, 2L), 2L)
    AreEqual(operator.__truediv__(-4L, 2L), -2L)
    
    #__mod__
    AreEqual(operator.__mod__(0, 1), 0)
    AreEqual(operator.__mod__(4, 2), 0)
    AreEqual(operator.__mod__(-1, 2), 1)
    AreEqual(operator.__mod__(0.0, 1.0), 0.0)
    AreEqual(operator.__mod__(4.0, 2.0), 0.0)
    AreEqual(operator.__mod__(-1.0, 2.0), 1.0)
    AreEqual(operator.__mod__(0L, 1L), 0L)
    AreEqual(operator.__mod__(4L, 2L), 0L)
    AreEqual(operator.__mod__(-4L, 2L), 0L)
    
    #__inv__
    AreEqual(operator.__inv__(0), -1)
    AreEqual(operator.__inv__(1), -2)
    AreEqual(operator.__inv__(-1), 0)
    AreEqual(operator.__inv__(0L), -1L)
    AreEqual(operator.__inv__(1L), -2L)
    AreEqual(operator.__inv__(-1L), 0L)

    #__invert__
    AreEqual(operator.__invert__(0), -1)
    AreEqual(operator.__invert__(1), -2)
    AreEqual(operator.__invert__(-1), 0)
    AreEqual(operator.__invert__(0L), -1L)
    AreEqual(operator.__invert__(1L), -2L)
    AreEqual(operator.__invert__(-1L), 0L)

    #__lshift__
    AreEqual(operator.__lshift__(0, 1), 0)
    AreEqual(operator.__lshift__(1, 1), 2)
    AreEqual(operator.__lshift__(-1, 1), -2)
    AreEqual(operator.__lshift__(0L, 1), 0L)
    AreEqual(operator.__lshift__(1L, 1), 2L)
    AreEqual(operator.__lshift__(-1L, 1), -2L)
    
    #__rshift__
    AreEqual(operator.__rshift__(1, 1), 0)
    AreEqual(operator.__rshift__(2, 1), 1)
    AreEqual(operator.__rshift__(-1, 1), -1)
    AreEqual(operator.__rshift__(1L, 1), 0L)
    AreEqual(operator.__rshift__(2L, 1), 1L)
    AreEqual(operator.__rshift__(-1L, 1), -1L)
    
    #__not__
    AreEqual(operator.__not__(0), 1)
    AreEqual(operator.__not__(1), 0)
    AreEqual(operator.__not__(-1), 0)
    AreEqual(operator.__not__(0L), 1)
    AreEqual(operator.__not__(1L), 0)
    AreEqual(operator.__not__(-1L), 0)
    
    #__and__
    AreEqual(operator.__and__(0, 0), 0)
    AreEqual(operator.__and__(1, 1), 1)
    AreEqual(operator.__and__(0, 1), 0)
    AreEqual(operator.__and__(1, 0), 0)
    
    #__xor__
    AreEqual(operator.__xor__(0, 0), 0)
    AreEqual(operator.__xor__(1, 1), 0)
    AreEqual(operator.__xor__(0, 1), 1)
    AreEqual(operator.__xor__(1, 0), 1)
    
    #__or__
    AreEqual(operator.__or__(0, 0), 0)
    AreEqual(operator.__or__(1, 1), 1)
    AreEqual(operator.__or__(0, 1), 1)
    AreEqual(operator.__or__(1, 0), 1)

    #__concat__
    AreEqual(operator.__concat__([0], [1]), [0,1])
    AreEqual(operator.__concat__([2], [1]), [2,1])
    AreEqual(operator.__concat__([-1], [1]), [-1,1])
    
    #__contains__
    Assert(operator.__contains__("abc", "c"))
    Assert(not operator.__contains__("abc", "d"))
    Assert(operator.__contains__("abc", ""))
    Assert(not operator.__contains__("", "c"))
    Assert(operator.__contains__([1,2,3], 1))
    Assert(not operator.__contains__([1,2,3], 4))
    
    #__getitem__
    AreEqual(operator.__getitem__("abc", 2), "c")
    AssertError(IndexError, operator.__getitem__, "abc", 3)
    AreEqual(operator.__getitem__([1,2,3], 2), 3)
    AssertError(IndexError, operator.__getitem__, [1,2,3], 3)
    
    #__setitem__
    AssertError(TypeError, operator.__setitem__, "abc", 2, "d")
    t_list = [1,2,3]
    operator.__setitem__(t_list, 2, 4)
    AreEqual(t_list, [1,2,4])
    AssertError(IndexError, operator.__setitem__, [1,2,3], 4, 9)
    
    #__delitem__
    #UNIMPLEMENTED
    #AssertError(TypeError, operator.__delitem__, "abc", 2)
    t_list = [1,2,3]
    operator.__delitem__(t_list, 2)
    AreEqual(t_list, [1,2])
    AssertError(IndexError, operator.__delitem__, [1,2,3], 4)
    
    #__repeat__
    AreEqual(operator.__repeat__("abc", 2), "abcabc")
    AreEqual(operator.__repeat__("", 2), "")
    AreEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3])
    
    #__getslice__
    AreEqual(operator.__getslice__("abc", 1, 2), "b")
    AreEqual(operator.__getslice__("abc", 0, 3), "abc")
    AreEqual(operator.__getslice__("", 0, 0), "")
    AreEqual(operator.__getslice__([1,2,3], 1, 2), [2])
    AreEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3])
    AreEqual(operator.__getslice__([], 0, 0), [])
    
    #__delslice__
    t_list = [1,2,3]
    operator.__delslice__(t_list, 1, 2)
    AreEqual(t_list, [1,3])
    
    t_list = [1,2,3]
    operator.__delslice__(t_list, 0, 3)
    AreEqual(t_list, [])
    
    t_list = [1,2,3]
    operator.__delslice__(t_list, 0, 0)
    AreEqual(t_list, [1,2,3])
    
    #__setslice__
    t_list = [1,2,3]
    operator.__setslice__(t_list, 1, 2, [9])
    AreEqual(t_list, [1,9,3])
    
    t_list = [1,2,3]
    operator.__setslice__(t_list, 0, 3, [9, 8])
    AreEqual(t_list, [9, 8])
    
    t_list = [1,2,3]
    operator.__setslice__(t_list, 0, 0, [9])
    AreEqual(t_list, [9,1, 2,3])
    def test_sanity(self):
        """Performs a set of simple sanity checks on most operators."""

        #__abs__
        self.assertEqual(operator.__abs__(0), 0)
        self.assertEqual(operator.__abs__(1), 1)
        self.assertEqual(operator.__abs__(-1), 1)
        self.assertEqual(operator.__abs__(0.0), 0.0)
        self.assertEqual(operator.__abs__(1.1), 1.1)
        self.assertEqual(operator.__abs__(-1.1), 1.1)
        self.assertEqual(operator.__abs__(long(0)), long(0))
        self.assertEqual(operator.__abs__(long(1)), long(1))
        self.assertEqual(operator.__abs__(-long(1)), long(1))

        #__neg__
        self.assertEqual(operator.__neg__(0), 0)
        self.assertEqual(operator.__neg__(1), -1)
        self.assertEqual(operator.__neg__(-1), 1)
        self.assertEqual(operator.__neg__(0.0), 0.0)
        self.assertEqual(operator.__neg__(1.1), -1.1)
        self.assertEqual(operator.__neg__(-1.1), 1.1)
        self.assertEqual(operator.__neg__(long(0)), long(0))
        self.assertEqual(operator.__neg__(long(1)), -long(1))
        self.assertEqual(operator.__neg__(-long(1)), long(1))

        #__pos__
        self.assertEqual(operator.__pos__(0), 0)
        self.assertEqual(operator.__pos__(1), 1)
        self.assertEqual(operator.__pos__(-1), -1)
        self.assertEqual(operator.__pos__(0.0), 0.0)
        self.assertEqual(operator.__pos__(1.1), 1.1)
        self.assertEqual(operator.__pos__(-1.1), -1.1)
        self.assertEqual(operator.__pos__(long(0)), long(0))
        self.assertEqual(operator.__pos__(long(1)), long(1))
        self.assertEqual(operator.__pos__(-long(1)), -long(1))

        #__add__
        self.assertEqual(operator.__add__(0, 0), 0)
        self.assertEqual(operator.__add__(1, 2), 3)
        self.assertEqual(operator.__add__(-1, 2), 1)
        self.assertEqual(operator.__add__(0.0, 0.0), 0.0)
        self.assertEqual(operator.__add__(1.1, 2.1), 3.2)
        self.assertEqual(operator.__add__(-1.1, 2.1), 1.0)
        self.assertEqual(operator.__add__(long(0), long(0)), long(0))
        self.assertEqual(operator.__add__(long(1), long(2)), long(3))
        self.assertEqual(operator.__add__(-long(1), long(2)), long(1))

        #__sub__
        self.assertEqual(operator.__sub__(0, 0), 0)
        self.assertEqual(operator.__sub__(1, 2), -1)
        self.assertEqual(operator.__sub__(-1, 2), -3)
        self.assertEqual(operator.__sub__(0.0, 0.0), 0.0)
        self.assertEqual(operator.__sub__(1.1, 2.1), -1.0)
        self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2)
        self.assertEqual(operator.__sub__(long(0), long(0)), long(0))
        self.assertEqual(operator.__sub__(long(1), long(2)), -long(1))
        self.assertEqual(operator.__sub__(-long(1), long(2)), -long(3))

        #__mul__
        self.assertEqual(operator.__mul__(0, 0), 0)
        self.assertEqual(operator.__mul__(1, 2), 2)
        self.assertEqual(operator.__mul__(-1, 2), -2)
        self.assertEqual(operator.__mul__(0.0, 0.0), 0.0)
        self.assertEqual(operator.__mul__(2.0, 3.0), 6.0)
        self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0)
        self.assertEqual(operator.__mul__(long(0), long(0)), long(0))
        self.assertEqual(operator.__mul__(long(1), long(2)), long(2))
        self.assertEqual(operator.__mul__(-long(1), long(2)), -long(2))

        #__div__
        self.assertEqual(operator.__div__(0, 1), 0)
        self.assertEqual(operator.__div__(4, 2), 2)
        self.assertEqual(operator.__div__(-1, 2), -1)
        self.assertEqual(operator.__div__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__div__(4.0, 2.0), 2.0)
        self.assertEqual(operator.__div__(-4.0, 2.0), -2.0)
        self.assertEqual(operator.__div__(long(0), long(1)), long(0))
        self.assertEqual(operator.__div__(long(4), long(2)), long(2))
        self.assertEqual(operator.__div__(-long(4), long(2)), -long(2))

        #__floordiv__
        self.assertEqual(operator.__floordiv__(0, 1), 0)
        self.assertEqual(operator.__floordiv__(4, 2), 2)
        self.assertEqual(operator.__floordiv__(-1, 2), -1)
        self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0)
        self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0)
        self.assertEqual(operator.__floordiv__(long(0), long(1)), long(0))
        self.assertEqual(operator.__floordiv__(long(4), long(2)), long(2))
        self.assertEqual(operator.__floordiv__(-long(4), long(2)), -long(2))

        #__truediv__
        self.assertEqual(operator.__truediv__(0, 1), 0)
        self.assertEqual(operator.__truediv__(4, 2), 2)
        self.assertEqual(operator.__truediv__(-1, 2), -0.5)
        self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0)
        self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5)
        self.assertEqual(operator.__truediv__(long(0), long(1)), long(0))
        self.assertEqual(operator.__truediv__(long(4), long(2)), long(2))
        self.assertEqual(operator.__truediv__(-long(4), long(2)), -long(2))

        #__mod__
        self.assertEqual(operator.__mod__(0, 1), 0)
        self.assertEqual(operator.__mod__(4, 2), 0)
        self.assertEqual(operator.__mod__(-1, 2), 1)
        self.assertEqual(operator.__mod__(0.0, 1.0), 0.0)
        self.assertEqual(operator.__mod__(4.0, 2.0), 0.0)
        self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0)
        self.assertEqual(operator.__mod__(long(0), long(1)), long(0))
        self.assertEqual(operator.__mod__(long(4), long(2)), long(0))
        self.assertEqual(operator.__mod__(-long(4), long(2)), long(0))

        #__inv__
        self.assertEqual(operator.__inv__(0), -1)
        self.assertEqual(operator.__inv__(1), -2)
        self.assertEqual(operator.__inv__(-1), 0)
        self.assertEqual(operator.__inv__(long(0)), -long(1))
        self.assertEqual(operator.__inv__(long(1)), -long(2))
        self.assertEqual(operator.__inv__(-long(1)), long(0))

        #__invert__
        self.assertEqual(operator.__invert__(0), -1)
        self.assertEqual(operator.__invert__(1), -2)
        self.assertEqual(operator.__invert__(-1), 0)
        self.assertEqual(operator.__invert__(long(0)), -long(1))
        self.assertEqual(operator.__invert__(long(1)), -long(2))
        self.assertEqual(operator.__invert__(-long(1)), long(0))

        #__lshift__
        self.assertEqual(operator.__lshift__(0, 1), 0)
        self.assertEqual(operator.__lshift__(1, 1), 2)
        self.assertEqual(operator.__lshift__(-1, 1), -2)
        self.assertEqual(operator.__lshift__(long(0), 1), long(0))
        self.assertEqual(operator.__lshift__(long(1), 1), long(2))
        self.assertEqual(operator.__lshift__(-long(1), 1), -long(2))

        #__rshift__
        self.assertEqual(operator.__rshift__(1, 1), 0)
        self.assertEqual(operator.__rshift__(2, 1), 1)
        self.assertEqual(operator.__rshift__(-1, 1), -1)
        self.assertEqual(operator.__rshift__(long(1), 1), long(0))
        self.assertEqual(operator.__rshift__(long(2), 1), long(1))
        self.assertEqual(operator.__rshift__(-long(1), 1), -long(1))

        #__not__
        self.assertEqual(operator.__not__(0), 1)
        self.assertEqual(operator.__not__(1), 0)
        self.assertEqual(operator.__not__(-1), 0)
        self.assertEqual(operator.__not__(long(0)), 1)
        self.assertEqual(operator.__not__(long(1)), 0)
        self.assertEqual(operator.__not__(-long(1)), 0)

        #__and__
        self.assertEqual(operator.__and__(0, 0), 0)
        self.assertEqual(operator.__and__(1, 1), 1)
        self.assertEqual(operator.__and__(0, 1), 0)
        self.assertEqual(operator.__and__(1, 0), 0)

        #__xor__
        self.assertEqual(operator.__xor__(0, 0), 0)
        self.assertEqual(operator.__xor__(1, 1), 0)
        self.assertEqual(operator.__xor__(0, 1), 1)
        self.assertEqual(operator.__xor__(1, 0), 1)

        #__or__
        self.assertEqual(operator.__or__(0, 0), 0)
        self.assertEqual(operator.__or__(1, 1), 1)
        self.assertEqual(operator.__or__(0, 1), 1)
        self.assertEqual(operator.__or__(1, 0), 1)

        #__concat__
        self.assertEqual(operator.__concat__([0], [1]), [0,1])
        self.assertEqual(operator.__concat__([2], [1]), [2,1])
        self.assertEqual(operator.__concat__([-1], [1]), [-1,1])

        #__contains__
        self.assertTrue(operator.__contains__("abc", "c"))
        self.assertTrue(not operator.__contains__("abc", "d"))
        self.assertTrue(operator.__contains__("abc", ""))
        self.assertTrue(not operator.__contains__("", "c"))
        self.assertTrue(operator.__contains__([1,2,3], 1))
        self.assertTrue(not operator.__contains__([1,2,3], 4))

        #__getitem__
        self.assertEqual(operator.__getitem__("abc", 2), "c")
        self.assertRaises(IndexError, operator.__getitem__, "abc", 3)
        self.assertEqual(operator.__getitem__([1,2,3], 2), 3)
        self.assertRaises(IndexError, operator.__getitem__, [1,2,3], 3)

        #__setitem__
        self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d")
        t_list = [1,2,3]
        operator.__setitem__(t_list, 2, 4)
        self.assertEqual(t_list, [1,2,4])
        self.assertRaises(IndexError, operator.__setitem__, [1,2,3], 4, 9)

        #__delitem__
        #UNIMPLEMENTED
        #self.assertRaises(TypeError, operator.__delitem__, "abc", 2)
        t_list = [1,2,3]
        operator.__delitem__(t_list, 2)
        self.assertEqual(t_list, [1,2])
        self.assertRaises(IndexError, operator.__delitem__, [1,2,3], 4)

        #__repeat__
        self.assertEqual(operator.__repeat__("abc", 2), "abcabc")
        self.assertEqual(operator.__repeat__("", 2), "")
        self.assertEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3])

        #__getslice__
        self.assertEqual(operator.__getslice__("abc", 1, 2), "b")
        self.assertEqual(operator.__getslice__("abc", 0, 3), "abc")
        self.assertEqual(operator.__getslice__("", 0, 0), "")
        self.assertEqual(operator.__getslice__([1,2,3], 1, 2), [2])
        self.assertEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3])
        self.assertEqual(operator.__getslice__([], 0, 0), [])

        #__delslice__
        t_list = [1,2,3]
        operator.__delslice__(t_list, 1, 2)
        self.assertEqual(t_list, [1,3])

        t_list = [1,2,3]
        operator.__delslice__(t_list, 0, 3)
        self.assertEqual(t_list, [])

        t_list = [1,2,3]
        operator.__delslice__(t_list, 0, 0)
        self.assertEqual(t_list, [1,2,3])

        #__setslice__
        t_list = [1,2,3]
        operator.__setslice__(t_list, 1, 2, [9])
        self.assertEqual(t_list, [1,9,3])

        t_list = [1,2,3]
        operator.__setslice__(t_list, 0, 3, [9, 8])
        self.assertEqual(t_list, [9, 8])

        t_list = [1,2,3]
        operator.__setslice__(t_list, 0, 0, [9])
        self.assertEqual(t_list, [9,1, 2,3])
 def __delitem__(self, key):
     """del self[key]"""
     return __delitem__(get_wrapped_object(self), get_wrapped_object(key))
Exemple #7
0
 def update_event(self, inp=-1):
     self.set_output_val(0,
                         operator.__delitem__(self.input(0), self.input(1)))