Debian Codegraph @

rpython/rlib/test/test_rtime.py

raw symbols SCIP
1
2 from rpython.rtyper.test.tool import BaseRtypingTest
3 from rpython.rtyper.lltypesystem import lltype, rffi
4 from rpython.rlib import rtime
5
6 import time, sys, py
7
8 class TestTime(BaseRtypingTest):
9 def test_time_time(self):
10 def fn():
11 return time.time()
12
13 t0 = time.time()
14 res0 = self.interpret(fn, [])
15 t1 = time.time()
16 res1 = self.interpret(fn, [])
17 assert t0 <= res0 <= t1 <= res1
18
19 def test_time_clock(self):
20 def sleep(t):
21 # a version of time.sleep() that consumes actual CPU time
22 start = time.clock()
23 while abs(time.clock() - start) <= t:
24 pass
25 def f():
26 return time.clock()
27 t0 = time.clock()
28 sleep(0.011)
29 t1 = self.interpret(f, [])
30 sleep(0.011)
31 t2 = time.clock()
32 sleep(0.011)
33 t3 = self.interpret(f, [])
34 sleep(0.011)
35 t4 = time.clock()
36 sleep(0.011)
37 t5 = self.interpret(f, [])
38 sleep(0.011)
39 t6 = time.clock()
40 # time.clock() and t1() might have a different notion of zero, so
41 # we can only subtract two numbers returned by the same function.
42 # Moreover they might have different precisions, but it should
43 # be at least 0.01 seconds, hence the "sleeps".
44 assert 0.0099 <= t2-t0 <= 9.0
45 assert 0.0099 <= t3-t1 <= t4-t0 <= 9.0
46 assert 0.0099 <= t4-t2 <= t5-t1 <= t6-t0 <= 9.0
47 assert 0.0099 <= t5-t3 <= t6-t2 <= 9.0
48 assert 0.0099 <= t6-t4 <= 9.0
49
50 def test_time_sleep(self):
51 def does_nothing():
52 time.sleep(0.19)
53 t0 = time.time()
54 self.interpret(does_nothing, [])
55 t1 = time.time()
56 assert t0 <= t1
57 assert t1 - t0 >= 0.15
58
59 def test_clock_gettime(self):
60 if not rtime.HAS_CLOCK_GETTIME_RUNTIME:
61 py.test.skip("no clock_gettime()")
62 lst = []
63 for i in range(50):
64 with lltype.scoped_alloc(rtime.TIMESPEC) as a1:
65 res = rtime.c_clock_gettime(rtime.CLOCK_MONOTONIC, a1)
66 assert res == 0
67 t = (float(rffi.getintfield(a1, 'c_tv_sec')) +
68 float(rffi.getintfield(a1, 'c_tv_nsec')) * 0.000000001)
69 lst.append(t)
70 assert lst == sorted(lst)
71
72 def test_clock_getres(self):
73 if not rtime.HAS_CLOCK_GETTIME_RUNTIME:
74 py.test.skip("no clock_gettime()")
75 lst = []
76 with lltype.scoped_alloc(rtime.TIMESPEC) as a1:
77 res = rtime.c_clock_getres(rtime.CLOCK_MONOTONIC, a1)
78 assert res == 0
79 t = (float(rffi.getintfield(a1, 'c_tv_sec')) +
80 float(rffi.getintfield(a1, 'c_tv_nsec')) * 0.000000001)
81 assert 0.0 < t <= 1.0