コード例 #1
0
ファイル: test_ironmath.py プロジェクト: dearman/iron-python
def test_bigint():
    AreEqual(BigInteger.Add(1L,99999999999999999999999999999999999999999999999999999999999L) ,BigInteger.Subtract(100000000000000000000000000000000000000000000000000000000001L,1L))
    AreEqual(BigInteger.Multiply(400L,500L) , BigInteger.Divide(1000000L,5L))
    AreEqual(BigInteger.Multiply(400L,8L) , BigInteger.LeftShift(400L,3L))
    AreEqual(BigInteger.Divide(400L,8L) , BigInteger.RightShift(400L,3L))
    AreEqual(BigInteger.RightShift(BigInteger.LeftShift(400L,100L),100L) , 400L)
    AreEqual(BigInteger.RightShift(BigInteger.LeftShift(-12345678987654321L,100L),100L) , -12345678987654321L)
    if is_net40:
        AssertError(ValueError, BigInteger.RightShift, 400L, -100L)
        AssertError(ValueError, BigInteger.LeftShift, 400L, -100L)
        AssertError(ValueError, BigInteger.RightShift, -12345678987654321L, -100L)
        AssertError(ValueError, BigInteger.LeftShift, -12345678987654321L, -100L)
    else:
        AreEqual(BigInteger.LeftShift(BigInteger.RightShift(400L,-100L),-100L) , 400L)
        AreEqual(BigInteger.LeftShift(BigInteger.RightShift(-12345678987654321L,-100L),-100L) , -12345678987654321L)
    AreEqual(BigInteger(-123456781234567812345678123456781234567812345678123456781234567812345678L).OnesComplement().OnesComplement() , -123456781234567812345678123456781234567812345678123456781234567812345678L)
    AreEqual(BigInteger(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678L).OnesComplement() , -(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678L + 1L ))
    Assert(BigInteger.Xor(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678L,BigInteger(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678L).OnesComplement()) , -1L)
    AreEqual(BigInteger.BitwiseAnd(0xff00ff00,BigInteger.BitwiseOr(0x00ff00ff,0xaabbaabb)) , BigInteger(0xaa00aa00))
    AreEqual(BigInteger.Mod(BigInteger(-9999999999999999999999999999999999999999),1000000000000000000) , -BigInteger.Mod(9999999999999999999999999999999999999999,BigInteger(-1000000000000000000)))
    
    AreEqual(BigInteger.ToInt64(0x7fffffffffffffff) , 9223372036854775807L)
    AssertError(OverflowError, BigInteger.ToInt64, 0x8000000000000000)
    
    AreEqual(BigInteger(-0).ToBoolean(p) , False )
    AreEqual(BigInteger(-1212321.3213).ToBoolean(p) , True )
    AreEqual(BigInteger(1212321384892342394723947L).ToBoolean(p) , True )
    
    AreEqual(BigInteger(0L).ToChar(p) , Char.MinValue)
    AreEqual(BigInteger(65L).ToChar(p) , IConvertible.ToChar('A', p))
    AreEqual(BigInteger(0xffff).ToChar(p) , Char.MaxValue)
    AssertError(OverflowError, BigInteger(-1).ToChar, p)
    
    AreEqual(BigInteger(100).ToDouble(p) , 100.0)
    AreEqual(BigInteger(BigInteger(100).ToDouble(p)).ToSingle(p) , BigInteger(100.1213123).ToFloat())
    
    Assert(BigInteger(100) != 100.32)
    AreEqual(BigInteger(100) , 100.0)
    
    Assert( 100.32 != BigInteger(100))
    AreEqual(100.0 , BigInteger(100) )
コード例 #2
0
    def test_bigint(self):
        from System import Char, IConvertible
        from System.Numerics import BigInteger, Complex
        self.assertEqual(BigInteger.Add(long(1),99999999999999999999999999999999999999999999999999999999999) ,BigInteger.Subtract(100000000000000000000000000000000000000000000000000000000001,long(1)))
        self.assertEqual(BigInteger.Multiply(long(400),long(500)) , BigInteger.Divide(long(1000000),long(5)))
        self.assertEqual(BigInteger.Multiply(long(400),long(8)) , BigInteger.LeftShift(long(400),long(3)))
        self.assertEqual(BigInteger.Divide(long(400),long(8)) , BigInteger.RightShift(long(400),long(3)))
        self.assertEqual(BigInteger.RightShift(BigInteger.LeftShift(long(400),long(100)),long(100)) , long(400))
        self.assertEqual(BigInteger.RightShift(BigInteger.LeftShift(-12345678987654321,long(100)),long(100)) , -12345678987654321)
        self.assertRaises(ValueError, BigInteger.RightShift, long(400), -long(100))
        self.assertRaises(ValueError, BigInteger.LeftShift, long(400), -long(100))
        self.assertRaises(ValueError, BigInteger.RightShift, -12345678987654321, -long(100))
        self.assertRaises(ValueError, BigInteger.LeftShift, -12345678987654321, -long(100))
        self.assertEqual(BigInteger(-123456781234567812345678123456781234567812345678123456781234567812345678).OnesComplement().OnesComplement() , -123456781234567812345678123456781234567812345678123456781234567812345678)
        self.assertEqual(BigInteger(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678).OnesComplement() , -(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678 + long(1) ))
        self.assertTrue(BigInteger.Xor(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678,BigInteger(-1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678).OnesComplement()) , -long(1))
        self.assertEqual(BigInteger.BitwiseAnd(0xff00ff00,BigInteger.BitwiseOr(0x00ff00ff,0xaabbaabb)) , BigInteger(0xaa00aa00))
        self.assertEqual(BigInteger.Mod(BigInteger(-9999999999999999999999999999999999999999),1000000000000000000) , -BigInteger.Mod(9999999999999999999999999999999999999999,BigInteger(-1000000000000000000)))

        self.assertEqual(BigInteger.ToInt64(0x7fffffffffffffff) , 9223372036854775807)
        self.assertRaises(OverflowError, BigInteger.ToInt64, 0x8000000000000000)

        self.assertEqual(BigInteger(-0).ToBoolean(self.p) , False )
        self.assertEqual(BigInteger(-1212321.3213).ToBoolean(self.p) , True )
        self.assertEqual(BigInteger(1212321384892342394723947).ToBoolean(self.p) , True )

        self.assertEqual(BigInteger(long(0)).ToChar(self.p) , Char.MinValue)
        self.assertEqual(BigInteger(long(65)).ToChar(self.p) , IConvertible.ToChar('A', self.p))
        self.assertEqual(BigInteger(0xffff).ToChar(self.p) , Char.MaxValue)
        self.assertRaises(OverflowError, BigInteger(-1).ToChar, self.p)

        self.assertEqual(BigInteger(100).ToDouble(self.p) , 100.0)
        self.assertEqual(BigInteger(BigInteger(100).ToDouble(self.p)).ToSingle(self.p) , BigInteger(100.1213123).ToFloat())

        self.assertTrue(BigInteger(100) != 100.32)
        self.assertEqual(BigInteger(100) , 100.0)

        self.assertTrue( 100.32 != BigInteger(100))
        self.assertEqual(100.0 , BigInteger(100) )