Debian Codegraph @

test/core/gpr/time_test.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 /* Test of gpr time support. */
20
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "gtest/gtest.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/time.h>
29
30 #include "test/core/util/test_config.h"
31
32 static void to_fp(void* arg, const char* buf, size_t len) {
33 fwrite(buf, 1, len, static_cast<FILE*>(arg));
34 }
35
36 /* Convert gpr_intmax x to ascii base b (2..16), and write with
37 (*writer)(arg, ...), zero padding to "chars" digits). */
38 static void i_to_s(intmax_t x, int base, int chars,
39 void (*writer)(void* arg, const char* buf, size_t len),
40 void* arg) {
41 char buf[64];
42 char fmt[32];
43 ASSERT_TRUE(base == 16 || base == 10);
44 sprintf(fmt, "%%0%d%s", chars, base == 16 ? PRIxMAX : PRIdMAX);
45 sprintf(buf, fmt, x);
46 (*writer)(arg, buf, strlen(buf));
47 }
48
49 /* Convert ts to ascii, and write with (*writer)(arg, ...). */
50 static void ts_to_s(gpr_timespec t,
51 void (*writer)(void* arg, const char* buf, size_t len),
52 void* arg) {
53 if (t.tv_sec < 0 && t.tv_nsec != 0) {
54 t.tv_sec++;
55 t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec;
56 }
57 i_to_s(t.tv_sec, 10, 0, writer, arg);
58 (*writer)(arg, ".", 1);
59 i_to_s(t.tv_nsec, 10, 9, writer, arg);
60 }
61
62 TEST(TimeTest, Values) {
63 int i;
64
65 gpr_timespec x = gpr_time_0(GPR_CLOCK_REALTIME);
66 ASSERT_TRUE(x.tv_sec == 0 && x.tv_nsec == 0);
67
68 x = gpr_inf_future(GPR_CLOCK_REALTIME);
69 fprintf(stderr, "far future ");
70 fflush(stderr);
71 i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
72 fprintf(stderr, "\n");
73 ASSERT_EQ(x.tv_sec, INT64_MAX);
74 fprintf(stderr, "far future ");
75 fflush(stderr);
76 ts_to_s(x, &to_fp, stderr);
77 fprintf(stderr, "\n");
78 fflush(stderr);
79
80 x = gpr_inf_past(GPR_CLOCK_REALTIME);
81 fprintf(stderr, "far past ");
82 fflush(stderr);
83 i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
84 fprintf(stderr, "\n");
85 fflush(stderr);
86 ASSERT_EQ(x.tv_sec, INT64_MIN);
87 fprintf(stderr, "far past ");
88 fflush(stderr);
89 ts_to_s(x, &to_fp, stderr);
90 fprintf(stderr, "\n");
91 fflush(stderr);
92
93 for (i = 1; i != 1000 * 1000 * 1000; i *= 10) {
94 x = gpr_time_from_micros(i, GPR_TIMESPAN);
95 ASSERT_TRUE(x.tv_sec == i / GPR_US_PER_SEC &&
96 x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US);
97 x = gpr_time_from_nanos(i, GPR_TIMESPAN);
98 ASSERT_TRUE(x.tv_sec == i / GPR_NS_PER_SEC &&
99 x.tv_nsec == (i % GPR_NS_PER_SEC));
100 x = gpr_time_from_millis(i, GPR_TIMESPAN);
101 ASSERT_TRUE(x.tv_sec == i / GPR_MS_PER_SEC &&
102 x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS);
103 }
104
105 /* Test possible overflow in conversion of -ve values. */
106 x = gpr_time_from_micros(-(INT64_MAX - 999997), GPR_TIMESPAN);
107 ASSERT_LT(x.tv_sec, 0);
108 ASSERT_TRUE(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
109 EXPECT_EQ((x.tv_sec * GPR_US_PER_SEC +
110 x.tv_nsec / (GPR_NS_PER_SEC / GPR_US_PER_SEC)),
111 -(INT64_MAX - 999997));
112
113 x = gpr_time_from_nanos(-(INT64_MAX - 999999997), GPR_TIMESPAN);
114 ASSERT_LT(x.tv_sec, 0);
115 ASSERT_TRUE(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
116 EXPECT_EQ((x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec), -(INT64_MAX - 999999997));
117
118 x = gpr_time_from_millis(-(INT64_MAX - 997), GPR_TIMESPAN);
119 ASSERT_LT(x.tv_sec, 0);
120 ASSERT_TRUE(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
121 EXPECT_EQ((x.tv_sec * GPR_MS_PER_SEC +
122 x.tv_nsec / (GPR_NS_PER_SEC / GPR_MS_PER_SEC)),
123 -(INT64_MAX - 997));
124
125 /* Test general -ve values. */
126 for (i = -1; i > -1000 * 1000 * 1000; i *= 7) {
127 x = gpr_time_from_micros(i, GPR_TIMESPAN);
128 ASSERT_EQ(x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US, i);
129 x = gpr_time_from_nanos(i, GPR_TIMESPAN);
130 ASSERT_EQ(x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec, i);
131 x = gpr_time_from_millis(i, GPR_TIMESPAN);
132 ASSERT_EQ(x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS, i);
133 }
134 }
135
136 TEST(TimeTest, AddSub) {
137 int i;
138 int j;
139 int k;
140 /* Basic addition and subtraction. */
141 for (i = -100; i <= 100; i++) {
142 for (j = -100; j <= 100; j++) {
143 for (k = 1; k <= 10000000; k *= 10) {
144 int sum = i + j;
145 int diff = i - j;
146 gpr_timespec it = gpr_time_from_micros(i * k, GPR_TIMESPAN);
147 gpr_timespec jt = gpr_time_from_micros(j * k, GPR_TIMESPAN);
148 gpr_timespec sumt = gpr_time_add(it, jt);
149 gpr_timespec difft = gpr_time_sub(it, jt);
150 if (gpr_time_cmp(gpr_time_from_micros(sum * k, GPR_TIMESPAN), sumt) !=
151 0) {
152 fprintf(stderr, "i %d j %d sum %d sumt ", i, j, sum);
153 fflush(stderr);
154 ts_to_s(sumt, &to_fp, stderr);
155 fprintf(stderr, "\n");
156 fflush(stderr);
157 ASSERT_TRUE(0);
158 }
159 if (gpr_time_cmp(gpr_time_from_micros(diff * k, GPR_TIMESPAN), difft) !=
160 0) {
161 fprintf(stderr, "i %d j %d diff %d diff ", i, j, diff);
162 fflush(stderr);
163 ts_to_s(sumt, &to_fp, stderr);
164 fprintf(stderr, "\n");
165 fflush(stderr);
166 ASSERT_TRUE(0);
167 }
168 }
169 }
170 }
171 }
172
173 TEST(TimeTest, Overflow) {
174 /* overflow */
175 gpr_timespec x = gpr_time_from_micros(1, GPR_TIMESPAN);
176 do {
177 x = gpr_time_add(x, x);
178 } while (gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) < 0);
179 ASSERT_EQ(gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)), 0);
180 x = gpr_time_from_micros(-1, GPR_TIMESPAN);
181 do {
182 x = gpr_time_add(x, x);
183 } while (gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) > 0);
184 ASSERT_EQ(gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)), 0);
185 }
186
187 TEST(TimeTest, StickyInfinities) {
188 int i;
189 int j;
190 int k;
191 gpr_timespec infinity[2];
192 gpr_timespec addend[3];
193 infinity[0] = gpr_inf_future(GPR_TIMESPAN);
194 infinity[1] = gpr_inf_past(GPR_TIMESPAN);
195 addend[0] = gpr_inf_future(GPR_TIMESPAN);
196 addend[1] = gpr_inf_past(GPR_TIMESPAN);
197 addend[2] = gpr_time_0(GPR_TIMESPAN);
198
199 /* Infinities are sticky */
200 for (i = 0; i != sizeof(infinity) / sizeof(infinity[0]); i++) {
201 for (j = 0; j != sizeof(addend) / sizeof(addend[0]); j++) {
202 gpr_timespec x = gpr_time_add(infinity[i], addend[j]);
203 ASSERT_EQ(gpr_time_cmp(x, infinity[i]), 0);
204 x = gpr_time_sub(infinity[i], addend[j]);
205 ASSERT_EQ(gpr_time_cmp(x, infinity[i]), 0);
206 }
207 for (k = -200; k <= 200; k++) {
208 gpr_timespec y = gpr_time_from_micros(k * 100000, GPR_TIMESPAN);
209 gpr_timespec x = gpr_time_add(infinity[i], y);
210 ASSERT_EQ(gpr_time_cmp(x, infinity[i]), 0);
211 x = gpr_time_sub(infinity[i], y);
212 ASSERT_EQ(gpr_time_cmp(x, infinity[i]), 0);
213 }
214 }
215 }
216
217 TEST(TimeTest, Similar) {
218 ASSERT_EQ(1, gpr_time_similar(gpr_inf_future(GPR_TIMESPAN),
219 gpr_inf_future(GPR_TIMESPAN),
220 gpr_time_0(GPR_TIMESPAN)));
221 ASSERT_EQ(1, gpr_time_similar(gpr_inf_past(GPR_TIMESPAN),
222 gpr_inf_past(GPR_TIMESPAN),
223 gpr_time_0(GPR_TIMESPAN)));
224 ASSERT_EQ(0, gpr_time_similar(gpr_inf_past(GPR_TIMESPAN),
225 gpr_inf_future(GPR_TIMESPAN),
226 gpr_time_0(GPR_TIMESPAN)));
227 ASSERT_EQ(0, gpr_time_similar(gpr_inf_future(GPR_TIMESPAN),
228 gpr_inf_past(GPR_TIMESPAN),
229 gpr_time_0(GPR_TIMESPAN)));
230 ASSERT_EQ(1, gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN),
231 gpr_time_from_micros(10, GPR_TIMESPAN),
232 gpr_time_0(GPR_TIMESPAN)));
233 ASSERT_EQ(1, gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN),
234 gpr_time_from_micros(15, GPR_TIMESPAN),
235 gpr_time_from_micros(10, GPR_TIMESPAN)));
236 ASSERT_EQ(1, gpr_time_similar(gpr_time_from_micros(15, GPR_TIMESPAN),
237 gpr_time_from_micros(10, GPR_TIMESPAN),
238 gpr_time_from_micros(10, GPR_TIMESPAN)));
239 ASSERT_EQ(0, gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN),
240 gpr_time_from_micros(25, GPR_TIMESPAN),
241 gpr_time_from_micros(10, GPR_TIMESPAN)));
242 ASSERT_EQ(0, gpr_time_similar(gpr_time_from_micros(25, GPR_TIMESPAN),
243 gpr_time_from_micros(10, GPR_TIMESPAN),
244 gpr_time_from_micros(10, GPR_TIMESPAN)));
245 }
246
247 TEST(TimeTest, ConvertExtreme) {
248 gpr_timespec realtime = {INT64_MAX, 1, GPR_CLOCK_REALTIME};
249 gpr_timespec monotime = gpr_convert_clock_type(realtime, GPR_CLOCK_MONOTONIC);
250 ASSERT_EQ(monotime.tv_sec, realtime.tv_sec);
251 ASSERT_EQ(monotime.clock_type, GPR_CLOCK_MONOTONIC);
252 }
253
254 TEST(TimeTest, CmpExtreme) {
255 gpr_timespec t1 = {INT64_MAX, 1, GPR_CLOCK_REALTIME};
256 gpr_timespec t2 = {INT64_MAX, 2, GPR_CLOCK_REALTIME};
257 ASSERT_EQ(gpr_time_cmp(t1, t2), 0);
258 t1.tv_sec = INT64_MIN;
259 t2.tv_sec = INT64_MIN;
260 ASSERT_EQ(gpr_time_cmp(t1, t2), 0);
261 }
262
263 int main(int argc, char** argv) {
264 grpc::testing::TestEnvironment env(&argc, argv);
265 ::testing::InitGoogleTest(&argc, argv);
266 return RUN_ALL_TESTS();
267 }