Skip to content

Instantly share code, notes, and snippets.

@kinkerl
Created July 4, 2019 21:40
Show Gist options
  • Save kinkerl/ecb2fde719d0930518926d1956d06e23 to your computer and use it in GitHub Desktop.
Save kinkerl/ecb2fde719d0930518926d1956d06e23 to your computer and use it in GitHub Desktop.
import pytest
def calc(v1_s, v1_ns, v1_sgn, v2_s, v2_ns):
if v1_sgn: # erstes value ist positiv
# Hier ist alles einfach, keine Probleme mit switches
ns = (v1_ns + v2_ns)
# calculate ns overflow
overflow = 0
while ns >= 1000000:
overflow += 1
ns -= 1000000
s = (v1_s + v2_s) + overflow
return (s, ns, v1_sgn)
else: # erstes value ist negativ
if v1_s < v2_s:
if v1_ns <= v2_ns:
ns = (v2_ns - v1_ns)
s = (v2_s - v1_s)
return (s, ns, 1)
else:
ns = (v1_ns - v2_ns)
ns = 1000000 - ns
s = (v2_s - v1_s) - 1
return (s, ns, 1)
elif v1_s == v2_s:
if v1_ns <= v2_ns:
ns = (v2_ns - v1_ns)
s = (v2_s - v1_s)
return (s, ns, 1)
else:
ns = (v1_ns - v2_ns)
s = (v2_s - v1_s)
return (s, ns, 0)
else:
ns = (v1_ns - v2_ns)
overflow = 0
if ns < 0:
ns = 1000000 + ns
overflow += 1
s = (v1_s - v2_s) - overflow
return (s, ns, 0)
examples = (
(1, 0, 1, 1, 0, (2, 0, 1)), # 1 + 1 = 2
(1, 0, 0, 1, 0, (0, 0, 1)), # -1 + 1 = 0
(1, 0, 0, 2, 0, (1, 0, 1)), # -1 + 2 = 1
(2, 0, 0, 1, 0, (1, 0, 0)), # -2 + 1 = -1
(1, 0, 1, 1, 1, (2, 1, 1)), # 1;0 + 1;1 = 2;1
(1, 500000, 1, 1, 500001, (3, 1, 1)), # 1;5000000 + 1;500001 = 3;1
(1, 0, 0, 1, 1, (0, 1, 1)), # -1;0 + 1;1 = 0;1
(1, 1, 0, 1, 0, (0, 1, 0)), # -1;1 + 1;0 = -0;1
(1, 3, 0, 1, 1, (0, 2, 0)), # -1;3 + 1;1 = -0;2
(1, 1, 0, 1, 4, (0, 3, 1)), # -1;1 + 1;4 = 0;3
(2, 1, 0, 1, 3, (0, 999998, 0)), # -2;1 + 1;3 = -0;999998
(2, 3, 0, 1, 1, (1, 2, 0)), # -2;3 + 1;1 = -1;2
(1, 1, 0, 2, 3, (1, 2, 1)), # -1;1 + 2;3 = 1;2
(1, 3, 0, 2, 1, (0, 999998, 1)), # -1;3 + 2;1 = 0;999998
)
@pytest.mark.parametrize("v1_s, v1_ns, v1_sgn, v2_s, v2_ns, result", examples)
def test_calc(v1_s, v1_ns, v1_sgn, v2_s, v2_ns, result):
assert result == calc(v1_s, v1_ns, v1_sgn, v2_s, v2_ns)
@kinkerl
Copy link
Author

kinkerl commented Jul 4, 2019

pytest test_heiko.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment