str_ut.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  162. {
  163. struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
  164. const int n_ents = UNIT_TEST_SUITE_COUNT(str_test);
  165. return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
  166. }