srawlins-gmp 0.1.1
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.
- data/CHANGELOG +23 -0
- data/INSTALL +4 -0
- data/README.rdoc +248 -0
- data/ext/extconf.rb +30 -0
- data/ext/gmp.c +561 -0
- data/ext/gmpf.h +442 -0
- data/ext/gmpq.h +486 -0
- data/ext/gmpz.h +833 -0
- data/ext/takeover.h +36 -0
- data/test/README +41 -0
- data/test/tc_cmp.rb +74 -0
- data/test/tc_q.rb +27 -0
- data/test/tc_q_basic.rb +41 -0
- data/test/tc_z.rb +59 -0
- data/test/tc_z_basic.rb +35 -0
- data/test/tc_z_exponentiation.rb +22 -0
- data/test/tc_z_logic.rb +54 -0
- data/test/test_helper.rb +2 -0
- data/test/unit_tests.rb +75 -0
- metadata +81 -0
data/ext/takeover.h
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#define DEFUN_TAKEOVER_LOGIC(fname, mpz_fname, old_fname) \
|
2
|
+
static VALUE takeover_fixnum_##fname(int argc, VALUE *argv, VALUE self) \
|
3
|
+
{ \
|
4
|
+
MP_INT *res_val, *arg_val; \
|
5
|
+
VALUE res; \
|
6
|
+
\
|
7
|
+
if (argc != 1 || !GMPZ_P(argv[0])) { \
|
8
|
+
return rb_funcall2 (self, rb_intern (old_fname), argc, argv); \
|
9
|
+
} else { \
|
10
|
+
mpz_make_struct(res, res_val); \
|
11
|
+
mpz_get_struct(argv[0], arg_val); \
|
12
|
+
mpz_init_set_si (res_val, FIX2INT(self)); \
|
13
|
+
mpz_fname (res_val, res_val, arg_val); \
|
14
|
+
return res; \
|
15
|
+
} \
|
16
|
+
} \
|
17
|
+
\
|
18
|
+
static VALUE takeover_bignum_##fname(int argc, VALUE *argv, VALUE self) \
|
19
|
+
{\
|
20
|
+
MP_INT *res_val, *arg_val; \
|
21
|
+
VALUE res; \
|
22
|
+
\
|
23
|
+
if (argc != 1 || !GMPZ_P(argv[0])) { \
|
24
|
+
return rb_funcall2 (self, rb_intern (old_fname), argc, argv); \
|
25
|
+
} else { \
|
26
|
+
mpz_get_struct(argv[0], arg_val); \
|
27
|
+
mpz_make_struct_init(res, res_val); \
|
28
|
+
mpz_set_bignum (res_val, self); \
|
29
|
+
mpz_fname (res_val, res_val, arg_val); \
|
30
|
+
return res; \
|
31
|
+
} \
|
32
|
+
}
|
33
|
+
|
34
|
+
DEFUN_TAKEOVER_LOGIC(and, mpz_and, "old_and")
|
35
|
+
DEFUN_TAKEOVER_LOGIC(or, mpz_ior, "old_or")
|
36
|
+
DEFUN_TAKEOVER_LOGIC(xor, mpz_xor, "old_xor")
|
data/test/README
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
TODO: Port everything to Test::Unit framework
|
2
|
+
Test files 1-6 ported.
|
3
|
+
|
4
|
+
Current version passes:
|
5
|
+
1-23
|
6
|
+
Current version fails on these tests because:
|
7
|
+
none
|
8
|
+
Tests:
|
9
|
+
1* initializations and to_s
|
10
|
+
2* basic integer arithmetics and coertion to Z
|
11
|
+
3* integer logical operators
|
12
|
+
4* comparisions between Z, Q, FixNum and BigNum
|
13
|
+
5* basic rational arithmetics and coertion to Q
|
14
|
+
6 bit operations, scan0, scan1
|
15
|
+
7* exponentiation and exponentiation modulo
|
16
|
+
8* divide by zero exceptions
|
17
|
+
9* sgn, neg, abs
|
18
|
+
10 fibonacci, factorial, nextprime
|
19
|
+
11 swap
|
20
|
+
12 floor, ceil, truncate
|
21
|
+
13 to_i, to_d
|
22
|
+
14 shifts, last_bits
|
23
|
+
15 logical tests, roots
|
24
|
+
16 floating point init with and without explicit precision
|
25
|
+
17 basic floating point arithmetics and coercion to F
|
26
|
+
18 default_prec and default_prec=
|
27
|
+
19 integer division methods
|
28
|
+
20 mpfr - exp and log
|
29
|
+
21 mpfr - trigonometry
|
30
|
+
22 mpfr - type tests
|
31
|
+
23 mpfr - sqrt and power
|
32
|
+
TODO:
|
33
|
+
jacobi, legendre, remove
|
34
|
+
gcd, lcm
|
35
|
+
cmpabs
|
36
|
+
integer and integer->rational division, modulo
|
37
|
+
more number theoretic functions
|
38
|
+
random numbers
|
39
|
+
range error exceptions
|
40
|
+
others ?
|
41
|
+
full coverage ?
|
data/test/tc_cmp.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TC_Cmp < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@a=GMP::Z.new(180)
|
6
|
+
@c=GMP::Q.new(2000,11) # ~181.82
|
7
|
+
@d=GMP::Q.new(3000,17) # ~176.47
|
8
|
+
@e=700
|
9
|
+
@f=2**32
|
10
|
+
@g=GMP::Q(360,2)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_cmp_q_q
|
14
|
+
assert_equal(@c > @d, true, "GMP::Q should compare correctly")
|
15
|
+
assert_equal(@c >= @d, true, "GMP::Q should compare correctly")
|
16
|
+
assert_equal(@c < @d, false, "GMP::Q should compare correctly")
|
17
|
+
assert_equal(@c <= @d, false, "GMP::Q should compare correctly")
|
18
|
+
assert_equal(@c == @d, false, "GMP::Q should == correctly")
|
19
|
+
assert_equal(@c == @c, true, "GMP::Q should == correctly")
|
20
|
+
assert_equal(@c != @d, true, "GMP::Q should != correctly")
|
21
|
+
assert_equal(@c != @c, false, "GMP::Q should != correctly")
|
22
|
+
|
23
|
+
assert_equal(@c <=> @d, 1, "GMP::Q should <=> correctly")
|
24
|
+
assert_equal(@c <=> @c, 0, "GMP::Q should <=> correctly")
|
25
|
+
assert_equal(@d <=> @c, -1, "GMP::Q should <=> correctly")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_cmp_q_z
|
29
|
+
assert_equal(@c > @a, true, "GMP::Q should compare correctly with GMP::Z")
|
30
|
+
assert_equal(@c >= @a, true, "GMP::Q should compare correctly with GMP::Z")
|
31
|
+
assert_equal(@c < @a, false, "GMP::Q should compare correctly with GMP::Z")
|
32
|
+
assert_equal(@c <= @a, false, "GMP::Q should compare correctly with GMP::Z")
|
33
|
+
assert_equal(@c == @a, false, "GMP::Q should == correctly with GMP::Z")
|
34
|
+
assert_equal(@c != @a, true, "GMP::Q should != correctly with GMP::Z")
|
35
|
+
|
36
|
+
assert_equal(@a < @c, true, "GMP::Z should compare correctly with GMP::Q")
|
37
|
+
assert_equal(@a <= @c, true, "GMP::Z should compare correctly with GMP::Q")
|
38
|
+
assert_equal(@a > @c, false, "GMP::Z should compare correctly with GMP::Q")
|
39
|
+
assert_equal(@a >= @c, false, "GMP::Z should compare correctly with GMP::Q")
|
40
|
+
assert_equal(@a == @c, false, "GMP::Z should == correctly with GMP::Q")
|
41
|
+
assert_equal(@a != @c, true, "GMP::Z should != correctly with GMP::Q")
|
42
|
+
|
43
|
+
assert_equal(@g == @a, true, "GMP::Q should == correctly with GMP::Z")
|
44
|
+
assert_equal(@g != @a, false, "GMP::Q should != correctly with GMP::Z")
|
45
|
+
|
46
|
+
assert_equal(@d > @a, false, "GMP::Q should compare correctly with GMP::Z")
|
47
|
+
assert_equal(@d >= @a, false, "GMP::Q should compare correctly with GMP::Z")
|
48
|
+
assert_equal(@d < @a, true, "GMP::Q should compare correctly with GMP::Z")
|
49
|
+
assert_equal(@d <= @a, true, "GMP::Q should compare correctly with GMP::Z")
|
50
|
+
|
51
|
+
assert_equal(@a < @d, false, "GMP::Z should compare correctly with GMP::Q")
|
52
|
+
assert_equal(@a <= @d, false, "GMP::Z should compare correctly with GMP::Q")
|
53
|
+
assert_equal(@a > @d, true, "GMP::Z should compare correctly with GMP::Q")
|
54
|
+
assert_equal(@a >= @d, true, "GMP::Z should compare correctly with GMP::Q")
|
55
|
+
|
56
|
+
assert_equal(@c <=> @a, 1, "GMP::Q should <=> correctly with GMP::Z")
|
57
|
+
assert_equal(@g <=> @a, 0, "GMP::Q should <=> correctly with GMP::Z")
|
58
|
+
assert_equal(@a <=> @c, -1, "GMP::Q should <=> correctly with GMP::Z")
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_cmp_z_int
|
62
|
+
assert_equal(@a <=> @e, -1, "GMP::Z should <=> correctly with Fixnum")
|
63
|
+
assert_equal(@e <=> @a, 1, "Fixnum should <=> correctly with GMP::Z")
|
64
|
+
assert_equal(@a <=> @f, -1, "GMP::Z should <=> correctly with Bignum")
|
65
|
+
assert_equal(@f <=> @a, 1, "Bignum should <=> correctly with GMP::Z")
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_cmp_q_int
|
69
|
+
assert_equal(@c <=> @e, -1, "GMP::Q should <=> correctly with Fixnum")
|
70
|
+
assert_equal(@e <=> @c, 1, "Fixnum should <=> correctly with GMP::Q")
|
71
|
+
assert_equal(@c <=> @f, -1, "GMP::Q should <=> correctly with Bignum")
|
72
|
+
assert_equal(@f <=> @c, 1, "Bignum should <=> correctly with GMP::Q")
|
73
|
+
end
|
74
|
+
end
|
data/test/tc_q.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TC_Q < Test::Unit::TestCase
|
4
|
+
def test_init_null
|
5
|
+
assert_equal(GMP::Q.new(), 0, "GMP::Q.new() should initialize to 0")
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_init_fixnum
|
9
|
+
assert_equal(GMP::Q.new(1), 1, "GMP::Q.new(x : Fixnum) should initialize to 0")
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_init_fixnum2
|
13
|
+
assert_equal(GMP::Q.new(1, 2).to_s, "1/2", "GMP::Q.new(num : Fixnum, den : Fixnum) should initialize to num/den")
|
14
|
+
assert_equal(GMP::Q.new(1, 3).to_s, "1/3", "GMP::Q.new(num : Fixnum, den : Fixnum) should initialize to num/den")
|
15
|
+
assert_equal(GMP::Q.new(2, 4).to_s, "1/2", "GMP::Q.new(num : Fixnum, den : Fixnum) should initialize to num/den")
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_init_string
|
19
|
+
assert_equal(GMP::Q.new("1/2").to_s, "1/2", "GMP::Q.new(x : String) should initialize to x")
|
20
|
+
assert_equal(GMP::Q.new("1/3").to_s, "1/3", "GMP::Q.new(x : String) should initialize to x")
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_init_q
|
24
|
+
a = GMP::Q.new(1,2)
|
25
|
+
assert_equal(GMP::Q.new(a), a, "GMP::Q.new(x : Q) should initialize to x")
|
26
|
+
end
|
27
|
+
end
|
data/test/tc_q_basic.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
# [Q op Q, Q op Z, Z op Q, Q op FixNum, Q op BigNum, FixNum op Q, BigNum op Q]
|
4
|
+
class TC_Q_Basic < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@a=GMP::Q.new(100,11)
|
7
|
+
@b=GMP::Q.new(200,17)
|
8
|
+
@c=GMP::Z.new(40)
|
9
|
+
@d=2**32
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_add
|
13
|
+
assert_equal(@a + @b, GMP::Q(3900, 187), "GMP::Q should add GMP::Q correctly")
|
14
|
+
assert_equal(@a + @c, GMP::Q(540, 11), "GMP::Q should add GMP::Z correctly")
|
15
|
+
assert_equal(@c + @a, GMP::Q(540, 11), "GMP::Z should add GMP::Q correctly")
|
16
|
+
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")
|
19
|
+
assert_equal(@d + @a, GMP::Q(47244640356, 11), "Bignum should add GMP::Q correctly")
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_sub
|
23
|
+
assert_equal(@a - @b, GMP::Q(-500, 187), "GMP::Q should subtract GMP::Q correctly")
|
24
|
+
assert_equal(@a - @c, GMP::Q(-340, 11), "GMP::Q should subtract GMP::Z correctly")
|
25
|
+
assert_equal(@c - @a, GMP::Q(340, 11), "GMP::Z should subtract GMP::Q correctly")
|
26
|
+
assert_equal(@a - 2, GMP::Q(78, 11), "GMP::Z should subtract Fixnum correctly")
|
27
|
+
assert_equal(@a - @d, GMP::Q(-47244640156, 11),"GMP::Z should subtract Bignum correctly")
|
28
|
+
assert_equal( 2 - @a, GMP::Q(-78, 11), "Fixnum should subtract GMP::Q correctly")
|
29
|
+
assert_equal(@d - @a, GMP::Q(47244640156, 11), "Bignum should subtract GMP::Q correctly")
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_mul
|
33
|
+
assert_equal(@a * @b, GMP::Q(20000, 187), "GMP::Q should multiply GMP::Q correctly")
|
34
|
+
assert_equal(@a * @c, GMP::Q(4000, 11), "GMP::Q should multiply GMP::Z correctly")
|
35
|
+
assert_equal(@c * @a, GMP::Q(4000, 11), "GMP::Z should multiply GMP::Q correctly")
|
36
|
+
assert_equal(@a * 2, GMP::Q(200, 11), "GMP::Z should multiply Fixnum correctly")
|
37
|
+
assert_equal(@a * @d, GMP::Q(429496729600, 11),"GMP::Z should multiply Bignum correctly")
|
38
|
+
assert_equal( 2 * @a, GMP::Q(200, 11), "Fixnum should multiply GMP::Q correctly")
|
39
|
+
assert_equal(@d * @a, GMP::Q(429496729600, 11),"Bignum should multiply GMP::Q correctly")
|
40
|
+
end
|
41
|
+
end
|
data/test/tc_z.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TC_Z < Test::Unit::TestCase
|
4
|
+
def test_init_null
|
5
|
+
assert_equal(GMP::Z.new(), 0, "GMP::Z.new() should initialize to 0")
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_init_fixnum
|
9
|
+
assert_equal(GMP::Z.new(1), 1, "GMP::Z.new(x : Fixnum) should initialize to x")
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_init_z
|
13
|
+
b = GMP::Z.new(1)
|
14
|
+
assert_equal(GMP::Z.new(b), b, "GMP::Z.new(x : GMP::Z) should initialize to x")
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_init_string
|
18
|
+
assert_equal(GMP::Z.new("1"), 1, "GMP::Z.new(x : String) should initialize to x")
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_init_bignum
|
22
|
+
assert_equal(GMP::Z.new(2**32), 2**32, "GMP::Z.new(x : Bignum) should initialize to x")
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_cmp_z
|
26
|
+
a = GMP::Z.new(2)
|
27
|
+
b = GMP::Z.new(1)
|
28
|
+
assert_equal(a > b, true, "GMP::Z should compare correctly with GMP::Z")
|
29
|
+
assert_equal(a > a, false, "GMP::Z should compare correctly with GMP::Z")
|
30
|
+
assert_equal(b > a, false, "GMP::Z should compare correctly with GMP::Z")
|
31
|
+
|
32
|
+
assert_equal(a >= b, true, "GMP::Z should compare correctly with GMP::Z")
|
33
|
+
assert_equal(a >= a, true, "GMP::Z should compare correctly with GMP::Z")
|
34
|
+
assert_equal(b >= a, false, "GMP::Z should compare correctly with GMP::Z")
|
35
|
+
|
36
|
+
assert_equal(a <= b, false, "GMP::Z should compare correctly with GMP::Z")
|
37
|
+
assert_equal(a <= a, true, "GMP::Z should compare correctly with GMP::Z")
|
38
|
+
assert_equal(b <= a, true, "GMP::Z should compare correctly with GMP::Z")
|
39
|
+
|
40
|
+
assert_equal(a < b, false, "GMP::Z should compare correctly with GMP::Z")
|
41
|
+
assert_equal(a < a, false, "GMP::Z should compare correctly with GMP::Z")
|
42
|
+
assert_equal(b < a, true, "GMP::Z should compare correctly with GMP::Z")
|
43
|
+
|
44
|
+
assert_equal(a == b, false, "GMP::Z should == correctly with GMP::Z")
|
45
|
+
assert_equal(a == a, true, "GMP::Z should == correctly with GMP::Z")
|
46
|
+
|
47
|
+
assert_equal(a != b, true, "GMP::Z should != correctly with GMP::Z")
|
48
|
+
assert_equal(a != a, false, "GMP::Z should != correctly with GMP::Z")
|
49
|
+
|
50
|
+
assert_equal(a <=> b, 1, "GMP::Z should <=> correctly with GMP::Z")
|
51
|
+
assert_equal(a <=> a, 0, "GMP::Z should <=> correctly with GMP::Z")
|
52
|
+
assert_equal(b <=> a, -1, "GMP::Z should <=> correctly with GMP::Z")
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_to_s
|
56
|
+
assert_equal(GMP::Z.new(1).to_s, "1", "Z(1).to_s should equal '1'")
|
57
|
+
assert_equal(GMP::Z.new(-1).to_s, "-1", "Z(1).to_s should equal '-1'")
|
58
|
+
end
|
59
|
+
end
|
data/test/tc_z_basic.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
# Tested: [Z op Z, Z op FixNum, Z op BigNum, FixNum op Z, BigNum op Z]
|
4
|
+
# Things are tested both ways because the implementation is asymetrical
|
5
|
+
class TC_Z_Basic < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@a=GMP::Z.new(100)
|
8
|
+
@b=GMP::Z.new(200)
|
9
|
+
@c=2**32
|
10
|
+
end
|
11
|
+
|
12
|
+
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")
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_sub
|
21
|
+
assert_equal(@a - @b, 100 - 200, "GMP::Z should subtract correctly")
|
22
|
+
assert_equal(@a - 2, 100 - 2, "GMP::Z should subtract correctly")
|
23
|
+
assert_equal(@a - @c, 100 - 2**32, "GMP::Z should subtract correctly")
|
24
|
+
assert_equal( 2 - @a, 2 - 100, "GMP::Z should subtract correctly")
|
25
|
+
assert_equal(@c - @a, 2**32 - 100, "GMP::Z should subtract correctly")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_mul
|
29
|
+
assert_equal(@a * @b, 100 * 200, "GMP::Z should multiply correctly")
|
30
|
+
assert_equal(@a * 2, 100 * 2, "GMP::Z should multiply correctly")
|
31
|
+
assert_equal(@a * @c, 100 * 2**32, "GMP::Z should multiply correctly")
|
32
|
+
assert_equal( 2 * @a, 2 * 100, "GMP::Z should multiply correctly")
|
33
|
+
assert_equal(@c * @a, 2**32 * 100, "GMP::Z should multiply correctly")
|
34
|
+
end
|
35
|
+
end
|
@@ -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(100), @a.powmod(@b,@c), "(a : GMP::Z).powmod((b : GMP::Z), (c : GMP::Z)) should work correctly")
|
21
|
+
end
|
22
|
+
end
|
data/test/tc_z_logic.rb
ADDED
@@ -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
|
data/test/test_helper.rb
ADDED
data/test/unit_tests.rb
ADDED
@@ -0,0 +1,75 @@
|
|
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
|
+
|
12
|
+
|
13
|
+
class TC_ZeroDivisionExceptions < Test::Unit::TestCase
|
14
|
+
def setup
|
15
|
+
@a = GMP::Z.new(10)
|
16
|
+
@b = GMP::Z.new()
|
17
|
+
@c = GMP::Q.new(1)
|
18
|
+
@d = GMP::Q.new()
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_division_by_zero
|
22
|
+
assert_raise(ZeroDivisionError) { @a.tdiv(0) }
|
23
|
+
assert_raise(ZeroDivisionError) { @a.tdiv(@b) }
|
24
|
+
assert_raise(ZeroDivisionError) { @d.inv }
|
25
|
+
assert_raise(ZeroDivisionError) { @d.inv! }
|
26
|
+
assert_raise(ZeroDivisionError) { @c/0 }
|
27
|
+
assert_raise(ZeroDivisionError) { @c/@d }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class TC_sgn_neg_abs < Test::Unit::TestCase
|
32
|
+
def setup
|
33
|
+
@a=GMP::Z.new(10)
|
34
|
+
@b=GMP::Z.new()
|
35
|
+
@c=GMP::Z.new(-10)
|
36
|
+
@d=GMP::Q.new(10)
|
37
|
+
@e=GMP::Q.new()
|
38
|
+
@f=GMP::Q.new(-10)
|
39
|
+
@k=GMP::Z.new(10)
|
40
|
+
@l=GMP::Z.new()
|
41
|
+
@m=GMP::Z.new(-10)
|
42
|
+
@n=GMP::Q.new(10)
|
43
|
+
@o=GMP::Q.new()
|
44
|
+
@p=GMP::Q.new(-10)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_sgn
|
48
|
+
assert_equal([1, 0, 1, 0], [@a.sgn, @b.sgn, @d.sgn, @e.sgn], "GMP::Z, GMP::Q should calculate sgn correctly")
|
49
|
+
assert_equal([-1, -1], [@c.sgn, @f.sgn], "GMP::Z, GMP::Q should calculate sgn correctly")
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_neg
|
53
|
+
assert_equal(-@a, @c, "-(x : GMP::Z) should be calculated correctly.")
|
54
|
+
assert_equal(-@c, @a, "-(x : GMP::Z) should be calculated correctly.")
|
55
|
+
assert_equal(-@b, @b, "-GMP::Z.new() should equal GMP::Z.new().")
|
56
|
+
assert_equal(-@d, @f, "-(x : GMP::Q) should be calculated correctly.")
|
57
|
+
assert_equal(-@f, @d, "-(x : GMP::Q) should be calculated correctly.")
|
58
|
+
assert_equal(-@e, @e, "-GMP::Q.new() should equal GMP::Q.new().")
|
59
|
+
@k.neg!; @l.neg!; @m.neg!; @n.neg!; @o.neg!; @p.neg!
|
60
|
+
assert_equal(@k, @c, "(x : GMP::Z).neg! should be calculated correctly.")
|
61
|
+
assert_equal(@l, @b, "(x : GMP::Z).neg! should be calculated correctly.")
|
62
|
+
assert_equal(@m, @a, "GMP::Z.new().neg! should equal GMP::Z.new().")
|
63
|
+
assert_equal(@n, @f, "(x : GMP::Q).neg! should be calculated correctly.")
|
64
|
+
assert_equal(@o, @e, "(x : GMP::Q).neg! should be calculated correctly.")
|
65
|
+
assert_equal(@p, @d, "GMP::Q.new().neg! should equal GMP::Q.new().")
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_abs
|
69
|
+
assert_equal([10, 0, 10], [@a.abs, @b.abs, @c.abs], "(x : GMP::Z).abs should be calculated correctly.")
|
70
|
+
assert_equal([10, 0, 10], [@d.abs, @e.abs, @f.abs], "(x : GMP::Q).abs should be calculated correctly.")
|
71
|
+
@a.abs!; @b.abs!; @c.abs!; @d.abs!; @e.abs!; @f.abs!
|
72
|
+
assert_equal([10, 0, 10], [@a, @b, @c], "(x : GMP::Z).abs! should be calculated correctly.")
|
73
|
+
assert_equal([10, 0, 10], [@d, @e, @f], "(x : GMP::Q).abs! should be calculated correctly.")
|
74
|
+
end
|
75
|
+
end
|