diff mbox series

[RFC,v2,3/4] test: unit test for u16_strlcat()

Message ID 20220222160235.24748-4-masahisa.kojima@linaro.org
State Superseded
Headers show
Series enable menu-driven boot device selection | expand

Commit Message

Masahisa Kojima Feb. 22, 2022, 4:02 p.m. UTC
Provide a unit test for function u16_strlcat().

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
 test/unicode_ut.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
diff mbox series

Patch

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);