Improved and more extensive hypothesis tests for IBMFloat.

This commit is contained in:
Robert Smallshire
2015-04-14 21:50:56 +02:00
parent ff40995995
commit dd24ea6bd8
+38 -13
View File
@@ -1,13 +1,13 @@
from math import trunc, floor
import unittest
from hypothesis import given, assume
from hypothesis.descriptors import integers_in_range, floats_in_range, just
from hypothesis import given, assume, example
import math
from hypothesis.specifiers import integers_in_range, floats_in_range, just
from segpy.portability import byte_string
from segpy.ibm_float import (ieee2ibm, ibm2ieee, MAX_IBM_FLOAT, SMALLEST_POSITIVE_NORMAL_IBM_FLOAT,
LARGEST_NEGATIVE_NORMAL_IBM_FLOAT, MIN_IBM_FLOAT, IBMFloat, EPSILON_IBM_FLOAT, truncate,
LARGEST_NEGATIVE_NORMAL_IBM_FLOAT, MIN_IBM_FLOAT, IBMFloat, EPSILON_IBM_FLOAT,
MAX_EXACT_INTEGER_IBM_FLOAT, MIN_EXACT_INTEGER_IBM_FLOAT, EXPONENT_BIAS)
from segpy.util import almost_equal
@@ -179,7 +179,7 @@ class TestIBMFloat(unittest.TestCase):
with self.assertRaises(OverflowError):
IBMFloat.from_float(MIN_IBM_FLOAT * 10)
@given(float)
@given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT))
def test_bool(self, f):
self.assertEqual(bool(IBMFloat.from_float(f)), bool(f))
@@ -197,32 +197,32 @@ class TestIBMFloat(unittest.TestCase):
ibm = IBMFloat.from_float(f)
self.assertTrue(almost_equal(f, float(ibm), epsilon=EPSILON_IBM_FLOAT))
@given(integers_in_range(0, MAX_EXACT_INTEGER_IBM_FLOAT - 1))
@given(floats_in_range(0.0, 1.0))
@given(integers_in_range(0, MAX_EXACT_INTEGER_IBM_FLOAT - 1),
floats_in_range(0.0, 0.9))
def test_trunc_above_zero(self, i, f):
assume(f != 1.0)
ieee = i + f
ibm = IBMFloat.from_float(ieee)
self.assertEqual(trunc(ibm), i)
@given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT + 1, 0))
@given(floats_in_range(0.0, 1.0))
@given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT + 1, 0),
floats_in_range(0.0, 0.9))
def test_trunc_below_zero(self, i, f):
assume(f != 1.0)
ieee = i - f
ibm = IBMFloat.from_float(ieee)
self.assertEqual(trunc(ibm), i)
@given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1))
@given(floats_in_range(0.0, 1.0))
@given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1),
floats_in_range(0.0, 0.9))
def test_ceil(self, i, f):
assume(f != 1.0)
ieee = i + f
ibm = IBMFloat.from_float(ieee)
self.assertEqual(math.ceil(ibm), i + 1)
@given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1))
@given(floats_in_range(0.0, 1.0))
@given(integers_in_range(MIN_EXACT_INTEGER_IBM_FLOAT, MAX_EXACT_INTEGER_IBM_FLOAT - 1),
floats_in_range(0.0, 0.9))
def test_floor(self, i, f):
assume(f != 1.0)
ieee = i + f
@@ -236,7 +236,7 @@ class TestIBMFloat(unittest.TestCase):
with self.assertRaises(FloatingPointError):
ibm.normalize()
def test_normalise_subnormal(self):
def test_normalise_subnormal1(self):
ibm = IBMFloat.from_bytes((0b01000000, 0b00000000, 0b11111111, 0b00000000))
assert ibm.is_subnormal()
normalized = ibm.normalize()
@@ -329,9 +329,34 @@ class TestIBMFloat(unittest.TestCase):
f, e = ibm.frexp()
self.assertTrue(almost_equal(fraction * 2**exponent, f * 2**e, epsilon=EPSILON_IBM_FLOAT))
@given(floats_in_range(MIN_IBM_FLOAT, MAX_IBM_FLOAT),
floats_in_range(0.0, 1.0))
def test_add(self, f, p):
a = f * p
b = f - a
ibm_a = IBMFloat.from_float(a)
ibm_b = IBMFloat.from_float(b)
ibm_c = ibm_a + ibm_b
ieee_a = float(ibm_a)
ieee_b = float(ibm_b)
ieee_c = ieee_a + ieee_b
self.assertTrue(almost_equal(ieee_c, ibm_c, epsilon=EPSILON_IBM_FLOAT * 4))
@given(floats_in_range(0, MAX_IBM_FLOAT),
floats_in_range(0, MAX_IBM_FLOAT))
def test_sub(self, a, b):
ibm_a = IBMFloat.from_float(a)
ibm_b = IBMFloat.from_float(b)
ibm_c = ibm_a - ibm_b
ieee_a = float(ibm_a)
ieee_b = float(ibm_b)
ieee_c = ieee_a - ieee_b
self.assertTrue(almost_equal(ieee_c, ibm_c, epsilon=EPSILON_IBM_FLOAT))
if __name__ == '__main__':