1 /*****************************************************************************
2 * utf8.c: Test for UTF-8 encoding/decoding stuff
3 *****************************************************************************
4 * Copyright (C) 2006 Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
27 #include "vlc_charset.h"
33 static void test (const char *in, const char *out)
35 bool isutf8 = !strcmp (in, out);
36 char *str = strdup (in);
41 printf ("\"%s\" should be accepted...\n", in);
43 printf ("\"%s\" should be rewritten as \"%s\"...\n", in, out);
45 if ((IsUTF8 (in) != NULL) != isutf8)
47 printf (" ERROR: IsUTF8 (%s) failed\n", in);
51 if ((EnsureUTF8 (str) != NULL) != isutf8)
53 printf (" ERROR: EnsureUTF8 (%s) failed\n", in);
57 if (strcmp (str, out))
59 printf (" ERROR: got \"%s\"\n", str);
63 if ((EnsureUTF8 (str) == NULL) || IsUTF8 (str) == NULL)
65 printf (" ERROR: EnsureUTF8 (%s) is not UTF-8\n", in);
73 (void)setvbuf (stdout, NULL, _IONBF, 0);
76 test ("this_should_not_be_modified_1234",
77 "this_should_not_be_modified_1234");
79 test ("\xFF", "?"); // invalid byte
80 test ("\xEF\xBB\xBFHello", "\xEF\xBB\xBFHello"); // BOM
81 test ("\x00\xE9", ""); // no conversion past end of string
83 test ("T\xC3\xA9l\xC3\xA9vision \xE2\x82\xAC", "Télévision €");
84 test ("T\xE9l\xE9vision", "T?l?vision");
85 test ("\xC1\x94\xC3\xa9l\xC3\xA9vision", "??élévision"); /* overlong */
87 test ("Hel\xF0\x83\x85\x87lo", "Hel????lo"); /* more overlong */