str_ut.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2020 Google LLC
  4. */
  5. #include <common.h>
  6. #include <vsprintf.h>
  7. #include <test/suites.h>
  8. #include <test/test.h>
  9. #include <test/ut.h>
  10. /* This is large enough for any of the test strings */
  11. #define TEST_STR_SIZE 200
  12. static const char str1[] = "I'm sorry I'm late.";
  13. static const char str2[] = "1099abNo, don't bother apologising.";
  14. static const char str3[] = "0xbI'm sorry you're alive.";
  15. static const char str4[] = "1234567890123 I lost closer friends";
  16. static const char str5[] = "0x9876543210the last time I was deloused";
  17. static const char str6[] = "0778octal is seldom used";
  18. static const char str7[] = "707it is a piece of computing history";
  19. /* Declare a new str test */
  20. #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test)
  21. static int str_upper(struct unit_test_state *uts)
  22. {
  23. char out[TEST_STR_SIZE];
  24. /* Make sure it adds a terminator */
  25. out[strlen(str1)] = 'a';
  26. str_to_upper(str1, out, SIZE_MAX);
  27. ut_asserteq_str("I'M SORRY I'M LATE.", out);
  28. /* In-place operation */
  29. strcpy(out, str2);
  30. str_to_upper(out, out, SIZE_MAX);
  31. ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
  32. /* Limited length */
  33. str_to_upper(str1, out, 7);
  34. ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
  35. /* In-place with limited length */
  36. strcpy(out, str2);
  37. str_to_upper(out, out, 7);
  38. ut_asserteq_str("1099ABNo, don't bother apologising.", out);
  39. /* Copy an empty string to a buffer with space*/
  40. out[1] = 0x7f;
  41. str_to_upper("", out, SIZE_MAX);
  42. ut_asserteq('\0', *out);
  43. ut_asserteq(0x7f, out[1]);
  44. /* Copy an empty string to a buffer with no space*/
  45. out[0] = 0x7f;
  46. str_to_upper("", out, 0);
  47. ut_asserteq(0x7f, out[0]);
  48. return 0;
  49. }
  50. STR_TEST(str_upper, 0);
  51. static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
  52. ulong expect_val, int expect_endp_offset, bool upper)
  53. {
  54. char out[TEST_STR_SIZE];
  55. char *endp;
  56. ulong val;
  57. strcpy(out, str);
  58. if (upper)
  59. str_to_upper(out, out, -1);
  60. val = simple_strtoul(out, &endp, base);
  61. ut_asserteq(expect_val, val);
  62. ut_asserteq(expect_endp_offset, endp - out);
  63. return 0;
  64. }
  65. static int str_simple_strtoul(struct unit_test_state *uts)
  66. {
  67. int upper;
  68. /* Check that it is case-insentive */
  69. for (upper = 0; upper < 2; upper++) {
  70. /* Base 10 and base 16 */
  71. ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
  72. ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
  73. ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
  74. ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));
  75. /* Octal */
  76. ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper));
  77. ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper));
  78. /* Invalid string */
  79. ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
  80. /* Base 0 */
  81. ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
  82. ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
  83. ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
  84. /* Base 2 */
  85. ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
  86. ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
  87. }
  88. /* Check endp being NULL */
  89. ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
  90. return 0;
  91. }
  92. STR_TEST(str_simple_strtoul, 0);
  93. static int run_strtoull(struct unit_test_state *uts, const char *str, int base,
  94. unsigned long long expect_val, int expect_endp_offset,
  95. bool upper)
  96. {
  97. char out[TEST_STR_SIZE];
  98. char *endp;
  99. unsigned long long val;
  100. strcpy(out, str);
  101. if (upper)
  102. str_to_upper(out, out, -1);
  103. val = simple_strtoull(out, &endp, base);
  104. ut_asserteq(expect_val, val);
  105. ut_asserteq(expect_endp_offset, endp - out);
  106. return 0;
  107. }
  108. static int str_simple_strtoull(struct unit_test_state *uts)
  109. {
  110. int upper;
  111. /* Check that it is case-insentive */
  112. for (upper = 0; upper < 2; upper++) {
  113. /* Base 10 and base 16 */
  114. ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper));
  115. ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper));
  116. ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
  117. ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));
  118. /* Octal */
  119. ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper));
  120. ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper));
  121. /* Large values */
  122. ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
  123. upper));
  124. ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13,
  125. upper));
  126. ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12,
  127. upper));
  128. /* Invalid string */
  129. ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper));
  130. /* Base 0 */
  131. ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper));
  132. ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper));
  133. ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper));
  134. /* Base 2 */
  135. ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper));
  136. ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper));
  137. }
  138. /* Check endp being NULL */
  139. ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
  140. return 0;
  141. }
  142. STR_TEST(str_simple_strtoull, 0);
  143. static int str_hextoul(struct unit_test_state *uts)
  144. {
  145. char *endp;
  146. /* Just a simple test, since we know this uses simple_strtoul() */
  147. ut_asserteq(0x1099ab, hextoul(str2, &endp));
  148. ut_asserteq(6, endp - str2);
  149. return 0;
  150. }
  151. STR_TEST(str_hextoul, 0);
  152. static int str_dectoul(struct unit_test_state *uts)
  153. {
  154. char *endp;
  155. /* Just a simple test, since we know this uses simple_strtoul() */
  156. ut_asserteq(1099, dectoul(str2, &endp));
  157. ut_asserteq(4, endp - str2);
  158. return 0;
  159. }
  160. STR_TEST(str_dectoul, 0);
  161. static int str_itoa(struct unit_test_state *uts)
  162. {
  163. ut_asserteq_str("123", simple_itoa(123));
  164. ut_asserteq_str("0", simple_itoa(0));
  165. ut_asserteq_str("2147483647", simple_itoa(0x7fffffff));
  166. ut_asserteq_str("4294967295", simple_itoa(0xffffffff));
  167. /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
  168. #ifdef CONFIG_PHYS_64BIT
  169. if (sizeof(ulong) == 8) {
  170. ut_asserteq_str("9223372036854775807",
  171. simple_itoa((1UL << 63) - 1));
  172. ut_asserteq_str("18446744073709551615", simple_itoa(-1));
  173. }
  174. #endif /* CONFIG_PHYS_64BIT */
  175. return 0;
  176. }
  177. STR_TEST(str_itoa, 0);
  178. static int str_xtoa(struct unit_test_state *uts)
  179. {
  180. ut_asserteq_str("7f", simple_xtoa(127));
  181. ut_asserteq_str("00", simple_xtoa(0));
  182. ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff));
  183. ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff));
  184. /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
  185. #ifdef CONFIG_PHYS_64BIT
  186. if (sizeof(ulong) == 8) {
  187. ut_asserteq_str("7fffffffffffffff",
  188. simple_xtoa((1UL << 63) - 1));
  189. ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1));
  190. }
  191. #endif /* CONFIG_PHYS_64BIT */
  192. return 0;
  193. }
  194. STR_TEST(str_xtoa, 0);
  195. static int str_trailing(struct unit_test_state *uts)
  196. {
  197. const char str1[] = "abc123def";
  198. const char str2[] = "abc123def456";
  199. const char *end;
  200. ut_asserteq(-1, trailing_strtol(""));
  201. ut_asserteq(-1, trailing_strtol("123"));
  202. ut_asserteq(123, trailing_strtol("abc123"));
  203. ut_asserteq(4, trailing_strtol("12c4"));
  204. ut_asserteq(-1, trailing_strtol("abd"));
  205. ut_asserteq(-1, trailing_strtol("abc123def"));
  206. ut_asserteq(-1, trailing_strtoln(str1, NULL));
  207. ut_asserteq(123, trailing_strtoln(str1, str1 + 6));
  208. ut_asserteq(-1, trailing_strtoln(str1, str1 + 9));
  209. ut_asserteq(3, trailing_strtol("a3"));
  210. ut_asserteq(123, trailing_strtoln_end(str1, str1 + 6, &end));
  211. ut_asserteq(3, end - str1);
  212. ut_asserteq(-1, trailing_strtoln_end(str1, str1 + 7, &end));
  213. ut_asserteq(7, end - str1);
  214. ut_asserteq(456, trailing_strtoln_end(str2, NULL, &end));
  215. ut_asserteq(9, end - str2);
  216. return 0;
  217. }
  218. STR_TEST(str_trailing, 0);
  219. static int test_str_to_list(struct unit_test_state *uts)
  220. {
  221. const char **ptr;
  222. ulong start;
  223. /* check out of memory */
  224. start = ut_check_delta(0);
  225. malloc_enable_testing(0);
  226. ut_assertnull(str_to_list(""));
  227. ut_assertok(ut_check_delta(start));
  228. ut_assertnull(str_to_list("this is a test"));
  229. ut_assertok(ut_check_delta(start));
  230. malloc_enable_testing(1);
  231. ut_assertnull(str_to_list("this is a test"));
  232. ut_assertok(ut_check_delta(start));
  233. /* for an empty string, only one nalloc is needed */
  234. malloc_enable_testing(1);
  235. ptr = str_to_list("");
  236. ut_assertnonnull(ptr);
  237. ut_assertnull(ptr[0]);
  238. str_free_list(ptr);
  239. ut_assertok(ut_check_delta(start));
  240. malloc_disable_testing();
  241. /* test the same again, without any nalloc restrictions */
  242. ptr = str_to_list("");
  243. ut_assertnonnull(ptr);
  244. ut_assertnull(ptr[0]);
  245. str_free_list(ptr);
  246. ut_assertok(ut_check_delta(start));
  247. /* test a single string */
  248. start = ut_check_delta(0);
  249. ptr = str_to_list("hi");
  250. ut_assertnonnull(ptr);
  251. ut_assertnonnull(ptr[0]);
  252. ut_asserteq_str("hi", ptr[0]);
  253. ut_assertnull(ptr[1]);
  254. str_free_list(ptr);
  255. ut_assertok(ut_check_delta(start));
  256. /* test two strings */
  257. ptr = str_to_list("hi there");
  258. ut_assertnonnull(ptr);
  259. ut_assertnonnull(ptr[0]);
  260. ut_asserteq_str("hi", ptr[0]);
  261. ut_assertnonnull(ptr[1]);
  262. ut_asserteq_str("there", ptr[1]);
  263. ut_assertnull(ptr[2]);
  264. str_free_list(ptr);
  265. ut_assertok(ut_check_delta(start));
  266. /* test leading, trailing and multiple spaces */
  267. ptr = str_to_list(" more space ");
  268. ut_assertnonnull(ptr);
  269. ut_assertnonnull(ptr[0]);
  270. ut_asserteq_str("", ptr[0]);
  271. ut_assertnonnull(ptr[1]);
  272. ut_asserteq_str("more", ptr[1]);
  273. ut_assertnonnull(ptr[2]);
  274. ut_asserteq_str("", ptr[2]);
  275. ut_assertnonnull(ptr[3]);
  276. ut_asserteq_str("space", ptr[3]);
  277. ut_assertnonnull(ptr[4]);
  278. ut_asserteq_str("", ptr[4]);
  279. ut_assertnonnull(ptr[5]);
  280. ut_asserteq_str("", ptr[5]);
  281. ut_assertnull(ptr[6]);
  282. str_free_list(ptr);
  283. ut_assertok(ut_check_delta(start));
  284. /* test freeing a NULL pointer */
  285. str_free_list(NULL);
  286. return 0;
  287. }
  288. STR_TEST(test_str_to_list, 0);
  289. int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  290. {
  291. struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
  292. const int n_ents = UNIT_TEST_SUITE_COUNT(str_test);
  293. return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
  294. }