| 1 |
# frozen_string_literal: false |
| 2 |
require 'test/unit' |
| 3 |
require "-test-/rational" |
| 4 |
|
| 5 |
class TestRational < Test::Unit::TestCase |
| 6 |
class TestGCD < Test::Unit::TestCase |
| 7 |
|
| 8 |
def test_gcd_normal |
| 9 |
x = 2*2*3*3*3 |
| 10 |
y = 2*2*2*3*3 |
| 11 |
gcd = 2*2*3*3 |
| 12 |
assert_equal(gcd, Bug::Rational.gcd_normal(x, y)) |
| 13 |
end |
| 14 |
|
| 15 |
def test_gcd_gmp |
| 16 |
x = 2*2*3*3*3 |
| 17 |
y = 2*2*2*3*3 |
| 18 |
gcd = 2*2*3*3 |
| 19 |
assert_equal(gcd, Bug::Rational.gcd_gmp(x, y)) |
| 20 |
rescue NotImplementedError |
| 21 |
end |
| 22 |
|
| 23 |
def test_gcd_gmp_brute_force |
| 24 |
-13.upto(13) {|x| |
| 25 |
-13.upto(13) {|y| |
| 26 |
assert_equal(Bug::Rational.gcd_normal(x, y), Bug::Rational.gcd_gmp(x, y)) |
| 27 |
} |
| 28 |
} |
| 29 |
rescue NotImplementedError |
| 30 |
end |
| 31 |
end |
| 32 |
|
| 33 |
def test_rb_rational_raw |
| 34 |
rat = Bug::Rational.raw(1, 2) |
| 35 |
assert_equal(1, rat.numerator) |
| 36 |
assert_equal(2, rat.denominator) |
| 37 |
|
| 38 |
rat = Bug::Rational.raw(-1, 2) |
| 39 |
assert_equal(-1, rat.numerator) |
| 40 |
assert_equal(2, rat.denominator) |
| 41 |
|
| 42 |
rat = Bug::Rational.raw(1, -2) |
| 43 |
assert_equal(-1, rat.numerator) |
| 44 |
assert_equal(2, rat.denominator) |
| 45 |
|
| 46 |
assert_equal(1/2r, Bug::Rational.raw(1.0, 2.0)) |
| 47 |
|
| 48 |
assert_raise(TypeError) { Bug::Rational.raw("1", 2) } |
| 49 |
assert_raise(TypeError) { Bug::Rational.raw(1, "2") } |
| 50 |
|
| 51 |
class << (o = Object.new) |
| 52 |
def to_i; 42; end |
| 53 |
end |
| 54 |
|
| 55 |
assert_raise(TypeError) { Bug::Rational.raw(o, 2) } |
| 56 |
assert_raise(TypeError) { Bug::Rational.raw(1, o) } |
| 57 |
|
| 58 |
class << (o = Object.new) |
| 59 |
def to_int; 42; end |
| 60 |
end |
| 61 |
|
| 62 |
rat = Bug::Rational.raw(o, 2) |
| 63 |
assert_equal(42, rat.numerator) |
| 64 |
assert_equal(2, rat.denominator) |
| 65 |
|
| 66 |
rat = Bug::Rational.raw(2, o) |
| 67 |
assert_equal(2, rat.numerator) |
| 68 |
assert_equal(42, rat.denominator) |
| 69 |
end |
| 70 |
end |