Files
micropython/tests/extmod/time_time_ns.py
Damien George 09ea901317 tests/extmod: Add test to compare time_ns with time.
They should be close together.

Signed-off-by: Damien George <damien@micropython.org>
2024-10-24 23:24:09 +11:00

32 lines
720 B
Python

# test time.time_ns()
try:
import time
time.sleep_us
time.time_ns
except (ImportError, AttributeError):
print("SKIP")
raise SystemExit
t0 = time.time_ns()
time.sleep_us(5000)
t1 = time.time_ns()
# Check that time_ns increases.
print(t0 < t1)
# Check that time_ns counts correctly, but be very lenient with the bounds (2ms to 50ms).
if 2000000 < t1 - t0 < 50000000:
print(True)
else:
print(t0, t1, t1 - t0)
# Check that time.time() and time.time_ns() are within a second of each other.
# Note that time.time() may return an int or float.
for _ in range(10):
t_s, t_ns = time.time(), time.time_ns()
print(abs(t_s * 1_000 - t_ns // 1_000_000) <= 1_000)
time.sleep_us(100_000)