1 1.1 nia /*- 2 1.1 nia * Copyright (c) 2025 The NetBSD Foundation, Inc. 3 1.1 nia * All rights reserved. 4 1.1 nia * 5 1.1 nia * This code is derived from software contributed to The NetBSD Foundation 6 1.1 nia * by Nia Alarie. 7 1.1 nia * 8 1.1 nia * Redistribution and use in source and binary forms, with or without 9 1.1 nia * modification, are permitted provided that the following conditions 10 1.1 nia * are met: 11 1.1 nia * 1. Redistributions of source code must retain the above copyright 12 1.1 nia * notice, this list of conditions and the following disclaimer. 13 1.1 nia * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 nia * notice, this list of conditions and the following disclaimer in the 15 1.1 nia * documentation and/or other materials provided with the distribution. 16 1.1 nia * 17 1.1 nia * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 nia * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 nia * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 nia * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 nia * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 nia * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 nia * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 nia * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 nia * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 nia * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 nia * POSSIBILITY OF SUCH DAMAGE. 28 1.1 nia */ 29 1.1 nia 30 1.1 nia #include <atf-c.h> 31 1.1 nia 32 1.1 nia #include <limits.h> 33 1.1 nia #include <time.h> 34 1.1 nia 35 1.1 nia ATF_TC(timespec_getres); 36 1.1 nia ATF_TC_HEAD(timespec_getres, tc) 37 1.1 nia { 38 1.1 nia atf_tc_set_md_var(tc, "descr", "Resolution tests for timespec_getres"); 39 1.1 nia } 40 1.1 nia 41 1.1 nia ATF_TC_BODY(timespec_getres, tc) 42 1.1 nia { 43 1.1 nia struct timespec ts, ts2; 44 1.1 nia 45 1.1 nia ATF_REQUIRE_EQ(timespec_getres(&ts, TIME_MONOTONIC), TIME_MONOTONIC); 46 1.1 nia ATF_REQUIRE_EQ(clock_getres(CLOCK_MONOTONIC, &ts2), 0); 47 1.1 nia 48 1.1 nia ATF_REQUIRE_EQ(ts.tv_sec, ts2.tv_sec); 49 1.1 nia ATF_REQUIRE_EQ(ts.tv_nsec, ts2.tv_nsec); 50 1.1 nia 51 1.1 nia ATF_REQUIRE_EQ(timespec_getres(&ts, TIME_UTC), TIME_UTC); 52 1.1 nia ATF_REQUIRE_EQ(clock_getres(CLOCK_REALTIME, &ts2), 0); 53 1.1 nia 54 1.1 nia ATF_REQUIRE_EQ(ts.tv_sec, ts2.tv_sec); 55 1.1 nia ATF_REQUIRE_EQ(ts.tv_nsec, ts2.tv_nsec); 56 1.1 nia 57 1.1 nia /* now an invalid value */ 58 1.1 nia ATF_REQUIRE_EQ(timespec_getres(&ts, INT_MAX), 0); 59 1.1 nia } 60 1.1 nia 61 1.1 nia ATF_TC(timespec_get); 62 1.1 nia ATF_TC_HEAD(timespec_get, tc) 63 1.1 nia { 64 1.1 nia atf_tc_set_md_var(tc, "descr", "Basic tests for timespec_get"); 65 1.1 nia } 66 1.1 nia 67 1.1 nia ATF_TC_BODY(timespec_get, tc) 68 1.1 nia { 69 1.1 nia struct timespec ts, ts2; 70 1.1 nia 71 1.1 nia ATF_REQUIRE_EQ(timespec_get(&ts, TIME_UTC), TIME_UTC); 72 1.1 nia ATF_REQUIRE_EQ(clock_gettime(CLOCK_REALTIME, &ts2), 0); 73 1.1 nia 74 1.1 nia /* 75 1.1 nia * basically test that these clocks (which should be the same source) 76 1.1 nia * aren't too wildly apart... 77 1.1 nia */ 78 1.1 nia 79 1.1 nia if (ts2.tv_sec >= ts.tv_sec) { 80 1.1 nia ATF_REQUIRE((ts2.tv_sec - ts.tv_sec) < 86400); 81 1.1 nia } else { 82 1.1 nia ATF_REQUIRE((ts.tv_sec - ts2.tv_sec) < 86400); 83 1.1 nia } 84 1.1 nia 85 1.1 nia /* now an invalid value */ 86 1.1 nia ATF_REQUIRE_EQ(timespec_get(&ts, INT_MAX), 0); 87 1.1 nia } 88 1.1 nia 89 1.1 nia ATF_TC(timespec_get_monotonic); 90 1.1 nia ATF_TC_HEAD(timespec_get_monotonic, tc) 91 1.1 nia { 92 1.1 nia atf_tc_set_md_var(tc, "descr", "Monotonic tests for timespec_getres"); 93 1.1 nia } 94 1.1 nia 95 1.1 nia ATF_TC_BODY(timespec_get_monotonic, tc) 96 1.1 nia { 97 1.1 nia struct timespec ts, ts2; 98 1.1 nia 99 1.1 nia ATF_REQUIRE_EQ(timespec_get(&ts, TIME_MONOTONIC), TIME_MONOTONIC); 100 1.1 nia ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &ts2), 0); 101 1.1 nia 102 1.1 nia /* 103 1.1 nia * basically test that these clocks (which should be the same source) 104 1.1 nia * aren't too wildly apart... 105 1.1 nia */ 106 1.1 nia 107 1.1 nia ATF_REQUIRE(ts2.tv_sec >= ts.tv_sec); 108 1.1 nia ATF_REQUIRE((ts2.tv_sec - ts.tv_sec) < 86400); 109 1.1 nia 110 1.1 nia /* test that it's actually monotonic */ 111 1.1 nia ATF_REQUIRE_EQ(timespec_get(&ts2, TIME_MONOTONIC), TIME_MONOTONIC); 112 1.1 nia ATF_REQUIRE(ts2.tv_sec >= ts.tv_sec); 113 1.1 nia } 114 1.1 nia 115 1.1 nia ATF_TP_ADD_TCS(tp) 116 1.1 nia { 117 1.1 nia ATF_TP_ADD_TC(tp, timespec_getres); 118 1.1 nia ATF_TP_ADD_TC(tp, timespec_get); 119 1.1 nia ATF_TP_ADD_TC(tp, timespec_get_monotonic); 120 1.1 nia 121 1.1 nia return atf_no_error(); 122 1.1 nia } 123 1.1 nia 124