PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/-ext-/bignum/test_mul.rb

http://github.com/ruby/ruby
Ruby | 138 lines | 114 code | 23 blank | 1 comment | 0 complexity | 1497fcc62fe4fac91a27bc10b67b6c47 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require "-test-/bignum"
  4. class Test_Bignum < Test::Unit::TestCase
  5. class TestMul < Test::Unit::TestCase
  6. SIZEOF_BDIGIT = Integer::SIZEOF_BDIGIT
  7. BITSPERDIG = Integer::BITSPERDIG
  8. BDIGMAX = (1 << BITSPERDIG) - 1
  9. def test_mul_normal
  10. x = (1 << BITSPERDIG) | 1
  11. y = (1 << BITSPERDIG) | 1
  12. z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
  13. assert_equal(z, x.big_mul_normal(y))
  14. end
  15. def test_mul_normal_zero_in_x
  16. x = (1 << (2*BITSPERDIG)) | 1
  17. y = (1 << BITSPERDIG) | 1
  18. z = (1 << (BITSPERDIG*3)) | (1 << (BITSPERDIG*2)) | (1 << BITSPERDIG) | 1
  19. assert_equal(z, x.big_mul_normal(y))
  20. end
  21. def test_mul_normal_zero_in_y
  22. x = (1 << BITSPERDIG) | 1
  23. y = (1 << (2*BITSPERDIG)) | 1
  24. z = (1 << (BITSPERDIG*3)) | (1 << (BITSPERDIG*2)) | (1 << BITSPERDIG) | 1
  25. assert_equal(z, x.big_mul_normal(y))
  26. end
  27. def test_mul_normal_max_max
  28. x = (1 << (2*BITSPERDIG)) - 1
  29. y = (1 << (2*BITSPERDIG)) - 1
  30. z = (1 << (4*BITSPERDIG)) - (1 << (2*BITSPERDIG+1)) + 1
  31. assert_equal(z, x.big_mul_normal(y))
  32. end
  33. def test_sq_fast
  34. x = (1 << BITSPERDIG) | 1
  35. z = (1 << 2*BITSPERDIG) | (2 << BITSPERDIG) | 1
  36. assert_equal(z, x.big_sq_fast)
  37. end
  38. def test_sq_fast_max2
  39. x = (BDIGMAX << BITSPERDIG) | BDIGMAX
  40. assert_equal(x.big_mul_normal(x), x.big_sq_fast)
  41. end
  42. def test_sq_fast_zero_in_middle
  43. x = (BDIGMAX << 2*BITSPERDIG) | BDIGMAX
  44. assert_equal(x.big_mul_normal(x), x.big_sq_fast)
  45. end
  46. def test_mul_balance
  47. x = (1 << BITSPERDIG) | 1
  48. y = (1 << BITSPERDIG) | 1
  49. z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
  50. assert_equal(z, x.big_mul_balance(y))
  51. end
  52. def test_mul_balance_2x16
  53. x = (1 << Integer::BITSPERDIG) | 1
  54. y = (1 << Integer::BITSPERDIG*16) | 1
  55. assert_equal(x.big_mul_normal(y), x.big_mul_balance(y))
  56. end
  57. def test_mul_balance_2x17
  58. x = (1 << Integer::BITSPERDIG) | 1
  59. y = (1 << Integer::BITSPERDIG*17) | 1
  60. assert_equal(x.big_mul_normal(y), x.big_mul_balance(y))
  61. end
  62. def test_mul_karatsuba
  63. x = (1 << BITSPERDIG) | 1
  64. y = (1 << BITSPERDIG) | 1
  65. z = (1 << (BITSPERDIG*2)) | (2 << BITSPERDIG) | 1
  66. assert_equal(z, x.big_mul_karatsuba(y))
  67. end
  68. def test_mul_karatsuba_odd_y
  69. x = (1 << BITSPERDIG) | 1
  70. y = (1 << (2*BITSPERDIG)) | 1
  71. assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
  72. end
  73. def test_mul_karatsuba_odd_xy
  74. x = (1 << (2*BITSPERDIG)) | 1
  75. y = (1 << (2*BITSPERDIG)) | 1
  76. assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
  77. end
  78. def test_mul_karatsuba_x1_gt_x0
  79. x = (2 << BITSPERDIG) | 1
  80. y = (1 << BITSPERDIG) | 2
  81. assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
  82. end
  83. def test_mul_karatsuba_y1_gt_y0
  84. x = (1 << BITSPERDIG) | 2
  85. y = (2 << BITSPERDIG) | 1
  86. assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
  87. end
  88. def test_mul_karatsuba_x1_gt_x0_and_y1_gt_y0
  89. x = (2 << BITSPERDIG) | 1
  90. y = (2 << BITSPERDIG) | 1
  91. assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
  92. end
  93. def test_mul_karatsuba_carry2
  94. x = (1 << BITSPERDIG) | BDIGMAX
  95. y = (1 << BITSPERDIG) | BDIGMAX
  96. assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
  97. end
  98. def test_mul_karatsuba_borrow
  99. x = (BDIGMAX << BITSPERDIG) | 1
  100. y = (BDIGMAX << BITSPERDIG) | 1
  101. assert_equal(x.big_mul_normal(y), x.big_mul_karatsuba(y))
  102. end
  103. def test_mul_toom3
  104. x = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
  105. y = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
  106. assert_equal(x.big_mul_normal(y), x.big_mul_toom3(y))
  107. end
  108. def test_mul_gmp
  109. x = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
  110. y = (1 << 2*BITSPERDIG) | (1 << BITSPERDIG) | 1
  111. assert_equal(x.big_mul_normal(y), x.big_mul_gmp(y))
  112. rescue NotImplementedError
  113. end
  114. end
  115. end