gmp 0.4.7 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/CHANGELOG +31 -0
  2. data/README.rdoc +16 -8
  3. data/benchmark/multiply +1 -1
  4. data/benchmark/multiply.gc +57 -0
  5. data/benchmark/pi +126 -0
  6. data/benchmark/srb.sh +21 -0
  7. data/ext/extconf.rb +3 -0
  8. data/ext/gmp.c +16 -7
  9. data/ext/gmpbench_timing.c +1 -1
  10. data/ext/gmpf.c +445 -104
  11. data/ext/gmpq.c +25 -17
  12. data/ext/gmpz.c +232 -120
  13. data/ext/mprnd.c +23 -5
  14. data/ext/ruby_gmp.h +75 -9
  15. data/lib/gmp.rb +9 -0
  16. data/manual.pdf +0 -0
  17. data/manual.tex +494 -60
  18. data/test/README +1 -0
  19. data/test/mpfr_tcbrt.rb +95 -0
  20. data/test/mpfr_tisnan.rb +70 -0
  21. data/test/mpfr_trec_sqrt.rb +62 -0
  22. data/test/mpfr_tsqrt.rb +142 -6
  23. data/test/tc_cmp.rb +4 -4
  24. data/test/tc_constants.rb +10 -0
  25. data/test/tc_division.rb +13 -2
  26. data/test/tc_f_arithmetics_coersion.rb +2 -2
  27. data/test/tc_f_precision.rb +4 -3
  28. data/test/tc_fib_fac_nextprime.rb +2 -2
  29. data/test/tc_floor_ceil_truncate.rb +2 -2
  30. data/test/tc_hashes.rb +0 -2
  31. data/test/tc_logical_roots.rb +1 -3
  32. data/test/tc_mpfr_constants.rb +11 -0
  33. data/test/tc_mpfr_functions.rb +22 -9
  34. data/test/tc_mpfr_random.rb +1 -3
  35. data/test/tc_mpfr_rounding.rb +10 -7
  36. data/test/tc_q.rb +1 -3
  37. data/test/tc_q_basic.rb +3 -5
  38. data/test/tc_random.rb +1 -3
  39. data/test/tc_sgn_neg_abs.rb +1 -3
  40. data/test/tc_swap.rb +1 -3
  41. data/test/tc_z.rb +3 -3
  42. data/test/tc_z_addmul.rb +92 -0
  43. data/test/tc_z_basic.rb +6 -8
  44. data/test/tc_z_exponentiation.rb +1 -3
  45. data/test/tc_z_gcd_lcm_invert.rb +1 -3
  46. data/test/tc_z_jac_leg_rem.rb +1 -3
  47. data/test/tc_z_logic.rb +2 -2
  48. data/test/tc_z_shifts_last_bits.rb +2 -2
  49. data/test/tc_z_to_d_to_i.rb +2 -2
  50. data/test/test_helper.rb +1 -1
  51. data/test/test_unit/assertions.rb +31 -0
  52. data/test/unit_tests.rb +33 -27
  53. metadata +31 -6
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require './test_helper'
2
2
 
3
3
  class TC_division < Test::Unit::TestCase
4
4
  def setup
@@ -15,12 +15,23 @@ class TC_division < Test::Unit::TestCase
15
15
  assert_equal(GMP::Q, (@a / 3 ).class, "GMP::Z / Fixnum should be GMP::Q.")
16
16
  assert_equal(GMP::Q, (@a / 2**32).class, "GMP::Z / Bignum should be GMP::Q.")
17
17
  assert_equal(GMP::Q, (@a / @c ).class, "GMP::Z / GMP::Z should be GMP::Q.")
18
+ begin
18
19
  assert_in_delta(0.7142857142, @a / @b, 1e-7, "GMP::Z./ should work.")
19
20
  assert_in_delta(1.4 , @b / @a, 1e-7, "GMP::Z./ should work.")
