Implemented tests for the Float datatype.

This required the implementation of a new method on BaseDatatypeTester.

dynamic_assert_equal is overridden by floating point datatypes, and
instead uses TestCase.assertAlmostEqual.
This commit is contained in:
Jeppe Klitgaard 2015-04-16 18:49:23 +02:00
parent cc184f3e4d
commit 20e7c70acf
2 changed files with 47 additions and 9 deletions

View File

@ -276,8 +276,11 @@ class Float(NumberDatatype):
FORMAT = "f"
SIZE = 4
ALLOWED_SERIALIZATION_TYPES = (int, long, float)
DISALLOWED_SERIALIZATION_TYPES = (bool,)
class Double(NumberDatatype):
class Double(Float):
FORMAT = "d"
SIZE = 8

View File

@ -43,6 +43,13 @@ class BaseDatatypeTester(unittest.TestCase):
# containing the value and the expected exception.
INVALID_DESERIALIZATION_VALUES = []
def dynamic_assert_equal(self, first, second):
"""
Overriden by floating point datatypes in order to handle
the floating point issue.
"""
return self.assertEqual(first, second)
def test_init(self):
d = self.DATATYPE_CLS() # noqa
@ -53,12 +60,14 @@ class BaseDatatypeTester(unittest.TestCase):
def test_valid_data_serialization_values(self):
for deserialized_val, serialized_val in self.VALID_VALUES:
self.assertEqual(self.DATATYPE_CLS.serialize(deserialized_val),
self.dynamic_assert_equal(
self.DATATYPE_CLS.serialize(deserialized_val),
serialized_val)
def test_valid_data_deserialization_values(self):
for deserialized_val, serialized_val in self.VALID_VALUES:
self.assertEqual(self.DATATYPE_CLS.deserialize(serialized_val),
self.dynamic_assert_equal(
self.DATATYPE_CLS.deserialize(serialized_val),
deserialized_val)
def test_invalid_data_serialization_values(self):
@ -84,6 +93,10 @@ class BaseNumberDatatypeTester(BaseDatatypeTester):
def base_number_invalid_data_serialization_values(self):
values_to_test = BASE_INVALID_SERIALIZATION_VALUES
if (cls.MIN_NUMBER_VALUE is not None
and cls.MAX_NUMBER_VALUE is not None):
values_to_test.extend([
(self.DATATYPE_CLS.MIN_NUMBER_VALUE - 1, ValueError),
(self.DATATYPE_CLS.MAX_NUMBER_VALUE + 1, ValueError)
@ -282,6 +295,28 @@ class UnsignedLongLongTest(BaseNumberDatatypeTester):
LongLongTest.INVALID_DESERIALIZATION_VALUES
class FloatTest(BaseNumberDatatypeTester):
DATATYPE_CLS = Float
VALID_VALUES = [
(-100.5467, b"\xc2\xc9\x17\xe9"),
(0.00000, b"\x00\x00\x00\x00"),
(5000.72, b"E\x9cE\xc3"),
(65.123565787856342347, b"B\x82?D"),
]
INVALID_DESERIALIZATION_VALUES = list(BASE_INVALID_DESERIALIZATION_VALUES)
INVALID_DESERIALIZATION_VALUES.extend([
(b"\xff", ValueError),
(b"\x00\x01", ValueError),
(b"\x76\x80\x80\x10\xff", ValueError),
(b"\x55\x44\x33\x22\x11\x66\x77\x88\x99", ValueError)
])
def dynamic_assert_equal(self, first, second):
return self.assertAlmostEqual(first, second, places=3)
# def _bin(binstr):
# """
# Accepts a pretty looking string of binary numbers and