1 /* Tests of removing leading and/or trailing whitespaces.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
18
19 #include <config.h>
20
21 /* Specification. */
22 #include "trim.h"
23
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "macros.h"
30
31 static void
32 test_ascii (void)
33 {
34 {
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);
44 }
45
46 {
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);
56 }
57
58 {
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);
68 }
69
70 {
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);
80 }
81
82 {
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);
92 }
93
94 {
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);
104 }
105 }
106
107 int
108 main (int argc, char *argv[])
109 {
110 /* configure should already have checked that the locale is supported. */
111 if (setlocale (LC_ALL, "") == NULL)
112 return 1;
113
114 /* Test ASCII arguments. */
115 test_ascii ();
116
117 if (argc > 1)
118 switch (argv[1][0])
119 {
120 case '1':
121 /* C or POSIX locale. */
122 return 0;
123
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);
130 }
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);
135 }
136 return 0;
137
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);
145 }
146 #endif
147 #if !(defined __FreeBSD__ || defined __DragonFly__)
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);
152 }
153 #endif
154 return 0;
155 }
156
157 return 1;
158 }