Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add (indirect) tests for PyTime_AsNanoseconds
  • Loading branch information
encukou committed Feb 9, 2024
commit 8f7992e5313eb00f41369c3fca24d8c0da3a205b
34 changes: 19 additions & 15 deletions Lib/test/test_capi/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DAY_TO_SEC = (24 * 60 * 60)
# Worst clock resolution: maximum delta between two clock reads.
CLOCK_RES = 0.050
CLOCK_RES_NS = int(CLOCK_RES * SEC_TO_NS)


class CAPITest(unittest.TestCase):
Expand All @@ -18,15 +19,15 @@ def test_min_max(self):
self.assertEqual(PyTime_MIN, -2**63)
self.assertEqual(PyTime_MAX, 2**63 - 1)

def check_clock(self, c_func, py_func):
t1 = c_func()
t2 = py_func()
self.assertAlmostEqual(t1, t2, delta=CLOCK_RES)

def check_clock_ns(self, c_func, py_func):
t1 = c_func()
t2 = py_func()+2_000_000_000
self.assertAlmostEqual(t1, t2, delta=CLOCK_RES * SEC_TO_NS)
def check_clock(self, c_func, py_func, c_func_ns, py_func_ns):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: it can be moved after test_assecondsdouble().

t_c = c_func()
t_py = py_func()
t_c_ns = c_func_ns()
t_py_ns = py_func_ns()
self.assertAlmostEqual(t_c, t_py, delta=CLOCK_RES)
self.assertAlmostEqual(t_c_ns, t_py_ns, delta=CLOCK_RES_NS)
self.assertAlmostEqual(t_c * SEC_TO_NS, t_c_ns, delta=CLOCK_RES_NS)
self.assertAlmostEqual(t_py * SEC_TO_NS, t_py_ns, delta=CLOCK_RES_NS)

def test_assecondsdouble(self):
# Test PyTime_AsSecondsDouble()
Expand Down Expand Up @@ -65,15 +66,18 @@ def ns_to_sec(ns):

def test_monotonic(self):
# Test PyTime_Monotonic()
self.check_clock(_testcapi.PyTime_Monotonic, time.monotonic)
self.check_clock_ns(_testcapi.PyTime_Monotonic_ns, time.monotonic_ns)
self.check_clock(
_testcapi.PyTime_Monotonic, time.monotonic,
_testcapi.PyTime_Monotonic_ns, time.monotonic_ns)

def test_perf_counter(self):
# Test PyTime_PerfCounter()
self.check_clock(_testcapi.PyTime_PerfCounter, time.perf_counter)
self.check_clock_ns(_testcapi.PyTime_PerfCounter_ns, time.perf_counter_ns)
self.check_clock(
_testcapi.PyTime_PerfCounter, time.perf_counter,
_testcapi.PyTime_PerfCounter_ns, time.perf_counter_ns)

def test_time(self):
# Test PyTime_time()
self.check_clock(_testcapi.PyTime_Time, time.time)
self.check_clock_ns(_testcapi.PyTime_PerfCounter_ns, time.perf_counter_ns)
self.check_clock(
_testcapi.PyTime_Time, time.time,
_testcapi.PyTime_Time_ns, time.time_ns)
Comment thread
encukou marked this conversation as resolved.
Outdated