chiark / gitweb /
utf8: fix utf8_is_printable
[elogind.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
25 static 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));
29         assert_se(utf8_is_printable("ąę", 4));
30 }
31
32 static void test_utf8_is_valid(void) {
33         assert_se(utf8_is_valid("ascii is valid unicode"));
34         assert_se(utf8_is_valid("\342\204\242"));
35         assert_se(!utf8_is_valid("\341\204"));
36 }
37
38 static 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
44 static void test_ascii_filter(void) {
45         char *f;
46
47         f = ascii_filter("alsdjf\t\vbarr\nba z");
48         assert_se(streq(f, "alsdjf\t\vbarr\nba z"));
49         free(f);
50
51         f = ascii_filter("\342\204\242");
52         assert_se(streq(f, ""));
53         free(f);
54
55         f = ascii_filter("foo\341\204bar");
56         assert_se(streq(f, "foobar"));
57         free(f);
58 }
59
60 static void test_utf8_encoded_valid_unichar(void) {
61         assert_se(utf8_encoded_valid_unichar("\342\204\242") == 3);
62         assert_se(utf8_encoded_valid_unichar("\302\256") == 2);
63         assert_se(utf8_encoded_valid_unichar("a") == 1);
64         assert_se(utf8_encoded_valid_unichar("\341\204") < 0);
65         assert_se(utf8_encoded_valid_unichar("\341\204\341\204") < 0);
66
67 }
68
69 int main(int argc, char *argv[]) {
70         test_utf8_is_valid();
71         test_utf8_is_printable();
72         test_ascii_is_valid();
73         test_ascii_filter();
74         test_utf8_encoded_valid_unichar();
75
76         return 0;
77 }