2021-11-24 16:34:13 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-17 15:09:29 +01:00
|
|
|
require 'minitest/autorun'
|
2021-11-24 16:34:13 +01:00
|
|
|
|
2022-02-15 17:06:19 +01:00
|
|
|
require 'rpl'
|
2021-11-24 16:34:13 +01:00
|
|
|
|
2022-02-17 15:09:29 +01:00
|
|
|
class TestLanguageBranch < MiniTest::Test
|
2022-02-26 18:53:39 +01:00
|
|
|
include Types
|
|
|
|
|
2021-12-09 16:31:29 +01:00
|
|
|
def test_loop
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-10-12 17:04:50 +02:00
|
|
|
interpreter.run! '« "hello no." swap + » 11 16 loop'
|
2021-12-09 16:31:29 +01:00
|
|
|
|
2022-02-28 11:40:47 +01:00
|
|
|
assert_equal [Types.new_object( RplString, '"hello no.11"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.12"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.13"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.14"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.15"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.16"' )],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-12-09 16:31:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_times
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-10-12 17:04:50 +02:00
|
|
|
interpreter.run! '« "hello no." swap + » 5 times'
|
2021-12-09 16:31:29 +01:00
|
|
|
|
2022-02-28 11:40:47 +01:00
|
|
|
assert_equal [Types.new_object( RplString, '"hello no.0"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.1"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.2"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.3"' ),
|
|
|
|
Types.new_object( RplString, '"hello no.4"' )],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-12-09 16:31:29 +01:00
|
|
|
end
|
|
|
|
|
2021-11-24 16:34:13 +01:00
|
|
|
def test_ifte
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-10-12 17:04:50 +02:00
|
|
|
interpreter.run! 'true « 2 3 + » « 2 3 - » ifte'
|
2021-11-24 16:34:13 +01:00
|
|
|
|
2022-02-28 11:40:47 +01:00
|
|
|
assert_equal [Types.new_object( RplNumeric, 5 )],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-11-24 16:34:13 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-10-12 17:04:50 +02:00
|
|
|
interpreter.run! 'false « 2 3 + » « 2 3 - » ifte'
|
2021-11-24 16:34:13 +01:00
|
|
|
|
2022-02-28 11:40:47 +01:00
|
|
|
assert_equal [Types.new_object( RplNumeric, -1 )],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-11-24 16:34:13 +01:00
|
|
|
end
|
2021-12-08 13:14:09 +01:00
|
|
|
|
|
|
|
def test_ift
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-10-12 17:04:50 +02:00
|
|
|
interpreter.run! 'true « 2 3 + » ift'
|
2021-12-08 13:14:09 +01:00
|
|
|
|
2022-02-28 11:40:47 +01:00
|
|
|
assert_equal [Types.new_object( RplNumeric, 5 )],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-12-08 13:14:09 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-10-12 17:04:50 +02:00
|
|
|
interpreter.run! 'false « 2 3 + » ift'
|
2021-12-08 13:14:09 +01:00
|
|
|
|
|
|
|
assert_equal [],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-12-08 13:14:09 +01:00
|
|
|
end
|
2021-11-24 16:34:13 +01:00
|
|
|
end
|