]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-utf8.c
bootchart: add standalone bootchart service
[thirdparty/systemd.git] / src / test / test-utf8.c
CommitLineData
02a36bc9
DR
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
02a36bc9
DR
22#include "utf8.h"
23#include "util.h"
24
7991ac34
DR
25static void test_utf8_is_printable(void) {
26 assert_se(utf8_is_printable("ascii is valid\tunicode", 22));
27 assert_se(utf8_is_printable("\342\204\242", 3));
28 assert_se(!utf8_is_printable("\341\204", 2));
a7176505 29 assert_se(utf8_is_printable("ąę", 4));
7991ac34
DR
30}
31
02a36bc9
DR
32static void test_utf8_is_valid(void) {
33 assert_se(utf8_is_valid("ascii is valid unicode"));
8f6ce71f 34 assert_se(utf8_is_valid("\342\204\242"));
02a36bc9
DR
35 assert_se(!utf8_is_valid("\341\204"));
36}
37
e7363c59
DR
38static void test_ascii_is_valid(void) {
39 assert_se(ascii_is_valid("alsdjf\t\vbarr\nba z"));
40 assert_se(!ascii_is_valid("\342\204\242"));
41 assert_se(!ascii_is_valid("\341\204"));
42}
43
e7363c59
DR
44static void test_utf8_encoded_valid_unichar(void) {
45 assert_se(utf8_encoded_valid_unichar("\342\204\242") == 3);
46 assert_se(utf8_encoded_valid_unichar("\302\256") == 2);
47 assert_se(utf8_encoded_valid_unichar("a") == 1);
48 assert_se(utf8_encoded_valid_unichar("\341\204") < 0);
49 assert_se(utf8_encoded_valid_unichar("\341\204\341\204") < 0);
50
51}
52
550a40ec
ZJS
53static 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
02a36bc9
DR
69int main(int argc, char *argv[]) {
70 test_utf8_is_valid();
7991ac34 71 test_utf8_is_printable();
e7363c59 72 test_ascii_is_valid();
e7363c59 73 test_utf8_encoded_valid_unichar();
550a40ec 74 test_utf8_escaping();
7991ac34
DR
75
76 return 0;
02a36bc9 77}