| 1 |
import functools |
| 2 |
import unittest |
| 3 |
from math import isnan, nextafter |
| 4 |
from test.support import requires_IEEE_754 |
| 5 |
from test.support.hypothesis_helper import hypothesis |
| 6 |
|
| 7 |
floats = hypothesis.strategies.floats |
| 8 |
integers = hypothesis.strategies.integers |
| 9 |
|
| 10 |
|
| 11 |
def assert_equal_float(x, y): |
| 12 |
assert isnan(x) and isnan(y) or x == y |
| 13 |
|
| 14 |
|
| 15 |
def via_reduce(x, y, steps): |
| 16 |
return functools.reduce(nextafter, [y] * steps, x) |
| 17 |
|
| 18 |
|
| 19 |
class NextafterTests(unittest.TestCase): |
| 20 |
@requires_IEEE_754 |
| 21 |
@hypothesis.given( |
| 22 |
x=floats(), |
| 23 |
y=floats(), |
| 24 |
steps=integers(min_value=0, max_value=2**16)) |
| 25 |
def test_count(self, x, y, steps): |
| 26 |
assert_equal_float(via_reduce(x, y, steps), |
| 27 |
nextafter(x, y, steps=steps)) |
| 28 |
|
| 29 |
@requires_IEEE_754 |
| 30 |
@hypothesis.given( |
| 31 |
x=floats(), |
| 32 |
y=floats(), |
| 33 |
a=integers(min_value=0), |
| 34 |
b=integers(min_value=0)) |
| 35 |
def test_addition_commutes(self, x, y, a, b): |
| 36 |
first = nextafter(x, y, steps=a) |
| 37 |
second = nextafter(first, y, steps=b) |
| 38 |
combined = nextafter(x, y, steps=a+b) |
| 39 |
hypothesis.note(f"{first} -> {second} == {combined}") |
| 40 |
|
| 41 |
assert_equal_float(second, combined) |