feat(data-types): add skeleton of data types, add an Integer type

- add convenience functions to instantiate Integer
- add tests for the basic functions of integers
This commit is contained in:
Arthur Meyre
2021-07-19 16:16:21 +02:00
parent ce358ca838
commit 063b2db9db
5 changed files with 121 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
"""Test file for HDK's common/data_types/integers.py"""
import random
import pytest
from hdk.common.data_types.integers import Integer, SignedInteger, UnsignedInteger
@pytest.mark.parametrize(
"integer,expected_min,expected_max",
[
pytest.param(Integer(8, is_signed=False), 0, 255, id="8 bits unsigned Integer"),
pytest.param(UnsignedInteger(8), 0, 255, id="8 bits UnsignedInteger"),
pytest.param(Integer(8, is_signed=True), -128, 127, id="8 bits signed Integer"),
pytest.param(SignedInteger(8), -128, 127, id="8 bits SignedInteger"),
pytest.param(Integer(32, is_signed=False), 0, 4_294_967_295, id="32 bits unsigned Integer"),
pytest.param(UnsignedInteger(32), 0, 4_294_967_295, id="32 bits UnsignedInteger"),
pytest.param(
Integer(32, is_signed=True),
-2_147_483_648,
2_147_483_647,
id="32 bits signed Integer",
),
pytest.param(
SignedInteger(32),
-2_147_483_648,
2_147_483_647,
id="32 bits SignedInteger",
),
],
)
def test_basic_integers(integer: Integer, expected_min: int, expected_max: int):
"""Test integer class basic functions"""
assert integer.min_value() == expected_min
assert integer.max_value() == expected_max
assert integer.can_represent_value(random.randint(expected_min, expected_max))
assert not integer.can_represent_value(expected_min - 1)
assert not integer.can_represent_value(expected_max + 1)