aboutsummaryrefslogtreecommitdiff
path: root/time/tst-gettimeofday.c
blob: 978ae28587d486f29eab9f190fa891def58870a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Test gettimeofday function.
   Copyright (C) 2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <sys/time.h>
#include <time.h>

#include <support/check.h>
#include <support/test-driver.h>

/* Compare two struct timeval values, returning a value -1, 0 or 1.  */

int
compare_timeval (const struct timeval *tv1, const struct timeval *tv2)
{
  if (tv1->tv_sec < tv2->tv_sec)
    return -1;
  if (tv1->tv_sec > tv2->tv_sec)
    return 1;
  if (tv1->tv_usec < tv2->tv_usec)
    return -1;
  if (tv1->tv_usec > tv2->tv_usec)
    return 1;
  return 0;
}

int
do_test (void)
{
  struct timeval tv1, tv2, tv3;
  int ret;
  time_t t1;
  /* Verify that the calls to gettimeofday succeed, that the time does
     not decrease, and that time returns a truncated (not rounded)
     version of the time.  */
  t1 = time (NULL);
  TEST_VERIFY_EXIT (t1 != (time_t) -1);
  ret = gettimeofday (&tv1, NULL);
  TEST_VERIFY_EXIT (ret == 0);
  TEST_VERIFY (t1 <= tv1.tv_sec);
  TEST_VERIFY (tv1.tv_usec >= 0);
  TEST_VERIFY (tv1.tv_usec < 1000000);
  ret = gettimeofday (&tv2, NULL);
  TEST_VERIFY_EXIT (ret == 0);
  TEST_VERIFY (compare_timeval (&tv1, &tv2) <= 0);
  TEST_VERIFY (tv2.tv_usec >= 0);
  TEST_VERIFY (tv2.tv_usec < 1000000);
  /* Also verify that after sleeping, the time returned has increased.
     Repeat several times to verify that each time, the time from the
     time function is truncated not rounded.  */
  const struct timespec duration = { .tv_nsec = 100000000 };
  for (int i = 0; i < 10; i++)
    {
      ret = nanosleep (&duration, NULL);
      TEST_VERIFY_EXIT (ret == 0);
      t1 = time (NULL);
      TEST_VERIFY_EXIT (t1 != (time_t) -1);
      ret = gettimeofday (&tv3, NULL);
      TEST_VERIFY_EXIT (ret == 0);
      TEST_VERIFY (compare_timeval (&tv2, &tv3) < 0);
      TEST_VERIFY (t1 <= tv3.tv_sec);
      TEST_VERIFY (tv3.tv_usec >= 0);
      TEST_VERIFY (tv3.tv_usec < 1000000);
      tv2 = tv3;
    }
  /* Also test with the obsolete tz argument not being NULL.  */
  struct timezone tz = { 0 };
  t1 = time (NULL);
  TEST_VERIFY_EXIT (t1 != (time_t) -1);
  ret = gettimeofday (&tv3, &tz);
  TEST_VERIFY_EXIT (ret == 0);
  TEST_VERIFY (t1 <= tv3.tv_sec);
  TEST_VERIFY (compare_timeval (&tv2, &tv3) <= 0);
  TEST_VERIFY (tv3.tv_usec >= 0);
  TEST_VERIFY (tv3.tv_usec < 1000000);
  return 0;
}

#include <support/test-driver.c>