tests/extmod: Add test for the precision of utime functions.

According to documentation time() has a precision of at least 1 second.
This test runs for 2.5 seconds and calls all utime functions every 100ms.
Then it checks if they returned enough different results.  All functions
with sub-second precision will return ~25 results.  This test passes with
15 results or more.  Functions that do not exist are skipped silently.
This commit is contained in:
Oliver Joos 2020-12-16 21:25:18 +01:00 committed by Damien George
parent 769e822f19
commit 419134bea4
2 changed files with 74 additions and 0 deletions

65
tests/extmod/utime_res.py Normal file
View File

@ -0,0 +1,65 @@
# test utime resolutions
try:
import utime
except ImportError:
print("SKIP")
raise SystemExit
def gmtime_time():
return utime.gmtime(utime.time())
def localtime_time():
return utime.localtime(utime.time())
def test():
TEST_TIME = 2500
EXPECTED_MAP = (
# (function name, min. number of results in 2.5 sec)
("time", 3),
("gmtime", 3),
("localtime", 3),
("gmtime_time", 3),
("localtime_time", 3),
("ticks_ms", 15),
("ticks_us", 15),
("ticks_ns", 15),
("ticks_cpu", 15),
)
# call time functions
results_map = {}
end_time = utime.ticks_ms() + TEST_TIME
while utime.ticks_diff(end_time, utime.ticks_ms()) > 0:
utime.sleep_ms(100)
for func_name, _ in EXPECTED_MAP:
try:
time_func = getattr(utime, func_name, None) or globals()[func_name]
now = time_func() # may raise AttributeError
except (KeyError, AttributeError):
continue
try:
results_map[func_name].add(now)
except KeyError:
results_map[func_name] = {now}
# check results
for func_name, min_len in EXPECTED_MAP:
print("Testing %s" % func_name)
results = results_map.get(func_name)
if results is None:
pass
elif func_name == "ticks_cpu" and results == {0}:
# ticks_cpu() returns 0 on some ports (e.g. unix)
pass
elif len(results) < min_len:
print(
"%s() returns %s result%s in %s ms, expecting >= %s"
% (func_name, len(results), "s"[: len(results) != 1], TEST_TIME, min_len)
)
test()

View File

@ -0,0 +1,9 @@
Testing time
Testing gmtime
Testing localtime
Testing gmtime_time
Testing localtime_time
Testing ticks_ms
Testing ticks_us
Testing ticks_ns
Testing ticks_cpu