aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/support/time_test.c
blob: c1dce777b09326cd94e9ee38e20102db1dee609a (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 *
 * Copyright 2015, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/* Test of gpr time support. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <grpc/support/log.h>
#include <grpc/support/sync.h>
#include <grpc/support/thd.h>
#include <grpc/support/time.h>
#include "test/core/util/test_config.h"

static void to_fp(void *arg, const char *buf, int len) {
  fwrite(buf, 1, len, (FILE *)arg);
}

/* Convert gpr_uintmax x to ascii base b (2..16), and write with
   (*writer)(arg, ...), zero padding to "chars" digits).  */
static void u_to_s(gpr_uintmax x, unsigned base, int chars,
                   void (*writer)(void *arg, const char *buf, int len),
                   void *arg) {
  char buf[64];
  char *p = buf + sizeof(buf);
  do {
    *--p = "0123456789abcdef"[x % base];
    x /= base;
    chars--;
  } while (x != 0 || chars > 0);
  (*writer)(arg, p, buf + sizeof(buf) - p);
}

/* Convert gpr_intmax x to ascii base b (2..16), and write with
   (*writer)(arg, ...), zero padding to "chars" digits).  */
static void i_to_s(gpr_intmax x, unsigned base, int chars,
                   void (*writer)(void *arg, const char *buf, int len),
                   void *arg) {
  if (x < 0) {
    (*writer)(arg, "-", 1);
    u_to_s(-x, base, chars - 1, writer, arg);
  } else {
    u_to_s(x, base, chars, writer, arg);
  }
}

/* Convert ts to ascii, and write with (*writer)(arg, ...).  */
static void ts_to_s(gpr_timespec t,
                    void (*writer)(void *arg, const char *buf, int len),
                    void *arg) {
  if (t.tv_sec < 0 && t.tv_nsec != 0) {
    t.tv_sec++;
    t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec;
  }
  i_to_s(t.tv_sec, 10, 0, writer, arg);
  (*writer)(arg, ".", 1);
  i_to_s(t.tv_nsec, 10, 9, writer, arg);
}

static void test_values(void) {
  int i;

  gpr_timespec x = gpr_time_0;
  GPR_ASSERT(x.tv_sec == 0 && x.tv_nsec == 0);

  x = gpr_inf_future;
  fprintf(stderr, "far future ");
  u_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
  fprintf(stderr, "\n");
  GPR_ASSERT(x.tv_sec >= INT_MAX);
  fprintf(stderr, "far future ");
  ts_to_s(x, &to_fp, stderr);
  fprintf(stderr, "\n");

  x = gpr_inf_past;
  fprintf(stderr, "far past   ");
  u_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
  fprintf(stderr, "\n");
  GPR_ASSERT(x.tv_sec <= INT_MIN);
  fprintf(stderr, "far past   ");
  ts_to_s(x, &to_fp, stderr);
  fprintf(stderr, "\n");

  for (i = 1; i != 1000 * 1000 * 1000; i *= 10) {
    x = gpr_time_from_micros(i);
    GPR_ASSERT(x.tv_sec == i / GPR_US_PER_SEC &&
               x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US);
    x = gpr_time_from_nanos(i);
    GPR_ASSERT(x.tv_sec == i / GPR_NS_PER_SEC &&
               x.tv_nsec == (i % GPR_NS_PER_SEC));
    x = gpr_time_from_millis(i);
    GPR_ASSERT(x.tv_sec == i / GPR_MS_PER_SEC &&
               x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS);
  }

  /* Test possible overflow in conversion of -ve values. */
  x = gpr_time_from_micros(-(LONG_MAX - 999997));
  GPR_ASSERT(x.tv_sec < 0);
  GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);

  x = gpr_time_from_nanos(-(LONG_MAX - 999999997));
  GPR_ASSERT(x.tv_sec < 0);
  GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);

  x = gpr_time_from_millis(-(LONG_MAX - 997));
  GPR_ASSERT(x.tv_sec < 0);
  GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);

  /* Test general -ve values. */
  for (i = -1; i > -1000 * 1000 * 1000; i *= 7) {
    x = gpr_time_from_micros(i);
    GPR_ASSERT(x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i);
    x = gpr_time_from_nanos(i);
    GPR_ASSERT(x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i);
    x = gpr_time_from_millis(i);
    GPR_ASSERT(x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i);
  }
}

