[PATCH v4 02/33] test: Add tests for trailing_strtol()

Simon Glass sjg at chromium.org
Sun Mar 6 13:49:45 CET 2022


This function currently has no tests. Add some.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v4:
- Use new Return style in function comments

 include/vsprintf.h |  4 ++--
 test/str_ut.c      | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/include/vsprintf.h b/include/vsprintf.h
index 532ef3650b..3d1f968df4 100644
--- a/include/vsprintf.h
+++ b/include/vsprintf.h
@@ -99,7 +99,7 @@ long long simple_strtoll(const char *cp, char **endp, unsigned int base);
  * For example, "abc123" would return 123.
  *
  * @str:	String to exxamine
- * Return: training number if found, else -1
+ * Return: trailing number if found, else -1
  */
 long trailing_strtol(const char *str);
 
@@ -114,7 +114,7 @@ long trailing_strtol(const char *str);
  * @str:	String to exxamine
  * @end:	Pointer to end of string to examine, or NULL to use the
  *		whole string
- * Return: training number if found, else -1
+ * Return: trailing number if found, else -1
  */
 long trailing_strtoln(const char *str, const char *end);
 
diff --git a/test/str_ut.c b/test/str_ut.c
index 6c817f6f72..9674a59f2a 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -242,6 +242,25 @@ static int str_xtoa(struct unit_test_state *uts)
 }
 STR_TEST(str_xtoa, 0);
 
+static int str_trailing(struct unit_test_state *uts)
+{
+	char str1[] = "abc123def";
+
+	ut_asserteq(-1, trailing_strtol(""));
+	ut_asserteq(-1, trailing_strtol("123"));
+	ut_asserteq(123, trailing_strtol("abc123"));
+	ut_asserteq(4, trailing_strtol("12c4"));
+	ut_asserteq(-1, trailing_strtol("abd"));
+	ut_asserteq(-1, trailing_strtol("abc123def"));
+
+	ut_asserteq(-1, trailing_strtoln(str1, NULL));
+	ut_asserteq(123, trailing_strtoln(str1, str1 + 6));
+	ut_asserteq(-1, trailing_strtoln(str1, str1 + 9));
+
+	return 0;
+}
+STR_TEST(str_trailing, 0);
+
 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
-- 
2.35.1.616.g0bdcbb4464-goog



More information about the U-Boot mailing list