usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-gettimeofday.c
blobf5990aedcebbccf7aba5bad7fda56eb9e8aa6164
1 /*
2 * Copyright (C) 2005, 2007, 2009-2024 Free Software Foundation, Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering and Bruno Haible. */
19 #include <config.h>
21 #include <sys/time.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (gettimeofday, int,
25 (struct timeval *, GETTIMEOFDAY_TIMEZONE *));
27 #include <time.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include "macros.h"
35 static void
36 test_clobber ()
38 time_t t = 0;
39 struct tm *lt;
40 struct tm saved_lt;
41 struct timeval tv;
42 lt = localtime (&t);
43 saved_lt = *lt;
44 gettimeofday (&tv, NULL);
45 if (memcmp (lt, &saved_lt, sizeof (struct tm)) != 0)
47 fprintf (stderr, "gettimeofday still clobbers the localtime buffer!\n");
48 exit (1);
52 static void
53 test_consistency ()
55 struct timeval tv1;
56 time_t tt2;
57 struct timeval tv3;
58 time_t tt4;
60 ASSERT (gettimeofday (&tv1, NULL) == 0);
61 tt2 = time (NULL);
62 ASSERT (gettimeofday (&tv3, NULL) == 0);
63 tt4 = time (NULL);
65 /* Verify monotonicity of gettimeofday(). */
66 ASSERT (tv1.tv_sec < tv3.tv_sec
67 || (tv1.tv_sec == tv3.tv_sec && tv1.tv_usec <= tv3.tv_usec));
69 /* Verify monotonicity of time(). */
70 ASSERT (tt2 <= tt4);
72 /* Verify that the tv_sec field of the result is the same as time(NULL). */
73 /* Note: It's here that the dependency to the 'time' module is needed.
74 Without it, this assertion would sometimes fail on glibc systems, see
75 https://sourceware.org/bugzilla/show_bug.cgi?id=30200 */
76 ASSERT (tv1.tv_sec <= tt2);
77 ASSERT (tt2 <= tv3.tv_sec);
78 ASSERT (tv3.tv_sec <= tt4);
81 int
82 main (void)
84 test_clobber ();
85 test_consistency ();
87 return test_exit_status;