test_time.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2013, 2018 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_time.c
  18. * @brief testcase for time.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. int
  23. main (int argc, char *argv[])
  24. {
  25. struct GNUNET_TIME_Absolute now;
  26. struct GNUNET_TIME_AbsoluteNBO nown;
  27. struct GNUNET_TIME_Absolute future;
  28. struct GNUNET_TIME_Absolute past;
  29. struct GNUNET_TIME_Absolute last;
  30. struct GNUNET_TIME_Absolute forever;
  31. struct GNUNET_TIME_Absolute zero;
  32. struct GNUNET_TIME_Relative rel;
  33. struct GNUNET_TIME_Relative relForever;
  34. struct GNUNET_TIME_Relative relUnit;
  35. struct GNUNET_TIME_RelativeNBO reln;
  36. unsigned int i;
  37. GNUNET_log_setup ("test-time", "WARNING", NULL);
  38. forever = GNUNET_TIME_UNIT_FOREVER_ABS;
  39. relForever = GNUNET_TIME_UNIT_FOREVER_REL;
  40. relUnit = GNUNET_TIME_UNIT_MILLISECONDS;
  41. zero.abs_value_us = 0;
  42. last = now = GNUNET_TIME_absolute_get ();
  43. while (now.abs_value_us == last.abs_value_us)
  44. now = GNUNET_TIME_absolute_get ();
  45. GNUNET_assert (now.abs_value_us > last.abs_value_us);
  46. /* test overflow checking in multiply */
  47. rel = GNUNET_TIME_UNIT_MILLISECONDS;
  48. GNUNET_log_skip (1, GNUNET_NO);
  49. for (i = 0; i < 55; i++)
  50. rel = GNUNET_TIME_relative_multiply (rel, 2);
  51. GNUNET_log_skip (0, GNUNET_NO);
  52. GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
  53. /*check zero */
  54. rel.rel_value_us = (UINT64_MAX) -1024;
  55. GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
  56. GNUNET_TIME_relative_multiply (rel, 0).rel_value_us);
  57. /* test infinity-check for relative to absolute */
  58. GNUNET_log_skip (1, GNUNET_NO);
  59. last = GNUNET_TIME_relative_to_absolute (rel);
  60. GNUNET_assert (last.abs_value_us ==
  61. GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
  62. GNUNET_log_skip (0, GNUNET_YES);
  63. /* check relative to absolute */
  64. rel.rel_value_us = 1000000;
  65. GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us <
  66. GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
  67. /*check forever */
  68. rel.rel_value_us = UINT64_MAX;
  69. GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us ==
  70. GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
  71. /* check overflow for r2a */
  72. rel.rel_value_us = (UINT64_MAX) -1024;
  73. GNUNET_log_skip (1, GNUNET_NO);
  74. last = GNUNET_TIME_relative_to_absolute (rel);
  75. GNUNET_log_skip (0, GNUNET_NO);
  76. GNUNET_assert (last.abs_value_us ==
  77. GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
  78. /* check overflow for relative add */
  79. GNUNET_log_skip (1, GNUNET_NO);
  80. rel = GNUNET_TIME_relative_add (rel, rel);
  81. GNUNET_log_skip (0, GNUNET_NO);
  82. GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
  83. GNUNET_log_skip (1, GNUNET_NO);
  84. rel = GNUNET_TIME_relative_add (relForever, relForever);
  85. GNUNET_log_skip (0, GNUNET_NO);
  86. GNUNET_assert (rel.rel_value_us == relForever.rel_value_us);
  87. GNUNET_log_skip (1, GNUNET_NO);
  88. rel = GNUNET_TIME_relative_add (relUnit, relUnit);
  89. GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us);
  90. /* check relation check in get_duration */
  91. future.abs_value_us = now.abs_value_us + 1000000;
  92. GNUNET_assert (GNUNET_TIME_absolute_get_difference (now,
  93. future).rel_value_us ==
  94. 1000000);
  95. GNUNET_assert (GNUNET_TIME_absolute_get_difference (future,
  96. now).rel_value_us ==
  97. 0);
  98. GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero,
  99. forever).rel_value_us
  100. == forever.abs_value_us);
  101. past.abs_value_us = now.abs_value_us - 1000000;
  102. rel = GNUNET_TIME_absolute_get_duration (future);
  103. GNUNET_assert (rel.rel_value_us == 0);
  104. rel = GNUNET_TIME_absolute_get_duration (past);
  105. GNUNET_assert (rel.rel_value_us >= 1000000);
  106. /* check get remaining */
  107. rel = GNUNET_TIME_absolute_get_remaining (now);
  108. GNUNET_assert (rel.rel_value_us == 0);
  109. rel = GNUNET_TIME_absolute_get_remaining (past);
  110. GNUNET_assert (rel.rel_value_us == 0);
  111. rel = GNUNET_TIME_absolute_get_remaining (future);
  112. GNUNET_assert (rel.rel_value_us > 0);
  113. GNUNET_assert (rel.rel_value_us <= 1000000);
  114. forever = GNUNET_TIME_UNIT_FOREVER_ABS;
  115. GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
  116. GNUNET_TIME_absolute_get_remaining (forever).rel_value_us);
  117. /* check endianness */
  118. reln = GNUNET_TIME_relative_hton (rel);
  119. GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (
  120. reln).rel_value_us);
  121. nown = GNUNET_TIME_absolute_hton (now);
  122. GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (
  123. nown).abs_value_us);
  124. /* check absolute addition */
  125. future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
  126. GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL);
  127. future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
  128. GNUNET_assert (future.abs_value_us == forever.abs_value_us);
  129. rel.rel_value_us = (UINT64_MAX) -1024;
  130. now.abs_value_us = rel.rel_value_us;
  131. future = GNUNET_TIME_absolute_add (now, rel);
  132. GNUNET_assert (future.abs_value_us == forever.abs_value_us);
  133. /* check zero */
  134. future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
  135. GNUNET_assert (future.abs_value_us == now.abs_value_us);
  136. GNUNET_assert (forever.abs_value_us ==
  137. GNUNET_TIME_absolute_subtract (forever,
  138. GNUNET_TIME_UNIT_MINUTES).
  139. abs_value_us);
  140. /*check absolute subtract */
  141. now.abs_value_us = 50000;
  142. rel.rel_value_us = 100000;
  143. GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
  144. (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
  145. rel.rel_value_us = 10000;
  146. GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now,
  147. rel)).abs_value_us);
  148. /*check relative divide */
  149. GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
  150. (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us);
  151. rel = GNUNET_TIME_UNIT_FOREVER_REL;
  152. GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
  153. (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us);
  154. rel = GNUNET_TIME_relative_divide (relUnit, 2);
  155. GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2);
  156. /* check Return absolute time of 0ms */
  157. zero = GNUNET_TIME_UNIT_ZERO_ABS;
  158. /* check GNUNET_TIME_calculate_eta */
  159. last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024;
  160. forever = GNUNET_TIME_UNIT_FOREVER_ABS;
  161. forever.abs_value_us = forever.abs_value_us - 1024;
  162. GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
  163. GNUNET_TIME_calculate_eta (forever, 50000,
  164. 100000).rel_value_us);
  165. /* check zero */
  166. GNUNET_log_skip (1, GNUNET_NO);
  167. GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
  168. (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us);
  169. GNUNET_log_skip (0, GNUNET_YES);
  170. /*check forever */
  171. GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
  172. (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us);
  173. /*check relative subtract */
  174. now = GNUNET_TIME_absolute_get ();
  175. rel.rel_value_us = now.abs_value_us;
  176. relForever.rel_value_us = rel.rel_value_us + 1024;
  177. GNUNET_assert (1024 ==
  178. GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us);
  179. /*check zero */
  180. GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
  181. GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
  182. /*check forever */
  183. rel.rel_value_us = UINT64_MAX;
  184. GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
  185. GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
  186. /*check GNUNET_TIME_relative_min */
  187. now = GNUNET_TIME_absolute_get ();
  188. rel.rel_value_us = now.abs_value_us;
  189. relForever.rel_value_us = rel.rel_value_us - 1024;
  190. GNUNET_assert (relForever.rel_value_us ==
  191. GNUNET_TIME_relative_min (rel, relForever).rel_value_us);
  192. /*check GNUNET_TIME_relative_max */
  193. GNUNET_assert (rel.rel_value_us ==
  194. GNUNET_TIME_relative_max (rel, relForever).rel_value_us);
  195. /*check GNUNET_TIME_absolute_min */
  196. now = GNUNET_TIME_absolute_get ();
  197. last.abs_value_us = now.abs_value_us - 1024;
  198. GNUNET_assert (last.abs_value_us ==
  199. GNUNET_TIME_absolute_min (now, last).abs_value_us);
  200. /*check GNUNET_TIME_absolute_max */
  201. GNUNET_assert (now.abs_value_us ==
  202. GNUNET_TIME_absolute_max (now, last).abs_value_us);
  203. for (unsigned int i = 0; i < 30; i++)
  204. {
  205. struct GNUNET_CONFIGURATION_Handle *cfg;
  206. cfg = GNUNET_CONFIGURATION_create ();
  207. last = GNUNET_TIME_absolute_get_monotonic (cfg);
  208. now = GNUNET_TIME_absolute_get_monotonic (cfg);
  209. GNUNET_assert (now.abs_value_us > last.abs_value_us);
  210. (void) GNUNET_TIME_absolute_get_monotonic (NULL);
  211. GNUNET_CONFIGURATION_set_value_string (cfg,
  212. "util",
  213. "MONOTONIC_TIME_FILENAME",
  214. "monotonic-time.dat");
  215. last = GNUNET_TIME_absolute_get_monotonic (cfg);
  216. now = GNUNET_TIME_absolute_get_monotonic (cfg);
  217. (void) GNUNET_TIME_absolute_get_monotonic (NULL);
  218. GNUNET_assert (now.abs_value_us > last.abs_value_us);
  219. GNUNET_CONFIGURATION_destroy (cfg);
  220. }
  221. GNUNET_break (GNUNET_OK ==
  222. GNUNET_DISK_directory_remove ("monotonic-time.dat"));
  223. return 0;
  224. }
  225. /* end of test_time.c */