PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/-ext-/rational/test_rat.rb

http://github.com/ruby/ruby
Ruby | 70 lines | 55 code | 14 blank | 1 comment | 0 complexity | 8e0a075862e2568f206c726a99f09d22 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-/rational"
  4. class TestRational < Test::Unit::TestCase
  5. class TestGCD < Test::Unit::TestCase
  6. def test_gcd_normal
  7. x = 2*2*3*3*3
  8. y = 2*2*2*3*3
  9. gcd = 2*2*3*3
  10. assert_equal(gcd, x.gcd_normal(y))
  11. end
  12. def test_gcd_gmp
  13. x = 2*2*3*3*3
  14. y = 2*2*2*3*3
  15. gcd = 2*2*3*3
  16. assert_equal(gcd, x.gcd_gmp(y))
  17. rescue NotImplementedError
  18. end
  19. def test_gcd_gmp_brute_force
  20. -13.upto(13) {|x|
  21. -13.upto(13) {|y|
  22. assert_equal(x.gcd_normal(y), x.gcd_gmp(y))
  23. }
  24. }
  25. rescue NotImplementedError
  26. end
  27. end
  28. def test_rb_rational_raw
  29. rat = Rational.raw(1, 2)
  30. assert_equal(1, rat.numerator)
  31. assert_equal(2, rat.denominator)
  32. rat = Rational.raw(-1, 2)
  33. assert_equal(-1, rat.numerator)
  34. assert_equal(2, rat.denominator)
  35. rat = Rational.raw(1, -2)
  36. assert_equal(-1, rat.numerator)
  37. assert_equal(2, rat.denominator)
  38. assert_equal(1/2r, Rational.raw(1.0, 2.0))
  39. assert_raise(TypeError) { Rational.raw("1", 2) }
  40. assert_raise(TypeError) { Rational.raw(1, "2") }
  41. class << (o = Object.new)
  42. def to_i; 42; end
  43. end
  44. assert_raise(TypeError) { Rational.raw(o, 2) }
  45. assert_raise(TypeError) { Rational.raw(1, o) }
  46. class << (o = Object.new)
  47. def to_int; 42; end
  48. end
  49. rat = Rational.raw(o, 2)
  50. assert_equal(42, rat.numerator)
  51. assert_equal(2, rat.denominator)
  52. rat = Rational.raw(2, o)
  53. assert_equal(2, rat.numerator)
  54. assert_equal(42, rat.denominator)
  55. end
  56. end