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,
26 * \brief Source: testsuite for basic support for extended character sets
36 #include "lib/global.h"
39 #ifdef EXTCHARSET_ENABLED
41 change_locale(const char *loc
)
45 ident
= setlocale(LC_CTYPE
, loc
);
47 (void) printf("Skipping %s locale\n", loc
);
50 (void) printf("Testing %s locale \"%s\"\n", loc
, ident
);
58 if (!change_locale("C")) return;
60 assert(ecs_strlen(ECS_STR("foo")) == 3);
61 assert(ecs_strlen(ECS_STR("Zuckert\374te")) == 10);
65 test_locale_en_US_UTF_8(void)
67 const char *teststr_mb
= "Zuckert\303\214te";
68 const ecs_char
*teststr_ecs
= ECS_STR("Zuckert\374te");
69 const char *teststr_c
= "Zuckert\374te";
74 if (!change_locale("en_US.UTF-8")) return;
76 valid
= ecs_mbstr_to_str(&ecs
, teststr_c
);
79 valid
= ecs_mbstr_to_str(&ecs
, teststr_mb
);
81 assert(ecs_strlen(ecs
) == 10);
84 valid
= ecs_str_to_mbstr(&mbs
, teststr_ecs
);
86 assert(strlen(mbs
) == 11);
99 /* This test assumes ASCII encoding */
101 (void) puts("Testing ecs_strcmp ...");
102 assert(ecs_strcmp(ECS_STR("foo"), ECS_STR("bar")) > 0);
103 assert(ecs_strcmp(ECS_STR("bar"), ECS_STR("foo")) < 0);
104 assert(ecs_strcmp(ECS_STR(""), ECS_STR("")) == 0);
105 assert(ecs_strcmp(ECS_STR("f"), ECS_STR("")) > 0);
106 assert(ecs_strcmp(ECS_STR(""), ECS_STR("f")) < 0);
114 test_ecs_strchr(void)
116 const ecs_char foo
[] = ECS_STR("foo");
118 (void) puts("Testing ecs_strchr ...");
119 assert(ecs_strchr(foo
, ECS_CHAR('f')) == foo
);
120 assert(ecs_strchr(foo
, ECS_CHAR('o')) == foo
+ 1);
121 assert(ecs_strchr(foo
, ECS_CHAR('\0')) == foo
+ 3);
122 assert(ecs_strchr(foo
, ECS_CHAR('b')) == NULL
);
126 test_ecs_strcspn(void)
128 const ecs_char test
[] = ECS_STR("test string0123");
130 (void) puts("Testing ecs_strcspn ...");
131 assert(ecs_strcspn(test
, ECS_STR("t")) == 0);
132 assert(ecs_strcspn(test
, ECS_STR("e")) == 1);
133 assert(ecs_strcspn(test
, ECS_STR("te")) == 0);
134 assert(ecs_strcspn(test
, ECS_STR("et")) == 0);
135 assert(ecs_strcspn(test
, ECS_STR("")) == 15);
136 assert(ecs_strcspn(test
, ECS_STR("XXX")) == 15);
142 test_ecs_strrchr(void)
144 const ecs_char foo
[] = ECS_STR("foo");
146 (void) puts("Testing ecs_strrchr ...");
147 assert(ecs_strrchr(foo
, ECS_CHAR('f')) == foo
);
148 assert(ecs_strrchr(foo
, ECS_CHAR('o')) == foo
+ 2);
149 assert(ecs_strrchr(foo
, ECS_CHAR('\0')) == foo
+ 3);
150 assert(ecs_strrchr(foo
, ECS_CHAR('b')) == NULL
);
153 /* extern ecs_char *ecs_strstr(const ecs_char *, const ecs_char *); */
158 test_ecs_strlen(void)
160 (void) puts("Testing ecs_strlen ...");
161 assert(ecs_strlen(ECS_STR("")) == 0);
162 assert(ecs_strlen(ECS_STR("foo")) == 3);
163 assert(ecs_strlen(ECS_STR("\1\2\3\4\5")) == 5);
166 /* extern ecs_char *ecs_xstrdup(const ecs_char *); */
169 test_ecs_strlcpy(void)
173 (void) puts("Testing ecs_strlcpy ...");
174 assert(ecs_strlcpy(dest
, ECS_STR(""), sizeof(dest
)) == 0);
175 assert(dest
[0] == ECS_CHAR('\0'));
176 assert(ecs_strlcpy(dest
, ECS_STR("onetwothree"), sizeof(dest
)) == 11);
177 assert(dest
[11] == ECS_CHAR('\0'));
178 assert(ecs_strcmp(dest
, ECS_STR("onetwothree")) == 0);
179 assert(ecs_strlcpy(dest
, ECS_STR("onetwothree"), 5) == 11);
180 assert(dest
[4] == ECS_CHAR('\0'));
181 assert(ecs_strcmp(dest
, ECS_STR("onet")) == 0);
185 test_ecs_strlcat(void)
189 (void) puts("Testing ecs_strlcat ...");
190 dest
[0] = ECS_CHAR('\0');
191 assert(ecs_strlcat(dest
, ECS_STR("foo"), 0) == 3);
192 assert(dest
[0] == ECS_CHAR('\0'));
193 assert(ecs_strlcat(dest
, ECS_STR("foo"), 1) == 3);
194 assert(dest
[0] == ECS_CHAR('\0'));
195 assert(ecs_strlcat(dest
, ECS_STR("foo"), 2) == 3);
196 assert(dest
[0] == ECS_CHAR('f'));
197 assert(dest
[1] == ECS_CHAR('\0'));
198 dest
[1] = ECS_CHAR('X');
199 assert(ecs_strlcat(dest
, ECS_STR("bar"), 1) == 4);
200 assert(dest
[0] == ECS_CHAR('f'));
201 assert(dest
[1] == ECS_CHAR('X'));
204 /* extern void ecs_strbox(const ecs_char *, size_t *ret_width,
205 size_t *ret_height); */
209 #ifdef EXTCHARSET_ENABLED
211 test_locale_en_US_UTF_8();
220 (void) puts("All tests passed.");