|
6 | 6 |
|
7 | 7 | #include <zephyr/ztest.h>
|
8 | 8 | #include <zephyr/sys/util.h>
|
| 9 | +#include <stdio.h> |
9 | 10 | #include <string.h>
|
10 | 11 |
|
11 | 12 | ZTEST(util, test_u8_to_dec) {
|
@@ -813,4 +814,103 @@ ZTEST(util, test_SIZEOF_FIELD)
|
813 | 814 | BUILD_ASSERT(SIZEOF_FIELD(struct test_t, d) == 2, "The d member is 2-byte wide.");
|
814 | 815 | }
|
815 | 816 |
|
| 817 | +ZTEST(util, test_utf8_trunc_truncated) |
| 818 | +{ |
| 819 | + char test_str[] = "€€€"; |
| 820 | + char expected_result[] = "€€"; |
| 821 | + |
| 822 | + /* Remove last byte from truncated_test_str and verify that it first is incorrectly |
| 823 | + * truncated, followed by a proper truncation and verification |
| 824 | + */ |
| 825 | + test_str[strlen(test_str) - 1] = '\0'; |
| 826 | + zassert(strcmp(test_str, "€€€") != 0, "Failed to do invalid truncation"); |
| 827 | + zassert(strcmp(test_str, expected_result) != 0, "Failed to do invalid truncation"); |
| 828 | + |
| 829 | + utf8_trunc(test_str); |
| 830 | + |
| 831 | + zassert_str_equal(test_str, expected_result, "Failed to truncate"); |
| 832 | +} |
| 833 | + |
| 834 | +ZTEST(util, test_utf8_trunc_not_truncated) |
| 835 | +{ |
| 836 | + /* Attempt to truncate a valid UTF8 string and verify no changed */ |
| 837 | + char test_str[] = "€€€"; |
| 838 | + char expected_result[] = "€€€"; |
| 839 | + |
| 840 | + utf8_trunc(test_str); |
| 841 | + |
| 842 | + zassert_str_equal(test_str, expected_result, "Failed to truncate"); |
| 843 | +} |
| 844 | + |
| 845 | +ZTEST(util, test_utf8_trunc_zero_length) |
| 846 | +{ |
| 847 | + /* Attempt to truncate a valid UTF8 string and verify no changed */ |
| 848 | + char test_str[] = ""; |
| 849 | + char expected_result[] = ""; |
| 850 | + |
| 851 | + utf8_trunc(test_str); |
| 852 | + |
| 853 | + zassert_str_equal(test_str, expected_result, "Failed to truncate"); |
| 854 | +} |
| 855 | + |
| 856 | +ZTEST(util, test_utf8_lcpy_truncated) |
| 857 | +{ |
| 858 | + /* dest_str size is based on storing 2 * € plus the null terminator plus an extra space to |
| 859 | + * verify that it's truncated properly |
| 860 | + */ |
| 861 | + char dest_str[strlen("€") * 2 + 1 + 1]; |
| 862 | + char test_str[] = "€€€"; |
| 863 | + char expected_result[] = "€€"; |
| 864 | + |
| 865 | + utf8_lcpy(dest_str, test_str, sizeof((dest_str))); |
| 866 | + |
| 867 | + zassert_str_equal(dest_str, expected_result, "Failed to copy"); |
| 868 | +} |
| 869 | + |
| 870 | +ZTEST(util, test_utf8_lcpy_not_truncated) |
| 871 | +{ |
| 872 | + /* dest_str size is based on storing 3 * € plus the null terminator */ |
| 873 | + char dest_str[strlen("€") * 3 + 1]; |
| 874 | + char test_str[] = "€€€"; |
| 875 | + char expected_result[] = "€€€"; |
| 876 | + |
| 877 | + utf8_lcpy(dest_str, test_str, sizeof((dest_str))); |
| 878 | + |
| 879 | + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); |
| 880 | +} |
| 881 | + |
| 882 | +ZTEST(util, test_utf8_lcpy_zero_length_copy) |
| 883 | +{ |
| 884 | + /* dest_str size is based on the null terminator */ |
| 885 | + char dest_str[1]; |
| 886 | + char test_str[] = ""; |
| 887 | + char expected_result[] = ""; |
| 888 | + |
| 889 | + utf8_lcpy(dest_str, test_str, sizeof((dest_str))); |
| 890 | + |
| 891 | + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); |
| 892 | +} |
| 893 | + |
| 894 | +ZTEST(util, test_utf8_lcpy_zero_length_dest) |
| 895 | +{ |
| 896 | + char dest_str[] = "A"; |
| 897 | + char test_str[] = ""; |
| 898 | + char expected_result[] = "A"; /* expect no changes to dest_str */ |
| 899 | + |
| 900 | + utf8_lcpy(dest_str, test_str, 0); |
| 901 | + |
| 902 | + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); |
| 903 | +} |
| 904 | + |
| 905 | +ZTEST(util, test_utf8_lcpy_null_termination) |
| 906 | +{ |
| 907 | + char dest_str[] = "DEADBEEF"; |
| 908 | + char test_str[] = "DEAD"; |
| 909 | + char expected_result[] = "DEAD"; |
| 910 | + |
| 911 | + utf8_lcpy(dest_str, test_str, sizeof(dest_str)); |
| 912 | + |
| 913 | + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); |
| 914 | +} |
| 915 | + |
816 | 916 | ZTEST_SUITE(util, NULL, NULL, NULL, NULL, NULL);
|
0 commit comments