From 01a8edea3530f0073f0d046c68853bfa17c058b5 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 26 Mar 2021 17:20:25 +0900 Subject: complex.rb: add test for arithmetic operators. [ci skip] Tests for (`Float` or `Integer`) `op` `Complex`. Also added test dependency to `mruby-rational` since `int_div` definition relies on `Rational` when `MRB_USE_RATIONAL` is defined. --- mrbgems/mruby-complex/mrbgem.rake | 1 + mrbgems/mruby-complex/test/complex.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mrbgems/mruby-complex/mrbgem.rake b/mrbgems/mruby-complex/mrbgem.rake index cd81ecd02..5e88c2337 100644 --- a/mrbgems/mruby-complex/mrbgem.rake +++ b/mrbgems/mruby-complex/mrbgem.rake @@ -4,4 +4,5 @@ MRuby::Gem::Specification.new('mruby-complex') do |spec| spec.summary = 'Complex class' spec.build.defines << "MRB_USE_COMPLEX" spec.add_dependency 'mruby-math', core: 'mruby-math' + spec.add_test_dependency('mruby-rational') end diff --git a/mrbgems/mruby-complex/test/complex.rb b/mrbgems/mruby-complex/test/complex.rb index 8f9634048..85ee6e998 100644 --- a/mrbgems/mruby-complex/test/complex.rb +++ b/mrbgems/mruby-complex/test/complex.rb @@ -31,6 +31,8 @@ assert 'Complex#*' do assert_complex Complex(-2, 9) * Complex(-9, 2), (0 - 85i) assert_complex Complex(9, 8) * 4, (36 + 32i) assert_complex Complex(20, 9) * 9.8, (196.0 + 88.2i) + assert_complex 4 * Complex(9, 8), (36 + 32i) + assert_complex 9.8 * Complex(20, 9), (196.0 + 88.2i) end assert 'Complex#+' do @@ -39,6 +41,8 @@ assert 'Complex#+' do assert_complex Complex(-2, 9) + Complex(-9, 2), (-11 + 11i) assert_complex Complex(9, 8) + 4 , (13 + 8i) assert_complex Complex(20, 9) + 9.8 , (29.8 + 9i) + assert_complex 4 + Complex(9, 8) , (13 + 8i) + assert_complex 9.8 + Complex(20, 9) , (29.8 + 9i) end assert 'Complex#-' do @@ -47,10 +51,12 @@ assert 'Complex#-' do assert_complex Complex(-2, 9) - Complex(-9, 2), (7 + 7i) assert_complex Complex(9, 8) - 4 , (5 + 8i) assert_complex Complex(20, 9) - 9.8 , (10.2 + 9i) + assert_complex 4 - Complex(9, 8) , (-5 - 8i) + assert_complex 10.5 - Complex(20, 9) , (-9.5 - 9i) end assert 'Complex#-@' do - assert_complex(-Complex(1, 2), (-1 - 2i)) + assert_complex((-1 - 2i), -Complex(1, 2)) end assert 'Complex#/' do @@ -59,6 +65,8 @@ assert 'Complex#/' do assert_complex Complex(-2, 9) / Complex(-9, 2), ((36.0 / 85) - (77i / 85)) assert_complex Complex(9, 8) / 4 , ((9.0 / 4) + 2i) assert_complex Complex(20, 9) / 9.8 , (2.0408163265306123 + 0.9183673469387754i) + assert_complex 4 / Complex(9, 8) , (0.2482758620689655 - 0.2206896551724138i) + assert_complex 9.8 / Complex(20, 9) , (0.4074844074844075 - 0.1833679833679834i) if 1e39.infinite? then # MRB_USE_FLOAT32 in effect ten = 1e21 @@ -74,6 +82,8 @@ assert 'Complex#==' do assert_true Complex(2, 3) == Complex(2, 3) assert_true Complex(5) == 5 assert_true Complex(0) == 0.0 + assert_true 5 == Complex(5) + assert_true 0.0 == Complex(0) end assert 'Complex#abs' do -- cgit v1.2.3