17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2016 Collabora, Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining
57ec681f3Smrg * a copy of this software and associated documentation files (the
67ec681f3Smrg * "Software"), to deal in the Software without restriction, including
77ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish,
87ec681f3Smrg * distribute, sublicense, and/or sell copies of the Software, and to
97ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to
107ec681f3Smrg * the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice (including the
137ec681f3Smrg * next paragraph) shall be included in all copies or substantial
147ec681f3Smrg * portions of the Software.
157ec681f3Smrg *
167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
177ec681f3Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
187ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
197ec681f3Smrg * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
207ec681f3Smrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
217ec681f3Smrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
227ec681f3Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
237ec681f3Smrg * SOFTWARE.
247ec681f3Smrg */
257ec681f3Smrg
267ec681f3Smrg#include <gtest/gtest.h>
277ec681f3Smrg
287ec681f3Smrg#include "util/timespec.h"
297ec681f3Smrg
307ec681f3SmrgTEST(timespec_test, timespec_add)
317ec681f3Smrg{
327ec681f3Smrg   struct timespec a, b, r;
337ec681f3Smrg
347ec681f3Smrg   a.tv_sec = 1;
357ec681f3Smrg   a.tv_nsec = NSEC_PER_SEC - 1;
367ec681f3Smrg   b.tv_sec = 1;
377ec681f3Smrg   b.tv_nsec = 2;
387ec681f3Smrg   timespec_add(&r, &a, &b);
397ec681f3Smrg   EXPECT_EQ(r.tv_sec, 3);
407ec681f3Smrg   EXPECT_EQ(r.tv_nsec, 1);
417ec681f3Smrg}
427ec681f3Smrg
437ec681f3SmrgTEST(timespec_test, timespec_sub)
447ec681f3Smrg{
457ec681f3Smrg   struct timespec a, b, r;
467ec681f3Smrg
477ec681f3Smrg   a.tv_sec = 1;
487ec681f3Smrg   a.tv_nsec = 1;
497ec681f3Smrg   b.tv_sec = 0;
507ec681f3Smrg   b.tv_nsec = 2;
517ec681f3Smrg   timespec_sub(&r, &a, &b);
527ec681f3Smrg   EXPECT_EQ(r.tv_sec, 0);
537ec681f3Smrg   EXPECT_EQ(r.tv_nsec, NSEC_PER_SEC - 1);
547ec681f3Smrg}
557ec681f3Smrg
567ec681f3SmrgTEST(timespec_test, timespec_to_nsec)
577ec681f3Smrg{
587ec681f3Smrg   struct timespec a;
597ec681f3Smrg
607ec681f3Smrg   a.tv_sec = 4;
617ec681f3Smrg   a.tv_nsec = 4;
627ec681f3Smrg   EXPECT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4);
637ec681f3Smrg}
647ec681f3Smrg
657ec681f3SmrgTEST(timespec_test, timespec_to_usec)
667ec681f3Smrg{
677ec681f3Smrg   struct timespec a;
687ec681f3Smrg
697ec681f3Smrg   a.tv_sec = 4;
707ec681f3Smrg   a.tv_nsec = 4000;
717ec681f3Smrg   EXPECT_EQ(timespec_to_usec(&a), (4000000ULL) + 4);
727ec681f3Smrg}
737ec681f3Smrg
747ec681f3SmrgTEST(timespec_test, timespec_to_msec)
757ec681f3Smrg{
767ec681f3Smrg   struct timespec a;
777ec681f3Smrg
787ec681f3Smrg   a.tv_sec = 4;
797ec681f3Smrg   a.tv_nsec = 4000000;
807ec681f3Smrg   EXPECT_EQ(timespec_to_msec(&a), (4000ULL) + 4);
817ec681f3Smrg}
827ec681f3Smrg
837ec681f3SmrgTEST(timespec_test, timespec_to_proto)
847ec681f3Smrg{
857ec681f3Smrg   struct timespec a;
867ec681f3Smrg   uint32_t tv_sec_hi;
877ec681f3Smrg   uint32_t tv_sec_lo;
887ec681f3Smrg   uint32_t tv_nsec;
897ec681f3Smrg
907ec681f3Smrg   a.tv_sec = 0;
917ec681f3Smrg   a.tv_nsec = 0;
927ec681f3Smrg   timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
937ec681f3Smrg   EXPECT_EQ(0, tv_sec_hi);
947ec681f3Smrg   EXPECT_EQ(0, tv_sec_lo);
957ec681f3Smrg   EXPECT_EQ(0, tv_nsec);
967ec681f3Smrg
977ec681f3Smrg   a.tv_sec = 1234;
987ec681f3Smrg   a.tv_nsec = NSEC_PER_SEC - 1;
997ec681f3Smrg   timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
1007ec681f3Smrg   EXPECT_EQ(0, tv_sec_hi);
1017ec681f3Smrg   EXPECT_EQ(1234, tv_sec_lo);
1027ec681f3Smrg   EXPECT_EQ(NSEC_PER_SEC - 1, tv_nsec);
1037ec681f3Smrg
1047ec681f3Smrg   a.tv_sec = (time_t)0x7000123470005678LL;
1057ec681f3Smrg   a.tv_nsec = 1;
1067ec681f3Smrg   timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
1077ec681f3Smrg   EXPECT_EQ((uint64_t)a.tv_sec >> 32, tv_sec_hi);
1087ec681f3Smrg   EXPECT_EQ(0x70005678, tv_sec_lo);
1097ec681f3Smrg   EXPECT_EQ(1, tv_nsec);
1107ec681f3Smrg}
1117ec681f3Smrg
1127ec681f3SmrgTEST(timespec_test, millihz_to_nsec)
1137ec681f3Smrg{
1147ec681f3Smrg   EXPECT_EQ(millihz_to_nsec(60000), 16666666);
1157ec681f3Smrg}
1167ec681f3Smrg
1177ec681f3SmrgTEST(timespec_test, timespec_add_nsec)
1187ec681f3Smrg{
1197ec681f3Smrg   struct timespec a, r;
1207ec681f3Smrg
1217ec681f3Smrg   a.tv_sec = 0;
1227ec681f3Smrg   a.tv_nsec = NSEC_PER_SEC - 1;
1237ec681f3Smrg   timespec_add_nsec(&r, &a, 1);
1247ec681f3Smrg   EXPECT_EQ(1, r.tv_sec);
1257ec681f3Smrg   EXPECT_EQ(0, r.tv_nsec);
1267ec681f3Smrg
1277ec681f3Smrg   timespec_add_nsec(&r, &a, 2);
1287ec681f3Smrg   EXPECT_EQ(1, r.tv_sec);
1297ec681f3Smrg   EXPECT_EQ(1, r.tv_nsec);
1307ec681f3Smrg
1317ec681f3Smrg   timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL));
1327ec681f3Smrg   EXPECT_EQ(2, r.tv_sec);
1337ec681f3Smrg   EXPECT_EQ(NSEC_PER_SEC - 1, r.tv_nsec);
1347ec681f3Smrg
1357ec681f3Smrg   timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL) + 2);
1367ec681f3Smrg   EXPECT_EQ(r.tv_sec, 3);
1377ec681f3Smrg   EXPECT_EQ(r.tv_nsec, 1);
1387ec681f3Smrg
1397ec681f3Smrg   r.tv_sec = 4;
1407ec681f3Smrg   r.tv_nsec = 0;
1417ec681f3Smrg   timespec_add_nsec(&r, &r, NSEC_PER_SEC + 10ULL);
1427ec681f3Smrg   EXPECT_EQ(5, r.tv_sec);
1437ec681f3Smrg   EXPECT_EQ(10, r.tv_nsec);
1447ec681f3Smrg
1457ec681f3Smrg   timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 3ULL) - 9ULL);
1467ec681f3Smrg   EXPECT_EQ(8, r.tv_sec);
1477ec681f3Smrg   EXPECT_EQ(1, r.tv_nsec);
1487ec681f3Smrg
1497ec681f3Smrg   timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 7ULL) + (NSEC_PER_SEC - 1ULL));
1507ec681f3Smrg   EXPECT_EQ(16, r.tv_sec);
1517ec681f3Smrg   EXPECT_EQ(0, r.tv_nsec);
1527ec681f3Smrg}
1537ec681f3Smrg
1547ec681f3SmrgTEST(timespec_test, timespec_add_msec)
1557ec681f3Smrg{
1567ec681f3Smrg   struct timespec a, r;
1577ec681f3Smrg
1587ec681f3Smrg   a.tv_sec = 1000;
1597ec681f3Smrg   a.tv_nsec = 1;
1607ec681f3Smrg   timespec_add_msec(&r, &a, 2002);
1617ec681f3Smrg   EXPECT_EQ(1002, r.tv_sec);
1627ec681f3Smrg   EXPECT_EQ(2000001, r.tv_nsec);
1637ec681f3Smrg}
1647ec681f3Smrg
1657ec681f3SmrgTEST(timespec_test, timespec_sub_to_nsec)
1667ec681f3Smrg{
1677ec681f3Smrg   struct timespec a, b;
1687ec681f3Smrg
1697ec681f3Smrg   a.tv_sec = 1000;
1707ec681f3Smrg   a.tv_nsec = 1;
1717ec681f3Smrg   b.tv_sec = 1;
1727ec681f3Smrg   b.tv_nsec = 2;
1737ec681f3Smrg   EXPECT_EQ((999LL * NSEC_PER_SEC) - 1, timespec_sub_to_nsec(&a, &b));
1747ec681f3Smrg}
1757ec681f3Smrg
1767ec681f3SmrgTEST(timespec_test, timespec_sub_to_msec)
1777ec681f3Smrg{
1787ec681f3Smrg   struct timespec a, b;
1797ec681f3Smrg
1807ec681f3Smrg   a.tv_sec = 1000;
1817ec681f3Smrg   a.tv_nsec = 2000000L;
1827ec681f3Smrg   b.tv_sec = 2;
1837ec681f3Smrg   b.tv_nsec = 1000000L;
1847ec681f3Smrg   EXPECT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b));
1857ec681f3Smrg}
1867ec681f3Smrg
1877ec681f3SmrgTEST(timespec_test, timespec_from_nsec)
1887ec681f3Smrg{
1897ec681f3Smrg   struct timespec a;
1907ec681f3Smrg
1917ec681f3Smrg   timespec_from_nsec(&a, 0);
1927ec681f3Smrg   EXPECT_EQ(0, a.tv_sec);
1937ec681f3Smrg   EXPECT_EQ(0, a.tv_nsec);
1947ec681f3Smrg
1957ec681f3Smrg   timespec_from_nsec(&a, NSEC_PER_SEC - 1);
1967ec681f3Smrg   EXPECT_EQ(0, a.tv_sec);
1977ec681f3Smrg   EXPECT_EQ(NSEC_PER_SEC - 1, a.tv_nsec);
1987ec681f3Smrg
1997ec681f3Smrg   timespec_from_nsec(&a, NSEC_PER_SEC);
2007ec681f3Smrg   EXPECT_EQ(1, a.tv_sec);
2017ec681f3Smrg   EXPECT_EQ(0, a.tv_nsec);
2027ec681f3Smrg
2037ec681f3Smrg   timespec_from_nsec(&a, (5LL * NSEC_PER_SEC) + 1);
2047ec681f3Smrg   EXPECT_EQ(5, a.tv_sec);
2057ec681f3Smrg   EXPECT_EQ(1, a.tv_nsec);
2067ec681f3Smrg
2077ec681f3Smrg   timespec_from_nsec(&a, UINT64_MAX);
2087ec681f3Smrg   EXPECT_EQ(a.tv_nsec, UINT64_MAX % NSEC_PER_SEC);
2097ec681f3Smrg   EXPECT_EQ(a.tv_sec, (time_t)(UINT64_MAX / NSEC_PER_SEC));
2107ec681f3Smrg}
2117ec681f3Smrg
2127ec681f3SmrgTEST(timespec_test, timespec_from_usec)
2137ec681f3Smrg{
2147ec681f3Smrg   struct timespec a;
2157ec681f3Smrg
2167ec681f3Smrg   timespec_from_usec(&a, 0);
2177ec681f3Smrg   EXPECT_EQ(0, a.tv_sec);
2187ec681f3Smrg   EXPECT_EQ(0, a.tv_nsec);
2197ec681f3Smrg
2207ec681f3Smrg   timespec_from_usec(&a, 999999);
2217ec681f3Smrg   EXPECT_EQ(0, a.tv_sec);
2227ec681f3Smrg   EXPECT_EQ(999999 * 1000, a.tv_nsec);
2237ec681f3Smrg
2247ec681f3Smrg   timespec_from_usec(&a, 1000000);
2257ec681f3Smrg   EXPECT_EQ(1, a.tv_sec);
2267ec681f3Smrg   EXPECT_EQ(0, a.tv_nsec);
2277ec681f3Smrg
2287ec681f3Smrg   timespec_from_usec(&a, 5000001);
2297ec681f3Smrg   EXPECT_EQ(5, a.tv_sec);
2307ec681f3Smrg   EXPECT_EQ(1000, a.tv_nsec);
2317ec681f3Smrg}
2327ec681f3Smrg
2337ec681f3SmrgTEST(timespec_test, timespec_from_msec)
2347ec681f3Smrg{
2357ec681f3Smrg   struct timespec a;
2367ec681f3Smrg
2377ec681f3Smrg   timespec_from_msec(&a, 0);
2387ec681f3Smrg   EXPECT_EQ(0, a.tv_sec);
2397ec681f3Smrg   EXPECT_EQ(0, a.tv_nsec);
2407ec681f3Smrg
2417ec681f3Smrg   timespec_from_msec(&a, 999);
2427ec681f3Smrg   EXPECT_EQ(0, a.tv_sec);
2437ec681f3Smrg   EXPECT_EQ(999 * 1000000, a.tv_nsec);
2447ec681f3Smrg
2457ec681f3Smrg   timespec_from_msec(&a, 1000);
2467ec681f3Smrg   EXPECT_EQ(1, a.tv_sec);
2477ec681f3Smrg   EXPECT_EQ(0, a.tv_nsec);
2487ec681f3Smrg
2497ec681f3Smrg   timespec_from_msec(&a, 5001);
2507ec681f3Smrg   EXPECT_EQ(5, a.tv_sec);
2517ec681f3Smrg   EXPECT_EQ(1000000, a.tv_nsec);
2527ec681f3Smrg}
2537ec681f3Smrg
2547ec681f3SmrgTEST(timespec_test, timespec_from_proto)
2557ec681f3Smrg{
2567ec681f3Smrg   struct timespec a;
2577ec681f3Smrg
2587ec681f3Smrg   timespec_from_proto(&a, 0, 0, 0);
2597ec681f3Smrg   EXPECT_EQ(0, a.tv_sec);
2607ec681f3Smrg   EXPECT_EQ(0, a.tv_nsec);
2617ec681f3Smrg
2627ec681f3Smrg   timespec_from_proto(&a, 0, 1234, 9999);
2637ec681f3Smrg   EXPECT_EQ(1234, a.tv_sec);
2647ec681f3Smrg   EXPECT_EQ(9999, a.tv_nsec);
2657ec681f3Smrg
2667ec681f3Smrg   timespec_from_proto(&a, 0x1234, 0x5678, 1);
2677ec681f3Smrg   EXPECT_EQ((time_t)0x0000123400005678LL, a.tv_sec);
2687ec681f3Smrg   EXPECT_EQ(1, a.tv_nsec);
2697ec681f3Smrg}
2707ec681f3Smrg
2717ec681f3SmrgTEST(timespec_test, timespec_is_zero)
2727ec681f3Smrg{
2737ec681f3Smrg   struct timespec zero = { 0 };
2747ec681f3Smrg   struct timespec non_zero_sec = { .tv_sec = 1, .tv_nsec = 0 };
2757ec681f3Smrg   struct timespec non_zero_nsec = { .tv_sec = 0, .tv_nsec = 1 };
2767ec681f3Smrg
2777ec681f3Smrg   EXPECT_TRUE(timespec_is_zero(&zero));
2787ec681f3Smrg   EXPECT_FALSE(timespec_is_zero(&non_zero_nsec));
2797ec681f3Smrg   EXPECT_FALSE(timespec_is_zero(&non_zero_sec));
2807ec681f3Smrg}
2817ec681f3Smrg
2827ec681f3SmrgTEST(timespec_test, timespec_eq)
2837ec681f3Smrg{
2847ec681f3Smrg   struct timespec a = { .tv_sec = 2, .tv_nsec = 1 };
2857ec681f3Smrg   struct timespec b = { .tv_sec = -1, .tv_nsec = 2 };
2867ec681f3Smrg
2877ec681f3Smrg   EXPECT_TRUE(timespec_eq(&a, &a));
2887ec681f3Smrg   EXPECT_TRUE(timespec_eq(&b, &b));
2897ec681f3Smrg
2907ec681f3Smrg   EXPECT_FALSE(timespec_eq(&a, &b));
2917ec681f3Smrg   EXPECT_FALSE(timespec_eq(&b, &a));
2927ec681f3Smrg}
293