rpl.rb/spec/language_operations_spec.rb

318 lines
9 KiB
Ruby
Raw Normal View History

2021-11-10 16:44:57 +01:00
# coding: utf-8
# frozen_string_literal: true
2022-02-17 15:09:29 +01:00
require 'minitest/autorun'
2021-11-10 16:44:57 +01:00
2022-02-15 17:06:19 +01:00
require 'rpl'
2021-11-10 16:44:57 +01:00
2022-02-17 15:09:29 +01:00
class TesttLanguageOperations < MiniTest::Test
2021-11-23 13:00:42 +01:00
def test_add
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 2 +'
assert_equal [{ value: 3, type: :numeric, base: 10 }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 "a" +'
assert_equal [{ value: '1a', type: :string }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 \'a\' +'
assert_equal [{ value: '1a', type: :string }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 dup dup →list +'
2021-12-16 16:34:59 +01:00
assert_equal [{ value: [{ value: 1, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
type: :list }],
interpreter.stack
2021-12-16 16:34:59 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"a" "b" +'
assert_equal [{ value: 'ab', type: :string }],
interpreter.stack
2021-12-16 16:34:59 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"a" \'b\' +'
assert_equal [{ value: 'ab', type: :string }],
interpreter.stack
2021-12-16 16:34:59 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"a" 1 +'
assert_equal [{ value: 'a1', type: :string }],
interpreter.stack
2021-12-16 16:34:59 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"a" 1 dup →list +'
assert_equal [{ value: [{ value: 'a', type: :string },
2021-12-16 16:34:59 +01:00
{ value: 1, type: :numeric, base: 10 }],
type: :list }],
interpreter.stack
2021-12-16 16:34:59 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '\'a\' 1 +'
assert_equal [{ value: 'a1', type: :name }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '\'a\' "b" +'
assert_equal [{ value: 'ab', type: :string }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '\'a\' \'b\' +'
assert_equal [{ value: 'ab', type: :name }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '\'a\' 1 dup →list +'
assert_equal [{ value: [{ value: 'a', type: :name },
2021-12-16 16:34:59 +01:00
{ value: 1, type: :numeric, base: 10 }],
type: :list }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 a "test" 3 →list dup rev +'
2021-12-16 16:34:59 +01:00
assert_equal [{ type: :list,
value: [{ value: 1, type: :numeric, base: 10 },
{ type: :name, value: 'a' },
{ value: 'test', type: :string },
{ value: 'test', type: :string },
{ type: :name, value: 'a' },
2021-12-16 16:34:59 +01:00
{ value: 1, type: :numeric, base: 10 }] }],
interpreter.stack
2021-11-10 16:44:57 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 a "test" 3 →list 9 +'
2021-12-16 16:34:59 +01:00
assert_equal [{ type: :list,
value: [{ value: 1, type: :numeric, base: 10 },
{ type: :name, value: 'a' },
{ value: 'test', type: :string },
2021-12-16 16:34:59 +01:00
{ value: 9, type: :numeric, base: 10 }] }],
interpreter.stack
2021-11-10 16:44:57 +01:00
end
2021-11-23 13:01:17 +01:00
def test_subtract
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 2 -'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 1 -'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_negate
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '-1 chs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 chs'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_multiply
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '3 4 *'
assert_equal [{ value: 12, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_divide
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '3.0 4 /'
assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_inverse
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '4 inv'
assert_equal [{ value: 0.25, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_power
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '3 4 ^'
assert_equal [{ value: 81, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_sqrt
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '16 √'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_sq
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '4 sq'
assert_equal [{ value: 16, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_abs
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '-1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_dec
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '0x1 dec'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_hex
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 hex'
assert_equal [{ value: 1, type: :numeric, base: 16 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_bin
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 bin'
assert_equal [{ value: 1, type: :numeric, base: 2 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_base
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 31 base'
assert_equal [{ value: 1, type: :numeric, base: 31 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
def test_sign
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '-10 sign'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '10 sign'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '0 sign'
assert_equal [{ value: 0, type: :numeric, base: 10 }],
interpreter.stack
2021-11-23 13:01:17 +01:00
end
2021-12-02 15:33:22 +01:00
def test_percent
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 33 %'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 0.66, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_inverse_percent
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 0.66 %CH'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 33, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_mod
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '9 4 mod'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_fact
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '5 !'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 24, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_floor
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '5.23 floor'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 5, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_ceil
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '5.23 ceil'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 6, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_min
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 2 min'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 1 min'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 1, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_max
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 2 max'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 2, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 1 max'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 2, type: :numeric, base: 10 }],
interpreter.stack
2021-12-02 15:33:22 +01:00
end
def test_ip
interpreter = Rpl.new
interpreter.run '3.14 ip'
assert_equal [{ value: 3, type: :numeric, base: 10 }],
interpreter.stack
end
def test_fp
interpreter = Rpl.new
interpreter.run '3.14 fp'
assert_equal [{ value: 0.14, type: :numeric, base: 10 }],
interpreter.stack
end
2022-02-15 18:48:19 +01:00
def test_mant
interpreter = Rpl.new
interpreter.run '123.456 mant -123.456 mant 0 mant'
2022-02-22 16:38:42 +01:00
assert_equal [{ type: :numeric, base: 10, value: BigDecimal( 0.123456, interpreter.precision ) },
{ type: :numeric, base: 10, value: BigDecimal( 0.123456, interpreter.precision ) },
{ type: :numeric, base: 10, value: BigDecimal( 0 ) }],
2022-02-15 18:48:19 +01:00
interpreter.stack
end
def test_xpon
interpreter = Rpl.new
interpreter.run '123.456 xpon -123.456 xpon 0 xpon'
assert_equal [{ value: 3, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 0, type: :numeric, base: 10 }],
interpreter.stack
end
2021-11-10 16:44:57 +01:00
end