Ejemplo n.º 1
0
    def test_same_target(self):
        from Merlin.Testing.Call import AllOpsClass
        x = AllOpsClass(4)
        x *= x; self.AreValueFlagEqual(x, 16, 380)

        y = AllOpsClass(6)
        y /= y; self.AreValueFlagEqual(y, 1, 480)
        
        x = y = AllOpsClass(3)
        x *= y; self.AreValueFlagEqual(x, 9, 380); self.assertEqual(y.Value, 3)
Ejemplo n.º 2
0
    def test_future_division_inherited(self):
        """Test future division operators for all numeric types and types inherited from them"""

        class myint(int): pass
        class mylong(long): pass
        class myfloat(float): pass
        class mycomplex(complex): pass

        l = [2, 10L, (1+2j), 3.4, myint(7), mylong(5), myfloat(2.32), mycomplex(3, 2), True]

        if is_cli:
            import System
            l.append(System.Int64.Parse("9"))

        for a in l:
            for b in l:
                try:
                    r = a / b
                except:
                    self.fail("True division failed: %r(%s) / %r(%s)" % (a, type(a), b, type(b)))

        # check division by zero exceptions for true
        threes = [ 3, 3L, 3.0 ]
        zeroes = [ 0, 0L, 0.0 ]

        if is_cli:
            import System
            threes.append(System.Int64.Parse("3"))
            zeroes.append(System.Int64.Parse("0"))

        if is_cli:    
            #Verify true division of a overloaded / operator in a C# class
            self.add_clr_assemblies("operators")
            from Merlin.Testing.Call import AllOpsClass
            x = AllOpsClass(5)
            y = AllOpsClass(4)
            z = x/y
            self.assertEqual(z.Value , 1) #normal division happens since __truediv__ is not found __div__ is called.

        for i in threes:
            for j in zeroes:
                try:
                    r = i / j
                except ZeroDivisionError:
                    pass
                else:
                    self.fail("Didn't get ZeroDivisionError %s, %s, %s, %s" % (type(i).__name__, type(j).__name__, str(i), str(j)))
Ejemplo n.º 3
0
 def test_explicitly_call(self):
     from Merlin.Testing import Flag
     from Merlin.Testing.Call import AllOpsClass
     x = AllOpsClass(1)
     y = AllOpsClass(2)
     
     # binary
     z = AllOpsClass.op_Comma(x, y)
     self.assertEqual(z.Count, 2)
     self.assertEqual(z[1].Value, 2)
     Flag.Check(490)
     
     # unary
     z = AllOpsClass.op_LogicalNot(x)
     self.assertEqual(z, False)
     Flag.Check(130)
     
     self.AreValueFlagEqual(AllOpsClass.op_Increment(x), 2, 150)
     self.AreValueFlagEqual(AllOpsClass.op_Decrement(x), 0, 160)
Ejemplo n.º 4
0
    def test_explicitly_call(self):
        from Merlin.Testing import Flag
        from Merlin.Testing.Call import AllOpsClass
        x = AllOpsClass(1)
        y = AllOpsClass(2)

        # binary
        z = AllOpsClass.op_Comma(x, y)
        self.assertEqual(z.Count, 2)
        self.assertEqual(z[1].Value, 2)
        Flag.Check(490)

        # unary
        z = AllOpsClass.op_LogicalNot(x)
        self.assertEqual(z, False)
        Flag.Check(130)

        self.AreValueFlagEqual(AllOpsClass.op_Increment(x), 2, 150)
        self.AreValueFlagEqual(AllOpsClass.op_Decrement(x), 0, 160)
