basic tests of complexes' operations

This commit is contained in:
Gwenhael Le Moine 2022-08-31 13:05:26 +02:00
parent d3d24cb145
commit 6562a4297e
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
5 changed files with 181 additions and 105 deletions

View file

@ -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 )

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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