From dbcfa1bdc80a803a0db958829d191999c139d3f8 Mon Sep 17 00:00:00 2001 From: Teufelchen1 Date: Tue, 6 Feb 2024 13:58:29 +0100 Subject: [PATCH] tests: Add NULL pointer argument test for fmt_lpad/to_lower/str --- sys/include/fmt.h | 5 +++++ tests/unittests/tests-fmt/tests-fmt.c | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/sys/include/fmt.h b/sys/include/fmt.h index 0fae2294d9..afc99bf3ef 100644 --- a/sys/include/fmt.h +++ b/sys/include/fmt.h @@ -393,8 +393,13 @@ size_t fmt_str(char *out, const char *str); /** * @brief Copy null-terminated string to a lowercase string (excluding terminating \0) * + * If @p out is NULL, will only return the number of characters that would have + * been written. + * * @param[out] out Pointer to output buffer, or NULL * @param[in] str Pointer to null-terminated source string + * + * @return nr of characters written to (or needed in) @p out */ size_t fmt_to_lower(char *out, const char *str); diff --git a/tests/unittests/tests-fmt/tests-fmt.c b/tests/unittests/tests-fmt/tests-fmt.c index c3541dad66..1089897762 100644 --- a/tests/unittests/tests-fmt/tests-fmt.c +++ b/tests/unittests/tests-fmt/tests-fmt.c @@ -814,6 +814,7 @@ static void test_fmt_str(void) const char *string1 = "string1"; char string2[] = "StRiNg2"; + TEST_ASSERT_EQUAL_INT(7, fmt_str(NULL, string1)); TEST_ASSERT_EQUAL_INT(fmt_strlen(string1), fmt_str(&string2[0], string1)); TEST_ASSERT_EQUAL_STRING(string1, &string2[0]); } @@ -833,6 +834,7 @@ static void test_fmt_to_lower(void) const char string_up[] = "AbCdeFGHijkLM"; char string[] = "zzzzzzzzzzzzzzz"; + TEST_ASSERT_EQUAL_INT(fmt_strlen(string_up), fmt_to_lower(NULL, string_up)); TEST_ASSERT_EQUAL_INT(fmt_strlen(string_up), fmt_to_lower(string, string_up)); string[fmt_strlen(string_up)] = '\0'; TEST_ASSERT_EQUAL_STRING("abcdefghijklm", &string[0]); @@ -866,6 +868,8 @@ static void test_fmt_lpad(void) strcpy(string, base); + TEST_ASSERT_EQUAL_INT(8, fmt_lpad(NULL, 4, 8, ' ')); + fmt_lpad(string, 4, 8, ' '); TEST_ASSERT_EQUAL_STRING(" abcd", (char*)string);