Files
AuDaGe/tests/test_iec.py
2026-09-22 13:09:45 -05:00

119 lines
4.2 KiB
Python

"""
AuDaGe IEC tests
name: test_iec.py
by: Josh Lucas
date: 2026-09-14
desc: Unit tests for the AuDaGe IEC module.
"""
import unittest
import warnings
from math import ceil, floor
from AuDaGe.iec import *
class TestIEC(unittest.TestCase):
"""Module-level tests."""
def test_get_data_length(self):
data_types = [
("BOOL", DataUnit.BIT, 1),
("CHAR", DataUnit.BIT, 8),
("INT", DataUnit.BIT, 16),
("BOOL", DataUnit.WORD, 1),
("ARRAY [1..10, 0..7] OF INT", DataUnit.WORD, 80),
("STRING[10]", DataUnit.WORD, 5),
("UNKNOWN_UDT", DataUnit.WORD, -1),
]
for data_type in data_types:
self.assertEqual(
get_data_length(data_type[0], data_type[1]), data_type[2], data_type[0]
)
def test_iec_definitions(self):
definitions = iec_definitions()
self.assertTrue(definitions.is_valid())
self.assertEqual(definitions.shape, (len(IECTypes.__members__), 7))
class TestIECArray(unittest.TestCase):
def test_array_length(self):
good_array_specs = [
("ARRAY [0..3] OF INT", DataUnit.WORD, ceil, 4),
("ARRAY [0..0] OF REAL", DataUnit.WORD, ceil, 2),
("ARRAY [0..3, 0..3] OF INT", DataUnit.WORD, ceil, 16),
("array[15..20] of bool", DataUnit.BIT, ceil, 6),
("ARRAY [0..1, 0..2, 0..3, 0..4] OF INT", DataUnit.WORD, ceil, 120),
]
warning_array_specs = [
("array[15..20] of bool", DataUnit.WORD, ceil, 1),
("array[15..20] of bool", DataUnit.WORD, floor, 0),
]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
for spec in good_array_specs + warning_array_specs:
self.assertEqual(
IECArray(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0]
)
for spec in warning_array_specs:
self.assertWarns(
RuntimeWarning, IECArray(spec[0]).get_length, spec[1], spec[2]
)
def test_array_spec_is_valid(self):
valid_array_specs = (
"ARRAY [0..3] OF INT", # Uppercase, space, two dots
"array[12...1234] of bool", # Lowercase, no space, three dots
" \tarray[12..1234] of bool \t", # Superfluous whitespace
"arr[0..0]int", # Minimal unambiguous specification, bounds equal
)
invalid_array_specs = (
"fleebleflorp", # Nonsense
"arry [0..5] of char", # Misspelling
"array [..6] of word", # Missing index
"array [1..2 of int", # Missing bracket
"array [1..7] of float", # Invalid data type
"array [5..1] of dint", # Lower bound greater than upper bound
)
for spec in valid_array_specs:
self.assertTrue(IECArray(spec).is_valid(), spec)
for spec in invalid_array_specs:
self.assertRaises(ValueError, IECArray, spec)
class TestIECString(unittest.TestCase):
def test_string_length(self):
good_string_specs = [
("STRING[20]", DataUnit.WORD, ceil, 10),
("string [80]\t", DataUnit.BYTE, ceil, 80),
(" string [10] ", DataUnit.BIT, ceil, 80),
]
warning_string_specs = [
("\tSTRING[15]", DataUnit.WORD, ceil, 8),
("STRING [15]", DataUnit.WORD, floor, 7),
]
error_string_specs = [
("STRNG[20]", DataUnit.WORD, ceil, 10),
("STRING10]", DataUnit.WORD, ceil, 5),
("string[]", DataUnit.BYTE, ceil, 0),
]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
for spec in good_string_specs + warning_string_specs:
self.assertEqual(
IECString(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0]
)
for spec in warning_string_specs:
self.assertWarns(
RuntimeWarning, IECString(spec[0]).get_length, spec[1], spec[2]
)
for spec in error_string_specs:
self.assertRaises(ValueError, IECString, spec[0])
if __name__ == "__main__":
unittest.main(failfast=True)