commit f08a3c795d5b65ccdeac1a19e17ae573377337e9
parent 35f7eea5187e3d65408df39ad3bba3fed7f271dd
Author: Byron Torres <b@torresjrjr.com>
Date: Wed, 20 Apr 2022 02:05:36 +0100
time: add @test add()
Signed-off-by: Byron Torres <b@torresjrjr.com>
Diffstat:
M | time/arithm.ha | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 79 insertions(+), 0 deletions(-)
diff --git a/time/arithm.ha b/time/arithm.ha
@@ -36,6 +36,85 @@ export fn compare(a: instant, b: instant) i8 = {
else 0;
};
+@test fn add() void = {
+ const cases = [
+ // instant a duration d instant b
+ ( 0, 000000000, -2000000001, -3, 999999999),
+ ( 0, 000000000, -2000000000, -2, 000000000),
+ ( 0, 000000000, -1999999999, -2, 000000001),
+ ( 0, 000000000, -1000000001, -2, 999999999),
+ ( 0, 000000000, -1000000000, -1, 000000000),
+ ( 0, 000000000, -0999999999, -1, 000000001),
+ ( 0, 000000000, -0000000001, -1, 999999999),
+ ( 0, 000000000, 0000000000, 0, 000000000),
+ ( 0, 000000000, 0000000001, 0, 000000001),
+ ( 0, 000000000, 0999999999, 0, 999999999),
+ ( 0, 000000000, 1000000000, 1, 000000000),
+ ( 0, 000000000, 1000000001, 1, 000000001),
+ ( 0, 000000000, 1999999999, 1, 999999999),
+ ( 0, 000000000, 2000000000, 2, 000000000),
+ ( 0, 000000000, 2000000001, 2, 000000001),
+
+ ( 0, 000000001, -2000000001, -2, 000000000),
+ ( 0, 000000001, -2000000000, -2, 000000001),
+ ( 0, 000000001, -1999999999, -2, 000000002),
+ ( 0, 000000001, -1000000001, -1, 000000000),
+ ( 0, 000000001, -1000000000, -1, 000000001),
+ ( 0, 000000001, -0999999999, -1, 000000002),
+ ( 0, 000000001, -0000000001, 0, 000000000),
+ ( 0, 000000001, 0000000000, 0, 000000001),
+ ( 0, 000000001, 0000000001, 0, 000000002),
+ ( 0, 000000001, 0999999999, 1, 000000000),
+ ( 0, 000000001, 1000000000, 1, 000000001),
+ ( 0, 000000001, 1000000001, 1, 000000002),
+ ( 0, 000000001, 1999999999, 2, 000000000),
+ ( 0, 000000001, 2000000000, 2, 000000001),
+ ( 0, 000000001, 2000000001, 2, 000000002),
+
+ (-1, 999999999, -2000000001, -3, 999999998),
+ (-1, 999999999, -2000000000, -3, 999999999),
+ (-1, 999999999, -1999999999, -2, 000000000),
+ (-1, 999999999, -1000000001, -2, 999999998),
+ (-1, 999999999, -1000000000, -2, 999999999),
+ (-1, 999999999, -0999999999, -1, 000000000),
+ (-1, 999999999, -0000000001, -1, 999999998),
+ (-1, 999999999, 0000000000, -1, 999999999),
+ (-1, 999999999, 0000000001, 0, 000000000),
+ (-1, 999999999, 0999999999, 0, 999999998),
+ (-1, 999999999, 1000000000, 0, 999999999),
+ (-1, 999999999, 1000000001, 1, 000000000),
+ (-1, 999999999, 1999999999, 1, 999999998),
+ (-1, 999999999, 2000000000, 1, 999999999),
+ (-1, 999999999, 2000000001, 2, 000000000),
+
+ ( 0, 999999999, -2000000001, -2, 999999998),
+ ( 0, 999999999, -2000000000, -2, 999999999),
+ ( 0, 999999999, -1999999999, -1, 000000000),
+ ( 0, 999999999, -1000000001, -1, 999999998),
+ ( 0, 999999999, -1000000000, -1, 999999999),
+ ( 0, 999999999, -0999999999, 0, 000000000),
+ ( 0, 999999999, -0000000001, 0, 999999998),
+ ( 0, 999999999, 0000000000, 0, 999999999),
+ ( 0, 999999999, 0000000001, 1, 000000000),
+ ( 0, 999999999, 0999999999, 1, 999999998),
+ ( 0, 999999999, 1000000000, 1, 999999999),
+ ( 0, 999999999, 1000000001, 2, 000000000),
+ ( 0, 999999999, 1999999999, 2, 999999998),
+ ( 0, 999999999, 2000000000, 2, 999999999),
+ ( 0, 999999999, 2000000001, 3, 000000000),
+ ];
+
+ for (let i = 0z; i < len(cases); i += 1) {
+ const C = cases[i];
+ const a = instant { sec = C.0, nsec = C.1 };
+ const d = C.2;
+ const b = instant { sec = C.3, nsec = C.4 };
+ const B = add(a, d);
+ assert(B.sec == b.sec, "time::add() .sec error");
+ assert(B.nsec == b.nsec, "time::add() .nsec error");
+ };
+};
+
@test fn compare() void = {
let a = now(clock::MONOTONIC);
sleep(1 * MILLISECOND);