[PATCH v4 03/11] test: unit test for u16_strlcat()

Masahisa Kojima masahisa.kojima at linaro.org
Thu Mar 24 14:54:35 CET 2022


Provide a unit test for function u16_strlcat().

Signed-off-by: Masahisa Kojima <masahisa.kojima at linaro.org>
Reviewed-by: Simon Glass <sjg at chromium.org>
---
No change since v2

Newly created in v2

 test/unicode_ut.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/test/unicode_ut.c b/test/unicode_ut.c
index f2f63d5367..f79b0439bc 100644
--- a/test/unicode_ut.c
+++ b/test/unicode_ut.c
@@ -758,6 +758,51 @@ static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
 UNICODE_TEST(unicode_test_efi_create_indexed_name);
 #endif
 
+static int unicode_test_u16_strlcat(struct unit_test_state *uts)
+{
+	u16 buf[11];
+	u16 dest[] = u"U-Boot";
+	u16 src[] = u"test";
+	u16 expected[] = u"U-Boottest";
+	u16 null_src = u'\0';
+	size_t ret;
+
+	memcpy(buf, dest, sizeof(dest));
+	ret = u16_strlcat(buf, src, sizeof(buf));
+	ut_asserteq(20, ret);
+	ut_assert(!unicode_test_u16_strcmp(buf, expected, 11));
+
+	/* dest is empty string */
+	memset(buf, 0, sizeof(buf));
+	ret = u16_strlcat(buf, src, sizeof(buf));
+	ut_asserteq(8, ret);
+	ut_assert(!unicode_test_u16_strcmp(buf, src, 11));
+
+	/* src is empty string */
+	memset(buf, 0, sizeof(buf));
+	memcpy(buf, dest, sizeof(dest));
+	ret = u16_strlcat(buf, &null_src, sizeof(buf));
+	ut_asserteq(12, ret);
+	ut_assert(!unicode_test_u16_strcmp(buf, dest, 11));
+
+	/* size is smaller than dest size */
+	memset(buf, 0, sizeof(buf));
+	memcpy(buf, dest, sizeof(dest));
+	ret = u16_strlcat(buf, src, 6);
+	ut_asserteq(14, ret);
+	ut_assert(!unicode_test_u16_strcmp(buf, dest, 11));
+
+	/* size is insufficient to append src string */
+	memset(buf, 0, sizeof(buf));
+	memcpy(buf, dest, sizeof(dest));
+	ret = u16_strlcat(buf, src, 20);
+	ut_asserteq(20, ret);
+	ut_assert(!unicode_test_u16_strcmp(buf, u"U-Boottes", 11));
+
+	return 0;
+}
+UNICODE_TEST(unicode_test_u16_strlcat);
+
 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
-- 
2.17.1



More information about the U-Boot mailing list