gmp 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class TC_Z_Exponentiation < Test::Unit::TestCase
4
+ def setup
5
+ @a = GMP::Z.new(100)
6
+ @b = GMP::Z.new(16)
7
+ @c = @a**5
8
+ end
9
+
10
+ def test_exponentiation
11
+ assert_equal(GMP::Z(10000000000), @a**5, "GMP::Z should **(Fixnum) correctly")
12
+ assert_equal(GMP::Z("100000000000000000000000000000000"), @a**@b, "GMP::Z should **(GMP::Z) correctly")
13
+ assert_equal(GMP::Z(65536), 2**@b, "Fixnum should **(GMP::Z) correctly")
14
+ end
15
+
16
+ def test_powmod
17
+ assert_equal(GMP::Z(16), @a.powmod(2,256), "(a : GMP::Z).powmod((b : Fixnum), (c : Fixnum)) should work correctly")
18
+ assert_equal(GMP::Z(76), @b.powmod(10,@a), "(a : GMP::Z).powmod((b : Fixnum), (c : GMP::Z)) should work correctly")
19
+ assert_equal(GMP::Z(0), @a.powmod(@b,256), "(a : GMP::Z).powmod((b : GMP::Z), (c : Fixnum)) should work correctly")
20
+ assert_equal(GMP::Z(0), @a.powmod(@b,@c), "(a : GMP::Z).powmod((b : GMP::Z), (c : GMP::Z)) should work correctly")
21
+ end
22
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class TC_Z_Logic < Test::Unit::TestCase
4
+ def setup
5
+ @a=GMP::Z.new(0xffff00000000ffff)
6
+ @b=GMP::Z.new(0xffff0000ffff0000)
7
+ @c=0xff00ff00ff00ff00
8
+ @d=0x1248
9
+ end
10
+
11
+ def test_and
12
+ assert_equal(@a & @b, 0xffff00000000ffff & 0xffff0000ffff0000, "GMP::Z should AND correctly")
13
+ assert_equal(@a & @d, 0xffff00000000ffff & 0x1248, "GMP::Z should AND correctly")
14
+ assert_equal(@a & @c, 0xffff00000000ffff & 0xff00ff00ff00ff00, "GMP::Z should AND correctly")
15
+ assert_equal(@d & @a, 0x1248 & 0xffff00000000ffff, "GMP::Z should AND correctly")
16
+ assert_equal(@c & @a, 0xff00ff00ff00ff00 & 0xffff00000000ffff, "GMP::Z should AND correctly")
17
+ end
18
+
19
+ def test_or
20
+ assert_equal(@a | @b, 0xffff00000000ffff | 0xffff0000ffff0000, "GMP::Z should OR correctly")
21
+ assert_equal(@a | @d, 0xffff00000000ffff | 0x1248, "GMP::Z should OR correctly")
22
+ assert_equal(@a | @c, 0xffff00000000ffff | 0xff00ff00ff00ff00, "GMP::Z should OR correctly")
23
+ assert_equal(@d | @a, 0x1248 | 0xffff00000000ffff, "GMP::Z should OR correctly")
24
+ assert_equal(@c | @a, 0xff00ff00ff00ff00 | 0xffff00000000ffff, "GMP::Z should OR correctly")
25
+ end
26
+
27
+ def test_xor
28
+ assert_equal(@a ^ @b, 0xffff00000000ffff ^ 0xffff0000ffff0000, "GMP::Z should XOR correctly")
29
+ assert_equal(@a ^ @d, 0xffff00000000ffff ^ 0x1248, "GMP::Z should XOR correctly")
30
+ assert_equal(@a ^ @c, 0xffff00000000ffff ^ 0xff00ff00ff00ff00, "GMP::Z should XOR correctly")
31
+ assert_equal(@d ^ @a, 0x1248 ^ 0xffff00000000ffff, "GMP::Z should XOR correctly")
32
+ assert_equal(@c ^ @a, 0xff00ff00ff00ff00 ^ 0xffff00000000ffff, "GMP::Z should XOR correctly")
33
+ end
34
+
35
+ def test_com
36
+ assert_equal(GMP::Z().com, -1, "GMP::Z.com should complement")
37
+ assert_equal(GMP::Z(1).com, -2, "GMP::Z.com should complement")
38
+ assert_equal(GMP::Z(-1).com, 0, "GMP::Z.com should complement")
39
+ assert_equal(@a.com, -0xffff00000000ffff-1, "GMP::Z.com should complement")
40
+ end
41
+
42
+ def test_logic
43
+ a=GMP::Z.new(0x1234)
44
+ b=GMP::Z.new(a)
45
+ b[0]=true
46
+
47
+ assert_equal(a, 0x1234, "GMP::Z.new() should create a new copy, not alias")
48
+ assert_equal(b, 0x1235, "GMP::Z#[]= should modify bits")
49
+ assert_equal(a[2], true, "GMP::Z#[] should access bits")
50
+ assert_equal(a.scan0(0), 0, "GMP::Z#scan0 should scan for 0s")
51
+ assert_equal(a.scan1(0), 2, "GMP::Z#scan1 should scan for 1s")
52
+
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class TC_shifts_last_bits < Test::Unit::TestCase
4
+ def setup
5
+ @a = GMP::Z.new(100) # 01100100
6
+ @b =- @a # 10011100
7
+ end
8
+
9
+ def test_shifts
10
+ assert_equal( 3, @a >> 5, "GMP::Z should >> correctly.") # 00000011
11
+ assert_equal(- 4, @b >> 5, "GMP::Z should >> correctly.") # 11111100
12
+ assert_equal( 400, @a << 2, "GMP::Z should << correctly.") # 0110010000
13
+ assert_equal(-400, @b << 2, "GMP::Z should << correctly.") # 1110010000
14
+ end
15
+
16
+ def test_last_bits
17
+ #assert_equal( 5, @a.lastbits_pos(5), "GMP::Z should lastbits_pos correctly.") # 100
18
+ #assert_equal(28, @b.lastbits_pos(5), "GMP::Z should lastbits_pos correctly.") # 11100
19
+ #assert_equal(-4, @b.lastbits_sgn(5), "GMP::Z should lastbits_sgn correctly.")
20
+ # a.tshr 5 ???
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class TC_to_i_to_d < Test::Unit::TestCase
4
+ def setup
5
+ @a = GMP::Z.new(100)
6
+ #@b = GMP::Z.pow(2,32)
7
+ @c = GMP::Q.new(200,11)
8
+ end
9
+
10
+ def test_to_i
11
+ assert_equal(@a.to_i, 100, "GMP::Z should to_i correctly.")
12
+ assert_equal(@a.to_i.class, Fixnum, "GMP::Z.to_i should be a Fixnum.")
13
+ #assert_equal(@b.to_i, 2**32, "GMP::Z (Bignum) should to_i correctly.")
14
+ #assert_equal(@b.to_i.class, Bignum, "GMP::Z.to_i should be a Bignum.")
15
+ end
16
+
17
+ def test_to_d
18
+ assert_equal(@a.to_d, 100.0, "GMP::Z should to_d correctly.")
19
+ assert_equal(@a.to_d.class, Float, "GMP::Z.to_d should be a Float.")
20
+ #assert_equal(@b.to_d, 2**32*1.0, "GMP::Z should to_d correctly.")
21
+ #assert_equal(@b.to_d.class, Float, "GMP::Z.to_d should be a Float.")
22
+ assert_equal(@c.to_d.class, Float, "GMP::Q.to_d should be a Float.")
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ class TC_ZeroDivisionExceptions < Test::Unit::TestCase
2
+ def setup
3
+ @a = GMP::Z.new(10)
4
+ @b = GMP::Z.new()
5
+ @c = GMP::Q.new(1)
6
+ @d = GMP::Q.new()
7
+ end
8
+
9
+ def test_division_by_zero
10
+ assert_raise(ZeroDivisionError) { @a.tdiv(0) }
11
+ assert_raise(ZeroDivisionError) { @a.tdiv(@b) }
12
+ assert_raise(ZeroDivisionError) { @d.inv }
13
+ assert_raise(ZeroDivisionError) { @d.inv! }
14
+ assert_raise(ZeroDivisionError) { @c/0 }
15
+ assert_raise(ZeroDivisionError) { @c/@d }
16
+ end
17
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../ext/gmp'
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test_helper'
4
+ require 'tc_z'
5
+ require 'tc_z_basic'
6
+ require 'tc_z_logic'
7
+ require 'tc_q'
8
+ require 'tc_cmp'
9
+ require 'tc_q_basic'
10
+ require 'tc_z_exponentiation'
11
+ require 'tc_zerodivisionexceptions'
12
+ require 'tc_sgn_neg_abs'
13
+ require 'tc_fib_fac_nextprime'
14
+ require 'tc_swap'
15
+ require 'tc_floor_ceil_truncate'
16
+ require 'tc_z_to_d_to_i'
17
+ require 'tc_z_shifts_last_bits'
18
+ require 'tc_logical_roots'
19
+
20
+ class TC_precision < Test::Unit::TestCase
21
+ def setup
22
+ @pi = GMP::F.new(3.14)
23
+ @seven_halves = GMP::F.new(GMP::Q.new(7,2), 1000)
24
+ end
25
+
26
+ def test_default_precision
27
+ assert_equal("0.e+0", GMP::F.new().to_s)
28
+ assert_equal(64, GMP::F.new().prec)
29
+ assert_equal("0.314000000000000012434e+1", @pi.to_s)
30
+ assert_equal(64, @pi.prec)
31
+ assert_equal("0.1e+1", GMP::F.new(1).to_s)
32
+ assert_equal(64, GMP::F.new(1).prec)
33
+ assert_equal("0.314e+1", GMP::F.new("3.14").to_s)
34
+ assert_equal(64, GMP::F.new("3.14").prec)
35
+ assert_equal("0.4294967296e+10", GMP::F.new(2**32).to_s)
36
+ assert_equal(64, GMP::F.new(2**32).prec)
37
+ assert_equal("0.3e+1", GMP::F.new(GMP::Z.new(3)).to_s)
38
+ assert_equal(64, GMP::F.new(GMP::Z.new(3)).prec)
39
+ assert_equal("0.35e+1", GMP::F.new(GMP::Q.new(7,2)).to_s)
40
+ assert_equal(64, GMP::F.new(GMP::Q.new(7,2)).prec)
41
+ assert_equal("0.314000000000000012434e+1", GMP::F.new(@pi).to_s)
42
+ assert_equal(64, GMP::F.new(@pi).prec)
43
+ end
44
+
45
+ def test_specific_precision
46
+ assert_equal("0.3140000000000000124344978758017532527446746826171875e+1", GMP::F.new(3.14, 1000).to_s)
47
+ assert_equal(1024, GMP::F.new(3.14, 1000).prec)
48
+ assert_equal("0.1e+1", GMP::F.new(1, 1000).to_s)
49
+ assert_equal(1024, GMP::F.new(1, 1000).prec)
50
+ assert_equal("0.314e+1", GMP::F.new("3.14", 1000).to_s)
51
+ assert_equal(1024, GMP::F.new("3.14", 1000).prec)
52
+ assert_equal("0.4294967296e+10", GMP::F.new(2**32, 1000).to_s)
53
+ assert_equal(1024, GMP::F.new(2**32, 1000).prec)
54
+ assert_equal("0.3e+1", GMP::F.new(GMP::Z.new(3), 1000).to_s)
55
+ assert_equal(1024, GMP::F.new(GMP::Z.new(3), 1000).prec)
56
+ assert_equal("0.35e+1", @seven_halves.to_s)
57
+ assert_equal(1024, @seven_halves.prec)
58
+ assert_equal("0.35e+1", GMP::F.new(@seven_halves).to_s)
59
+ assert_equal(1024, GMP::F.new(@seven_halves).prec)
60
+ assert_equal("0.3140000000000000124344978758017532527446746826171875e+1", GMP::F.new(@pi, 1000).to_s)
61
+ assert_equal(1024, GMP::F.new(@pi, 1000).prec)
62
+ assert_equal("0.35e+1", GMP::F.new(@seven_halves, 0).to_s)
63
+ assert_equal(64, GMP::F.new(@seven_halves, 0).prec)
64
+ end
65
+ end
66
+
67
+ class TC_F_arithmetics_coersion < Test::Unit::TestCase
68
+ def setup
69
+ @a = GMP::F.new(3.14, 100)
70
+ @b = GMP::F.new(2.71, 200)
71
+ @c = GMP::Z(3)
72
+ @d = GMP::Q(7,2)
73
+ @e = 2**32
74
+ end
75
+
76
+ def test_add
77
+ assert_in_delta( 5.85, @a + @b, 1e-6)
78
+ assert_in_delta( 5.85, @b + @a, 1e-6)
79
+ assert_in_delta( 6.14, @a + @c, 1e-6)
80
+ assert_in_delta( 6.14, @c + @a, 1e-6)
81
+ assert_in_delta( 6.64, @a + @d, 1e-6)
82
+ assert_in_delta( 6.64, @d + @a, 1e-6)
83
+ assert_in_delta( 5.14, @a + 2, 1e-6)
84
+ assert_in_delta( 5.14, 2 + @a, 1e-6)
85
+ assert_in_delta(4294967299.14, @a + @e, 1e-6)
86
+ assert_in_delta(4294967299.14, @e + @a, 1e-6)
87
+ assert_in_delta( 7.65, @a + 4.51, 1e-6)
88
+ assert_in_delta( 7.65, 4.51 + @a, 1e-6)
89
+ end
90
+
91
+ def test_sub
92
+ assert_in_delta( 0.43, @a - @b, 1e-6)
93
+ assert_in_delta( -0.43, @b - @a, 1e-6)
94
+ assert_in_delta( 0.14, @a - @c, 1e-6)
95
+ assert_in_delta( -0.14, @c - @a, 1e-6)
96
+ assert_in_delta( -0.36, @a - @d, 1e-6)
97
+ assert_in_delta( 0.36, @d - @a, 1e-6)
98
+ assert_in_delta( 1.14, @a - 2, 1e-6)
99
+ assert_in_delta( -1.14, 2 - @a, 1e-6)
100
+ assert_in_delta(-4294967292.86, @a - @e, 1e-6)
101
+ assert_in_delta( 4294967292.86, @e - @a, 1e-6)
102
+ assert_in_delta( -1.37, @a - 4.51, 1e-6)
103
+ assert_in_delta( 1.37, 4.51 - @a, 1e-6)
104
+ end
105
+
106
+ def test_mul
107
+ assert_in_delta( 8.5094, @a * @b, 1e-6)
108
+ assert_in_delta( 8.5094, @b * @a, 1e-6)
109
+ assert_in_delta( 9.42 , @a * @c, 1e-6)
110
+ assert_in_delta( 9.42 , @c * @a, 1e-6)
111
+ assert_in_delta( 10.99 , @a * @d, 1e-6)
112
+ assert_in_delta( 10.99 , @d * @a, 1e-6)
113
+ assert_in_delta( 6.28 , @a * 2, 1e-6)
114
+ assert_in_delta( 6.28 , 2 * @a, 1e-6)
115
+ assert_in_delta(13486197309.44 , @a * @e, 1e-6)
116
+ assert_in_delta(13486197309.44 , @e * @a, 1e-6)
117
+ assert_in_delta( 14.1614, @a * 4.51, 1e-6)
118
+ assert_in_delta( 14.1614, 4.51 * @a, 1e-6)
119
+ end
120
+
121
+ def test_div
122
+ assert_in_delta( 1.1586715867, @a / @b, 1e-6)
123
+ assert_in_delta( 0.8630573248, @b / @a, 1e-6)
124
+ assert_in_delta( 1.0466666667, @a / @c, 1e-6)
125
+ assert_in_delta( 0.9554140127, @c / @a, 1e-6)
126
+ assert_in_delta( 0.8971428571, @a / @d, 1e-6)
127
+ assert_in_delta( 1.1146496815, @d / @a, 1e-6)
128
+ assert_in_delta( 1.57 , @a / 2, 1e-6)
129
+ assert_in_delta( 0.6369426752, 2 / @a, 1e-6)
130
+ assert_in_delta( 0.0000000007, @a / @e, 1e-6)
131
+ assert_in_delta( 1367823979.6178343949, @e / @a, 1e-6)
132
+ assert_in_delta( 0.6962305987, @a / 4.51, 1e-6)
133
+ assert_in_delta( 1.4363057325, 4.51 / @a, 1e-6)
134
+ end
135
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gmp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Wegrzanowski
8
+ - srawlins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-10-01 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: gmp - providing Ruby bindings to the GMP library.
18
+ email:
19
+ - Tomasz.Wegrzanowski@gmail.com
20
+ - sam.rawlins@gmail.com
21
+ executables: []
22
+
23
+ extensions:
24
+ - ext/extconf.rb
25
+ extra_rdoc_files: []
26
+
27
+ files:
28
+ - ext/extconf.rb
29
+ - ext/gmp.c
30
+ - ext/gmpf.c
31
+ - ext/gmpf.h
32
+ - ext/gmpq.c
33
+ - ext/gmpq.h
34
+ - ext/gmpz.c
35
+ - ext/gmpz.h
36
+ - ext/ruby_gmp.h
37
+ - ext/takeover.h
38
+ - test/README
39
+ - test/tc_cmp.rb
40
+ - test/tc_fib_fac_nextprime.rb
41
+ - test/tc_floor_ceil_truncate.rb
42
+ - test/tc_logical_roots.rb
43
+ - test/tc_q.rb
44
+ - test/tc_q_basic.rb
45
+ - test/tc_sgn_neg_abs.rb
46
+ - test/tc_swap.rb
47
+ - test/tc_z.rb
48
+ - test/tc_z_basic.rb
49
+ - test/tc_z_exponentiation.rb
50
+ - test/tc_z_logic.rb
51
+ - test/tc_z_shifts_last_bits.rb
52
+ - test/tc_z_to_d_to_i.rb
53
+ - test/tc_zerodivisionexceptions.rb
54
+ - test/test_helper.rb
55
+ - test/unit_tests.rb
56
+ - CHANGELOG
57
+ - INSTALL
58
+ - README.rdoc
59
+ - manual.tex
60
+ has_rdoc: false
61
+ homepage: http://github.com/srawlins/gmp/tree/master
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - ext
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 1.8.1
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements:
82
+ - GMP compiled and working properly.
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.5
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Provides Ruby bindings to the GMP library.
88
+ test_files: []
89
+