]> arthur.barton.de Git - bup.git/blob - lib/bup/t/txstat.py
Merge branch 'master' into meta
[bup.git] / lib / bup / t / txstat.py
1 import math
2 from wvtest import *
3 import bup._helpers as _helpers
4 from bup.xstat import FSTime
5
6 def _test_fstime():
7     def approx_eq(x, y):
8         return math.fabs(x - y) < 1 / 10e8
9     def ts_eq_ish(x, y):
10         return approx_eq(x[0], y[0]) and approx_eq(x[1], y[1])
11     def fst_eq_ish(x, y):
12         return approx_eq(x.approx_secs(), y.approx_secs())
13     def s_ns_eq_ish(fst, s, ns):
14         (fst_s, fst_ns) = fst.secs_nsecs()
15         return approx_eq(fst_s, s) and approx_eq(fst_ns, ns)
16     from_secs = FSTime.from_secs
17     from_ts = FSTime.from_timespec
18     WVPASS(from_secs(0) == from_secs(0))
19     WVPASS(from_secs(0) < from_secs(1))
20     WVPASS(from_secs(-1) < from_secs(1))
21     WVPASS(from_secs(1) > from_secs(0))
22     WVPASS(from_secs(1) > from_secs(-1))
23
24     WVPASS(fst_eq_ish(from_secs(0), from_ts((0, 0))))
25     WVPASS(fst_eq_ish(from_secs(1), from_ts((1, 0))))
26     WVPASS(fst_eq_ish(from_secs(-1), from_ts((-1, 0))))
27     WVPASS(fst_eq_ish(from_secs(-0.5), from_ts((-1, 10**9 / 2))))
28     WVPASS(fst_eq_ish(from_secs(-1.5), from_ts((-2, 10**9 / 2))))
29     WVPASS(fst_eq_ish(from_secs(1 / 10e8), from_ts((0, 1))))
30     WVPASS(fst_eq_ish(from_secs(-1 / 10e8), from_ts((-1, 10**9 - 1))))
31
32     WVPASS(ts_eq_ish(from_secs(0).to_timespec(), (0, 0)))
33     WVPASS(ts_eq_ish(from_secs(1).to_timespec(), (1, 0)))
34     WVPASS(ts_eq_ish(from_secs(-1).to_timespec(), (-1, 0)))
35     WVPASS(ts_eq_ish(from_secs(-0.5).to_timespec(), (-1, 10**9 / 2)))
36     WVPASS(ts_eq_ish(from_secs(-1.5).to_timespec(), (-2, 10**9 / 2)))
37     WVPASS(ts_eq_ish(from_secs(1 / 10e8).to_timespec(), (0, 1)))
38     WVPASS(ts_eq_ish(from_secs(-1 / 10e8).to_timespec(), (-1, 10**9 - 1)))
39
40     WVPASS(s_ns_eq_ish(from_secs(0), 0, 0))
41     WVPASS(s_ns_eq_ish(from_secs(1), 1, 0))
42     WVPASS(s_ns_eq_ish(from_secs(-1), -1, 0))
43     WVPASS(s_ns_eq_ish(from_secs(-0.5), 0, - 10**9 / 2))
44     WVPASS(s_ns_eq_ish(from_secs(-1.5), -1, - 10**9 / 2))
45     WVPASS(s_ns_eq_ish(from_secs(-1 / 10e8), 0, -1))
46
47 @wvtest
48 def test_fstime():
49     _test_fstime();
50     if _helpers._have_ns_fs_timestamps: # Test native python timestamp rep too.
51         orig = _helpers._have_ns_fs_timestamps
52         try:
53             _helpers._have_ns_fs_timestamps = None
54             _test_fstime();
55         finally:
56             _helpers._have_ns_fs_timestamps = orig