Ejemplo n.º 5
0
    def test_python_style(self):
        from Merlin.Testing import Flag
        from Merlin.Testing.Call import AllOpsClass
        x = AllOpsClass(7)
        y = AllOpsClass(2)
        
        # http://www.python.org/doc/ref/numeric-types.html
        
        z = AllOpsClass.__add__(x, y); self.AreValueFlagEqual(z, 9, 200)
        z = AllOpsClass.__sub__(x, y); self.AreValueFlagEqual(z, 5, 210)
        z = AllOpsClass.__mul__(x, y); self.AreValueFlagEqual(z, 14, 220)
        z = AllOpsClass.__div__(x, y); self.AreValueFlagEqual(z, 3, 230)
        z = AllOpsClass.__mod__(x, y); self.AreValueFlagEqual(z, 1, 240)
        z = AllOpsClass.__xor__(x, y); self.AreValueFlagEqual(z, 5, 250)
        z = AllOpsClass.__and__(x, y); self.AreValueFlagEqual(z, 2, 260)
        z = AllOpsClass.__or__(x, y); self.AreValueFlagEqual(z, 7, 270)
        z = AllOpsClass.__lshift__(x, 2); self.AreValueFlagEqual(z, 7 << 2, 280)
        z = AllOpsClass.__rshift__(x, 2); self.AreValueFlagEqual(z, 7 >> 2, 290)
        
        z = AllOpsClass.__radd__(y, x); self.AreValueFlagEqual(z, 9, 200)
        z = AllOpsClass.__rsub__(y, x); self.AreValueFlagEqual(z, 5, 210)
        z = AllOpsClass.__rmul__(y, x); self.AreValueFlagEqual(z, 14, 220)
        z = AllOpsClass.__rdiv__(y, x); self.AreValueFlagEqual(z, 3, 230)
        z = AllOpsClass.__rmod__(y, x); self.AreValueFlagEqual(z, 1, 240)
        z = AllOpsClass.__rxor__(y, x); self.AreValueFlagEqual(z, 5, 250)
        z = AllOpsClass.__rand__(y, x); self.AreValueFlagEqual(z, 2, 260)
        z = AllOpsClass.__ror__(y, x); self.AreValueFlagEqual(z, 7, 270)
        
        # bad msg? 'type'
        self.assertRaisesMessage(AttributeError, "'type' object has no attribute '__rlshift__'", lambda: AllOpsClass.__rlshift__(2, x))
        self.assertRaisesMessage(AttributeError, "'type' object has no attribute '__rrshift__'", lambda: AllOpsClass.__rrshift__(2, x))

        z = AllOpsClass.__iadd__(x, y); self.AreValueFlagEqual(z, 9, 450)
        z = AllOpsClass.__isub__(x, y); self.AreValueFlagEqual(z, 5, 390)
        z = AllOpsClass.__imul__(x, y); self.AreValueFlagEqual(z, 14, 380)
        z = AllOpsClass.__idiv__(x, y); self.AreValueFlagEqual(z, 3, 480)
        z = AllOpsClass.__imod__(x, y); self.AreValueFlagEqual(z, 1, 440)
        z = AllOpsClass.__ixor__(x, y); self.AreValueFlagEqual(z, 5, 400)
        z = AllOpsClass.__iand__(x, y); self.AreValueFlagEqual(z, 2, 460)
        z = AllOpsClass.__ior__(x, y); self.AreValueFlagEqual(z, 7, 470)
        z = AllOpsClass.__ilshift__(x, 2); self.AreValueFlagEqual(z, 7 << 2, 410)
        z = AllOpsClass.__irshift__(x, 2); self.AreValueFlagEqual(z, 7 >> 2, 420)
        
        z = AllOpsClass.__neg__(x); self.AreValueFlagEqual(z, -7, 170)
        z = AllOpsClass.__pos__(x); self.AreValueFlagEqual(z, 7, 180)
        z = AllOpsClass.__invert__(x); self.AreValueFlagEqual(z, ~7, 140)

        # http://www.python.org/doc/ref/customization.html
        
        self.assertEqual(AllOpsClass.__lt__(x, y), False); Flag.Check(320)
        self.assertEqual(AllOpsClass.__le__(x, y), False); Flag.Check(350)
        self.assertEqual(AllOpsClass.__eq__(x, y), False); Flag.Check(300)
        self.assertEqual(AllOpsClass.__ne__(x, y), True); Flag.Check(330)
        self.assertEqual(AllOpsClass.__gt__(x, y), True); Flag.Check(310)
        self.assertEqual(AllOpsClass.__ge__(x, y), True); Flag.Check(340)
