test/core/gpr/time_test.cc
Go to the documentation of this file.
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 <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <grpc/support/log.h>
28 #include <grpc/support/sync.h>
29 #include <grpc/support/time.h>
30 
32 
33 static void to_fp(void* arg, const char* buf, size_t len) {
34  fwrite(buf, 1, len, static_cast<FILE*>(arg));
35 }
36 
37 /* Convert gpr_intmax x to ascii base b (2..16), and write with
38  (*writer)(arg, ...), zero padding to "chars" digits). */
39 static void i_to_s(intmax_t x, int base, int chars,
40  void (*writer)(void* arg, const char* buf, size_t len),
41  void* arg) {
42  char buf[64];
43  char fmt[32];
44  GPR_ASSERT(base == 16 || base == 10);
45  sprintf(fmt, "%%0%d%s", chars, base == 16 ? PRIxMAX : PRIdMAX);
46  sprintf(buf, fmt, x);
47  (*writer)(arg, buf, strlen(buf));
48 }
49 
50 /* Convert ts to ascii, and write with (*writer)(arg, ...). */
51 static void ts_to_s(gpr_timespec t,
52  void (*writer)(void* arg, const char* buf, size_t len),
53  void* arg) {
54  if (t.tv_sec < 0 && t.tv_nsec != 0) {
55  t.tv_sec++;
56  t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec;
57  }
58  i_to_s(t.tv_sec, 10, 0, writer, arg);
59  (*writer)(arg, ".", 1);
60  i_to_s(t.tv_nsec, 10, 9, writer, arg);
61 }
62 
63 static void test_values(void) {
64  int i;
65 
67  GPR_ASSERT(x.tv_sec == 0 && x.tv_nsec == 0);
68 
70  fprintf(stderr, "far future ");
71  fflush(stderr);
72  i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
73  fprintf(stderr, "\n");
74  GPR_ASSERT(x.tv_sec == INT64_MAX);
75  fprintf(stderr, "far future ");
76  fflush(stderr);
77  ts_to_s(x, &to_fp, stderr);
78  fprintf(stderr, "\n");
79  fflush(stderr);
80 
82  fprintf(stderr, "far past ");
83  fflush(stderr);
84  i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
85  fprintf(stderr, "\n");
86  fflush(stderr);
87  GPR_ASSERT(x.tv_sec == INT64_MIN);
88  fprintf(stderr, "far past ");
89  fflush(stderr);
90  ts_to_s(x, &to_fp, stderr);
91  fprintf(stderr, "\n");
92  fflush(stderr);
93 
94  for (i = 1; i != 1000 * 1000 * 1000; i *= 10) {
96  GPR_ASSERT(x.tv_sec == i / GPR_US_PER_SEC &&
97  x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US);
99  GPR_ASSERT(x.tv_sec == i / GPR_NS_PER_SEC &&
100  x.tv_nsec == (i % GPR_NS_PER_SEC));
102  GPR_ASSERT(x.tv_sec == i / GPR_MS_PER_SEC &&
103  x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS);
104  }
105 
106  /* Test possible overflow in conversion of -ve values. */
108  GPR_ASSERT(x.tv_sec < 0);
109  GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
110 
111  x = gpr_time_from_nanos(-(INT64_MAX - 999999997), GPR_TIMESPAN);
112  GPR_ASSERT(x.tv_sec < 0);
113  GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
114 
116  GPR_ASSERT(x.tv_sec < 0);
117  GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
118 
119  /* Test general -ve values. */
120  for (i = -1; i > -1000 * 1000 * 1000; i *= 7) {
122  GPR_ASSERT(x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i);
124  GPR_ASSERT(x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i);
126  GPR_ASSERT(x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i);
127  }
128 }
129 
130 static void test_add_sub(void) {
131  int i;
132  int j;
133  int k;
134  /* Basic addition and subtraction. */
135  for (i = -100; i <= 100; i++) {
136  for (j = -100; j <= 100; j++) {
137  for (k = 1; k <= 10000000; k *= 10) {
138  int sum = i + j;
139  int diff = i - j;
142  gpr_timespec sumt = gpr_time_add(it, jt);
143  gpr_timespec difft = gpr_time_sub(it, jt);
145  0) {
146  fprintf(stderr, "i %d j %d sum %d sumt ", i, j, sum);
147  fflush(stderr);
148  ts_to_s(sumt, &to_fp, stderr);
149  fprintf(stderr, "\n");
150  fflush(stderr);
151  GPR_ASSERT(0);
152  }
154  0) {
155  fprintf(stderr, "i %d j %d diff %d diff ", i, j, diff);
156  fflush(stderr);
157  ts_to_s(sumt, &to_fp, stderr);
158  fprintf(stderr, "\n");
159  fflush(stderr);
160  GPR_ASSERT(0);
161  }
162  }
163  }
164  }
165 }
166 
167 static void test_overflow(void) {
168  /* overflow */
170  do {
171  x = gpr_time_add(x, x);
172  } while (gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) < 0);
175  do {
176  x = gpr_time_add(x, x);
177  } while (gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) > 0);
179 }
180 
181 static void test_sticky_infinities(void) {
182  int i;
183  int j;
184  int k;
185  gpr_timespec infinity[2];
186  gpr_timespec addend[3];
187  infinity[0] = gpr_inf_future(GPR_TIMESPAN);
188  infinity[1] = gpr_inf_past(GPR_TIMESPAN);
189  addend[0] = gpr_inf_future(GPR_TIMESPAN);
190  addend[1] = gpr_inf_past(GPR_TIMESPAN);
191  addend[2] = gpr_time_0(GPR_TIMESPAN);
192 
193  /* Infinities are sticky */
194  for (i = 0; i != sizeof(infinity) / sizeof(infinity[0]); i++) {
195  for (j = 0; j != sizeof(addend) / sizeof(addend[0]); j++) {
196  gpr_timespec x = gpr_time_add(infinity[i], addend[j]);
197  GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
198  x = gpr_time_sub(infinity[i], addend[j]);
199  GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
200  }
201  for (k = -200; k <= 200; k++) {
203  gpr_timespec x = gpr_time_add(infinity[i], y);
204  GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
205  x = gpr_time_sub(infinity[i], y);
206  GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
207  }
208  }
209 }
210 
211 static void test_similar(void) {
239 }
240 
241 static void test_convert_extreme(void) {
242  gpr_timespec realtime = {INT64_MAX, 1, GPR_CLOCK_REALTIME};
244  GPR_ASSERT(monotime.tv_sec == realtime.tv_sec);
246 }
247 
248 static void test_cmp_extreme(void) {
251  GPR_ASSERT(gpr_time_cmp(t1, t2) == 0);
252  t1.tv_sec = INT64_MIN;
253  t2.tv_sec = INT64_MIN;
254  GPR_ASSERT(gpr_time_cmp(t1, t2) == 0);
255 }
256 
257 int main(int argc, char* argv[]) {
258  grpc::testing::TestEnvironment env(&argc, argv);
259 
260  test_values();
261  test_add_sub();
262  test_overflow();
264  test_similar();
267  return 0;
268 }
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
gpr_timespec::tv_sec
int64_t tv_sec
Definition: gpr_types.h:51
regen-readme.it
it
Definition: regen-readme.py:15
log.h
gpr_time_similar
GPRAPI int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold)
Definition: src/core/lib/gpr/time.cc:206
generate.env
env
Definition: generate.py:37
gpr_time_0
GPRAPI gpr_timespec gpr_time_0(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:47
main
int main(int argc, char *argv[])
Definition: test/core/gpr/time_test.cc:257
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
grpc::testing::sum
double sum(const T &container, F functor)
Definition: test/cpp/qps/stats.h:30
INT64_MAX
#define INT64_MAX
Definition: stdint-msvc2008.h:139
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
test_cmp_extreme
static void test_cmp_extreme(void)
Definition: test/core/gpr/time_test.cc:248
GPR_US_PER_SEC
#define GPR_US_PER_SEC
Definition: include/grpc/support/time.h:40
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
time.h
intmax_t
int64_t intmax_t
Definition: stdint-msvc2008.h:123
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
GPR_NS_PER_SEC
#define GPR_NS_PER_SEC
Definition: include/grpc/support/time.h:41
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
test_sticky_infinities
static void test_sticky_infinities(void)
Definition: test/core/gpr/time_test.cc:181
gpr_time_from_nanos
GPRAPI gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:111
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
gpr_time_sub
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:168
ts_to_s
static void ts_to_s(gpr_timespec t, void(*writer)(void *arg, const char *buf, size_t len), void *arg)
Definition: test/core/gpr/time_test.cc:51
bm_diff.diff
diff
Definition: bm_diff.py:274
arg
Definition: cmdline.cc:40
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
GPR_NS_PER_US
#define GPR_NS_PER_US
Definition: include/grpc/support/time.h:43
writer
void writer(void *n)
Definition: libuv/docs/code/locks/main.c:22
GPR_NS_PER_MS
#define GPR_NS_PER_MS
Definition: include/grpc/support/time.h:42
test_values
static void test_values(void)
Definition: test/core/gpr/time_test.cc:63
gpr_timespec::clock_type
gpr_clock_type clock_type
Definition: gpr_types.h:55
i_to_s
static void i_to_s(intmax_t x, int base, int chars, void(*writer)(void *arg, const char *buf, size_t len), void *arg)
Definition: test/core/gpr/time_test.cc:39
test_config.h
gpr_inf_past
GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:63
test_add_sub
static void test_add_sub(void)
Definition: test/core/gpr/time_test.cc:130
INT64_MIN
#define INT64_MIN
Definition: stdint-msvc2008.h:138
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1808
benchmark.FILE
FILE
Definition: benchmark.py:21
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
gpr_convert_clock_type
GPRAPI gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:241
gpr_time_from_micros
GPRAPI gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:115
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
to_fp
static void to_fp(void *arg, const char *buf, size_t len)
Definition: test/core/gpr/time_test.cc:33
arg
struct arg arg
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
GPR_MS_PER_SEC
#define GPR_MS_PER_SEC
Definition: include/grpc/support/time.h:39
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
gpr_timespec
Definition: gpr_types.h:50
test_similar
static void test_similar(void)
Definition: test/core/gpr/time_test.cc:211
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
test_convert_extreme
static void test_convert_extreme(void)
Definition: test/core/gpr/time_test.cc:241
t1
Table t1
Definition: abseil-cpp/absl/container/internal/raw_hash_set_allocator_test.cc:185
sync.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
test_overflow
static void test_overflow(void)
Definition: test/core/gpr/time_test.cc:167


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:36