time_test.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /* Test of gpr time support. */
  19. #include <inttypes.h>
  20. #include <limits.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/sync.h>
  26. #include <grpc/support/time.h>
  27. #include "test/core/util/test_config.h"
  28. static void to_fp(void* arg, const char* buf, size_t len) {
  29. fwrite(buf, 1, len, static_cast<FILE*>(arg));
  30. }
  31. /* Convert gpr_intmax x to ascii base b (2..16), and write with
  32. (*writer)(arg, ...), zero padding to "chars" digits). */
  33. static void i_to_s(intmax_t x, int base, int chars,
  34. void (*writer)(void* arg, const char* buf, size_t len),
  35. void* arg) {
  36. char buf[64];
  37. char fmt[32];
  38. GPR_ASSERT(base == 16 || base == 10);
  39. sprintf(fmt, "%%0%d%s", chars, base == 16 ? PRIxMAX : PRIdMAX);
  40. sprintf(buf, fmt, x);
  41. (*writer)(arg, buf, strlen(buf));
  42. }
  43. /* Convert ts to ascii, and write with (*writer)(arg, ...). */
  44. static void ts_to_s(gpr_timespec t,
  45. void (*writer)(void* arg, const char* buf, size_t len),
  46. void* arg) {
  47. if (t.tv_sec < 0 && t.tv_nsec != 0) {
  48. t.tv_sec++;
  49. t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec;
  50. }
  51. i_to_s(t.tv_sec, 10, 0, writer, arg);
  52. (*writer)(arg, ".", 1);
  53. i_to_s(t.tv_nsec, 10, 9, writer, arg);
  54. }
  55. static void test_values(void) {
  56. int i;
  57. gpr_timespec x = gpr_time_0(GPR_CLOCK_REALTIME);
  58. GPR_ASSERT(x.tv_sec == 0 && x.tv_nsec == 0);
  59. x = gpr_inf_future(GPR_CLOCK_REALTIME);
  60. fprintf(stderr, "far future ");
  61. fflush(stderr);
  62. i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
  63. fprintf(stderr, "\n");
  64. GPR_ASSERT(x.tv_sec == INT64_MAX);
  65. fprintf(stderr, "far future ");
  66. fflush(stderr);
  67. ts_to_s(x, &to_fp, stderr);
  68. fprintf(stderr, "\n");
  69. fflush(stderr);
  70. x = gpr_inf_past(GPR_CLOCK_REALTIME);
  71. fprintf(stderr, "far past ");
  72. fflush(stderr);
  73. i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
  74. fprintf(stderr, "\n");
  75. fflush(stderr);
  76. GPR_ASSERT(x.tv_sec == INT64_MIN);
  77. fprintf(stderr, "far past ");
  78. fflush(stderr);
  79. ts_to_s(x, &to_fp, stderr);
  80. fprintf(stderr, "\n");
  81. fflush(stderr);
  82. for (i = 1; i != 1000 * 1000 * 1000; i *= 10) {
  83. x = gpr_time_from_micros(i, GPR_TIMESPAN);
  84. GPR_ASSERT(x.tv_sec == i / GPR_US_PER_SEC &&
  85. x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US);
  86. x = gpr_time_from_nanos(i, GPR_TIMESPAN);
  87. GPR_ASSERT(x.tv_sec == i / GPR_NS_PER_SEC &&
  88. x.tv_nsec == (i % GPR_NS_PER_SEC));
  89. x = gpr_time_from_millis(i, GPR_TIMESPAN);
  90. GPR_ASSERT(x.tv_sec == i / GPR_MS_PER_SEC &&
  91. x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS);
  92. }
  93. /* Test possible overflow in conversion of -ve values. */
  94. x = gpr_time_from_micros(-(INT64_MAX - 999997), GPR_TIMESPAN);
  95. GPR_ASSERT(x.tv_sec < 0);
  96. GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
  97. x = gpr_time_from_nanos(-(INT64_MAX - 999999997), GPR_TIMESPAN);
  98. GPR_ASSERT(x.tv_sec < 0);
  99. GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
  100. x = gpr_time_from_millis(-(INT64_MAX - 997), GPR_TIMESPAN);
  101. GPR_ASSERT(x.tv_sec < 0);
  102. GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);
  103. /* Test general -ve values. */
  104. for (i = -1; i > -1000 * 1000 * 1000; i *= 7) {
  105. x = gpr_time_from_micros(i, GPR_TIMESPAN);
  106. GPR_ASSERT(x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i);
  107. x = gpr_time_from_nanos(i, GPR_TIMESPAN);
  108. GPR_ASSERT(x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i);
  109. x = gpr_time_from_millis(i, GPR_TIMESPAN);
  110. GPR_ASSERT(x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i);
  111. }
  112. }
  113. static void test_add_sub(void) {
  114. int i;
  115. int j;
  116. int k;
  117. /* Basic addition and subtraction. */
  118. for (i = -100; i <= 100; i++) {
  119. for (j = -100; j <= 100; j++) {
  120. for (k = 1; k <= 10000000; k *= 10) {
  121. int sum = i + j;
  122. int diff = i - j;
  123. gpr_timespec it = gpr_time_from_micros(i * k, GPR_TIMESPAN);
  124. gpr_timespec jt = gpr_time_from_micros(j * k, GPR_TIMESPAN);
  125. gpr_timespec sumt = gpr_time_add(it, jt);
  126. gpr_timespec difft = gpr_time_sub(it, jt);
  127. if (gpr_time_cmp(gpr_time_from_micros(sum * k, GPR_TIMESPAN), sumt) !=
  128. 0) {
  129. fprintf(stderr, "i %d j %d sum %d sumt ", i, j, sum);
  130. fflush(stderr);
  131. ts_to_s(sumt, &to_fp, stderr);
  132. fprintf(stderr, "\n");
  133. fflush(stderr);
  134. GPR_ASSERT(0);
  135. }
  136. if (gpr_time_cmp(gpr_time_from_micros(diff * k, GPR_TIMESPAN), difft) !=
  137. 0) {
  138. fprintf(stderr, "i %d j %d diff %d diff ", i, j, diff);
  139. fflush(stderr);
  140. ts_to_s(sumt, &to_fp, stderr);
  141. fprintf(stderr, "\n");
  142. fflush(stderr);
  143. GPR_ASSERT(0);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. static void test_overflow(void) {
  150. /* overflow */
  151. gpr_timespec x = gpr_time_from_micros(1, GPR_TIMESPAN);
  152. do {
  153. x = gpr_time_add(x, x);
  154. } while (gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) < 0);
  155. GPR_ASSERT(gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) == 0);
  156. x = gpr_time_from_micros(-1, GPR_TIMESPAN);
  157. do {
  158. x = gpr_time_add(x, x);
  159. } while (gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) > 0);
  160. GPR_ASSERT(gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) == 0);
  161. }
  162. static void test_sticky_infinities(void) {
  163. int i;
  164. int j;
  165. int k;
  166. gpr_timespec infinity[2];
  167. gpr_timespec addend[3];
  168. infinity[0] = gpr_inf_future(GPR_TIMESPAN);
  169. infinity[1] = gpr_inf_past(GPR_TIMESPAN);
  170. addend[0] = gpr_inf_future(GPR_TIMESPAN);
  171. addend[1] = gpr_inf_past(GPR_TIMESPAN);
  172. addend[2] = gpr_time_0(GPR_TIMESPAN);
  173. /* Infinities are sticky */
  174. for (i = 0; i != sizeof(infinity) / sizeof(infinity[0]); i++) {
  175. for (j = 0; j != sizeof(addend) / sizeof(addend[0]); j++) {
  176. gpr_timespec x = gpr_time_add(infinity[i], addend[j]);
  177. GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
  178. x = gpr_time_sub(infinity[i], addend[j]);
  179. GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
  180. }
  181. for (k = -200; k <= 200; k++) {
  182. gpr_timespec y = gpr_time_from_micros(k * 100000, GPR_TIMESPAN);
  183. gpr_timespec x = gpr_time_add(infinity[i], y);
  184. GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
  185. x = gpr_time_sub(infinity[i], y);
  186. GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0);
  187. }
  188. }
  189. }
  190. static void test_similar(void) {
  191. GPR_ASSERT(1 == gpr_time_similar(gpr_inf_future(GPR_TIMESPAN),
  192. gpr_inf_future(GPR_TIMESPAN),
  193. gpr_time_0(GPR_TIMESPAN)));
  194. GPR_ASSERT(1 == gpr_time_similar(gpr_inf_past(GPR_TIMESPAN),
  195. gpr_inf_past(GPR_TIMESPAN),
  196. gpr_time_0(GPR_TIMESPAN)));
  197. GPR_ASSERT(0 == gpr_time_similar(gpr_inf_past(GPR_TIMESPAN),
  198. gpr_inf_future(GPR_TIMESPAN),
  199. gpr_time_0(GPR_TIMESPAN)));
  200. GPR_ASSERT(0 == gpr_time_similar(gpr_inf_future(GPR_TIMESPAN),
  201. gpr_inf_past(GPR_TIMESPAN),
  202. gpr_time_0(GPR_TIMESPAN)));
  203. GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN),
  204. gpr_time_from_micros(10, GPR_TIMESPAN),
  205. gpr_time_0(GPR_TIMESPAN)));
  206. GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN),
  207. gpr_time_from_micros(15, GPR_TIMESPAN),
  208. gpr_time_from_micros(10, GPR_TIMESPAN)));
  209. GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(15, GPR_TIMESPAN),
  210. gpr_time_from_micros(10, GPR_TIMESPAN),
  211. gpr_time_from_micros(10, GPR_TIMESPAN)));
  212. GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN),
  213. gpr_time_from_micros(25, GPR_TIMESPAN),
  214. gpr_time_from_micros(10, GPR_TIMESPAN)));
  215. GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(25, GPR_TIMESPAN),
  216. gpr_time_from_micros(10, GPR_TIMESPAN),
  217. gpr_time_from_micros(10, GPR_TIMESPAN)));
  218. }
  219. static void test_convert_extreme(void) {
  220. gpr_timespec realtime = {INT64_MAX, 1, GPR_CLOCK_REALTIME};
  221. gpr_timespec monotime = gpr_convert_clock_type(realtime, GPR_CLOCK_MONOTONIC);
  222. GPR_ASSERT(monotime.tv_sec == realtime.tv_sec);
  223. GPR_ASSERT(monotime.clock_type == GPR_CLOCK_MONOTONIC);
  224. }
  225. static void test_cmp_extreme(void) {
  226. gpr_timespec t1 = {INT64_MAX, 1, GPR_CLOCK_REALTIME};
  227. gpr_timespec t2 = {INT64_MAX, 2, GPR_CLOCK_REALTIME};
  228. GPR_ASSERT(gpr_time_cmp(t1, t2) == 0);
  229. t1.tv_sec = INT64_MIN;
  230. t2.tv_sec = INT64_MIN;
  231. GPR_ASSERT(gpr_time_cmp(t1, t2) == 0);
  232. }
  233. int main(int argc, char* argv[]) {
  234. grpc::testing::TestEnvironment env(argc, argv);
  235. test_values();
  236. test_add_sub();
  237. test_overflow();
  238. test_sticky_infinities();
  239. test_similar();
  240. test_convert_extreme();
  241. test_cmp_extreme();
  242. return 0;
  243. }