Ejemplo n.º 6
0
    def test_python_style(self):
        from Merlin.Testing import Flag
        from Merlin.Testing.Call import AllOpsClass
        x = AllOpsClass(7)
        y = AllOpsClass(2)

        # http://www.python.org/doc/ref/numeric-types.html

        z = AllOpsClass.__add__(x, y)
        self.AreValueFlagEqual(z, 9, 200)
        z = AllOpsClass.__sub__(x, y)
        self.AreValueFlagEqual(z, 5, 210)
        z = AllOpsClass.__mul__(x, y)
        self.AreValueFlagEqual(z, 14, 220)
        z = AllOpsClass.__div__(x, y)
        self.AreValueFlagEqual(z, 3, 230)
        z = AllOpsClass.__mod__(x, y)
        self.AreValueFlagEqual(z, 1, 240)
        z = AllOpsClass.__xor__(x, y)
        self.AreValueFlagEqual(z, 5, 250)
        z = AllOpsClass.__and__(x, y)
        self.AreValueFlagEqual(z, 2, 260)
        z = AllOpsClass.__or__(x, y)
        self.AreValueFlagEqual(z, 7, 270)
        z = AllOpsClass.__lshift__(x, 2)
        self.AreValueFlagEqual(z, 7 << 2, 280)
        z = AllOpsClass.__rshift__(x, 2)
        self.AreValueFlagEqual(z, 7 >> 2, 290)

        z = AllOpsClass.__radd__(y, x)
        self.AreValueFlagEqual(z, 9, 200)
        z = AllOpsClass.__rsub__(y, x)
        self.AreValueFlagEqual(z, 5, 210)
        z = AllOpsClass.__rmul__(y, x)
        self.AreValueFlagEqual(z, 14, 220)
        z = AllOpsClass.__rdiv__(y, x)
        self.AreValueFlagEqual(z, 3, 230)
        z = AllOpsClass.__rmod__(y, x)
        self.AreValueFlagEqual(z, 1, 240)
        z = AllOpsClass.__rxor__(y, x)
        self.AreValueFlagEqual(z, 5, 250)
        z = AllOpsClass.__rand__(y, x)
        self.AreValueFlagEqual(z, 2, 260)
        z = AllOpsClass.__ror__(y, x)
        self.AreValueFlagEqual(z, 7, 270)

        # bad msg? 'type'
        self.assertRaisesMessage(
            AttributeError, "'type' object has no attribute '__rlshift__'",
            lambda: AllOpsClass.__rlshift__(2, x))
        self.assertRaisesMessage(
            AttributeError, "'type' object has no attribute '__rrshift__'",
            lambda: AllOpsClass.__rrshift__(2, x))

        z = AllOpsClass.__iadd__(x, y)
        self.AreValueFlagEqual(z, 9, 450)
        z = AllOpsClass.__isub__(x, y)
        self.AreValueFlagEqual(z, 5, 390)
        z = AllOpsClass.__imul__(x, y)
        self.AreValueFlagEqual(z, 14, 380)
        z = AllOpsClass.__idiv__(x, y)
        self.AreValueFlagEqual(z, 3, 480)
        z = AllOpsClass.__imod__(x, y)
        self.AreValueFlagEqual(z, 1, 440)
        z = AllOpsClass.__ixor__(x, y)
        self.AreValueFlagEqual(z, 5, 400)
        z = AllOpsClass.__iand__(x, y)
        self.AreValueFlagEqual(z, 2, 460)
        z = AllOpsClass.__ior__(x, y)
        self.AreValueFlagEqual(z, 7, 470)
        z = AllOpsClass.__ilshift__(x, 2)
        self.AreValueFlagEqual(z, 7 << 2, 410)
        z = AllOpsClass.__irshift__(x, 2)
        self.AreValueFlagEqual(z, 7 >> 2, 420)

        z = AllOpsClass.__neg__(x)
        self.AreValueFlagEqual(z, -7, 170)
        z = AllOpsClass.__pos__(x)
        self.AreValueFlagEqual(z, 7, 180)
        z = AllOpsClass.__invert__(x)
        self.AreValueFlagEqual(z, ~7, 140)

        # http://www.python.org/doc/ref/customization.html

        self.assertEqual(AllOpsClass.__lt__(x, y), False)
        Flag.Check(320)
        self.assertEqual(AllOpsClass.__le__(x, y), False)
        Flag.Check(350)
        self.assertEqual(AllOpsClass.__eq__(x, y), False)
        Flag.Check(300)
        self.assertEqual(AllOpsClass.__ne__(x, y), True)
        Flag.Check(330)
        self.assertEqual(AllOpsClass.__gt__(x, y), True)
        Flag.Check(310)
        self.assertEqual(AllOpsClass.__ge__(x, y), True)
        Flag.Check(340)
