2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2013-07-07 10:01:40 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require "-test-/bignum"
|
|
|
|
|
2018-03-13 02:29:02 -04:00
|
|
|
class Test_Bignum < Test::Unit::TestCase
|
2013-07-07 10:01:40 -04:00
|
|
|
class TestMul < Test::Unit::TestCase
|
|
|
|
|
2021-01-08 05:07:16 -05:00
|
|
|
SIZEOF_BDIGIT = Bug::Bignum::SIZEOF_BDIGIT
|
|
|
|
BITSPERDIG = Bug::Bignum::BITSPERDIG
|
2013-07-07 11:21:26 -04:00
|
|
|
BDIGMAX = (1 << BITSPERDIG) - 1
|
2013-07-07 10:01:40 -04:00
|
|
|
|
|
|
|
def test_mul_normal
|
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << BITSPERDIG) | 1
|
|
|
|
z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(z, Bug::Bignum.big_mul_normal(x, y))
|
2013-07-07 10:01:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_normal_zero_in_x
|
|
|
|
x = (1 << (2*BITSPERDIG)) | 1
|
|
|
|
y = (1 << BITSPERDIG) | 1
|
|
|
|
z = (1 << (BITSPERDIG*3)) | (1 << (BITSPERDIG*2)) | (1 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(z, Bug::Bignum.big_mul_normal(x, y))
|
2013-07-07 10:01:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_normal_zero_in_y
|
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << (2*BITSPERDIG)) | 1
|
|
|
|
z = (1 << (BITSPERDIG*3)) | (1 << (BITSPERDIG*2)) | (1 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(z, Bug::Bignum.big_mul_normal(x, y))
|
2013-07-07 10:01:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_normal_max_max
|
|
|
|
x = (1 << (2*BITSPERDIG)) - 1
|
|
|
|
y = (1 << (2*BITSPERDIG)) - 1
|
|
|
|
z = (1 << (4*BITSPERDIG)) - (1 << (2*BITSPERDIG+1)) + 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(z, Bug::Bignum.big_mul_normal(x, y))
|
2013-07-07 10:01:40 -04:00
|
|
|
end
|
|
|
|
|
2013-07-08 09:05:57 -04:00
|
|
|
def test_sq_fast
|
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
z = (1 << 2*BITSPERDIG) | (2 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(z, Bug::Bignum.big_sq_fast(x))
|
2013-07-08 09:05:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sq_fast_max2
|
|
|
|
x = (BDIGMAX << BITSPERDIG) | BDIGMAX
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, x), Bug::Bignum.big_sq_fast(x))
|
2013-07-08 09:05:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sq_fast_zero_in_middle
|
|
|
|
x = (BDIGMAX << 2*BITSPERDIG) | BDIGMAX
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, x), Bug::Bignum.big_sq_fast(x))
|
2013-07-08 09:05:57 -04:00
|
|
|
end
|
|
|
|
|
2013-07-07 10:01:40 -04:00
|
|
|
def test_mul_balance
|
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << BITSPERDIG) | 1
|
|
|
|
z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(z, Bug::Bignum.big_mul_balance(x, y))
|
2013-07-07 10:01:40 -04:00
|
|
|
end
|
|
|
|
|
2013-07-08 07:56:55 -04:00
|
|
|
def test_mul_balance_2x16
|
2021-01-08 05:07:16 -05:00
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << BITSPERDIG*16) | 1
|
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_balance(x, y))
|
2013-07-08 07:56:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_balance_2x17
|
2021-01-08 05:07:16 -05:00
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << BITSPERDIG*17) | 1
|
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_balance(x, y))
|
2013-07-08 07:56:55 -04:00
|
|
|
end
|
|
|
|
|
2013-07-07 10:01:40 -04:00
|
|
|
def test_mul_karatsuba
|
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << BITSPERDIG) | 1
|
|
|
|
z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(z, Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 10:01:40 -04:00
|
|
|
end
|
|
|
|
|
2013-07-07 11:21:26 -04:00
|
|
|
def test_mul_karatsuba_odd_y
|
|
|
|
x = (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << (2*BITSPERDIG)) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 11:21:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_karatsuba_odd_xy
|
|
|
|
x = (1 << (2*BITSPERDIG)) | 1
|
|
|
|
y = (1 << (2*BITSPERDIG)) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 11:21:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_karatsuba_x1_gt_x0
|
|
|
|
x = (2 << BITSPERDIG) | 1
|
|
|
|
y = (1 << BITSPERDIG) | 2
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 11:21:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_karatsuba_y1_gt_y0
|
|
|
|
x = (1 << BITSPERDIG) | 2
|
|
|
|
y = (2 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 11:21:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_karatsuba_x1_gt_x0_and_y1_gt_y0
|
|
|
|
x = (2 << BITSPERDIG) | 1
|
|
|
|
y = (2 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 11:21:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_karatsuba_carry2
|
|
|
|
x = (1 << BITSPERDIG) | BDIGMAX
|
|
|
|
y = (1 << BITSPERDIG) | BDIGMAX
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 11:21:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul_karatsuba_borrow
|
|
|
|
x = (BDIGMAX << BITSPERDIG) | 1
|
|
|
|
y = (BDIGMAX << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_karatsuba(x, y))
|
2013-07-07 11:21:26 -04:00
|
|
|
end
|
|
|
|
|
2013-07-08 09:05:57 -04:00
|
|
|
def test_mul_toom3
|
|
|
|
x = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_toom3(x, y))
|
2013-07-08 09:05:57 -04:00
|
|
|
end
|
|
|
|
|
2013-09-03 07:20:17 -04:00
|
|
|
def test_mul_gmp
|
|
|
|
x = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
|
|
|
|
y = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
|
2021-01-08 05:07:16 -05:00
|
|
|
assert_equal(Bug::Bignum.big_mul_normal(x, y), Bug::Bignum.big_mul_gmp(x, y))
|
2013-09-03 07:20:17 -04:00
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
2013-07-07 10:01:40 -04:00
|
|
|
end
|
|
|
|
end
|