2 Testsuite for basic support for extended character sets.
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,
35 #ifdef EXTCHARSET_ENABLED
37 change_locale(const char *loc
)
41 ident
= setlocale(LC_CTYPE
, loc
);
43 (void) printf("Skipping %s locale\n", loc
);
46 (void) printf("Testing %s locale \"%s\"\n", loc
, ident
);
54 if (!change_locale("C")) return;
56 assert(ecs_strlen(ECS_STR("foo")) == 3);
57 assert(ecs_strlen(ECS_STR("Zuckert\374te")) == 10);
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";
70 if (!change_locale("en_US.UTF-8")) return;
72 valid
= ecs_mbstr_to_str(&ecs
, teststr_c
);
75 valid
= ecs_mbstr_to_str(&ecs
, teststr_mb
);
77 assert(ecs_strlen(ecs
) == 10);
80 valid
= ecs_str_to_mbstr(&mbs
, teststr_ecs
);
82 assert(strlen(mbs
) == 11);
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);
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
);
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);
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 *); */
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 *); */
165 test_ecs_strlcpy(void)
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);
181 test_ecs_strlcat(void)
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); */
205 #ifdef EXTCHARSET_ENABLED
207 test_locale_en_US_UTF_8();
216 (void) puts("All tests passed.");