]> git.sesse.net Git - vlc/blob - src/test/utf8.c
mux: avi: don't try to delete failed stream
[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 it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_charset.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdbool.h>
32
33 static void test (const char *in, const char *out)
34 {
35     bool isutf8 = !strcmp (in, out);
36     char *str = strdup (in);
37     if (str == NULL)
38         abort ();
39
40     if (isutf8)
41         printf ("\"%s\" should be accepted...\n", in);
42     else
43         printf ("\"%s\" should be rewritten as \"%s\"...\n", in, out);
44
45     if ((IsUTF8 (in) != NULL) != isutf8)
46     {
47         printf (" ERROR: IsUTF8 (%s) failed\n", in);
48         exit (1);
49     }
50
51     if ((EnsureUTF8 (str) != NULL) != isutf8)
52     {
53         printf (" ERROR: EnsureUTF8 (%s) failed\n", in);
54         exit (2);
55     }
56
57     if (strcmp (str, out))
58     {
59         printf (" ERROR: got \"%s\"\n", str);
60         exit (3);
61     }
62
63     if ((EnsureUTF8 (str) == NULL) || IsUTF8 (str) == NULL)
64     {
65         printf (" ERROR: EnsureUTF8 (%s) is not UTF-8\n", in);
66         exit (4);
67     }
68     free (str);
69 }
70
71 static void test_strcasestr (const char *h, const char *n, ssize_t offset)
72 {
73     printf ("\"%s\" should %sbe found in \"%s\"...\n", n,
74             (offset != -1) ? "" : "not ", h);
75
76     const char *ret = vlc_strcasestr (h, n);
77     if (offset == -1)
78     {
79         if (ret != NULL)
80         {
81             printf ("ERROR: got \"%s\"\n", ret);
82             exit (10);
83         }
84     }
85     else
86     {
87         if (ret == NULL)
88         {
89             printf ("ERROR: not found\n");
90             exit (11);
91         }
92         if ((ret - h) != offset)
93         {
94             printf ("ERROR: got \"%s\" instead of \"%s\"\n",
95                     ret, h + offset);
96             exit (12);
97         }
98     }
99 }
100
101
102 int main (void)
103 {
104     (void)setvbuf (stdout, NULL, _IONBF, 0);
105     test ("", "");
106
107     test ("this_should_not_be_modified_1234",
108           "this_should_not_be_modified_1234");
109
110     test ("\xFF", "?"); // invalid byte
111     test ("\xEF\xBB\xBFHello", "\xEF\xBB\xBFHello"); // BOM
112     test ("\x00\xE9", ""); // no conversion past end of string
113
114     test ("T\xC3\xA9l\xC3\xA9vision \xE2\x82\xAC", "Télévision €");
115     test ("T\xE9l\xE9vision", "T?l?vision");
116     test ("\xC1\x94\xC3\xa9l\xC3\xA9vision", "??élévision"); /* overlong */
117
118     test ("Hel\xF0\x83\x85\x87lo", "Hel????lo"); /* more overlong */
119
120     test_strcasestr ("", "", 0);
121     test_strcasestr ("", "a", -1);
122     test_strcasestr ("a", "", 0);
123     test_strcasestr ("heLLo", "l", 2);
124     test_strcasestr ("heLLo", "lo", 3);
125     test_strcasestr ("heLLo", "llo", 2);
126     test_strcasestr ("heLLo", "la", -1);
127     test_strcasestr ("heLLo", "oa", -1);
128     test_strcasestr ("Télé", "é", 1);
129     test_strcasestr ("Télé", "élé", 1);
130     test_strcasestr ("Télé", "léé", -1);
131
132     return 0;
133 }