test_strings.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 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_strings.c
  18. * @brief testcase for strings.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #define WANT(a, b) if (0 != strcmp (a, b)) { fprintf (stderr, \
  23. "Got `%s', wanted `%s'\n", \
  24. b, a); GNUNET_free (b); \
  25. GNUNET_break (0); \
  26. return 1; } else { GNUNET_free (b); \
  27. }
  28. #define WANTNF(a, b) do { if (0 != strcmp (a, b)) { fprintf (stderr, \
  29. "Got `%s', wanted `%s'\n", \
  30. b, a); \
  31. GNUNET_break (0); return 1; \
  32. } } while (0)
  33. #define WANTB(a, b, l) if (0 != memcmp (a, b, l)) { GNUNET_break (0); return 1; \
  34. } else { }
  35. int
  36. main (int argc, char *argv[])
  37. {
  38. char buf[128];
  39. char *r;
  40. char *b;
  41. const char *bc;
  42. struct GNUNET_TIME_Absolute at;
  43. struct GNUNET_TIME_Absolute atx;
  44. struct GNUNET_TIME_Relative rt;
  45. struct GNUNET_TIME_Relative rtx;
  46. const char *hdir;
  47. GNUNET_log_setup ("test_strings", "ERROR", NULL);
  48. sprintf (buf, "4 %s", _ (/* size unit */ "b"));
  49. b = GNUNET_STRINGS_byte_size_fancy (4);
  50. WANT (buf, b);
  51. sprintf (buf, "10 %s", _ (/* size unit */ "KiB"));
  52. b = GNUNET_STRINGS_byte_size_fancy (10240);
  53. WANT (buf, b);
  54. sprintf (buf, "10 %s", _ (/* size unit */ "TiB"));
  55. b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
  56. WANT (buf, b);
  57. sprintf (buf, "4 %s", _ (/* time unit */ "ms"));
  58. bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
  59. (GNUNET_TIME_UNIT_MILLISECONDS,
  60. 4), GNUNET_YES);
  61. WANTNF (buf, bc);
  62. sprintf (buf, "7 %s", _ (/* time unit */ "s"));
  63. bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
  64. (GNUNET_TIME_UNIT_MILLISECONDS,
  65. 7 * 1000), GNUNET_YES);
  66. WANTNF (buf, bc);
  67. sprintf (buf, "7 %s", _ (/* time unit */ "h"));
  68. bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
  69. (GNUNET_TIME_UNIT_MILLISECONDS,
  70. 7 * 60 * 60 * 1000),
  71. GNUNET_YES);
  72. WANTNF (buf, bc);
  73. hdir = getenv ("HOME");
  74. GNUNET_snprintf (buf, sizeof(buf), "%s%s", hdir, DIR_SEPARATOR_STR);
  75. b = GNUNET_STRINGS_filename_expand ("~");
  76. GNUNET_assert (b != NULL);
  77. WANT (buf, b);
  78. GNUNET_STRINGS_buffer_fill (buf, sizeof(buf), 3, "a", "btx", "c");
  79. WANTB ("a\0btx\0c", buf, 8);
  80. if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof(buf), 2, &r, &b))
  81. return 1;
  82. r = GNUNET_strdup (r);
  83. WANT ("a", r);
  84. b = GNUNET_strdup (b);
  85. WANT ("btx", b);
  86. if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
  87. return 1;
  88. at.abs_value_us = 5000000;
  89. bc = GNUNET_STRINGS_absolute_time_to_string (at);
  90. /* bc should be something like "Wed Dec 31 17:00:05 1969"
  91. * where the details of the day and hour depend on the timezone;
  92. * however, the "0:05 19" should always be there; hence: */
  93. if (NULL == strstr (bc, "0:05 19"))
  94. {
  95. fprintf (stderr, "Got %s\n", bc);
  96. GNUNET_break (0);
  97. return 1;
  98. }
  99. b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
  100. WANT ("TEST", b);
  101. at = GNUNET_TIME_UNIT_FOREVER_ABS;
  102. bc = GNUNET_STRINGS_absolute_time_to_string (at);
  103. GNUNET_assert (GNUNET_OK ==
  104. GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
  105. GNUNET_assert (atx.abs_value_us == at.abs_value_us);
  106. at.abs_value_us = 50000000000;
  107. bc = GNUNET_STRINGS_absolute_time_to_string (at);
  108. GNUNET_assert (GNUNET_OK ==
  109. GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
  110. if (atx.abs_value_us != at.abs_value_us)
  111. {
  112. GNUNET_assert (0);
  113. }
  114. GNUNET_log_skip (2, GNUNET_NO);
  115. b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
  116. GNUNET_log_skip (0, GNUNET_YES);
  117. WANT ("TEST", b);
  118. GNUNET_assert (GNUNET_OK ==
  119. GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
  120. GNUNET_assert (GNUNET_OK ==
  121. GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
  122. GNUNET_assert (rt.rel_value_us == rtx.rel_value_us);
  123. return 0;
  124. }
  125. /* end of test_strings.c */