Ticket #1395 (Copying to fish is broken)
[midnight-commander.git] / src / ecs-test.c
blobb3e6b45ac0267abcdc29e538166e741d88d2b3d7
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 /** \file ecs-test.c
26 * \brief Source: testsuite for basic support for extended character sets
29 #include <config.h>
31 #undef NDEBUG
32 #include <assert.h>
33 #include <locale.h>
34 #include <stdio.h>
36 #include "global.h"
37 #include "ecs.h"
39 #ifdef EXTCHARSET_ENABLED
40 static gboolean
41 change_locale(const char *loc)
43 const char *ident;
45 ident = setlocale(LC_CTYPE, loc);
46 if (!ident) {
47 (void) printf("Skipping %s locale\n", loc);
48 return FALSE;
49 } else {
50 (void) printf("Testing %s locale \"%s\"\n", loc, ident);
51 return TRUE;
55 static void
56 test_locale_C(void)
58 if (!change_locale("C")) return;
60 assert(ecs_strlen(ECS_STR("foo")) == 3);
61 assert(ecs_strlen(ECS_STR("Zuckert\374te")) == 10);
64 static void
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";
70 ecs_char *ecs;
71 char *mbs;
72 gboolean valid;
74 if (!change_locale("en_US.UTF-8")) return;
76 valid = ecs_mbstr_to_str(&ecs, teststr_c);
77 assert(!valid);
79 valid = ecs_mbstr_to_str(&ecs, teststr_mb);
80 assert(valid);
81 assert(ecs_strlen(ecs) == 10);
82 g_free(ecs);
84 valid = ecs_str_to_mbstr(&mbs, teststr_ecs);
85 assert(valid);
86 assert(strlen(mbs) == 11);
87 g_free(mbs);
89 #endif
91 /* ecs_strcpy */
92 /* ecs_strncpy */
93 /* ecs_strcat */
94 /* ecs_strncat */
96 static void
97 test_ecs_strcmp(void)
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);
109 /* ecs_strcoll */
110 /* ecs_strncmp */
111 /* ecs_strxfrm */
113 static void
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);
125 static void
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);
139 /* ecs_strpbrk */
141 static void
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 *); */
155 /* ecs_strtok */
157 static void
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 *); */
168 static void
169 test_ecs_strlcpy(void)
171 ecs_char dest[20];
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);
184 static void
185 test_ecs_strlcat(void)
187 ecs_char dest[20];
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); */
207 int main(void)
209 #ifdef EXTCHARSET_ENABLED
210 test_locale_C();
211 test_locale_en_US_UTF_8();
212 #endif
213 test_ecs_strcmp();
214 test_ecs_strchr();
215 test_ecs_strrchr();
216 test_ecs_strlen();
217 test_ecs_strlcpy();
218 test_ecs_strlcat();
219 test_ecs_strcspn();
220 (void) puts("All tests passed.");
221 return 0;