]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-utf8.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / test / test-utf8.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Dave Reisner
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "utf8.h"
23 #include "util.h"
24 #include "string-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 int main(int argc, char *argv[]) {
110 test_utf8_is_valid();
111 test_utf8_is_printable();
112 test_ascii_is_valid();
113 test_utf8_encoded_valid_unichar();
114 test_utf8_escaping();
115 test_utf8_escaping_printable();
116 test_utf16_to_utf8();
117
118 return 0;
119 }