]> arthur.barton.de Git - bup.git/blob - lib/bup/t/txstat.py
Add initial timespec behavior tests.
[bup.git] / lib / bup / t / txstat.py
1 import math, tempfile, subprocess
2 from wvtest import *
3 import bup._helpers as _helpers
4 from bup import xstat
5
6 @wvtest
7 def test_fstime():
8     WVPASSEQ(xstat.timespec_to_nsecs((0, 0)), 0)
9     WVPASSEQ(xstat.timespec_to_nsecs((1, 0)), 10**9)
10     WVPASSEQ(xstat.timespec_to_nsecs((0, 10**9 / 2)), 500000000)
11     WVPASSEQ(xstat.timespec_to_nsecs((1, 10**9 / 2)), 1500000000)
12     WVPASSEQ(xstat.timespec_to_nsecs((-1, 0)), -10**9)
13     WVPASSEQ(xstat.timespec_to_nsecs((-1, 10**9 / 2)), -500000000)
14     WVPASSEQ(xstat.timespec_to_nsecs((-2, 10**9 / 2)), -1500000000)
15     WVEXCEPT(Exception, xstat.timespec_to_nsecs, (0, -1))
16     WVPASSEQ(type(xstat.timespec_to_nsecs((2, 22222222))), type(0))
17     WVPASSEQ(type(xstat.timespec_to_nsecs((-2, 22222222))), type(0))
18
19     WVPASSEQ(xstat.nsecs_to_timespec(0), (0, 0))
20     WVPASSEQ(xstat.nsecs_to_timespec(10**9), (1, 0))
21     WVPASSEQ(xstat.nsecs_to_timespec(500000000), (0, 10**9 / 2))
22     WVPASSEQ(xstat.nsecs_to_timespec(1500000000), (1, 10**9 / 2))
23     WVPASSEQ(xstat.nsecs_to_timespec(-10**9), (-1, 0))
24     WVPASSEQ(xstat.nsecs_to_timespec(-500000000), (-1, 10**9 / 2))
25     WVPASSEQ(xstat.nsecs_to_timespec(-1500000000), (-2, 10**9 / 2))
26     x = xstat.nsecs_to_timespec(1977777778)
27     WVPASSEQ(type(x[0]), type(0))
28     WVPASSEQ(type(x[1]), type(0))
29     x = xstat.nsecs_to_timespec(-1977777778)
30     WVPASSEQ(type(x[0]), type(0))
31     WVPASSEQ(type(x[1]), type(0))
32
33     WVPASSEQ(xstat.fstime_floor_secs(0), 0)
34     WVPASSEQ(xstat.fstime_floor_secs(10**9 / 2), 0)
35     WVPASSEQ(xstat.fstime_floor_secs(10**9), 1)
36     WVPASSEQ(xstat.fstime_floor_secs(-10**9 / 2), -1)
37     WVPASSEQ(xstat.fstime_floor_secs(-10**9), -1)
38     WVPASSEQ(type(xstat.fstime_floor_secs(10**9 / 2)), type(0))
39     WVPASSEQ(type(xstat.fstime_floor_secs(-10**9 / 2)), type(0))
40
41
42 @wvtest
43 def test_timespec_behavior():
44     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
45     try:
46         path = tmpdir + '/foo'
47         open(path, 'w').close()
48         frac_ts = (0, 10**9 / 2)
49         _helpers.utimensat(_helpers.AT_FDCWD, path, (frac_ts, frac_ts), 0)
50         st = _helpers.stat(path)
51         atime_ts = st[8]
52         mtime_ts = st[9]
53         WVPASSEQ(atime_ts[0], 0)
54         WVPASS(atime_ts[1] == 0 or atime_ts[1] == frac_ts[1])
55         WVPASSEQ(mtime_ts[0], 0)
56         WVPASS(mtime_ts[1] == 0 or mtime_ts[1] == frac_ts[1])
57         if(mtime_ts[1] == frac_ts[1]):
58             # Sub-second resolution -- check behavior of negative timespecs.
59             neg_ts = (-43, 10**9 / 2)
60             _helpers.utimensat(_helpers.AT_FDCWD, path, (neg_ts, neg_ts), 0)
61             st = _helpers.stat(path)
62             atime_ts = st[8]
63             mtime_ts = st[9]
64             WVPASSEQ(atime_ts, neg_ts)
65             WVPASSEQ(mtime_ts, neg_ts)
66     finally:
67         subprocess.call(['rm', '-rf', tmpdir])