Ejemplo n.º 7
0
    def test_all_ops(self):
        from Merlin.Testing import Flag
        from Merlin.Testing.Call import AllOpsClass
        x = AllOpsClass(5)
        y = AllOpsClass(6)
        z = AllOpsClass(-6)

        #if x: pass
        #else: self.assertUnreachable()
        #
        #Flag.Check(100)
        #
        #if z: self.assertUnreachable()
        #else: pass
        #
        #Flag.Check(100)
        #
        #if not x: self.assertUnreachable()
        #else: pass
        #
        #Flag.Check(100)
        #
        #if not z: pass
        #else: self.assertUnreachable()
        #
        #Flag.Check(100)

        # ! is not supported in python

        self.AreValueFlagEqual(~x, ~5, 140)

        # ++/-- not supported

        self.AreValueFlagEqual(+x, 5, 180)
        self.AreValueFlagEqual(-x, -5, 170)

        self.AreValueFlagEqual(+z, -6, 180)
        self.AreValueFlagEqual(-z, 6, 170)

        self.AreValueFlagEqual(x + y, 11, 200)
        self.AreValueFlagEqual(x - y, -1, 210)
        self.AreValueFlagEqual(x * y, 30, 220)
        self.AreValueFlagEqual(x / y, 0, 230)
        self.AreValueFlagEqual(x % y, 5, 240)
        self.AreValueFlagEqual(x ^ y, 5 ^ 6, 250)
        self.AreValueFlagEqual(x & y, 5 & 6, 260)
        self.AreValueFlagEqual(x | y, 5 | 6, 270)

        self.AreValueFlagEqual(x << 2, 5 << 2, 280)
        self.AreValueFlagEqual(x >> 1, 5 >> 1, 290)

        self.assertEqual(x == y, False)
        Flag.Check(300)
        self.assertEqual(x > y, False)
        Flag.Check(310)
        self.assertEqual(x < y, True)
        Flag.Check(320)
        self.assertEqual(x != y, True)
        Flag.Check(330)
        self.assertEqual(x >= y, False)
        Flag.Check(340)
        self.assertEqual(x <= y, True)
        Flag.Check(350)

        self.assertEqual(z == z, True)
        Flag.Check(300)
        self.assertEqual(z > z, False)
        Flag.Check(310)
        self.assertEqual(z < z, False)
        Flag.Check(320)
        self.assertEqual(z != z, False)
        Flag.Check(330)
        self.assertEqual(z >= z, True)
        Flag.Check(340)
        self.assertEqual(z <= z, True)
        Flag.Check(350)

        # the sequence below need be fixed
        x *= y
        self.AreValueFlagEqual(x, 30, 380)
        x /= y
        self.AreValueFlagEqual(x, 5, 480)

        x -= y
        self.AreValueFlagEqual(x, -1, 390)
        x += y
        self.AreValueFlagEqual(x, 5, 450)

        x <<= 2
        self.AreValueFlagEqual(x, 5 << 2, 410)
        x >>= 2
        self.AreValueFlagEqual(x, 5, 420)

        x ^= y
        self.AreValueFlagEqual(x, 3, 400)
        x |= y
        self.AreValueFlagEqual(x, 7, 470)
        x &= y
        self.AreValueFlagEqual(x, 6, 460)

        y = AllOpsClass(4)
        x %= y
        self.AreValueFlagEqual(x, 2, 440)