You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
15 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: test/unit/test.unit.util.strnlen/test.unit.util.strnlen.cpp
  3. *
  4. * Purpose: Implementation file for the test.unit.util.strnlen project.
  5. *
  6. * Created: 17th April 2009
  7. * Updated: 19th March 2012
  8. *
  9. * Status: Wizard-generated
  10. *
  11. * License: (Licensed under the Synesis Software Open License)
  12. *
  13. * Copyright (c) 2008-2012, Synesis Software Pty Ltd.
  14. * All rights reserved.
  15. *
  16. * www: http://www.synesis.com.au/software
  17. *
  18. * ////////////////////////////////////////////////////////////////////// */
  19. #include <pantheios/util/test/compiler_warnings_suppression.first_include.h>
  20. #include <pantheios/pantheios.h>
  21. #include <pantheios/internal/lean.h>
  22. #include <pantheios/init_codes.h>
  23. #include <xtests/xtests.h>
  24. #include <stlsoft/shims/access/string.hpp>
  25. #include <stlsoft/util/limit_traits.h>
  26. #include <stlsoft/util/minmax.hpp>
  27. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  28. /* /////////////////////////////////////////////////////////////////////////
  29. * Forward declarations
  30. */
  31. namespace
  32. {
  33. static void test_length_passed_through();
  34. static void test_length_via_strlen();
  35. static void test_length_calculated_forwards();
  36. static void test_length_calculated_backwards();
  37. static void test_strnlen_with_negatives();
  38. static void test_1_06();
  39. static void test_1_07();
  40. static void test_1_08();
  41. static void test_1_09();
  42. static void test_1_10();
  43. static void test_1_11();
  44. static void test_1_12();
  45. } // anonymous namespace
  46. /* ////////////////////////////////////////////////////////////////////// */
  47. const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.unit.util.strnlen");
  48. /* ////////////////////////////////////////////////////////////////////// */
  49. int main(int argc, char** argv)
  50. {
  51. int retCode = EXIT_SUCCESS;
  52. int verbosity = 2;
  53. XTESTS_COMMANDLINE_PARSEVERBOSITY(argc, argv, &verbosity);
  54. if(XTESTS_START_RUNNER("test.unit.util.strnlen", verbosity))
  55. {
  56. XTESTS_RUN_CASE(test_length_passed_through);
  57. XTESTS_RUN_CASE(test_length_via_strlen);
  58. XTESTS_RUN_CASE(test_length_calculated_forwards);
  59. XTESTS_RUN_CASE(test_length_calculated_backwards);
  60. XTESTS_RUN_CASE(test_strnlen_with_negatives);
  61. XTESTS_RUN_CASE(test_1_06);
  62. XTESTS_RUN_CASE(test_1_07);
  63. XTESTS_RUN_CASE(test_1_08);
  64. XTESTS_RUN_CASE(test_1_09);
  65. XTESTS_RUN_CASE(test_1_10);
  66. XTESTS_RUN_CASE(test_1_11);
  67. XTESTS_RUN_CASE(test_1_12);
  68. XTESTS_PRINT_RESULTS();
  69. XTESTS_END_RUNNER_UPDATE_EXITCODE(&retCode);
  70. }
  71. return retCode;
  72. }
  73. /* ////////////////////////////////////////////////////////////////////// */
  74. namespace
  75. {
  76. PAN_CHAR_T const* strings[] =
  77. {
  78. PANTHEIOS_LITERAL_STRING("")
  79. , PANTHEIOS_LITERAL_STRING("a")
  80. , PANTHEIOS_LITERAL_STRING("ab")
  81. , PANTHEIOS_LITERAL_STRING("abc")
  82. , PANTHEIOS_LITERAL_STRING("abcd")
  83. , PANTHEIOS_LITERAL_STRING("abcde")
  84. , PANTHEIOS_LITERAL_STRING("abcdef")
  85. , PANTHEIOS_LITERAL_STRING("abcdefghijklmnopqrstuvwxyz")
  86. , PANTHEIOS_LITERAL_STRING("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789")
  87. };
  88. inline size_t get_min_max(size_t minLen, size_t maxLen)
  89. {
  90. return pantheios::pan_slice_t::get_lazy_length(minLen, maxLen);
  91. }
  92. static void test_length_passed_through()
  93. {
  94. /* Verify that when the top two bits are not set, the length returned
  95. * is the length given
  96. */
  97. size_t iterations = stlsoft::minimum(size_t(65536u * 16u), size_t(stlsoft::limit_traits<size_t>::maximum() / 4000));
  98. { for(size_t i = 0; i < iterations; ++i)
  99. {
  100. XTESTS_TEST_INTEGER_EQUAL(i, pantheios::util::strnlen(PANTHEIOS_LITERAL_STRING(""), i));
  101. }}
  102. }
  103. static void test_length_via_strlen()
  104. {
  105. /* Verify that when the size given is size_t(-1) that the string length
  106. * is calculated
  107. */
  108. { for(size_t i = 0; i != STLSOFT_NUM_ELEMENTS(strings); ++i)
  109. {
  110. PAN_CHAR_T const* const string = strings[i];
  111. XTESTS_TEST_INTEGER_EQUAL(stlsoft::c_str_len(string), pantheios::util::strnlen(string, ~size_t(0)));
  112. }}
  113. }
  114. static void test_length_calculated_forwards()
  115. {
  116. /* Verify that when the size has the top-bit set, then the string length
  117. * is calculated between the given limits
  118. */
  119. const PAN_CHAR_T string[] = PANTHEIOS_LITERAL_STRING("abcdefghijklm\0nop\0qrs\0tuvwxyz"); // lengths: 13, 17, 21
  120. XTESTS_TEST_INTEGER_EQUAL( 0u, pantheios::util::strnlen(string, get_min_max( 0, 0)));
  121. XTESTS_TEST_INTEGER_EQUAL( 1u, pantheios::util::strnlen(string, get_min_max( 0, 1)));
  122. XTESTS_TEST_INTEGER_EQUAL( 2u, pantheios::util::strnlen(string, get_min_max( 0, 2)));
  123. XTESTS_TEST_INTEGER_EQUAL( 3u, pantheios::util::strnlen(string, get_min_max( 0, 3)));
  124. XTESTS_TEST_INTEGER_EQUAL( 4u, pantheios::util::strnlen(string, get_min_max( 0, 4)));
  125. XTESTS_TEST_INTEGER_EQUAL( 5u, pantheios::util::strnlen(string, get_min_max( 0, 5)));
  126. XTESTS_TEST_INTEGER_EQUAL( 6u, pantheios::util::strnlen(string, get_min_max( 0, 6)));
  127. XTESTS_TEST_INTEGER_EQUAL( 7u, pantheios::util::strnlen(string, get_min_max( 0, 7)));
  128. XTESTS_TEST_INTEGER_EQUAL( 8u, pantheios::util::strnlen(string, get_min_max( 0, 8)));
  129. XTESTS_TEST_INTEGER_EQUAL( 9u, pantheios::util::strnlen(string, get_min_max( 0, 9)));
  130. XTESTS_TEST_INTEGER_EQUAL(10u, pantheios::util::strnlen(string, get_min_max( 0, 10)));
  131. XTESTS_TEST_INTEGER_EQUAL(11u, pantheios::util::strnlen(string, get_min_max( 0, 11)));
  132. XTESTS_TEST_INTEGER_EQUAL(12u, pantheios::util::strnlen(string, get_min_max( 0, 12)));
  133. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 13)));
  134. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 14)));
  135. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 15)));
  136. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 16)));
  137. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 17)));
  138. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 18)));
  139. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 19)));
  140. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 20)));
  141. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 21)));
  142. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 22)));
  143. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 23)));
  144. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 24)));
  145. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 25)));
  146. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 26)));
  147. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 27)));
  148. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max( 0, 28)));
  149. XTESTS_TEST_INTEGER_EQUAL(10u, pantheios::util::strnlen(string, get_min_max(10, 10)));
  150. XTESTS_TEST_INTEGER_EQUAL(11u, pantheios::util::strnlen(string, get_min_max(10, 11)));
  151. XTESTS_TEST_INTEGER_EQUAL(12u, pantheios::util::strnlen(string, get_min_max(10, 12)));
  152. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(10, 13)));
  153. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(10, 14)));
  154. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(10, 15)));
  155. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(10, 16)));
  156. XTESTS_TEST_INTEGER_EQUAL(14u, pantheios::util::strnlen(string, get_min_max(14, 14)));
  157. XTESTS_TEST_INTEGER_EQUAL(15u, pantheios::util::strnlen(string, get_min_max(14, 15)));
  158. XTESTS_TEST_INTEGER_EQUAL(16u, pantheios::util::strnlen(string, get_min_max(14, 16)));
  159. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(14, 17)));
  160. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(14, 18)));
  161. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(14, 19)));
  162. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(14, 20)));
  163. XTESTS_TEST_INTEGER_EQUAL(18u, pantheios::util::strnlen(string, get_min_max(18, 18)));
  164. XTESTS_TEST_INTEGER_EQUAL(19u, pantheios::util::strnlen(string, get_min_max(18, 19)));
  165. XTESTS_TEST_INTEGER_EQUAL(20u, pantheios::util::strnlen(string, get_min_max(18, 20)));
  166. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(18, 21)));
  167. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(18, 22)));
  168. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(18, 23)));
  169. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(18, 24)));
  170. }
  171. static void test_length_calculated_backwards()
  172. {
  173. /* Verify that when the size has the top-bit set, then the string length
  174. * is calculated between the given limits
  175. */
  176. const PAN_CHAR_T string[] = PANTHEIOS_LITERAL_STRING("abcdefghijklm\0nop\0qrs\0tuvwxyz"); // lengths: 13, 17, 21
  177. XTESTS_TEST_INTEGER_EQUAL( 0u, pantheios::util::strnlen(string, get_min_max( 0, 0)));
  178. XTESTS_TEST_INTEGER_EQUAL( 1u, pantheios::util::strnlen(string, get_min_max( 1, 0)));
  179. XTESTS_TEST_INTEGER_EQUAL( 2u, pantheios::util::strnlen(string, get_min_max( 2, 0)));
  180. XTESTS_TEST_INTEGER_EQUAL( 3u, pantheios::util::strnlen(string, get_min_max( 3, 0)));
  181. XTESTS_TEST_INTEGER_EQUAL( 4u, pantheios::util::strnlen(string, get_min_max( 4, 0)));
  182. XTESTS_TEST_INTEGER_EQUAL( 5u, pantheios::util::strnlen(string, get_min_max( 5, 0)));
  183. XTESTS_TEST_INTEGER_EQUAL( 6u, pantheios::util::strnlen(string, get_min_max( 6, 0)));
  184. XTESTS_TEST_INTEGER_EQUAL( 7u, pantheios::util::strnlen(string, get_min_max( 7, 0)));
  185. XTESTS_TEST_INTEGER_EQUAL( 8u, pantheios::util::strnlen(string, get_min_max( 8, 0)));
  186. XTESTS_TEST_INTEGER_EQUAL( 9u, pantheios::util::strnlen(string, get_min_max( 9, 0)));
  187. XTESTS_TEST_INTEGER_EQUAL(10u, pantheios::util::strnlen(string, get_min_max(10, 0)));
  188. XTESTS_TEST_INTEGER_EQUAL(11u, pantheios::util::strnlen(string, get_min_max(11, 0)));
  189. XTESTS_TEST_INTEGER_EQUAL(12u, pantheios::util::strnlen(string, get_min_max(12, 0)));
  190. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(13, 0)));
  191. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(14, 0)));
  192. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(15, 0)));
  193. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(16, 0)));
  194. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(17, 0)));
  195. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(18, 0)));
  196. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(19, 0)));
  197. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(20, 0)));
  198. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(21, 0)));
  199. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(22, 0)));
  200. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(23, 0)));
  201. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(24, 0)));
  202. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(25, 0)));
  203. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(26, 0)));
  204. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(27, 0)));
  205. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(28, 0)));
  206. XTESTS_TEST_INTEGER_EQUAL(10u, pantheios::util::strnlen(string, get_min_max(10, 10)));
  207. XTESTS_TEST_INTEGER_EQUAL(11u, pantheios::util::strnlen(string, get_min_max(11, 10)));
  208. XTESTS_TEST_INTEGER_EQUAL(12u, pantheios::util::strnlen(string, get_min_max(12, 10)));
  209. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(13, 10)));
  210. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(14, 10)));
  211. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(15, 10)));
  212. XTESTS_TEST_INTEGER_EQUAL(13u, pantheios::util::strnlen(string, get_min_max(16, 10)));
  213. XTESTS_TEST_INTEGER_EQUAL(14u, pantheios::util::strnlen(string, get_min_max(14, 14)));
  214. XTESTS_TEST_INTEGER_EQUAL(15u, pantheios::util::strnlen(string, get_min_max(15, 14)));
  215. XTESTS_TEST_INTEGER_EQUAL(16u, pantheios::util::strnlen(string, get_min_max(16, 14)));
  216. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(17, 14)));
  217. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(18, 14)));
  218. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(19, 14)));
  219. XTESTS_TEST_INTEGER_EQUAL(17u, pantheios::util::strnlen(string, get_min_max(20, 14)));
  220. XTESTS_TEST_INTEGER_EQUAL(18u, pantheios::util::strnlen(string, get_min_max(18, 18)));
  221. XTESTS_TEST_INTEGER_EQUAL(19u, pantheios::util::strnlen(string, get_min_max(19, 18)));
  222. XTESTS_TEST_INTEGER_EQUAL(20u, pantheios::util::strnlen(string, get_min_max(20, 18)));
  223. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(21, 18)));
  224. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(22, 18)));
  225. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(23, 18)));
  226. XTESTS_TEST_INTEGER_EQUAL(21u, pantheios::util::strnlen(string, get_min_max(24, 18)));
  227. }
  228. static void test_strnlen_with_negatives()
  229. {
  230. PAN_CHAR_T const abcdefghi[] = PANTHEIOS_LITERAL_STRING("abcdefghi");
  231. XTESTS_TEST_INTEGER_EQUAL(9u, pantheios::util::strnlen(abcdefghi, static_cast<size_t>(-1)));
  232. {
  233. signed char minus1 = -1;
  234. XTESTS_TEST_INTEGER_EQUAL(9u, pantheios::util::strnlen(abcdefghi, static_cast<size_t>(minus1)));
  235. }
  236. {
  237. short minus1 = -1;
  238. XTESTS_TEST_INTEGER_EQUAL(9u, pantheios::util::strnlen(abcdefghi, static_cast<size_t>(minus1)));
  239. }
  240. {
  241. int minus1 = -1;
  242. XTESTS_TEST_INTEGER_EQUAL(9u, pantheios::util::strnlen(abcdefghi, static_cast<size_t>(minus1)));
  243. }
  244. {
  245. long minus1 = -1;
  246. XTESTS_TEST_INTEGER_EQUAL(9u, pantheios::util::strnlen(abcdefghi, static_cast<size_t>(minus1)));
  247. }
  248. }
  249. static void test_1_06()
  250. {
  251. }
  252. static void test_1_07()
  253. {
  254. }
  255. static void test_1_08()
  256. {
  257. }
  258. static void test_1_09()
  259. {
  260. }
  261. static void test_1_10()
  262. {
  263. }
  264. static void test_1_11()
  265. {
  266. }
  267. static void test_1_12()
  268. {
  269. }
  270. } // anonymous namespace
  271. /* ////////////////////////////////////////////////////////////////////// */