Last active
March 17, 2017 10:55
-
-
Save asmodehn/cae82c3267c3532fb26fd55aef1a0a1d to your computer and use it in GitHub Desktop.
rostime canon fix demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import pytest | |
import functools | |
import hypothesis | |
import hypothesis.strategies as st | |
def new_canon(secs, nsecs): | |
# canonical form: nsecs is always positive, nsecs < 1 second | |
secs_over = nsecs // 1000000000 | |
secs += secs_over | |
nsecs -= secs_over * 1000000000 | |
return secs, nsecs | |
def old_canon(secs, nsecs): | |
# canonical form: nsecs is always positive, nsecs < 1 second | |
while nsecs >= 1000000000: | |
secs += 1 | |
nsecs -= 1000000000 | |
while nsecs < 0: | |
secs -= 1 | |
nsecs += 1000000000 | |
return secs, nsecs | |
# we use min and max (of int32) to be meaningful but not loop for eternity... | |
@hypothesis.given(st.integers(), st.integers(min_value=-2147483648, max_value=2147483647)) | |
@hypothesis.settings(verbosity=hypothesis.Verbosity.verbose) | |
def test_valid_canon(s, ns): | |
o_s, o_ns = old_canon(s, ns) | |
n_s, n_ns = new_canon(s, ns) | |
assert n_s == o_s | |
assert n_ns == o_ns | |
if __name__ == '__main__': | |
pytest.main(['-s', | |
'test_canon.py::test_valid_canon', | |
]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment