Browse Source

tests: unit: util: increase coverage of utf-8 utils

Test all possible UTF-8 sequence lengths for utf8_trunc()

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
pull/91566/head
Benjamin Cabé 4 weeks ago committed by Henrik Brix Andersen
parent
commit
639cb9b3bc
  1. 46
      tests/unit/util/main.c

46
tests/unit/util/main.c

@ -898,30 +898,40 @@ ZTEST(util, test_SIZEOF_FIELD)
ZTEST(util, test_utf8_trunc_truncated) ZTEST(util, test_utf8_trunc_truncated)
{ {
char test_str[] = "€€€"; struct {
char expected_result[] = "€€"; char input[20];
char expected[20];
/* Remove last byte from truncated_test_str and verify that it first is incorrectly } tests[] = {
* truncated, followed by a proper truncation and verification {"ééé", "éé"}, /* 2-byte UTF-8 characters */
*/ {"€€€", "€€"}, /* 3-byte UTF-8 characters */
test_str[strlen(test_str) - 1] = '\0'; {"𠜎𠜎𠜎", "𠜎𠜎"}, /* 4-byte UTF-8 characters */
zassert(strcmp(test_str, "€€€") != 0, "Failed to do invalid truncation"); {"Hello 世界!🌍", "Hello 世界!"}, /* mixed UTF-8 characters */
zassert(strcmp(test_str, expected_result) != 0, "Failed to do invalid truncation"); };
utf8_trunc(test_str);
zassert_str_equal(test_str, expected_result, "Failed to truncate"); for (size_t i = 0; i < ARRAY_SIZE(tests); i++) {
tests[i].input[strlen(tests[i].input) - 1] = '\0';
utf8_trunc(tests[i].input);
zassert_str_equal(tests[i].input, tests[i].expected, "Failed to truncate");
}
} }
ZTEST(util, test_utf8_trunc_not_truncated) ZTEST(util, test_utf8_trunc_not_truncated)
{ {
/* Attempt to truncate a valid UTF8 string and verify no changed */ struct {
char test_str[] = "€€€"; char input[20];
char expected_result[] = "€€€"; char expected[20];
} tests[] = {
utf8_trunc(test_str); {"abc", "abc"}, /* 1-byte ASCII characters */
{"ééé", "ééé"}, /* 2-byte UTF-8 characters */
{"€€€", "€€€"}, /* 3-byte UTF-8 characters */
{"𠜎𠜎𠜎", "𠜎𠜎𠜎"}, /* 4-byte UTF-8 characters */
{"Hello 世界!🌍", "Hello 世界!🌍"}, /* mixed UTF-8 characters */
};
zassert_str_equal(test_str, expected_result, "Failed to truncate"); for (size_t i = 0; i < ARRAY_SIZE(tests); i++) {
utf8_trunc(tests[i].input);
zassert_str_equal(tests[i].input, tests[i].expected, "No-op truncation failed");
}
} }
ZTEST(util, test_utf8_trunc_zero_length) ZTEST(util, test_utf8_trunc_zero_length)

Loading…
Cancel
Save