Skip to content

Commit be6a765

Browse files
committed
tests/extmod/ticks_diff: Test for new semantics of ticks_diff().
1 parent 8908e50 commit be6a765

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

tests/extmod/ticks_diff.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from utime import ticks_diff, ticks_add
2+
3+
MAX = ticks_add(0, -1)
4+
# Should be done like this to avoid small int overflow
5+
MODULO_HALF = MAX // 2 + 1
6+
7+
# Invariants:
8+
# if ticks_diff(a, b) = c,
9+
# then ticks_diff(b, a) = -c
10+
11+
assert ticks_diff(1, 0) == 1, ticks_diff(1, 0)
12+
assert ticks_diff(0, 1) == -1
13+
14+
assert ticks_diff(0, MAX) == 1
15+
assert ticks_diff(MAX, 0) == -1
16+
17+
assert ticks_diff(0, MAX - 1) == 2
18+
19+
# Maximum "positive" distance
20+
assert ticks_diff(MODULO_HALF, 1) == MODULO_HALF - 1, ticks_diff(MODULO_HALF, 1)
21+
# Step further, and it becomes a negative distance
22+
assert ticks_diff(MODULO_HALF, 0) == -MODULO_HALF
23+
24+
# Offsetting that in either direction doesn't affect the result
25+
off = 100
26+
# Cheating and skipping to use ticks_add() when we know there's no wraparound
27+
# Real apps should use always it.
28+
assert ticks_diff(MODULO_HALF + off, 1 + off) == MODULO_HALF - 1
29+
assert ticks_diff(MODULO_HALF + off, 0 + off) == -MODULO_HALF
30+
assert ticks_diff(MODULO_HALF - off, ticks_add(1, -off)) == MODULO_HALF - 1
31+
assert ticks_diff(MODULO_HALF - off, ticks_add(0, -off)) == -MODULO_HALF
32+
33+
print("OK")

tests/extmod/ticks_diff.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OK

0 commit comments

Comments
 (0)