1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/-ext-/bignum/test_mul.rb
nobu dd3851d278 Rename test classes to allow stable test count when running test-all -j
[Fix GH-1763]

From: MSP-Greg <MSP-Greg@users.noreply.github.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62738 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-13 06:29:02 +00:00

138 lines
4 KiB
Ruby

# frozen_string_literal: false
require 'test/unit'
require "-test-/bignum"
class Test_Bignum < Test::Unit::TestCase
class TestMul < Test::Unit::TestCase
SIZEOF_BDIGIT = Integer::SIZEOF_BDIGIT
BITSPERDIG = Integer::BITSPERDIG
BDIGMAX = (1 << BITSPERDIG) - 1
def test_mul_normal
x = (1 << BITSPERDIG) | 1
y = (1 << BITSPERDIG) | 1
z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
assert_equal(z, x.big_mul_normal(y))
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
assert_equal(z, x.big_mul_normal(y))
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
assert_equal(z, x.big_mul_normal(y))
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
assert_equal(z, x.big_mul_normal(y))
end
def test_sq_fast
x = (1 << BITSPERDIG) | 1
z = (1 << 2*BITSPERDIG) | (2 << BITSPERDIG) | 1
assert_equal(z, x.big_sq_fast)
end
def test_sq_fast_max2
x = (BDIGMAX << BITSPERDIG) | BDIGMAX
assert_equal(x.big_mul_normal(x), x.big_sq_fast)
end
def test_sq_fast_zero_in_middle
x = (BDIGMAX << 2*BITSPERDIG) | BDIGMAX
assert_equal(x.big_mul_normal(x), x.big_sq_fast)
end
def test_mul_balance
x = (1 << BITSPERDIG) | 1
y = (1 << BITSPERDIG) | 1
z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
assert_equal(z, x.big_mul_balance(y))
end
def test_mul_balance_2x16
x = (1 << Integer::BITSPERDIG) | 1
y = (1 << Integer::BITSPERDIG*16) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_balance(y))
end
def test_mul_balance_2x17
x = (1 << Integer::BITSPERDIG) | 1
y = (1 << Integer::BITSPERDIG*17) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_balance(y))
end
def test_mul_karatsuba
x = (1 << BITSPERDIG) | 1
y = (1 << BITSPERDIG) | 1
z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
assert_equal(z, x.big_mul_karatsuba(y))
end
def test_mul_karatsuba_odd_y
x = (1 << BITSPERDIG) | 1
y = (1 << (2*BITSPERDIG)) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
end
def test_mul_karatsuba_odd_xy
x = (1 << (2*BITSPERDIG)) | 1
y = (1 << (2*BITSPERDIG)) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
end
def test_mul_karatsuba_x1_gt_x0
x = (2 << BITSPERDIG) | 1
y = (1 << BITSPERDIG) | 2
assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
end
def test_mul_karatsuba_y1_gt_y0
x = (1 << BITSPERDIG) | 2
y = (2 << BITSPERDIG) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
end
def test_mul_karatsuba_x1_gt_x0_and_y1_gt_y0
x = (2 << BITSPERDIG) | 1
y = (2 << BITSPERDIG) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
end
def test_mul_karatsuba_carry2
x = (1 << BITSPERDIG) | BDIGMAX
y = (1 << BITSPERDIG) | BDIGMAX
assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
end
def test_mul_karatsuba_borrow
x = (BDIGMAX << BITSPERDIG) | 1
y = (BDIGMAX << BITSPERDIG) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
end
def test_mul_toom3
x = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
y = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_toom3(y))
end
def test_mul_gmp
x = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
y = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
assert_equal(x.big_mul_normal(y), x.big_mul_gmp(y))
rescue NotImplementedError
end
end
end