aboutsummaryrefslogtreecommitdiff
path: root/inet/tst-deadline.c
blob: ed04345c3513214e17f93658bd566be7da9009c3 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Tests for computing deadlines for timeouts.
   Copyright (C) 2017 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
   <http://www.gnu.org/licenses/>.  */

#include <inet/net-internal.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <support/check.h>

/* Find the maximum value which can be represented in a time_t.  */
static time_t
time_t_max (void)
{
  _Static_assert (0 > (time_t) -1, "time_t is signed");
  uintmax_t current = 1;
  while (true)
    {
      uintmax_t next = current * 2;
      /* This cannot happen because time_t is signed.  */
      TEST_VERIFY_EXIT (next > current);
      ++next;
      if ((time_t) next < 0 || next != (uintmax_t) (time_t) next)
        /* Value cannot be represented in time_t.  Return the previous
           value. */
        return current;
      current = next;
    }
}

static int
do_test (void)
{
  {
    struct deadline_current_time current_time = __deadline_current_time ();
    TEST_VERIFY (current_time.current.tv_sec >= 0);
    current_time = __deadline_current_time ();
    /* Due to CLOCK_MONOTONIC, either seconds or nanoseconds are
       greater than zero.  This is also true for the gettimeofday
       fallback.  */
    TEST_VERIFY (current_time.current.tv_sec >= 0);
    TEST_VERIFY (current_time.current.tv_sec > 0
                 || current_time.current.tv_nsec > 0);
  }

  /* Check basic computations of deadlines.  */
  struct deadline_current_time current_time = { { 1, 123456789 } };
  struct deadline deadline = __deadline_from_timeval
    (current_time, (struct timeval) { 0, 1 });
  TEST_VERIFY (deadline.absolute.tv_sec == 1);
  TEST_VERIFY (deadline.absolute.tv_nsec == 123457789);
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1);

  deadline = __deadline_from_timeval
    (current_time, ((struct timeval) { 0, 2 }));
  TEST_VERIFY (deadline.absolute.tv_sec == 1);
  TEST_VERIFY (deadline.absolute.tv_nsec == 123458789);
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1);

  deadline = __deadline_from_timeval
    (current_time, ((struct timeval) { 1, 0 }));
  TEST_VERIFY (deadline.absolute.tv_sec == 2);
  TEST_VERIFY (deadline.absolute.tv_nsec == 123456789);
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);

  /* Check if timeouts are correctly rounded up to the next
     millisecond.  */
  for (int i = 0; i < 999999; ++i)
    {
      ++current_time.current.tv_nsec;
      TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
    }

  /* A full millisecond has elapsed, so the time to the deadline is
     now less than 1000.  */
  ++current_time.current.tv_nsec;
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 999);

  /* Check __deadline_to_ms carry-over.  */
  current_time = (struct deadline_current_time) { { 9, 123456789 } };
  deadline = (struct deadline) { { 10, 122456789 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 999);
  deadline = (struct deadline) { { 10, 122456790 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
  deadline = (struct deadline) { { 10, 123456788 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
  deadline = (struct deadline) { { 10, 123456789 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);

  /* Check __deadline_to_ms overflow.  */
  deadline = (struct deadline) { { INT_MAX - 1, 1 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == INT_MAX);

  /* Check __deadline_to_ms for elapsed deadlines.  */
  current_time = (struct deadline_current_time) { { 9, 123456789 } };
  deadline.absolute = current_time.current;
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
  current_time = (struct deadline_current_time) { { 9, 123456790 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
  current_time = (struct deadline_current_time) { { 10, 0 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
  current_time = (struct deadline_current_time) { { 10, 123456788 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
  current_time = (struct deadline_current_time) { { 10, 123456789 } };
  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);

  /* Check carry-over in __deadline_from_timeval.  */
  current_time = (struct deadline_current_time) { { 9, 998000001 } };
  for (int i = 0; i < 2000; ++i)
    {
      deadline = __deadline_from_timeval
        (current_time, (struct timeval) { 1, i });
      TEST_VERIFY (deadline.absolute.tv_sec == 10);
      TEST_VERIFY (deadline.absolute.tv_nsec == 998000001 + i * 1000);
    }
  for (int i = 2000; i < 3000; ++i)
    {
      deadline = __deadline_from_timeval
        (current_time, (struct timeval) { 2, i });
      TEST_VERIFY (deadline.absolute.tv_sec == 12);
      TEST_VERIFY (deadline.absolute.tv_nsec == 1 + (i - 2000) * 1000);
    }

  /* Check infinite deadlines.  */
  deadline = __deadline_from_timeval
    ((struct deadline_current_time) { { 0, 1000 * 1000 * 1000 - 1000 } },
     (struct timeval) { time_t_max (), 1 });
  TEST_VERIFY (__deadline_is_infinite (deadline));
  deadline = __deadline_from_timeval
    ((struct deadline_current_time) { { 0, 1000 * 1000 * 1000 - 1001 } },
     (struct timeval) { time_t_max (), 1 });
  TEST_VERIFY (!__deadline_is_infinite (deadline));
  deadline = __deadline_from_timeval
    ((struct deadline_current_time)
       { { time_t_max (), 1000 * 1000 * 1000 - 1000 } },
     (struct timeval) { 0, 1 });
  TEST_VERIFY (__deadline_is_infinite (deadline));
  deadline = __deadline_from_timeval
    ((struct deadline_current_time)
       { { time_t_max () / 2 + 1, 0 } },
     (struct timeval) { time_t_max () / 2 + 1, 0 });
  TEST_VERIFY (__deadline_is_infinite (deadline));

  /* Check __deadline_first behavior.  */
  deadline = __deadline_first
    ((struct deadline) { { 1, 2 } },
     (struct deadline) { { 1, 3 } });
  TEST_VERIFY (deadline.absolute.tv_sec == 1);
  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
  deadline = __deadline_first
    ((struct deadline) { { 1, 3 } },
     (struct deadline) { { 1, 2 } });
  TEST_VERIFY (deadline.absolute.tv_sec == 1);
  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
  deadline = __deadline_first
    ((struct deadline) { { 1, 2 } },
     (struct deadline) { { 2, 1 } });
  TEST_VERIFY (deadline.absolute.tv_sec == 1);
  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
  deadline = __deadline_first
    ((struct deadline) { { 1, 2 } },
     (struct deadline) { { 2, 4 } });
  TEST_VERIFY (deadline.absolute.tv_sec == 1);
  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
  deadline = __deadline_first
    ((struct deadline) { { 2, 4 } },
     (struct deadline) { { 1, 2 } });
  TEST_VERIFY (deadline.absolute.tv_sec == 1);
  TEST_VERIFY (deadline.absolute.tv_nsec == 2);

  return 0;
}

#include <support/test-driver.c>