2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
$KCODE = 'none'
|
|
|
|
|
|
|
|
class TestBignum < Test::Unit::TestCase
|
|
|
|
def fact(n)
|
|
|
|
return 1 if n == 0
|
|
|
|
f = 1
|
|
|
|
while n>0
|
|
|
|
f *= n
|
|
|
|
n -= 1
|
|
|
|
end
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bignum
|
|
|
|
$x = fact(40)
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal($x, $x)
|
|
|
|
assert_equal($x, fact(40))
|
2003-09-04 12:18:59 -04:00
|
|
|
assert($x < $x+2)
|
|
|
|
assert($x > $x-2)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(815915283247897734345611269596115894272000000000, $x)
|
|
|
|
assert_not_equal(815915283247897734345611269596115894272000000001, $x)
|
|
|
|
assert_equal(815915283247897734345611269596115894272000000001, $x+1)
|
|
|
|
assert_equal(335367096786357081410764800000, $x/fact(20))
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = -$x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(-815915283247897734345611269596115894272000000000, $x)
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal(2-(2**32), -(2**32-2))
|
|
|
|
assert_equal(2**32 - 5, (2**32-3)-2)
|
2003-09-04 12:18:59 -04:00
|
|
|
|
|
|
|
for i in 1000..1014
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(2 ** i, 1 << i)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
|
|
|
n1 = 1 << 1000
|
2003-09-04 12:18:59 -04:00
|
|
|
for i in 1000..1014
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(n1, 1 << i)
|
2003-09-04 12:18:59 -04:00
|
|
|
n1 *= 2
|
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
n2=n1
|
|
|
|
for i in 1..10
|
|
|
|
n1 = n1 / 2
|
|
|
|
n2 = n2 >> 1
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(n1, n2)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
for i in 4000..4096
|
|
|
|
n1 = 1 << i;
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(n1-1, (n1**2-1) / (n1+1))
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_calc
|
|
|
|
b = 10**80
|
|
|
|
a = b * 9 + 7
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal(7, a.modulo(b))
|
|
|
|
assert_equal(-b + 7, a.modulo(-b))
|
|
|
|
assert_equal(b + -7, (-a).modulo(b))
|
|
|
|
assert_equal(-7, (-a).modulo(-b))
|
|
|
|
assert_equal(7, a.remainder(b))
|
|
|
|
assert_equal(7, a.remainder(-b))
|
|
|
|
assert_equal(-7, (-a).remainder(b))
|
|
|
|
assert_equal(-7, (-a).remainder(-b))
|
2003-09-05 11:15:43 -04:00
|
|
|
|
|
|
|
assert_equal(10000000000000000000100000000000000000000, 10**40+10**20)
|
|
|
|
assert_equal(100000000000000000000, 10**40/10**20)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
a = 677330545177305025495135714080
|
|
|
|
b = 14269972710765292560
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(0, a % b)
|
|
|
|
assert_equal(0, -a % b)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift
|
|
|
|
def shift_test(a)
|
|
|
|
b = a / (2 ** 32)
|
|
|
|
c = a >> 32
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal(b, c)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
b = a * (2 ** 32)
|
|
|
|
c = a << 32
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal(b, c)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
shift_test(-4518325415524767873)
|
|
|
|
shift_test(-0xfffffffffffffffff)
|
|
|
|
end
|
|
|
|
end
|