Skip to content

Instantly share code, notes, and snippets.

@asmodehn
Last active March 17, 2017 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asmodehn/cae82c3267c3532fb26fd55aef1a0a1d to your computer and use it in GitHub Desktop.
Save asmodehn/cae82c3267c3532fb26fd55aef1a0a1d to your computer and use it in GitHub Desktop.
rostime canon fix demo
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