static void test_add_sub(void) {
  int i;
  int j;
  int k;
  /* Basic addition and subtraction. */
  for (i = -100; i <= 100; i++) {
    for (j = -100; j <= 100; j++) {
      for (k = 1; k <= 10000000; k *= 10) {
        int sum = i + j;
        int diff = i - j;
        gpr_timespec it = gpr_time_from_micros(i * k);
        gpr_timespec jt = gpr_time_from_micros(j * k);
        gpr_timespec sumt = gpr_time_add(it, jt);
        gpr_timespec difft = gpr_time_sub(it, jt);
        if (gpr_time_cmp(gpr_time_from_micros(sum * k), sumt) != 0) {
          fprintf(stderr, "i %d  j %d  sum %d    sumt ", i, j, sum);
          ts_to_s(sumt, &to_fp, stderr);
          fprintf(stderr, "\n");
          GPR_ASSERT(0);
        }
        if (gpr_time_cmp(gpr_time_from_micros(diff * k), difft) != 0) {
          fprintf(stderr, "i %d  j %d  diff %d    diff ", i, j, diff);
          ts_to_s(sumt, &to_fp, stderr);
          fprintf(stderr, "\n");
          GPR_ASSERT(0);
        }
      }
    }
  }
}

static void test_overflow(void) {
  /* overflow */
  gpr_timespec x = gpr_time_from_micros(1);
  do {
    x = gpr_time_add(x, x);
  } while (gpr_time_cmp(x, gpr_inf_future) < 0);
  GPR_ASSERT(gpr_time_cmp(x, gpr_inf_future) == 0);
  x = gpr_time_from_micros(-1);
  do {
    x = gpr_time_add(x, x);
  } while (gpr_time_cmp(x, gpr_inf_past) > 0);
  GPR_ASSERT(gpr_time_cmp(x, gpr_inf_past) == 0);
}

static void test_sticky_infinities(void) {
  int i;
  int j;
  int k;
  static const gpr_timespec *infinity[] = {&gpr_inf_future, &gpr_inf_past};
  static const gpr_timespec *addend[] = {&gpr_inf_future, &gpr_inf_past,
                                         &gpr_time_0, NULL};

  /* Infinities are sticky */
  for (i = 0; i != sizeof(infinity) / sizeof(infinity[0]); i++) {
    for (j = 0; j != sizeof(addend) / sizeof(addend[0]); j++) {
      if (addend[j] == NULL) {
        for (k = -200; k <= 200; k++) {
          gpr_timespec y = gpr_time_from_micros(k * 100000);
          gpr_timespec x = gpr_time_add(*infinity[i], y);
          GPR_ASSERT(gpr_time_cmp(x, *infinity[i]) == 0);
          x = gpr_time_sub(*infinity[i], y);
          GPR_ASSERT(gpr_time_cmp(x, *infinity[i]) == 0);
        }
      } else {
        gpr_timespec x = gpr_time_add(*infinity[i], *addend[j]);
        GPR_ASSERT(gpr_time_cmp(x, *infinity[i]) == 0);
        x = gpr_time_sub(*infinity[i], *addend[j]);
        GPR_ASSERT(gpr_time_cmp(x, *infinity[i]) == 0);
      }
    }
  }
}

static void test_similar(void) {
  GPR_ASSERT(1 == gpr_time_similar(gpr_inf_future, gpr_inf_future, gpr_time_0));
  GPR_ASSERT(1 == gpr_time_similar(gpr_inf_past, gpr_inf_past, gpr_time_0));
  GPR_ASSERT(0 == gpr_time_similar(gpr_inf_past, gpr_inf_future, gpr_time_0));
  GPR_ASSERT(0 == gpr_time_similar(gpr_inf_future, gpr_inf_past, gpr_time_0));
  GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10),
                                   gpr_time_from_micros(10), gpr_time_0));
  GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10),
                                   gpr_time_from_micros(15),
                                   gpr_time_from_micros(10)));
  GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(15),
                                   gpr_time_from_micros(10),
                                   gpr_time_from_micros(10)));
  GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(10),
                                   gpr_time_from_micros(25),
                                   gpr_time_from_micros(10)));
  GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(25),
                                   gpr_time_from_micros(10),
                                   gpr_time_from_micros(10)));
}

int main(int argc, char *argv[]) {
  grpc_test_init(argc, argv);

  test_values();
  test_add_sub();
  test_overflow();
  test_sticky_infinities();
  test_similar();
  return 0;
}