20
21
  assert_in_delta(1.6666666667, @a / 3, 1e-7, "GMP::Z./ should work.")
21
22
  assert_in_delta(0.6 , 3 / @a, 1e-7, "GMP::Z./ should work.")
22
23
  assert_in_delta(0.2 , @a / @c, 1e-7, "GMP::Z./ should work.")
23
24
  assert_in_delta(5.0 , @c / @a, 1e-7, "GMP::Z./ should work.")
25
+ rescue TypeError => e
26
+ if e.message == "GMP::Q can't be coerced into Float"
27
+ puts ""
28
+ puts "Suppressing error that should be fixed with a recent version of Test::Unit"
29
+ puts "installed."
30
+ else
31
+ puts "Accidentally rescued TypeError with message \"#{e.message}\", raising..."
32
+ raise e
33
+ end
34
+ end
24
35
  end
25
36
 
26
37
  def test_z_tdiv
@@ -106,4 +117,4 @@ class TC_division < Test::Unit::TestCase
106
117
  assert_raise(TypeError) { @a % GMP::Q(22,3) }
107
118
  assert_raise(TypeError) { @a % GMP::F(3.14) }
108
119
  end
109
- end
120
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require './test_helper'
2
2
 
3
3
  class TC_F_arithmetics_coersion < Test::Unit::TestCase
4
4
  def setup
@@ -68,4 +68,4 @@ class TC_F_arithmetics_coersion < Test::Unit::TestCase
68
68
  assert_in_delta( 0.6962305987, @a / 4.51, 1e-6)
69
69
  assert_in_delta( 1.4363057325, 4.51 / @a, 1e-6)
70
70
  end
71
- end
71
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require './test_helper'
2
2
 
3
3
  class TC_precision < Test::Unit::TestCase
4
4
  def setup
@@ -102,7 +102,8 @@ class TC_precision < Test::Unit::TestCase
102
102
  GMP::F.default_prec = 100
103
103
  assert_equal(128, GMP::F.default_prec, "GMP::F.default_prec should be assignable.")
104
104
  GMP::F.default_prec = 130
105
- assert_equal(160, GMP::F.default_prec, "GMP::F.default_prec should be assignable.")
105
+ adjusted = (130*1.0/GMP::GMP_BITS_PER_LIMB).ceil*GMP::GMP_BITS_PER_LIMB
106
+ assert_equal(adjusted, GMP::F.default_prec, "GMP::F.default_prec should be assignable.")
106
107
  GMP::F.default_prec = 1000
107
108
  assert_equal(1024, GMP::F.default_prec, "GMP::F.default_prec should be assignable.")
108
109
  assert_raise(RangeError) { GMP::F.default_prec = -64 }
@@ -110,4 +111,4 @@ class TC_precision < Test::Unit::TestCase
110
111
  end
111
112
  GMP::F.default_prec = @initial_default_prec
112
113
  end
113
- end
114
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require './test_helper'
2
2
 
3
3
  class TC_fib_fac_nextprime < Test::Unit::TestCase
4
4
  def setup
@@ -48,4 +48,4 @@ class TC_fib_fac_nextprime < Test::Unit::TestCase
48
48
  assert_equal(@z17, @z13.next_prime, "GMP::Z.nextprime should work.")
49
49
  assert_equal(@z19, @z17.nextprime, "GMP::Z.nextprime should work.")
50
50
  end
51
- end
51
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require './test_helper'
2
2
 
3
3
  class TC_floor_ceil_truncate < Test::Unit::TestCase
4
4
  def setup
@@ -18,4 +18,4 @@ class TC_floor_ceil_truncate < Test::Unit::TestCase
18
18
  assert_equal(@c.ceil, 7, "GMP::Q (integer) should ceil.")
19
19
  assert_equal(@c.trunc, 7, "GMP::Q (integer) should truncate.")
20
20
  end
