92 lines
3.6 KiB
Python
Executable file
92 lines
3.6 KiB
Python
Executable file
#!/usr/bin/python3
|
|
# copyright 2023 weber yann
|
|
#
|
|
# this file is part of rpnifs.
|
|
#
|
|
# geneifs is free software: you can redistribute it and/or modify
|
|
# it under the terms of the gnu general public license as published by
|
|
# the free software foundation, either version 3 of the license, or
|
|
# (at your option) any later version.
|
|
#
|
|
# geneifs is distributed in the hope that it will be useful,
|
|
# but without any warranty; without even the implied warranty of
|
|
# merchantability or fitness for a particular purpose. see the
|
|
# gnu general public license for more details.
|
|
#
|
|
# you should have received a copy of the gnu general public license
|
|
# along with geneifs. if not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
import random
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
try:
|
|
import pyrpn
|
|
except (ImportError, NameError) as e:
|
|
print("error importing pyrpn. try to run make.",
|
|
file=sys.stderr)
|
|
raise e
|
|
|
|
|
|
class TestTokens(unittest.TestCase):
|
|
""" Testing tokens module """
|
|
|
|
def test_argument(self):
|
|
""" Testing tokens.Argument type init """
|
|
for argno in range(1024):
|
|
arg = pyrpn.tokens.Argument(argno)
|
|
with self.subTest(argno=argno, arg=arg):
|
|
self.assertEqual(arg.argno, argno)
|
|
|
|
def test_value(self):
|
|
""" Testing tokens.Value type init """
|
|
for _ in range(1024):
|
|
v = random.randint(0, 0xFFFFFFFF)
|
|
val = pyrpn.tokens.Value(v)
|
|
with self.subTest(val=v, token_value=val):
|
|
self.assertEqual(val.value, v)
|
|
|
|
def test_op(self):
|
|
""" Testing tokens.Operand type init """
|
|
for opcode in range(pyrpn.tokens.Operand.opcode_max()+1):
|
|
operand = pyrpn.tokens.Operand(opcode)
|
|
with self.subTest(opcode=opcode, operand=operand):
|
|
self.assertEqual(opcode, operand.opcode)
|
|
|
|
op_fromstr = pyrpn.tokens.Operand(str(operand))
|
|
with self.subTest(from_str=op_fromstr, opcode=opcode):
|
|
self.assertEqual(opcode, op_fromstr.opcode)
|
|
|
|
def test_cmp(self):
|
|
""" Testing token comparison """
|
|
tests = {
|
|
'lt': ([('A0', 'A1'), ('0x10', '0x11'), ('-', '/'),
|
|
('-', 'A0'), ('-', '0x1'), ('A0', '0x1')],
|
|
[('A1', 'A0'), ('0x11', '0x10'), ('/', '-'),
|
|
('A0', '-'), ('0x1000', '+'), ('0x0', 'A1')]),
|
|
'eq': [[('A0', 'A0'), ('0x1', '0x1'), ('+', '+')],
|
|
[]]
|
|
}
|
|
tests['gt'] = (tests['lt'][1], tests['lt'][0])
|
|
tests['le'] = [tests['lt'][i] + tests['eq'][i] for i in range(2)]
|
|
tests['ge'] = [tests['gt'][i] + tests['eq'][i] for i in range(2)]
|
|
tests['eq'][1] = [('A0', '0x0'), ('A0', '+'), ('0x0', '+')]
|
|
|
|
assertions = {'lt': (self.assertLess, self.assertGreaterEqual),
|
|
'le': (self.assertLessEqual, self.assertGreater),
|
|
'eq': (self.assertEqual, self.assertNotEqual),
|
|
'gt': (self.assertGreater, self.assertLessEqual),
|
|
'ge': (self.assertGreaterEqual, self.assertLess)}
|
|
|
|
for comparison, (cmp_true, cmp_false) in tests.items():
|
|
assert_true, assert_false = assertions[comparison]
|
|
lst = [(assert_true, cmp_true), (assert_false, cmp_false)]
|
|
for assertion, cmplist in lst:
|
|
for test in cmplist:
|
|
a=pyrpn.tokens.Token.from_str(test[0])
|
|
b=pyrpn.tokens.Token.from_str(test[1])
|
|
with self.subTest(assertion=assertion, a=a, b=b):
|
|
assertion(a, b)
|
|
|
|
|