import math
from wvtest import *
import bup._helpers as _helpers
from bup import xstat

@wvtest
def test_fstime():
    WVPASSEQ(xstat.timespec_to_nsecs((0, 0)), 0)
    WVPASSEQ(xstat.timespec_to_nsecs((1, 0)), 10**9)
    WVPASSEQ(xstat.timespec_to_nsecs((0, 10**9 / 2)), 500000000)
    WVPASSEQ(xstat.timespec_to_nsecs((1, 10**9 / 2)), 1500000000)
    WVPASSEQ(xstat.timespec_to_nsecs((-1, 0)), -10**9)
    WVPASSEQ(xstat.timespec_to_nsecs((-1, 10**9 / 2)), -500000000)
    WVPASSEQ(xstat.timespec_to_nsecs((-2, 10**9 / 2)), -1500000000)
    WVEXCEPT(Exception, xstat.timespec_to_nsecs, (0, -1))
    WVPASSEQ(type(xstat.timespec_to_nsecs((2, 22222222))), type(0))
    WVPASSEQ(type(xstat.timespec_to_nsecs((-2, 22222222))), type(0))

    WVPASSEQ(xstat.nsecs_to_timespec(0), (0, 0))
    WVPASSEQ(xstat.nsecs_to_timespec(10**9), (1, 0))
    WVPASSEQ(xstat.nsecs_to_timespec(500000000), (0, 10**9 / 2))
    WVPASSEQ(xstat.nsecs_to_timespec(1500000000), (1, 10**9 / 2))
    WVPASSEQ(xstat.nsecs_to_timespec(-10**9), (-1, 0))
    WVPASSEQ(xstat.nsecs_to_timespec(-500000000), (-1, 10**9 / 2))
    WVPASSEQ(xstat.nsecs_to_timespec(-1500000000), (-2, 10**9 / 2))
    x = xstat.nsecs_to_timespec(1977777778)
    WVPASSEQ(type(x[0]), type(0))
    WVPASSEQ(type(x[1]), type(0))
    x = xstat.nsecs_to_timespec(-1977777778)
    WVPASSEQ(type(x[0]), type(0))
    WVPASSEQ(type(x[1]), type(0))

    WVPASSEQ(xstat.fstime_floor_secs(0), 0)
    WVPASSEQ(xstat.fstime_floor_secs(10**9 / 2), 0)
    WVPASSEQ(xstat.fstime_floor_secs(10**9), 1)
    WVPASSEQ(xstat.fstime_floor_secs(-10**9 / 2), -1)
    WVPASSEQ(xstat.fstime_floor_secs(-10**9), -1)
    WVPASSEQ(type(xstat.fstime_floor_secs(10**9 / 2)), type(0))
    WVPASSEQ(type(xstat.fstime_floor_secs(-10**9 / 2)), type(0))

@@ -4,16 +4,38 @@
 ort math
+, tempfile, subprocess
 %0Afrom wv
@@ -1755,28 +1755,1040 @@
 secs(-10**9 / 2)), type(0))%0A
+%0A%0A@wvtest%0Adef test_timespec_behavior():%0A    tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')%0A    try:%0A        path = tmpdir + '/foo'%0A        open(path, 'w').close()%0A        frac_ts = (0, 10**9 / 2)%0A        _helpers.utimensat(_helpers.AT_FDCWD, path, (frac_ts, frac_ts), 0)%0A        st = _helpers.stat(path)%0A        atime_ts = st%5B8%5D%0A        mtime_ts = st%5B9%5D%0A        WVPASSEQ(atime_ts%5B0%5D, 0)%0A        WVPASS(atime_ts%5B1%5D == 0 or atime_ts%5B1%5D == frac_ts%5B1%5D)%0A        WVPASSEQ(mtime_ts%5B0%5D, 0)%0A        WVPASS(mtime_ts%5B1%5D == 0 or mtime_ts%5B1%5D == frac_ts%5B1%5D)%0A        if(mtime_ts%5B1%5D == frac_ts%5B1%5D):%0A            # Sub-second resolution -- check behavior of negative timespecs.%0A            neg_ts = (-43, 10**9 / 2)%0A            _helpers.utimensat(_helpers.AT_FDCWD, path, (neg_ts, neg_ts), 0)%0A            st = _helpers.stat(path)%0A            atime_ts = st%5B8%5D%0A            mtime_ts = st%5B9%5D%0A            WVPASSEQ(atime_ts, neg_ts)%0A            WVPASSEQ(mtime_ts, neg_ts)%0A    finally:%0A        subprocess.call(%5B'rm', '-rf', tmpdir%5D)%0A

Add initial timespec behavior tests.