chiark / gitweb /
tests: add a simple test for utf8_n_codepoints()
[elogind.git] / src / test / test-utf8.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2013 Dave Reisner
6
7   systemd is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   systemd is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "string-util.h"
23 #include "utf8.h"
24 #include "util.h"
25
26 static void test_utf8_is_printable(void) {
27         assert_se(utf8_is_printable("ascii is valid\tunicode", 22));
28         assert_se(utf8_is_printable("\342\204\242", 3));
29         assert_se(!utf8_is_printable("\341\204", 2));
30         assert_se(utf8_is_printable("ąę", 4));
31 }
32
33 static void test_utf8_is_valid(void) {
34         assert_se(utf8_is_valid("ascii is valid unicode"));
35         assert_se(utf8_is_valid("\342\204\242"));
36         assert_se(!utf8_is_valid("\341\204"));
37 }
38
39 static void test_ascii_is_valid(void) {
40         assert_se(ascii_is_valid("alsdjf\t\vbarr\nba z"));
41         assert_se(!ascii_is_valid("\342\204\242"));
42         assert_se(!ascii_is_valid("\341\204"));
43 }
44
45 static void test_utf8_encoded_valid_unichar(void) {
46         assert_se(utf8_encoded_valid_unichar("\342\204\242") == 3);
47         assert_se(utf8_encoded_valid_unichar("\302\256") == 2);
48         assert_se(utf8_encoded_valid_unichar("a") == 1);
49         assert_se(utf8_encoded_valid_unichar("\341\204") < 0);
50         assert_se(utf8_encoded_valid_unichar("\341\204\341\204") < 0);
51 }
52
53 static void test_utf8_escaping(void) {
54         _cleanup_free_ char *p1, *p2, *p3;
55
56         p1 = utf8_escape_invalid("goo goo goo");
57         puts(p1);
58         assert_se(utf8_is_valid(p1));
59
60         p2 = utf8_escape_invalid("\341\204\341\204");
61         puts(p2);
62         assert_se(utf8_is_valid(p2));
63
64         p3 = utf8_escape_invalid("\341\204");
65         puts(p3);
66         assert_se(utf8_is_valid(p3));
67 }
68
69 static void test_utf8_escaping_printable(void) {
70         _cleanup_free_ char *p1, *p2, *p3, *p4, *p5, *p6;
71
72         p1 = utf8_escape_non_printable("goo goo goo");
73         puts(p1);
74         assert_se(utf8_is_valid(p1));
75
76         p2 = utf8_escape_non_printable("\341\204\341\204");
77         puts(p2);
78         assert_se(utf8_is_valid(p2));
79
80         p3 = utf8_escape_non_printable("\341\204");
81         puts(p3);
82         assert_se(utf8_is_valid(p3));
83
84         p4 = utf8_escape_non_printable("ąę\n가너도루\n1234\n\341\204\341\204\n\001 \019\20\a");
85         puts(p4);
86         assert_se(utf8_is_valid(p4));
87
88         p5 = utf8_escape_non_printable("\001 \019\20\a");
89         puts(p5);
90         assert_se(utf8_is_valid(p5));
91
92         p6 = utf8_escape_non_printable("\xef\xbf\x30\x13");
93         puts(p6);
94         assert_se(utf8_is_valid(p6));
95 }
96
97 static void test_utf16_to_utf8(void) {
98         char *a = NULL;
99         const uint16_t utf16[] = { htole16('a'), htole16(0xd800), htole16('b'), htole16(0xdc00), htole16('c'), htole16(0xd801), htole16(0xdc37) };
100         const char utf8[] = { 'a', 'b', 'c', 0xf0, 0x90, 0x90, 0xb7, 0 };
101
102         a = utf16_to_utf8(utf16, 14);
103         assert_se(a);
104         assert_se(streq(a, utf8));
105
106         free(a);
107 }
108
109 static void test_utf8_n_codepoints(void) {
110         assert_se(utf8_n_codepoints("abc") == 3);
111         assert_se(utf8_n_codepoints("zażółcić gęślą jaźń") == 19);
112         assert_se(utf8_n_codepoints("串") == 1);
113         assert_se(utf8_n_codepoints("") == 0);
114         assert_se(utf8_n_codepoints("…👊🔪💐…") == 5);
115         assert_se(utf8_n_codepoints("\xF1") == (size_t) -1);
116 }
117
118 int main(int argc, char *argv[]) {
119         test_utf8_is_valid();
120         test_utf8_is_printable();
121         test_ascii_is_valid();
122         test_utf8_encoded_valid_unichar();
123         test_utf8_escaping();
124         test_utf8_escaping_printable();
125         test_utf16_to_utf8();
126         test_utf8_n_codepoints();
127
128         return 0;
129 }