21
- end
21
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_Hashes < Test::Unit::TestCase
4
2
  def setup
5
3
  end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_logical_roots < Test::Unit::TestCase
4
2
  def setup
5
3
  @a = GMP::Z.new(100)
@@ -45,4 +43,4 @@ class TC_logical_roots < Test::Unit::TestCase
45
43
  assert_equal(3, @b.root(3), "GMP::Z should root correctly.")
46
44
  assert_equal(4, @c.root(3), "GMP::Z should root correctly.")
47
45
  end
48
- end
46
+ end
@@ -0,0 +1,11 @@
1
+ class TC_MPFR_Constants < Test::Unit::TestCase
2
+ def test_mpfr_constants
3
+ assert_instance_of(String, GMP::MPFR_VERSION, "GMP::MPFR_VERSION should be a String")
4
+ assert_instance_of(Fixnum, GMP::MPFR_PREC_MIN, "GMP::MPFR_PREC_MIN should be a Fixnum")
5
+ assert_instance_of(Fixnum, GMP::MPFR_PREC_MAX, "GMP::MPFR_PREC_MAX should be a Fixnum")
6
+ assert_instance_of(GMP::Rnd, GMP::GMP_RNDN, "GMP::GMP_RNDN should be a GMP::Rnd")
7
+ assert_instance_of(GMP::Rnd, GMP::GMP_RNDZ, "GMP::GMP_RNDZ should be a GMP::Rnd")
8
+ assert_instance_of(GMP::Rnd, GMP::GMP_RNDU, "GMP::GMP_RNDU should be a GMP::Rnd")
9
+ assert_instance_of(GMP::Rnd, GMP::GMP_RNDD, "GMP::GMP_RNDD should be a GMP::Rnd")
10
+ end
11
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_MPFR_Functions < Test::Unit::TestCase
4
2
  def setup
5
3
  @a = GMP::F(1)
@@ -13,7 +11,17 @@ class TC_MPFR_Functions < Test::Unit::TestCase
13
11
  end
14
12
 
15
13
  def test_function_existence
14
+ assert_nothing_raised("GMP::F.nan? should be callable.") { @a.nan? }
15
+ assert_nothing_raised("GMP::F.infinite? should be callable.") { @a.infinite? }
16
+ assert_nothing_raised("GMP::F.finite? should be callable.") { @a.finite? }
17
+ assert_nothing_raised("GMP::F.number? should be callable.") { @a.number? }
18
+ if GMP::MPFR_VERSION >= "3.0.0"
19
+ assert_nothing_raised("GMP::F.regular? should be callable.") { @a.regular? }
20
+ end
21
+
16
22
  assert_nothing_raised("GMP::F.sqrt should be callable.") { @a.sqrt }
23
+ assert_nothing_raised("GMP::F.rec_sqrt should be callable.") { @a.rec_sqrt }
24
+ assert_nothing_raised("GMP::F.cbrt should be callable.") { @a.cbrt }
17
25
 
18
26
  assert_nothing_raised("GMP::F.log should be callable.") { @a.log }
19
27
  assert_nothing_raised("GMP::F.log2 should be callable.") { @a.log2 }
@@ -50,6 +58,9 @@ class TC_MPFR_Functions < Test::Unit::TestCase
50
58
  assert_nothing_raised("GMP::F.gamma should be callable.") { @a.gamma }
51
59
  assert_nothing_raised("GMP::F.lngamma should be callable.") { @a.lngamma }
52
60
  #assert_nothing_raised("GMP::F.lgamma should be callable.") { @a.lgamma }
61
+ if GMP::MPFR_VERSION >= "3.0.0"
62
+ assert_nothing_raised("GMP::F.digamma should be callable.") { @a.digamma }
63
+ end
53
64
  assert_nothing_raised("GMP::F.zeta should be callable.") { @a.zeta }
54
65
  assert_nothing_raised("GMP::F.erf should be callable.") { @a.erf }
