powerpc64le: _init/_fini file changes for ROP
[glibc.git] / time / tst-gettimeofday.c
blob978ae28587d486f29eab9f190fa891def58870a9
1 /* Test gettimeofday function.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <sys/time.h>
20 #include <time.h>
22 #include <support/check.h>
23 #include <support/test-driver.h>
25 /* Compare two struct timeval values, returning a value -1, 0 or 1. */
27 int
28 compare_timeval (const struct timeval *tv1, const struct timeval *tv2)
30 if (tv1->tv_sec < tv2->tv_sec)
31 return -1;
32 if (tv1->tv_sec > tv2->tv_sec)
33 return 1;
34 if (tv1->tv_usec < tv2->tv_usec)
35 return -1;
36 if (tv1->tv_usec > tv2->tv_usec)
37 return 1;
38 return 0;
41 int
42 do_test (void)
44 struct timeval tv1, tv2, tv3;
45 int ret;
46 time_t t1;
47 /* Verify that the calls to gettimeofday succeed, that the time does
48 not decrease, and that time returns a truncated (not rounded)
49 version of the time. */
50 t1 = time (NULL);
51 TEST_VERIFY_EXIT (t1 != (time_t) -1);
52 ret = gettimeofday (&tv1, NULL);
53 TEST_VERIFY_EXIT (ret == 0);
54 TEST_VERIFY (t1 <= tv1.tv_sec);
55 TEST_VERIFY (tv1.tv_usec >= 0);
56 TEST_VERIFY (tv1.tv_usec < 1000000);
57 ret = gettimeofday (&tv2, NULL);
58 TEST_VERIFY_EXIT (ret == 0);
59 TEST_VERIFY (compare_timeval (&tv1, &tv2) <= 0);
60 TEST_VERIFY (tv2.tv_usec >= 0);
61 TEST_VERIFY (tv2.tv_usec < 1000000);
62 /* Also verify that after sleeping, the time returned has increased.
63 Repeat several times to verify that each time, the time from the
64 time function is truncated not rounded. */
65 const struct timespec duration = { .tv_nsec = 100000000 };
66 for (int i = 0; i < 10; i++)
68 ret = nanosleep (&duration, NULL);
69 TEST_VERIFY_EXIT (ret == 0);
70 t1 = time (NULL);
71 TEST_VERIFY_EXIT (t1 != (time_t) -1);
72 ret = gettimeofday (&tv3, NULL);
73 TEST_VERIFY_EXIT (ret == 0);
74 TEST_VERIFY (compare_timeval (&tv2, &tv3) < 0);
75 TEST_VERIFY (t1 <= tv3.tv_sec);
76 TEST_VERIFY (tv3.tv_usec >= 0);
77 TEST_VERIFY (tv3.tv_usec < 1000000);
78 tv2 = tv3;
80 /* Also test with the obsolete tz argument not being NULL. */
81 struct timezone tz = { 0 };
82 t1 = time (NULL);
83 TEST_VERIFY_EXIT (t1 != (time_t) -1);
84 ret = gettimeofday (&tv3, &tz);
85 TEST_VERIFY_EXIT (ret == 0);
86 TEST_VERIFY (t1 <= tv3.tv_sec);
87 TEST_VERIFY (compare_timeval (&tv2, &tv3) <= 0);
88 TEST_VERIFY (tv3.tv_usec >= 0);
89 TEST_VERIFY (tv3.tv_usec < 1000000);
90 return 0;
93 #include <support/test-driver.c>