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. */
35 char *result
= trim ("");
36 ASSERT (strcmp (result
, "") == 0);
38 result
= trim_leading ("");
39 ASSERT (strcmp (result
, "") == 0);
41 result
= trim_trailing ("");
42 ASSERT (strcmp (result
, "") == 0);
47 char *result
= trim (" ");
48 ASSERT (strcmp (result
, "") == 0);
50 result
= trim_leading (" ");
51 ASSERT (strcmp (result
, "") == 0);
53 result
= trim_trailing (" ");
54 ASSERT (strcmp (result
, "") == 0);
59 char *result
= trim ("Hello world");
60 ASSERT (strcmp (result
, "Hello world") == 0);
62 result
= trim_leading ("Hello world");
63 ASSERT (strcmp (result
, "Hello world") == 0);
65 result
= trim_trailing ("Hello world");
66 ASSERT (strcmp (result
, "Hello world") == 0);
71 char *result
= trim (" Hello world");
72 ASSERT (strcmp (result
, "Hello world") == 0);
74 result
= trim_leading (" Hello world");
75 ASSERT (strcmp (result
, "Hello world") == 0);
77 result
= trim_trailing (" Hello world");
78 ASSERT (strcmp (result
, " Hello world") == 0);
83 char *result
= trim ("Hello world ");
84 ASSERT (strcmp (result
, "Hello world") == 0);
86 result
= trim_leading ("Hello world ");
87 ASSERT (strcmp (result
, "Hello world ") == 0);
89 result
= trim_trailing ("Hello world ");
90 ASSERT (strcmp (result
, "Hello world") == 0);
95 char *result
= trim (" Hello world ");
96 ASSERT (strcmp (result
, "Hello world") == 0);
98 result
= trim_leading (" Hello world ");
99 ASSERT (strcmp (result
, "Hello world ") == 0);
101 result
= trim_trailing (" Hello world ");
102 ASSERT (strcmp (result
, " Hello world") == 0);
108 main (int argc
, char *argv
[])
110 /* configure should already have checked that the locale is supported. */
111 if (setlocale (LC_ALL
, "") == NULL
)
114 /* Test ASCII arguments. */
121 /* C or POSIX locale. */
122 return test_exit_status
;
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);
131 { /* U+3000 IDEOGRAPHIC SPACE */
132 char *result
= trim ("\343\200\200\302\267foo\343\200\200");
133 ASSERT (strcmp (result
, "\302\267foo") == 0);
136 return test_exit_status
;
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);
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);
154 return test_exit_status
;