]> git.sesse.net Git - vlc/blob - src/test/utf8.c
Simpler UTF-8 check functions + rudimentary unit test
[vlc] / src / test / utf8.c
1 /*****************************************************************************
2  * utf8.c: Test for UTF-8 encoding/decoding stuff
3  *****************************************************************************
4  * Copyright (C) 2006 Rémi Denis-Courmont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #include <vlc/vlc.h>
23 #include "vlc_charset.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28
29 static void test (const char *in, const char *out)
30 {
31     bool isutf8 = !strcmp (in, out);
32     char *str = strdup (in);
33     if (str == NULL)
34         abort ();
35
36     if (isutf8)
37         printf ("\"%s\" should be accepted...\n", in);
38     else
39         printf ("\"%s\" should be rewritten as \"%s\"...\n", in, out);
40
41     if ((IsUTF8 (in) != NULL) != isutf8)
42     {
43         printf (" ERROR: IsUTF8 (%s) failed\n", in);
44         exit (1);
45     }
46
47     if ((EnsureUTF8 (str) != NULL) != isutf8)
48     {
49         printf (" ERROR: EnsureUTF8 (%s) failed\n", in);
50         exit (2);
51     }
52
53     if (strcmp (str, out))
54     {
55         printf (" ERROR: got \"%s\"\n", str);
56         exit (3);
57     }
58
59     if ((EnsureUTF8 (str) == NULL) || IsUTF8 (str) == NULL)
60     {
61         printf (" ERROR: EnsureUTF8 (%s) is not UTF-8\n", in);
62         exit (4);
63     }
64     free (str);
65 }
66
67 int main (void)
68 {
69     (void)setvbuf (stdout, NULL, _IONBF, 0);
70     test ("", "");
71
72     test ("this_should_not_be_modified_1234",
73           "this_should_not_be_modified_1234");
74
75     test ("\xFF", "?"); // invalid byte
76     test ("\xEF\xBB\xBFHello", "\xEF\xBB\xBFHello"); // BOM
77     test ("\x00\xE9", ""); // no conversion past end of string
78
79     test ("T\xC3\xA9l\xC3\xA9vision \xE2\x82\xAC", "Télévision €");
80     test ("T\xE9l\xE9vision", "T?l?vision");
81     test ("\xC1\x94\xC3\xa9l\xC3\xA9vision", "??élévision"); /* overlong */
82
83     test ("Hel\xF0\x83\x85\x87lo", "Hel????lo"); /* more overlong */
84     return 0;
85 }