55
66
  assert_nothing_raised("GMP::F.erfc should be callable.") { @a.erfc }
@@ -68,7 +79,8 @@ class TC_MPFR_Functions < Test::Unit::TestCase
68
79
  end
69
80
 
70
81
  def test_function_parameters
71
- functions = [:sqrt, :log, :log2, :log10, :exp, :exp2, :exp10,
82
+ functions = [:sqrt, :rec_sqrt, :cbrt,
83
+ :log, :log2, :log10, :exp, :exp2, :exp10,
72
84
  :cos, :sin, :tan, :sec, :csc, :cot, :acos, :asin, :atan,
73
85
  :cosh, :sinh, :tanh, :sech, :csch, :coth,
74
86
  :acosh, :asinh, :atanh,
@@ -76,13 +88,14 @@ class TC_MPFR_Functions < Test::Unit::TestCase
76
88
  :zeta, :erf, :erfc,
77
89
  :j0, :j1, :y0, :y1
78
90
  ]
91
+ functions += [:digamma] if GMP::MPFR_VERSION >= "3.0.0"
79
92
 
80
93
  functions.each do |f|
81
- assert_nothing_raised("GMP::F.#{f} can be called with 1 argument, the rounding_mode.") { @a.send(f, GMP::GMP_RNDN) }
82
- assert_nothing_raised("GMP::F.#{f} can be called with 1 argument, the rounding_mode.") { @a.send(f, GMP::GMP_RNDZ) }
83
- assert_nothing_raised("GMP::F.#{f} can be called with 2 args, rounding_mode, prec.") { @a.send(f, GMP::GMP_RNDN, 32) }
84
- assert_nothing_raised("GMP::F.#{f} can be called with 2 args, rounding_mode, prec.") { @a.send(f, GMP::GMP_RNDN, 128) }
85
- assert_nothing_raised("GMP::F.#{f} can be called with 2 args, rounding_mode, prec.") { @a.send(f, GMP::GMP_RNDN, 200) }
94
+ assert_nothing_raised("GMP::F.#{f} can be called w/ 1 arg: rounding_mode.") { @a.send(f, GMP::GMP_RNDN) }
95
+ assert_nothing_raised("GMP::F.#{f} can be called w/ 1 arg: rounding_mode.") { @a.send(f, GMP::GMP_RNDZ) }
96
+ assert_nothing_raised("GMP::F.#{f} can be called w/ 2 args: rounding_mode, prec.") { @a.send(f, GMP::GMP_RNDN, 32) }
97
+ assert_nothing_raised("GMP::F.#{f} can be called w/ 2 args: rounding_mode, prec.") { @a.send(f, GMP::GMP_RNDN, 128) }
98
+ assert_nothing_raised("GMP::F.#{f} can be called w/ 2 args: rounding_mode, prec.") { @a.send(f, GMP::GMP_RNDN, 200) }
86
99
  end
87
100
  end
88
- end
101
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_MPFR_Random < Test::Unit::TestCase
4
2
  def setup
5
3
  end
@@ -46,4 +44,4 @@ class TC_MPFR_Random < Test::Unit::TestCase
46
44
  assert_in_delta(GMP::F("0.23084385480845748"), @d.mpfr_urandomb, 1e-12, "GMP::RandState should be independent correctly.")
47
45
  assert_in_delta(GMP::F("0.23084385480845748"), @e.mpfr_urandomb, 1e-12, "GMP::RandState should be independent correctly.")
48
46
  end
49
- end
47
+ end
@@ -1,7 +1,10 @@
1
- require 'test_helper'
2
-
3
1
  class TC_MPFR_Rounding < Test::Unit::TestCase
4
2
  def setup
3
+ if GMP::MPFR_VERSION < "3.0.0"
4
+ @prefix = "GMP"
5
+ else
6
+ @prefix = "MPFR"
7
+ end
5
8
  end
6
9
 
7
10
  def test_rounding_mode
@@ -12,10 +15,10 @@ class TC_MPFR_Rounding < Test::Unit::TestCase
12
15
  end
13
16
 
14
17
  def test_rounding_name
15
- assert_equal("GMP_RNDN", GMP::GMP_RNDN.name, "GMP::Rnd.name should be correct.")
16
- assert_equal("GMP_RNDZ", GMP::GMP_RNDZ.name, "GMP::Rnd.name should be correct.")
17
- assert_equal("GMP_RNDU", GMP::GMP_RNDU.name, "GMP::Rnd.name should be correct.")
18
- assert_equal("GMP_RNDD", GMP::GMP_RNDD.name, "GMP::Rnd.name should be correct.")
18
+ assert_equal(@prefix+"_RNDN", GMP::GMP_RNDN.name, "GMP::Rnd.name should be correct.")
19
+ assert_equal(@prefix+"_RNDZ", GMP::GMP_RNDZ.name, "GMP::Rnd.name should be correct.")
20
+ assert_equal(@prefix+"_RNDU", GMP::GMP_RNDU.name, "GMP::Rnd.name should be correct.")
21
+ assert_equal(@prefix+"_RNDD", GMP::GMP_RNDD.name, "GMP::Rnd.name should be correct.")
19
22
  end
20
23
 
21
24
  def test_rounding_ieee754
@@ -24,4 +27,4 @@ class TC_MPFR_Rounding < Test::Unit::TestCase
24
27
  assert_equal("roundTowardPositive", GMP::GMP_RNDU.ieee754, "GMP::Rnd.ieee754 should be correct.")
25
28
  assert_equal("roundTowardNegative", GMP::GMP_RNDD.ieee754, "GMP::Rnd.ieee754 should be correct.")
26
29
  end
27
- end
30
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_Q < Test::Unit::TestCase
4
2
  def test_init_null
5
3
  assert_equal(GMP::Q.new(), 0, "GMP::Q.new() should initialize to 0")
@@ -36,4 +34,4 @@ class TC_Q < Test::Unit::TestCase
36
34
  assert_equal("0", GMP::Q(0,2000).to_s, "GMP::Q should to_s properly.")
37
35
  assert_equal("0", GMP::Q(0,-2000).to_s, "GMP::Q should to_s properly.")
38
36
  end
39
- end
37
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  # [Q op Q, Q op Z, Z op Q, Q op FixNum, Q op BigNum, FixNum op Q, BigNum op Q]
4
2
  class TC_Q_Basic < Test::Unit::TestCase
5
3
  def setup
@@ -14,8 +12,8 @@ class TC_Q_Basic < Test::Unit::TestCase
14
12
  assert_equal(@a + @c, GMP::Q(540, 11), "GMP::Q should add GMP::Z correctly")
15
13
  assert_equal(@c + @a, GMP::Q(540, 11), "GMP::Z should add GMP::Q correctly")
16
14
  assert_equal(@a + 2, GMP::Q(122, 11), "GMP::Z should add Fixnum correctly")
17
- assert_equal(@a + @d, GMP::Q(47244640356, 11), "GMP::Z should add Bignum correctly")
18
- assert_equal( 2 + @a, GMP::Q(122, 11), "Fixnum should add GMP::Q correctly")
15
+ assert_equal(GMP::Q(47244640356, 11), @a + @d, "GMP::Z should add Bignum correctly")
16
+ assert_equal(GMP::Q(122, 11), 2 + @a, "Fixnum should add GMP::Q correctly")
19
17
  assert_equal(@d + @a, GMP::Q(47244640356, 11), "Bignum should add GMP::Q correctly")
20
18
  end
21
19
 
@@ -38,4 +36,4 @@ class TC_Q_Basic < Test::Unit::TestCase
38
36
  assert_equal( 2 * @a, GMP::Q(200, 11), "Fixnum should multiply GMP::Q correctly")
39
37
  # assert_equal(@d * @a, GMP::Q(429496729600, 11),"Bignum should multiply GMP::Q correctly") # SEGFAULT
40
38
  end
41
- end
39
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_Random < Test::Unit::TestCase
4
2
  def setup
5
3
  end
@@ -70,4 +68,4 @@ class TC_Random < Test::Unit::TestCase
70
68
  assert_equal(1657, @d.urandomb(12), "GMP::RandState should be independent correctly.")
71
69
  assert_equal(1657, @e.urandomb(12), "GMP::RandState should be independent correctly.")
72
70
  end
73
- end
71
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_sgn_neg_abs < Test::Unit::TestCase
4
2
  def setup
5
3
  @a=GMP::Z.new(10)
@@ -44,4 +42,4 @@ class TC_sgn_neg_abs < Test::Unit::TestCase
44
42
  assert_equal([10, 0, 10], [@a, @b, @c], "(x : GMP::Z).abs! should be calculated correctly.")
45
43
  assert_equal([10, 0, 10], [@d, @e, @f], "(x : GMP::Q).abs! should be calculated correctly.")
46
44
  end
47
- end
45
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  class TC_swap < Test::Unit::TestCase
4
2
  def setup
5
3
  @a=GMP::Z.new(100)
@@ -16,4 +14,4 @@ class TC_swap < Test::Unit::TestCase
16
14
  assert_equal(@c, GMP::Q.new(200,17), "GMP::Z should swap with GMP::Z.")
17
15
  assert_equal(@d, GMP::Q.new(100,11), "GMP::Z should swap with GMP::Z.")
18
16
  end
19
- end
17
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require './test_helper'
2
2
 
3
3
  class TC_Z < Test::Unit::TestCase
4
4
  def test_init_null
@@ -19,7 +19,7 @@ class TC_Z < Test::Unit::TestCase
19
19
  end
20
20
 
21
21
  def test_init_bignum
22
- assert_equal(GMP::Z.new(2**32), 2**32, "GMP::Z.new(x : Bignum) should initialize to x")
22
+ assert_equal(2**32, GMP::Z.new(2**32), "GMP::Z.new(x : Bignum) should initialize to x")
23
23
  end
24
24
 
25
25
  def test_cmp_z
@@ -68,4 +68,4 @@ class TC_Z < Test::Unit::TestCase
68
68
  assert_equal("255", GMP::Z.new(255).to_s(:dec), "GMP::Z should to_s(:dec) correctly.")
69
69
  assert_equal("ff", GMP::Z.new(255).to_s(:hex), "GMP::Z should to_s(:hex) correctly.")
70
70
  end
71
- end
71
+ end
@@ -0,0 +1,92 @@
1
+ # Tested: [Z op (Z,Z), Z op (Z,Fixnum), Z op (Z,Bignum),
2
+ # Z op (Fixnum,Z), Z op (Fixnum,Fixnum), Z op (Fixnum,Bignum)
3
+ # Z op (Bignum,Z), Z op (Bignum,Bignum), Z op (Bignum,Fixnum)]
4
+ # Things are tested both ways because the implementation is asymetrical
5
+ class TC_Z_Addmul < Test::Unit::TestCase
6
+ def setup
7
+ @_64bit = 1_000_000_000_000.is_a? Fixnum
8
+ end
9
+
10
+ def test_z
11
+ five = GMP::Z(5)
12
+ seven = GMP::Z(7)
13
+ five.addmul!(seven, seven) # <= 5 + 49
14
+ assert_equal(54, five, "GMP::Z should addmul GMP::Z correctly")
15
+ five = GMP::Z(5)
16
+ twenty = GMP::Z(20)
17
+ neg2 = GMP::Z(-2)
18
+ five.addmul!(twenty, neg2) # <= 5 + -40
19
+ assert_equal(-35, five, "GMP::Z should addmul GMP::Z correctly")
20
+ m20 = GMP::Z(2)**20 - 1 # 1_048_575
21
+ m19 = GMP::Z(2)**19 - 1 # 524_287
22
+ neg_two = GMP::Z(-2)
23
+ m20.addmul!(m19, neg_two) # <= 1_048_575 + -1_048_574
24
+ assert_equal(1, m20, "GMP::Z should addmul GMP::Z correctly")
25
+
26
+ neg_m40 = -GMP::Z(2)**40 # -1_099_511_627_776
27
+ ten_million = 10_000_000
28
+ neg_million = -GMP::Z(1_000_000)
29
+ million = GMP::Z(1_000_000)
30
+ thirty_thou = GMP::Z( 30_000)
31
+ thirty = GMP::Z( 30)
32
+ neg_m40.addmul!(neg_million, neg_million) # <= -1_099_511_627_776 + 1_000_000_000_000
33
+ assert_equal(GMP::Z(-99_511_627_776), neg_m40, "GMP::Z should addmul GMP::Z correctly")
34
+ neg_m40.addmul!(ten_million, GMP::Z(9_000)) # <= - 99_511_627_776 + 90_000_000_000
35
+ assert_equal(GMP::Z(- 9_511_627_776), neg_m40, "GMP::Z should addmul GMP::Z correctly")
36
+ neg_m40.addmul!( million, GMP::Z(9_000)) # <= - 9_511_627_776 + 9_000_000_000
37
+ assert_equal(GMP::Z(- 511_627_776), neg_m40, "GMP::Z should addmul GMP::Z correctly")
38
+ neg_m40.addmul!(GMP::Z(17_000), thirty_thou) # <= - 511_627_776 + 510_000_000
39
+ assert_equal(GMP::Z(- 1_627_776), neg_m40, "GMP::Z should addmul GMP::Z correctly")
40
+ neg_m40.addmul!(GMP::Z(-400), GMP::Z(-4000)) # <= - 1_627_776 + 1_600_000
41
+ assert_equal(GMP::Z(- 27_776), neg_m40, "GMP::Z should addmul GMP::Z correctly")
42
+ neg_m40.addmul!(GMP::Z(-30), thirty*(-30)) # <= - 27_776 + 27_000
43
+ assert_equal(GMP::Z(- 776), neg_m40, "GMP::Z should addmul GMP::Z correctly")
44
+ end
45
+
46
+ def test_fixnum
47
+ five = GMP::Z(5)
48
+ five.addmul!(7, 7) # <= 5 + 49
49
+ assert_equal(54, five, "GMP::Z should addmul Fixnum correctly")
50
+ five = GMP::Z(5)
51
+ five.addmul!(-20, 2) # <= 5 + -40
52
+ assert_equal(-35, five, "GMP::Z should addmul Fixnum correctly")
53
+ m20 = GMP::Z(2)**20 - 1 # 1_048_575
54
+ m20.addmul!(-524_287, 2) # <= 1_048_575 + -1_048_574
55
+ assert_equal(1, m20, "GMP::Z should addmul Fixnum correctly")
56
+ if @_64bit
57
+ neg_m40 = -GMP::Z(2)**40 # -1_099_511_627_776
58
+ neg_m40.addmul!( 1_000_000_000_000, 1) # <= -1_099_511_627_776 + 1_000_000_000_000
59
+ assert_equal(GMP::Z(-99_511_627_776), neg_m40, "GMP::Z should addmul 64-bit Fixnum correctly")
60
+ end
61
+ end
62
+
63
+ def test_bignum
64
+ if ! @_64bit
65
+ neg_m40 = -GMP::Z(2)**40 # -1_099_511_627_776
66
+ neg_m40.addmul!( 1_000_000_000_000, 1) # <= -1_099_511_627_776 + 1_000_000_000_000
67
+ assert_equal(GMP::Z(-99_511_627_776), neg_m40, "GMP::Z should addmul Bignum correctly")
68
+ neg_m40.addmul!(10, 9_000_000_000) # <= - 99_511_627_776 + 90_000_000_000
69
+ assert_equal(GMP::Z(- 9_511_627_776), neg_m40, "GMP::Z should addmul Bignum correctly")
70
+ neg_m40.addmul!( 3_000_000_000, 3) # <= - 9_511_627_776 + 9_000_000_000
71
+ assert_equal(GMP::Z(- 511_627_776), neg_m40, "GMP::Z should addmul Bignum correctly")
72
+ else
73
+ m70 = GMP::Z(2)**70 - 1 # 1_180_591_620_717_411_303_423
74
+ m70.addmul!(-11*10_000_000_000, 10_000_000_000)
75
+ assert_equal(GMP::Z(80_591_620_717_411_303_423), m70, "GMP::Z should addmul 64-bit Bignum correctly")
76
+ m70.addmul!(-1_000_000_000, 8*10_000_000_000)
77
+ assert_equal(GMP::Z( 591_620_717_411_303_423), m70, "GMP::Z should addmul 64-bit Bignum correctly")
78
+ end
79
+ end
80
+
81
+ def test_raise
82
+ if ! @_64bit
83
+ assert_raise(RangeError) { GMP::Z(123).addmul!(456, -7) }
84
+ assert_nothing_raised(RangeError) { GMP::Z(123).addmul!(456, -7_000_000_000) }
85
+ else
86
+ assert_raise(RangeError) { GMP::Z(123).addmul!(456, -7_000_000_000) }
87
+ assert_nothing_raised(RangeError) { GMP::Z(123).addmul!(456, -7_000_000_000_000_000_000) }
88
+ end
89
+ assert_nothing_raised(RangeError) { GMP::Z(123).addmul!(456, 7) }
90
+ assert_nothing_raised(RangeError) { GMP::Z(123).addmul!(456, GMP::Z(-7)) }
91
+ end
92
+ end
@@ -1,5 +1,3 @@
1
- require 'test_helper'
2
-
3
1
  # Tested: [Z op Z, Z op FixNum, Z op BigNum, FixNum op Z, BigNum op Z]
4
2
  # Things are tested both ways because the implementation is asymetrical
5
3
  class TC_Z_Basic < Test::Unit::TestCase
@@ -10,11 +8,11 @@ class TC_Z_Basic < Test::Unit::TestCase
10
8
  end
11
9
 
12
10
  def test_add
13
- assert_equal(@a + @b, 100 + 200, "GMP::Z should add correctly")
14
- assert_equal(@a + 2, 100 + 2, "GMP::Z should add correctly")
15
- assert_equal(@a + @c, 100 + 2**32, "GMP::Z should add correctly")
16
- assert_equal( 2 + @a, 2 + 100, "GMP::Z should add correctly")
17
- assert_equal(@c + @a, 2**32 + 100, "GMP::Z should add correctly")
11
+ assert_equal(100 + 200, @a + @b, "GMP::Z should add correctly")
12
+ assert_equal(100 + 2, @a + 2, "GMP::Z should add correctly")
13
+ assert_equal(100 + 2**32, @a + @c, "GMP::Z should add correctly")
14
+ assert_equal( 2 + 100, 2 + @a, "GMP::Z should add correctly")
15
+ assert_equal(2**32 + 100, @c + @a, "GMP::Z should add correctly")
18
16
  end
19
17
 
20
18
  def test_sub
@@ -32,4 +30,4 @@ class TC_Z_Basic < Test::Unit::TestCase
32
30
  assert_equal( 2 * @a, 2 * 100, "GMP::Z should multiply correctly")
33
31
  assert_equal(@c * @a, 2**32 * 100, "GMP::Z should multiply correctly")
34
32
  end
35
- end
33
+ end