1 /* Test timespec functions.
2 Copyright 2015-2016 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert. */
29 static struct { int s; int ns; } const prototype[] =
33 { INT_MIN, TIMESPEC_RESOLUTION - 1 },
36 { INT_MIN + 1, TIMESPEC_RESOLUTION - 1 },
39 { -1, TIMESPEC_RESOLUTION - 1 },
42 { 0, TIMESPEC_RESOLUTION - 1 },
45 { 1, TIMESPEC_RESOLUTION - 1 },
48 { 1234567890, TIMESPEC_RESOLUTION - 1 },
51 { INT_MAX - 1, TIMESPEC_RESOLUTION - 1 },
54 { INT_MAX, TIMESPEC_RESOLUTION - 1 },
55 { INT_MAX, 2 * TIMESPEC_RESOLUTION }
57 enum { nprototypes = sizeof prototype / sizeof *prototype };
60 valid (struct timespec a)
62 return 0 <= a.tv_nsec && a.tv_nsec < TIMESPEC_RESOLUTION;
68 return i < 0 ? -1 : 0 < i;
72 cmp (struct timespec a, struct timespec b)
74 return sign (timespec_cmp (a, b));
78 eq (struct timespec a, struct timespec b)
80 return timespec_cmp (a, b) == 0;
84 extremal (struct timespec a)
86 return ((a.tv_sec == TYPE_MINIMUM (time_t) && a.tv_nsec == 0)
87 || (a.tv_sec == TYPE_MAXIMUM (time_t)
88 && a.tv_nsec == TIMESPEC_RESOLUTION - 1));
95 struct timespec test[nprototypes + 1];
97 int computed_resolution = 1;
98 struct timespec prevroundtrip;
100 test[0] = make_timespec (TYPE_MINIMUM (time_t), -1);
102 for (i = 0; i < nprototypes; i++)
104 int s = prototype[i].s;
105 if (TYPE_SIGNED (time_t) || 0 <= s)
107 time_t t = (s <= INT_MIN + 1 ? s - INT_MIN + TYPE_MINIMUM (time_t)
108 : INT_MAX - 1 <= s ? s - INT_MAX + TYPE_MAXIMUM (time_t)
110 test[ntests++] = make_timespec (t, prototype[i].ns);
114 for (i = 0; i < LOG10_TIMESPEC_RESOLUTION; i++)
115 computed_resolution *= 10;
116 ASSERT (computed_resolution == TIMESPEC_RESOLUTION);
118 for (i = 0; i < ntests; i++)
120 struct timespec a = test[i];
122 struct timespec roundtrip = dtotimespec (timespectod (a));
124 ASSERT (cmp (prevroundtrip, roundtrip) <= 0);
125 prevroundtrip = roundtrip;
127 ASSERT (sign (timespec_sign (a)) == cmp (a, make_timespec (0, 0)));
130 for (j = 0; j < ntests; j++)
132 struct timespec b = test[j];
135 struct timespec sum = timespec_add (a, b);
136 struct timespec diff = timespec_sub (a, b);
137 struct timespec rdiff = timespec_sub (b, a);
138 ASSERT (cmp (a, b) == sign (i - j));
139 ASSERT (eq (sum, timespec_add (b, a)));
140 if (! extremal (sum))
142 ASSERT (eq (a, timespec_sub (sum, b)));
143 ASSERT (eq (b, timespec_sub (sum, a)));
145 for (k = 0; k < ntests; k++)
147 struct timespec c = test[k];
150 struct timespec sumbc = timespec_add (b, c);
151 if (! extremal (sumbc))
152 ASSERT (eq (timespec_add (a, sumbc),
153 timespec_add (sum, c)));
157 if (! extremal (diff))
158 ASSERT (eq (a, timespec_add (diff, b)));
159 if (! extremal (rdiff))
160 ASSERT (eq (b, timespec_add (rdiff, a)));