diff mbox series

[v5,02/17] test: unit test for u16_strlcat()

Message ID 20220428080950.23509-3-masahisa.kojima@linaro.org
State New
Headers show
Series enable menu-driven boot device selection | expand

Commit Message

Masahisa Kojima April 28, 2022, 8:09 a.m. UTC
Provide a unit test for function u16_strlcat().

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
Changes in v5:
- u16_strlcat() 3rd agument update from buffer size
  to maximum string count
- increase buffer size for test
- change u16 string for the test, the letters in the test are different each other
- buffer to be used for the test is filled with non-zero value except for the
  last two bytes
- add test cases

Newly created in v2

 test/unicode_ut.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
diff mbox series

Patch

diff --git a/test/unicode_ut.c b/test/unicode_ut.c
index f2f63d5367..81c9549ba5 100644
--- a/test/unicode_ut.c
+++ b/test/unicode_ut.c
@@ -758,6 +758,56 @@  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[40];
+	u16 dest[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, 0}; /* u"こんにちは" */
+	u16 src[] = {0x03B1, 0x2172, 0x6F5C, 0x8247, 0}; /* u"αⅲ潜艇" */
+	u16 concat_str[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f,
+			    0x03B1, 0x2172, 0x6F5C, 0x8247, 0};
+	u16 null_src = u'\0';
+	size_t ret, expected;
+	int i;
+
+	/* dest and src are empty string */
+	memset(buf, 0, sizeof(buf));
+	ret = u16_strlcat(buf, &null_src, sizeof(buf));
+	ut_asserteq(0, ret);
+
+	/* dest is empty string */
+	memset(buf, 0, sizeof(buf));
+	ret = u16_strlcat(buf, src, sizeof(buf));
+	ut_asserteq(4, ret);
+	ut_assert(!unicode_test_u16_strcmp(buf, src, 40));
+
+	/* src is empty string */
+	memset(buf, 0xCD, (sizeof(buf) - sizeof(u16)));
+	buf[39] = 0;
+	memcpy(buf, dest, sizeof(dest));
+	ret = u16_strlcat(buf, &null_src, sizeof(buf));
+	ut_asserteq(5, ret);
+	ut_assert(!unicode_test_u16_strcmp(buf, dest, 40));
+
+	for (i = 0; i <= 40; i++) {
+		memset(buf, 0xCD, (sizeof(buf) - sizeof(u16)));
+		buf[39] = 0;
+		memcpy(buf, dest, sizeof(dest));
+		expected = min(i, 5 /* u16_strlen(dest) */) + 4 /* u16_strlen(src) */;
+		ret = u16_strlcat(buf, src, i);
+		ut_asserteq(expected, ret);
+		if (i <= 6) {
+			ut_assert(!unicode_test_u16_strcmp(buf, dest, 40));
+		} else if (i < 10) {
+			ut_assert(!unicode_test_u16_strcmp(buf, concat_str, i - 1));
+		} else {
+			ut_assert(!unicode_test_u16_strcmp(buf, concat_str, 40));
+		}
+	}
+
+	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);