Merge branch '207_minor_regex_fixes' into mc-4.6
[midnight-commander.git] / src / ecs-test.c
bloba194280f1cb2d47d71510559a85c5a1aee33d0d1
1 /*
2 Testsuite for basic support for extended character sets.
4 Written by:
5 Roland Illig <roland.illig@gmx.de>, 2005.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 MA 02110-1301, USA.
25 #include <config.h>
27 #undef NDEBUG
28 #include <assert.h>
29 #include <locale.h>
30 #include <stdio.h>
32 #include "global.h"
33 #include "ecs.h"
35 #ifdef EXTCHARSET_ENABLED
36 static gboolean
37 change_locale(const char *loc)
39 const char *ident;
41 ident = setlocale(LC_CTYPE, loc);
42 if (!ident) {
43 (void) printf("Skipping %s locale\n", loc);
44 return FALSE;
45 } else {
46 (void) printf("Testing %s locale \"%s\"\n", loc, ident);
47 return TRUE;
51 static void
52 test_locale_C(void)
54 if (!change_locale("C")) return;
56 assert(ecs_strlen(ECS_STR("foo")) == 3);
57 assert(ecs_strlen(ECS_STR("Zuckert\374te")) == 10);
60 static void
61 test_locale_en_US_UTF_8(void)
63 const char *teststr_mb = "Zuckert\303\214te";
64 const ecs_char *teststr_ecs = ECS_STR("Zuckert\374te");
65 const char *teststr_c = "Zuckert\374te";
66 ecs_char *ecs;
67 char *mbs;
68 gboolean valid;
70 if (!change_locale("en_US.UTF-8")) return;
72 valid = ecs_mbstr_to_str(&ecs, teststr_c);
73 assert(!valid);
75 valid = ecs_mbstr_to_str(&ecs, teststr_mb);
76 assert(valid);
77 assert(ecs_strlen(ecs) == 10);
78 g_free(ecs);
80 valid = ecs_str_to_mbstr(&mbs, teststr_ecs);
81 assert(valid);
82 assert(strlen(mbs) == 11);
83 g_free(mbs);
85 #endif
87 /* ecs_strcpy */
88 /* ecs_strncpy */
89 /* ecs_strcat */
90 /* ecs_strncat */
92 static void
93 test_ecs_strcmp(void)
95 /* This test assumes ASCII encoding */
97 (void) puts("Testing ecs_strcmp ...");
98 assert(ecs_strcmp(ECS_STR("foo"), ECS_STR("bar")) > 0);
99 assert(ecs_strcmp(ECS_STR("bar"), ECS_STR("foo")) < 0);
100 assert(ecs_strcmp(ECS_STR(""), ECS_STR("")) == 0);
101 assert(ecs_strcmp(ECS_STR("f"), ECS_STR("")) > 0);
102 assert(ecs_strcmp(ECS_STR(""), ECS_STR("f")) < 0);
105 /* ecs_strcoll */
106 /* ecs_strncmp */
107 /* ecs_strxfrm */
109 static void
110 test_ecs_strchr(void)
112 const ecs_char foo[] = ECS_STR("foo");
114 (void) puts("Testing ecs_strchr ...");
115 assert(ecs_strchr(foo, ECS_CHAR('f')) == foo);
116 assert(ecs_strchr(foo, ECS_CHAR('o')) == foo + 1);
117 assert(ecs_strchr(foo, ECS_CHAR('\0')) == foo + 3);
118 assert(ecs_strchr(foo, ECS_CHAR('b')) == NULL);
121 static void
122 test_ecs_strcspn(void)
124 const ecs_char test[] = ECS_STR("test string0123");
126 (void) puts("Testing ecs_strcspn ...");
127 assert(ecs_strcspn(test, ECS_STR("t")) == 0);
128 assert(ecs_strcspn(test, ECS_STR("e")) == 1);
129 assert(ecs_strcspn(test, ECS_STR("te")) == 0);
130 assert(ecs_strcspn(test, ECS_STR("et")) == 0);
131 assert(ecs_strcspn(test, ECS_STR("")) == 15);
132 assert(ecs_strcspn(test, ECS_STR("XXX")) == 15);
135 /* ecs_strpbrk */
137 static void
138 test_ecs_strrchr(void)
140 const ecs_char foo[] = ECS_STR("foo");
142 (void) puts("Testing ecs_strrchr ...");
143 assert(ecs_strrchr(foo, ECS_CHAR('f')) == foo);
144 assert(ecs_strrchr(foo, ECS_CHAR('o')) == foo + 2);
145 assert(ecs_strrchr(foo, ECS_CHAR('\0')) == foo + 3);
146 assert(ecs_strrchr(foo, ECS_CHAR('b')) == NULL);
149 /* extern ecs_char *ecs_strstr(const ecs_char *, const ecs_char *); */
151 /* ecs_strtok */
153 static void
154 test_ecs_strlen(void)
156 (void) puts("Testing ecs_strlen ...");
157 assert(ecs_strlen(ECS_STR("")) == 0);
158 assert(ecs_strlen(ECS_STR("foo")) == 3);
159 assert(ecs_strlen(ECS_STR("\1\2\3\4\5")) == 5);
162 /* extern ecs_char *ecs_xstrdup(const ecs_char *); */
164 static void
165 test_ecs_strlcpy(void)
167 ecs_char dest[20];
169 (void) puts("Testing ecs_strlcpy ...");
170 assert(ecs_strlcpy(dest, ECS_STR(""), sizeof(dest)) == 0);
171 assert(dest[0] == ECS_CHAR('\0'));
172 assert(ecs_strlcpy(dest, ECS_STR("onetwothree"), sizeof(dest)) == 11);
173 assert(dest[11] == ECS_CHAR('\0'));
174 assert(ecs_strcmp(dest, ECS_STR("onetwothree")) == 0);
175 assert(ecs_strlcpy(dest, ECS_STR("onetwothree"), 5) == 11);
176 assert(dest[4] == ECS_CHAR('\0'));
177 assert(ecs_strcmp(dest, ECS_STR("onet")) == 0);
180 static void
181 test_ecs_strlcat(void)
183 ecs_char dest[20];
185 (void) puts("Testing ecs_strlcat ...");
186 dest[0] = ECS_CHAR('\0');
187 assert(ecs_strlcat(dest, ECS_STR("foo"), 0) == 3);
188 assert(dest[0] == ECS_CHAR('\0'));
189 assert(ecs_strlcat(dest, ECS_STR("foo"), 1) == 3);
190 assert(dest[0] == ECS_CHAR('\0'));
191 assert(ecs_strlcat(dest, ECS_STR("foo"), 2) == 3);
192 assert(dest[0] == ECS_CHAR('f'));
193 assert(dest[1] == ECS_CHAR('\0'));
194 dest[1] = ECS_CHAR('X');
195 assert(ecs_strlcat(dest, ECS_STR("bar"), 1) == 4);
196 assert(dest[0] == ECS_CHAR('f'));
197 assert(dest[1] == ECS_CHAR('X'));
200 /* extern void ecs_strbox(const ecs_char *, size_t *ret_width,
201 size_t *ret_height); */
203 int main(void)
205 #ifdef EXTCHARSET_ENABLED
206 test_locale_C();
207 test_locale_en_US_UTF_8();
208 #endif
209 test_ecs_strcmp();
210 test_ecs_strchr();
211 test_ecs_strrchr();
212 test_ecs_strlen();
213 test_ecs_strlcpy();
214 test_ecs_strlcat();
215 test_ecs_strcspn();
216 (void) puts("All tests passed.");
217 return 0;