summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/time_res.py
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2022-08-18 16:57:45 +1000
committerJim Mussared <jim.mussared@gmail.com>2023-06-08 17:54:24 +1000
commit4216bc7d1351feb8199e4ebbff1a9598aa1c5b02 (patch)
tree5085738ef65ab377c221f290c7fa90ec2acd4d29 /tests/extmod/time_res.py
parent5e50975a6dd9466afafbcd012c00078093fe1f57 (diff)
downloadmicropython-4216bc7d1351feb8199e4ebbff1a9598aa1c5b02.tar.gz
micropython-4216bc7d1351feb8199e4ebbff1a9598aa1c5b02.zip
tests: Replace umodule with module everywhere.
This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tests/extmod/time_res.py')
-rw-r--r--tests/extmod/time_res.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/extmod/time_res.py b/tests/extmod/time_res.py
new file mode 100644
index 0000000000..548bef1f17
--- /dev/null
+++ b/tests/extmod/time_res.py
@@ -0,0 +1,65 @@
+# test time resolutions
+
+try:
+ import time
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+def gmtime_time():
+ return time.gmtime(time.time())
+
+
+def localtime_time():
+ return time.localtime(time.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 = time.ticks_ms() + TEST_TIME
+ while time.ticks_diff(end_time, time.ticks_ms()) > 0:
+ time.sleep_ms(100)
+ for func_name, _ in EXPECTED_MAP:
+ try:
+ time_func = getattr(time, 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()