diff --git a/lib/rpl/types/complex.rb b/lib/rpl/types/complex.rb index cbe24b2..026fc7c 100644 --- a/lib/rpl/types/complex.rb +++ b/lib/rpl/types/complex.rb @@ -10,7 +10,9 @@ module Types raise RplTypeError unless self.class.can_parse?( value ) # we systematicalyl trim enclosing () - @value = Complex( value[1..-2] ) + value = value[1..-2] if value.is_a?( String ) && value[0] == '(' && value[-1] == ')' + + @value = Complex( value ) end def to_s @@ -18,17 +20,16 @@ module Types end def self.can_parse?( value ) - possibility = value[0] == '(' && value[-1] == ')' - - return possibility unless possibility + # we systematicalyl trim enclosing () + value = value[1..-2] if value.is_a?( String ) && value[0] == '(' && value[-1] == ')' begin - Complex( value[1..-2] ) + Complex( value ) rescue ArgumentError - possibility = false + return false end - possibility + true end def ==( other ) diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index ccbbe93..83dad1e 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -39,6 +39,13 @@ class TestParser < MiniTest::Test assert_equal BigDecimal( 3 ), result.first.value end + def test_complex + result = Parser.parse( '(1+2i)' ) + assert_equal 1, result.size + assert_equal RplComplex, result.first.class + assert_equal Complex( 1, 2 ), result.first.value + end + def test_word result = Parser.parse( 'dup' ) assert_equal 1, result.size diff --git a/spec/words_operations-complexes_spec.rb b/spec/words_operations-complexes_spec.rb new file mode 100644 index 0000000..264cc4d --- /dev/null +++ b/spec/words_operations-complexes_spec.rb @@ -0,0 +1,58 @@ +# coding: utf-8 +# frozen_string_literal: true + +require 'minitest/autorun' + +require 'rpl' + +class TesttLanguageOperations < MiniTest::Test + include Types + + def test_re + interpreter = Rpl.new + interpreter.run '(2+3i) re' + assert_equal [Types.new_object( RplNumeric, 2 )], + interpreter.stack + end + + def test_im + interpreter = Rpl.new + interpreter.run '(2+3i) im' + assert_equal [Types.new_object( RplNumeric, 3 )], + interpreter.stack + + interpreter = Rpl.new + interpreter.run '(2-4i) im' + assert_equal [Types.new_object( RplNumeric, -4 )], + interpreter.stack + end + + def test_conj + interpreter = Rpl.new + interpreter.run '(2+3i) conj' + assert_equal [Types.new_object( RplComplex, Complex( 2, -3 ) )], + interpreter.stack + end + + def test_arg + interpreter = Rpl.new + interpreter.run '(2+3i) arg' + assert_equal [Types.new_object( RplNumeric, 0.982793723247329 )], + interpreter.stack + end + + def test_c2r + interpreter = Rpl.new + interpreter.run '(2+3i) c→r' + assert_equal [Types.new_object( RplNumeric, 2 ), + Types.new_object( RplNumeric, 3 )], + interpreter.stack + end + + def test_r2c + interpreter = Rpl.new + interpreter.run '2 -3 r→c' + assert_equal [Types.new_object( RplComplex, Complex( 2, -3 ) )], + interpreter.stack + end +end diff --git a/spec/words_operations_spec.rb b/spec/words_operations-reals-complexes_spec.rb similarity index 82% rename from spec/words_operations_spec.rb rename to spec/words_operations-reals-complexes_spec.rb index 153d935..7067102 100644 --- a/spec/words_operations_spec.rb +++ b/spec/words_operations-reals-complexes_spec.rb @@ -338,102 +338,4 @@ class TesttLanguageOperations < MiniTest::Test assert_equal [Types.new_object( RplNumeric, 0 )], interpreter.stack end - - def test_percent - interpreter = Rpl.new - interpreter.run '2 33 %' - assert_equal [Types.new_object( RplNumeric, 0.66 )], - interpreter.stack - end - - def test_inverse_percent - interpreter = Rpl.new - interpreter.run '2 0.66 %CH' - assert_equal [Types.new_object( RplNumeric, 33 )], - interpreter.stack - end - - def test_mod - interpreter = Rpl.new - interpreter.run '9 4 mod' - assert_equal [Types.new_object( RplNumeric, 1 )], - interpreter.stack - end - - def test_fact - interpreter = Rpl.new - interpreter.run '5 !' - assert_equal [Types.new_object( RplNumeric, 24 )], - interpreter.stack - end - - def test_floor - interpreter = Rpl.new - interpreter.run '5.23 floor' - assert_equal [Types.new_object( RplNumeric, 5 )], - interpreter.stack - end - - def test_ceil - interpreter = Rpl.new - interpreter.run '5.23 ceil' - assert_equal [Types.new_object( RplNumeric, 6 )], - interpreter.stack - end - - def test_min - interpreter = Rpl.new - interpreter.run '1 2 min' - assert_equal [Types.new_object( RplNumeric, 1 )], - interpreter.stack - - interpreter = Rpl.new - interpreter.run '2 1 min' - assert_equal [Types.new_object( RplNumeric, 1 )], - interpreter.stack - end - - def test_max - interpreter = Rpl.new - interpreter.run '1 2 max' - assert_equal [Types.new_object( RplNumeric, 2 )], - interpreter.stack - - interpreter = Rpl.new - interpreter.run '2 1 max' - assert_equal [Types.new_object( RplNumeric, 2 )], - interpreter.stack - end - - def test_ip - interpreter = Rpl.new - interpreter.run '3.14 ip' - assert_equal [Types.new_object( RplNumeric, 3 )], - interpreter.stack - end - - def test_fp - interpreter = Rpl.new - interpreter.run '3.14 fp' - assert_equal [Types.new_object( RplNumeric, 0.14 )], - interpreter.stack - end - - def test_mant - interpreter = Rpl.new - interpreter.run '123.456 mant -123.456 mant 0 mant' - assert_equal [Types.new_object( RplNumeric, 0.123456 ), - Types.new_object( RplNumeric, 0.123456 ), - Types.new_object( RplNumeric, 0 )], - interpreter.stack - end - - def test_xpon - interpreter = Rpl.new - interpreter.run '123.456 xpon -123.456 xpon 0 xpon' - assert_equal [Types.new_object( RplNumeric, 3 ), - Types.new_object( RplNumeric, 3 ), - Types.new_object( RplNumeric, 0 )], - interpreter.stack - end end diff --git a/spec/words_operations-reals_spec.rb b/spec/words_operations-reals_spec.rb new file mode 100644 index 0000000..b2d6e9f --- /dev/null +++ b/spec/words_operations-reals_spec.rb @@ -0,0 +1,108 @@ +# coding: utf-8 +# frozen_string_literal: true + +require 'minitest/autorun' + +require 'rpl' + +class TesttLanguageOperations < MiniTest::Test + include Types + + def test_percent + interpreter = Rpl.new + interpreter.run '2 33 %' + assert_equal [Types.new_object( RplNumeric, 0.66 )], + interpreter.stack + end + + def test_inverse_percent + interpreter = Rpl.new + interpreter.run '2 0.66 %CH' + assert_equal [Types.new_object( RplNumeric, 33 )], + interpreter.stack + end + + def test_mod + interpreter = Rpl.new + interpreter.run '9 4 mod' + assert_equal [Types.new_object( RplNumeric, 1 )], + interpreter.stack + end + + def test_fact + interpreter = Rpl.new + interpreter.run '5 !' + assert_equal [Types.new_object( RplNumeric, 24 )], + interpreter.stack + end + + def test_floor + interpreter = Rpl.new + interpreter.run '5.23 floor' + assert_equal [Types.new_object( RplNumeric, 5 )], + interpreter.stack + end + + def test_ceil + interpreter = Rpl.new + interpreter.run '5.23 ceil' + assert_equal [Types.new_object( RplNumeric, 6 )], + interpreter.stack + end + + def test_min + interpreter = Rpl.new + interpreter.run '1 2 min' + assert_equal [Types.new_object( RplNumeric, 1 )], + interpreter.stack + + interpreter = Rpl.new + interpreter.run '2 1 min' + assert_equal [Types.new_object( RplNumeric, 1 )], + interpreter.stack + end + + def test_max + interpreter = Rpl.new + interpreter.run '1 2 max' + assert_equal [Types.new_object( RplNumeric, 2 )], + interpreter.stack + + interpreter = Rpl.new + interpreter.run '2 1 max' + assert_equal [Types.new_object( RplNumeric, 2 )], + interpreter.stack + end + + def test_ip + interpreter = Rpl.new + interpreter.run '3.14 ip' + assert_equal [Types.new_object( RplNumeric, 3 )], + interpreter.stack + end + + def test_fp + interpreter = Rpl.new + interpreter.run '3.14 fp' + assert_equal [Types.new_object( RplNumeric, 0.14 )], + interpreter.stack + end + + def test_mant + interpreter = Rpl.new + interpreter.run '123.456 mant -123.456 mant 0 mant' + assert_equal [Types.new_object( RplNumeric, 0.123456 ), + Types.new_object( RplNumeric, 0.123456 ), + Types.new_object( RplNumeric, 0 )], + interpreter.stack + end + + def test_xpon + interpreter = Rpl.new + interpreter.run '123.456 xpon -123.456 xpon 0 xpon' + assert_equal [Types.new_object( RplNumeric, 3 ), + Types.new_object( RplNumeric, 3 ), + Types.new_object( RplNumeric, 0 )], + interpreter.stack + end +end