rpl.rb/spec/language_operations_spec.rb

181 lines
6 KiB
Ruby
Raw Normal View History

2021-11-10 16:44:57 +01:00
# coding: utf-8
# frozen_string_literal: true
require 'test/unit'
require_relative '../lib/core'
2021-11-23 13:00:42 +01:00
class TesttLanguageOperations < Test::Unit::TestCase
def test_add
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }]
assert_equal [{ value: 3, type: :numeric, base: 10 }],
2021-11-10 16:44:57 +01:00
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 },
{ value: '"a"', type: :string }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"1a"', type: :string }],
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 },
{ value: "'a'", type: :name }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"1a"', type: :string }],
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: "'a'", type: :name },
{ value: 1, type: :numeric, base: 10 }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: "'a1'", type: :name }],
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: "'a'", type: :name },
{ value: '"b"', type: :string }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: "'ab'", type: :name }],
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: "'a'", type: :name },
{ value: "'b'", type: :name }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: "'ab'", type: :name }],
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: '"a"', type: :string },
{ value: '"b"', type: :string }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"ab"', type: :string }],
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: '"a"', type: :string },
{ value: "'b'", type: :name }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"ab"', type: :string }],
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.add [{ value: '"a"', type: :string },
{ value: 1, type: :numeric, base: 10 }]
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"a1"', type: :string }],
stack
end
2021-11-23 13:01:17 +01:00
def test_subtract
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.subtract [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }]
assert_equal [{ value: -1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.subtract [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }]
assert_equal [{ value: 1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_negate
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.negate [{ value: -1, type: :numeric, base: 10 }]
2021-11-23 13:01:17 +01:00
assert_equal [{ value: 1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.negate [{ value: 1, type: :numeric, base: 10 }]
2021-11-23 13:01:17 +01:00
assert_equal [{ value: -1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_multiply
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.multiply [{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }]
assert_equal [{ value: 12, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_divide
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.divide [{ value: 3.0, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }]
assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
2021-12-07 15:50:58 +01:00
# stack = Rpl::Lang::Core.divide [{ value: 3, type: :numeric, base: 10 },
# { value: 4, type: :numeric, base: 10 }]
# assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
# stack
end
def test_inverse
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.inverse [{ value: 4, type: :numeric, base: 10 }]
assert_equal [{ value: 0.25, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_power
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.power [{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }]
assert_equal [{ value: 81, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_sqrt
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.sqrt [{ value: 16, type: :numeric, base: 10 }]
assert_equal [{ value: 4, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_sq
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.sq [{ value: 4, type: :numeric, base: 10 }]
assert_equal [{ value: 16, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_abs
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.abs [{ value: -1, type: :numeric, base: 10 }]
2021-11-23 13:01:17 +01:00
assert_equal [{ value: 1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.abs [{ value: 1, type: :numeric, base: 10 }]
2021-11-23 13:01:17 +01:00
assert_equal [{ value: 1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
def test_dec
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.dec [{ value: 1, type: :numeric, base: 16 }]
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
2021-11-23 13:01:17 +01:00
end
def test_hex
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.hex [{ value: 1, type: :numeric, base: 10 }]
assert_equal [{ value: 1, type: :numeric, base: 16 }],
stack
2021-11-23 13:01:17 +01:00
end
def test_bin
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.bin [{ value: 1, type: :numeric, base: 10 }]
assert_equal [{ value: 1, type: :numeric, base: 2 }],
stack
2021-11-23 13:01:17 +01:00
end
def test_base
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.base [{ value: 1, type: :numeric, base: 10 },
{ value: 31, type: :numeric, base: 10 }]
assert_equal [{ value: 1, type: :numeric, base: 31 }],
stack
2021-11-23 13:01:17 +01:00
end
def test_sign
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.sign [{ value: -10, type: :numeric, base: 10 }]
2021-11-23 13:01:17 +01:00
assert_equal [{ value: -1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.sign [{ value: 10, type: :numeric, base: 10 }]
2021-11-23 13:01:17 +01:00
assert_equal [{ value: 1, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
2021-12-07 15:50:58 +01:00
stack = Rpl::Lang::Core.sign [{ value: 0, type: :numeric, base: 10 }]
2021-11-23 13:01:17 +01:00
assert_equal [{ value: 0, type: :numeric, base: 10 }],
2021-11-23 13:01:17 +01:00
stack
end
2021-11-10 16:44:57 +01:00
end