Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extmod/utime_mphal: Make ticks_add check for overflow of delta.
Work done in collaboration with @jimmo. Signed-off-by: Damien George <damien@micropython.org>
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
try: | ||
from utime import ticks_diff, ticks_add | ||
except ImportError: | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
# Maximum value returned from ticks_add, ticks_ms, etc. | ||
TICKS_MAX = ticks_add(0, -1) | ||
# Maximum value returned from ticks_diff. | ||
TICKS_INTERVAL_MAX = TICKS_MAX // 2 | ||
|
||
# Invariants: | ||
# - ticks_diff(ticks_add(T, delta), T) == delta | ||
# - ticks_diff(T, ticks_add(T, delta)) == -delta | ||
|
||
# Check actual values of ticks_add. | ||
print(ticks_add(20, 12)) | ||
print(ticks_add(20, -12)) | ||
|
||
# Check invariant. | ||
print(ticks_diff(ticks_add(100, 123), 100)) | ||
print(ticks_diff(ticks_add(100, -123), 100)) | ||
print(ticks_diff(100, ticks_add(100, 123))) | ||
print(ticks_diff(100, ticks_add(100, -123))) | ||
|
||
# Check limits. | ||
for T in (0, 10, TICKS_MAX): | ||
for delta in ( | ||
-TICKS_INTERVAL_MAX - 1, | ||
-TICKS_INTERVAL_MAX, | ||
0, | ||
TICKS_INTERVAL_MAX, | ||
TICKS_INTERVAL_MAX + 1, | ||
): | ||
try: | ||
print(ticks_diff(ticks_add(T, delta), T) == delta) | ||
except OverflowError: | ||
print("OverflowError") | ||
try: | ||
print(ticks_diff(T, ticks_add(T, delta)) == -delta) | ||
except OverflowError: | ||
print("OverflowError") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
32 | ||
8 | ||
123 | ||
-123 | ||
-123 | ||
123 | ||
OverflowError | ||
OverflowError | ||
True | ||
True | ||
True | ||
True | ||
True | ||
True | ||
OverflowError | ||
OverflowError | ||
OverflowError | ||
OverflowError | ||
True | ||
True | ||
True | ||
True | ||
True | ||
True | ||
OverflowError | ||
OverflowError | ||
OverflowError | ||
OverflowError | ||
True | ||
True | ||
True | ||
True | ||
True | ||
True | ||
OverflowError | ||
OverflowError |