source/projects/shoggoth/test/python/ichor/test_interpreter.py

75 lines
1.7 KiB
Python
Raw Normal View History

2022-03-29 07:29:18 +00:00
"""
Tests coverign the VM interpreter
"""
2022-05-31 15:41:10 +00:00
from .fixtures import * # noqa
2022-08-09 15:39:33 +00:00
from ichor.bootstrap import (
2022-07-16 01:37:34 +00:00
FALSE,
TRUE,
)
2022-08-09 15:39:33 +00:00
from ichor import isa
from ichor.interpreter import InterpreterError
2022-06-01 05:17:32 +00:00
import pytest
2022-03-29 07:29:18 +00:00
def test_return(vm):
2022-08-09 15:39:33 +00:00
with pytest.raises(InterpreterError):
vm.run([isa.RETURN()], stack=[])
assert vm.run([isa.RETURN()], stack=[FALSE, TRUE]) == [FALSE]
assert vm.run([isa.DROP(1), isa.RETURN()], stack=[TRUE, FALSE]) == [TRUE]
2022-03-29 07:29:18 +00:00
def test_dup(vm):
2022-08-09 15:39:33 +00:00
assert vm.run([isa.DUP(1), isa.BREAK()], stack=[FALSE, TRUE]) == [FALSE, TRUE, TRUE]
assert vm.run([isa.DUP(2), isa.BREAK()], stack=[FALSE, TRUE]) == [FALSE, TRUE, FALSE, TRUE]
2022-03-29 07:29:18 +00:00
def test_rot(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([
2022-07-16 01:33:32 +00:00
isa.ROT(2),
2022-08-09 15:39:33 +00:00
isa.BREAK()
2022-06-28 04:35:21 +00:00
], stack=[FALSE, TRUE]) == [TRUE, FALSE]
2022-03-29 07:29:18 +00:00
assert vm.run([
2022-07-16 01:33:32 +00:00
isa.ROT(2),
2022-08-09 15:39:33 +00:00
isa.BREAK()
2022-07-16 01:33:32 +00:00
], stack=[TRUE, TRUE, TRUE, FALSE, TRUE]) == [TRUE, TRUE, TRUE, TRUE, FALSE]
assert vm.run([
isa.ROT(3),
2022-08-09 15:39:33 +00:00
isa.BREAK()
2022-06-28 04:35:21 +00:00
], stack=[FALSE, TRUE, FALSE]) == [FALSE, FALSE, TRUE]
2022-03-29 07:29:18 +00:00
def test_drop(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([
2022-07-16 01:33:32 +00:00
isa.DROP(1),
2022-08-09 15:39:33 +00:00
isa.BREAK()
2022-06-28 04:35:21 +00:00
], stack=[TRUE, FALSE]) == [TRUE]
2022-03-29 07:29:18 +00:00
def test_dup_too_many(vm):
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DUP(1)])
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DUP(2)], stack=[FALSE])
def test_rot_too_many(vm):
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.ROT(1)])
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.ROT(2)], stack=[FALSE])
def test_drop_too_many(vm):
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DROP(1)])
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DROP(2)], stack=[FALSE])