From 0d2ec381658d305472511570a02d093506c50da6 Mon Sep 17 00:00:00 2001 From: SamW Date: Thu, 18 Jun 2026 13:20:28 -0700 Subject: [PATCH 1/3] first commit, cleanup ratoinal tests --- test/stdlib/Rational_test.rb | 198 +++++++++++------------------------ test/stdlib/test_helper.rb | 37 +++++++ 2 files changed, 100 insertions(+), 135 deletions(-) diff --git a/test/stdlib/Rational_test.rb b/test/stdlib/Rational_test.rb index 6dba75f70c..88c0091d9d 100644 --- a/test/stdlib/Rational_test.rb +++ b/test/stdlib/Rational_test.rb @@ -1,191 +1,119 @@ -require_relative "test_helper" +require_relative 'test_helper' -class RationalTest < StdlibTest - target Rational - - def test_calc - 10r % 10 - 10r % 1.3 - 10r % 2r - - 10r * 10 - 10r * 1.2 - 10r * 2r - 10r * Complex.rect(1,2) - - 3r ** 2 - 3r ** 2.1 - 3r ** 2r - 3r ** Complex.rect(1,2) +class RationalInstanceTest < Test::Unit::TestCase + include TestHelper - 10r + 10 - 10r + 1.2 - 10r + 2r - 10r + Complex.rect(1,2) + testing 'Rational' - 10r - 10 - 10r - 1.2 - 10r - 2r - 10r - Complex.rect(1,2) + def test_op_mul + omit + end - 10r / 10 - 10r / 1.2 - 10r / 2r - 10r / Complex.rect(1,2) + def test_op_pow + omit end - def test_compare - 10r <=> 1 - 10r < 1 - 10r <= 3r - 10r > 3.1 - 10r >= Complex.rect(1,0) + def test_op_add + omit end - def test_eq - a = 31r + def test_op_sub + omit + end - a == 1 - a == Object.new + def test_op_uneg + omit end - def test_abs - a = 31r + def test_op_div + omit + end - a.abs - a.abs2 + def test_op_cmp + omit end - def test_angle - a = 31r + def test_op_eq + omit + end - a.angle - a.arg - -a.phase + def test_abs + omit end def test_ceil - a = 312r/5 - - a.ceil - a.ceil(3) - # a.ceil(ToInt.new) # does not accept to_int + omit end - def test_denominator - a = 133r - - a.denominator + def test_coerce + omit end - def test_div - a = 123r - - a.div(3) - a.div(3.1) - a.div(12r) - - a.divmod(3) - a.divmod(3.1) - a.divmod(1r/5) + def test_denominator + omit end def test_fdiv - a = 3r/2 - - a.fdiv(3) - a.fdiv(3.1) - a.fdiv(1r/3) + omit end def test_floor - a = 3r/2 + omit + end - a.floor() - a.floor(-1) - # a.floor(ToInt.new) # No to_int support + def test_hash + omit end - def test_modulo - a = 3r/2 + def test_inspect + omit + end - a.modulo(2) - a.modulo(1.1) - a.modulo(a) + def test_magnitude + omit end - def test_numerator - a = 3r/2 + def test_negative? + omit + end - a.numerator + def test_numerator + omit end - def test_polar - 31r.polar - (-31r/2).polar + def test_positive? + omit end def test_quo - a = 1/11r - - a.quo(3) - a.quo(1.3) - a.quo(1r/3) - a.quo(Complex.rect(1,2)) + omit end def test_rationalize - a = 14r - - a.rationalize - a.rationalize(3.11) - end - - def test_reminder - a = 14r - - a.remainder(3) - a.remainder(3.1) - a.remainder(3r/5) + omit end def test_round - a = 1r/3 - - a.round(half: :up) - a.round(2, half: :up) - # a.round(ToInt.new(-2), half: :up) # to_int not supported + omit end - def test_step - a = 1r/3 - - a.step { break } - a.step(1, 2) { } - a.step(by: 3, to: 100) { } + def test_to_f + omit end - def test_truncate - a = 1r/3 + def test_to_i + omit + end - a.truncate - a.truncate(1) + def test_to_r + omit end -end -class RationalInstanceTest < Test::Unit::TestCase - include TestHelper + def test_to_s + omit + end - testing "::Rational" - - def test_multiply - assert_send_type "(Integer) -> Rational", - 1r, :*, 1 - assert_send_type "(Rational) -> Rational", - 1r, :*, 1r - assert_send_type "(Float) -> Float", - 1r, :*, 3.1 - assert_send_type "(Complex) -> Complex", - 1, :*, Complex.rect(1,2) + def test_truncate + omit end end diff --git a/test/stdlib/test_helper.rb b/test/stdlib/test_helper.rb index e9848e69c7..30eab1952b 100644 --- a/test/stdlib/test_helper.rb +++ b/test/stdlib/test_helper.rb @@ -80,6 +80,43 @@ def with_timeout(seconds: 1, nanoseconds: 0) end end +class Coercable < RBS::UnitTest::Convertibles::BlankSlate + class OpReturn < ::RBS::UnitTest::Convertibles::BlankSlate + end + + class CoerceOther < ::RBS::UnitTest::Convertibles::BlankSlate + attr_reader :__value__ + def initialize(value) = @__value__ = value + end + + class CoerceSelf < ::RBS::UnitTest::Convertibles::BlankSlate + def initialize(method, &block) + ::Kernel.instance_method(:define_singleton_method).bind_call(self, method, &block) + end + end + + def self.for_op(method, result: noresult = true, &block) + if block_given? + unless noresult + raise ArgumentError, "cannot provide both a block and a result" + end + else + block = proc { |other| noresult ? OpReturn.new : result } + end + + new(CoerceSelf.new(method, &block)) { |x| CoerceOther.new(x) } + end + + def initialize(self_ret, &converter) + @self_ret = self_ret + @converter = converter || ->(x) { x } + end + + def coerce(rhs) + [@self_ret, @converter.(rhs)] + end +end + class Writer attr_reader :buffer From 26524ee7bf88eb7fd667f10ff0c1c76b5e3ce067 Mon Sep 17 00:00:00 2001 From: SamW Date: Thu, 18 Jun 2026 20:04:27 -0700 Subject: [PATCH 2/3] next round of rational --- core/rational.rbs | 8 +++--- test/stdlib/Rational_test.rb | 52 ++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/core/rational.rbs b/core/rational.rbs index 5abca095ec..386ee7d2a9 100644 --- a/core/rational.rbs +++ b/core/rational.rbs @@ -135,7 +135,7 @@ class Rational < Numeric # -(1/3r) # => (-1/3) # -(-1/3r) # => (1/3) # - def -@: () -> Rational + def -@: () -> instance #