str_ut.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /* Declare a new str test */
  16. #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test)
  17. static int str_test_upper(struct unit_test_state *uts)
  18. {
  19. char out[TEST_STR_SIZE];
  20. /* Make sure it adds a terminator */
  21. out[strlen(str1)] = 'a';
  22. str_to_upper(str1, out, SIZE_MAX);
  23. ut_asserteq_str("I'M SORRY I'M LATE.", out);
  24. /* In-place operation */
  25. strcpy(out, str2);
  26. str_to_upper(out, out, SIZE_MAX);
  27. ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
  28. /* Limited length */
  29. str_to_upper(str1, out, 7);
  30. ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
  31. /* In-place with limited length */
  32. strcpy(out, str2);
  33. str_to_upper(out, out, 7);
  34. ut_asserteq_str("1099ABNo, don't bother apologising.", out);
  35. /* Copy an empty string to a buffer with space*/
  36. out[1] = 0x7f;
  37. str_to_upper("", out, SIZE_MAX);
  38. ut_asserteq('\0', *out);
  39. ut_asserteq(0x7f, out[1]);
  40. /* Copy an empty string to a buffer with no space*/
  41. out[0] = 0x7f;
  42. str_to_upper("", out, 0);
  43. ut_asserteq(0x7f, out[0]);
  44. return 0;
  45. }
  46. STR_TEST(str_test_upper, 0);
  47. static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
  48. ulong expect_val, int expect_endp_offset, bool upper)
  49. {
  50. char out[TEST_STR_SIZE];
  51. char *endp;
  52. ulong val;
  53. strcpy(out, str);
  54. if (upper)
  55. str_to_upper(out, out, -1);
  56. val = simple_strtoul(out, &endp, base);
  57. ut_asserteq(expect_val, val);
  58. ut_asserteq(expect_endp_offset, endp - out);
  59. return 0;
  60. }
  61. static int str_simple_strtoul(struct unit_test_state *uts)
  62. {
  63. int upper;
  64. /* Check that it is case-insentive */
  65. for (upper = 0; upper < 2; upper++) {
  66. /* Base 10 and base 16 */
  67. ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
  68. ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
  69. /* Invalid string */
  70. ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
  71. /* Base 0 */
  72. ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
  73. ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
  74. ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
  75. /* Base 2 */
  76. ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
  77. ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
  78. }
  79. /* Check endp being NULL */
  80. ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
  81. return 0;
  82. }
  83. STR_TEST(str_simple_strtoul, 0);
  84. int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  85. {
  86. struct unit_test *tests = ll_entry_start(struct unit_test,
  87. str_test);
  88. const int n_ents = ll_entry_count(struct unit_test, str_test);
  89. return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
  90. }