usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-trim.c
blobe765f23109dbe6a362ee00c11261a864c8babc88
1 /* Tests of removing leading and/or trailing whitespaces.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
19 #include <config.h>
21 /* Specification. */
22 #include "trim.h"
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "macros.h"
31 static void
32 test_ascii (void)
35 char *result = trim ("");
36 ASSERT (strcmp (result, "") == 0);
37 free (result);
38 result = trim_leading ("");
39 ASSERT (strcmp (result, "") == 0);
40 free (result);
41 result = trim_trailing ("");
42 ASSERT (strcmp (result, "") == 0);
43 free (result);
47 char *result = trim (" ");
48 ASSERT (strcmp (result, "") == 0);
49 free (result);
50 result = trim_leading (" ");
51 ASSERT (strcmp (result, "") == 0);
52 free (result);
53 result = trim_trailing (" ");
54 ASSERT (strcmp (result, "") == 0);
55 free (result);
59 char *result = trim ("Hello world");
60 ASSERT (strcmp (result, "Hello world") == 0);
61 free (result);
62 result = trim_leading ("Hello world");
63 ASSERT (strcmp (result, "Hello world") == 0);
64 free (result);
65 result = trim_trailing ("Hello world");
66 ASSERT (strcmp (result, "Hello world") == 0);
67 free (result);
71 char *result = trim (" Hello world");
72 ASSERT (strcmp (result, "Hello world") == 0);
73 free (result);
74 result = trim_leading (" Hello world");
75 ASSERT (strcmp (result, "Hello world") == 0);
76 free (result);
77 result = trim_trailing (" Hello world");
78 ASSERT (strcmp (result, " Hello world") == 0);
79 free (result);
83 char *result = trim ("Hello world ");
84 ASSERT (strcmp (result, "Hello world") == 0);
85 free (result);
86 result = trim_leading ("Hello world ");
87 ASSERT (strcmp (result, "Hello world ") == 0);
88 free (result);
89 result = trim_trailing ("Hello world ");
90 ASSERT (strcmp (result, "Hello world") == 0);
91 free (result);
95 char *result = trim (" Hello world ");
96 ASSERT (strcmp (result, "Hello world") == 0);
97 free (result);
98 result = trim_leading (" Hello world ");
99 ASSERT (strcmp (result, "Hello world ") == 0);
100 free (result);
101 result = trim_trailing (" Hello world ");
102 ASSERT (strcmp (result, " Hello world") == 0);
103 free (result);
108 main (int argc, char *argv[])
110 /* configure should already have checked that the locale is supported. */
111 if (setlocale (LC_ALL, "") == NULL)
112 return 1;
114 /* Test ASCII arguments. */
115 test_ascii ();
117 if (argc > 1)
118 switch (argv[1][0])
120 case '1':
121 /* C or POSIX locale. */
122 return test_exit_status;
124 case '2':
125 /* Locale encoding is UTF-8. */
126 { /* U+2002 EN SPACE */
127 char *result = trim ("\342\200\202\302\267foo\342\200\202");
128 ASSERT (strcmp (result, "\302\267foo") == 0);
129 free (result);
131 { /* U+3000 IDEOGRAPHIC SPACE */
132 char *result = trim ("\343\200\200\302\267foo\343\200\200");
133 ASSERT (strcmp (result, "\302\267foo") == 0);
134 free (result);
136 return test_exit_status;
138 case '3':
139 /* Locale encoding is GB18030. */
140 #if !(defined __FreeBSD__ || defined __DragonFly__ || defined __sun)
141 { /* U+2002 EN SPACE */
142 char *result = trim ("\201\066\243\070\241\244foo\201\066\243\070");
143 ASSERT (strcmp (result, "\241\244foo") == 0);
144 free (result);
146 #endif
147 #if !(defined __FreeBSD__ || defined __DragonFly__ || defined __illumos__)
148 { /* U+3000 IDEOGRAPHIC SPACE */
149 char *result = trim ("\241\241\241\244foo\241\241");
150 ASSERT (strcmp (result, "\241\244foo") == 0);
151 free (result);
153 #endif
154 return test_exit_status;
157 return 1;