1 /* Test of quotearg family of functions.
2 Copyright (C) 2008-2009 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, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Eric Blake <ebb9@byu.net>, 2008. */
35 #define ASSERT(expr) \
40 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 struct result_strings {
48 char const *str1; /* Translation of "". */
49 char const *str2; /* Translation of "\0""1\0". */
50 size_t len2; /* Length of str2. */
51 char const *str3; /* Translation of "simple". */
52 char const *str4; /* Translation of " \t\n'\"\033?""?/\\". */
53 char const *str5; /* Translation of "a:b". */
54 char const *str6; /* Translation of "a\\b". */
57 struct result_groups {
58 struct result_strings group1; /* Via quotearg_buffer. */
59 struct result_strings group2; /* Via quotearg{,_mem}. */
60 struct result_strings group3; /* Via quotearg_colon{,_mem}. */
63 static struct result_strings inputs = {
64 "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b"
67 static struct result_groups results_g[] = {
68 /* literal_quoting_style */
69 { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
70 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
71 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
73 /* shell_quoting_style */
74 { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
76 { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
78 { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
81 /* shell_always_quoting_style */
82 { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
84 { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
86 { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
90 { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
91 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
92 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
93 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
94 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
95 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } },
97 /* c_maybe_quoting_style */
98 { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
100 { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
102 { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
103 "\"a:b\"", "a\\b" } },
105 /* escape_quoting_style */
106 { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
108 { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
110 { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
113 /* locale_quoting_style */
114 { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
115 "`a:b'", "`a\\\\b'" },
116 { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
117 "`a:b'", "`a\\\\b'" },
118 { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
119 "`a\\:b'", "`a\\\\b'" } },
121 /* clocale_quoting_style */
122 { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
123 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
124 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
125 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
126 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
127 "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
130 static struct result_groups flag_results[] = {
131 /* literal_quoting_style and QA_ELIDE_NULL_BYTES */
132 { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
133 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
134 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
136 /* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
137 { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
139 { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
141 { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
142 "\"a:b\"", "a\\b" } },
144 /* c_quoting_style and QA_SPLIT_TRIGRAPHS */
145 { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
146 "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
147 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
148 "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
149 { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
150 "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
155 /* These quotes are borrowed from a pt_PT.utf8 translation. */
156 # define LQ "\302\253"
157 # define RQ "\302\273"
159 static struct result_groups locale_results[] = {
160 /* locale_quoting_style */
161 { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
162 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
163 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
164 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
165 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
166 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } },
168 /* clocale_quoting_style */
169 { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
170 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
171 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
172 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
173 { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
174 LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } }
177 #endif /* ENABLE_NLS */
180 compare (char const *a, size_t la, char const *b, size_t lb)
183 ASSERT (memcmp (a, b, la) == 0);
184 ASSERT (b[lb] == '\0');
188 compare_strings (char *(func) (char const *, size_t *),
189 struct result_strings *results)
195 p = func (inputs.str1, &len);
196 compare (results->str1, strlen (results->str1), p, len);
199 p = func (inputs.str2, &len);
200 compare (results->str2, results->len2, p, len);
203 p = func (inputs.str3, &len);
204 compare (results->str3, strlen (results->str3), p, len);
206 len = strlen (inputs.str4);
207 p = func (inputs.str4, &len);
208 compare (results->str4, strlen (results->str4), p, len);
211 p = func (inputs.str5, &len);
212 compare (results->str5, strlen (results->str5), p, len);
214 len = strlen (inputs.str6);
215 p = func (inputs.str6, &len);
216 compare (results->str6, strlen (results->str6), p, len);
220 use_quotearg_buffer (const char *str, size_t *len)
222 static char buf[100];
224 memset (buf, 0xa5, 100);
225 size = quotearg_buffer (buf, 100, str, *len, NULL);
227 ASSERT ((unsigned char) buf[size + 1] == 0xa5);
232 use_quotearg (const char *str, size_t *len)
234 char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
240 use_quotearg_colon (const char *str, size_t *len)
242 char *p = (*len == SIZE_MAX ? quotearg_colon (str)
243 : quotearg_colon_mem (str, *len));
249 main (int argc, char *argv[])
253 set_program_name (argv[0]);
255 /* This program part is hard-wired to the C locale since it does not call
257 ASSERT (!isprint ('\033'));
258 for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
260 set_quoting_style (NULL, i);
261 compare_strings (use_quotearg_buffer, &results_g[i].group1);
262 compare_strings (use_quotearg, &results_g[i].group2);
263 compare_strings (use_quotearg_colon, &results_g[i].group3);
266 set_quoting_style (NULL, literal_quoting_style);
267 ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
268 compare_strings (use_quotearg_buffer, &flag_results[0].group1);
269 compare_strings (use_quotearg, &flag_results[0].group2);
270 compare_strings (use_quotearg_colon, &flag_results[0].group3);
272 set_quoting_style (NULL, c_quoting_style);
273 ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
274 == QA_ELIDE_NULL_BYTES);
275 compare_strings (use_quotearg_buffer, &flag_results[1].group1);
276 compare_strings (use_quotearg, &flag_results[1].group2);
277 compare_strings (use_quotearg_colon, &flag_results[1].group3);
279 ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
280 == QA_ELIDE_OUTER_QUOTES);
281 compare_strings (use_quotearg_buffer, &flag_results[2].group1);
282 compare_strings (use_quotearg, &flag_results[2].group2);
283 compare_strings (use_quotearg_colon, &flag_results[2].group3);
285 ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
288 /* Clean up environment. */
289 unsetenv ("LANGUAGE");
291 unsetenv ("LC_MESSAGES");
292 unsetenv ("LC_CTYPE");
294 unsetenv ("OUTPUT_CHARSET");
296 /* This program part runs in a French UTF-8 locale. It uses
297 the test-quotearg.mo message catalog. */
299 const char *locale_name = getenv ("LOCALE");
301 if (locale_name != NULL && strcmp (locale_name, "none") != 0
302 && setenv ("LC_ALL", locale_name, 1) == 0
303 && setlocale (LC_ALL, "") != NULL)
305 textdomain ("test-quotearg");
306 bindtextdomain ("test-quotearg", getenv ("LOCALEDIR"));
308 set_quoting_style (NULL, locale_quoting_style);
309 compare_strings (use_quotearg_buffer, &locale_results[0].group1);
310 compare_strings (use_quotearg, &locale_results[0].group2);
311 compare_strings (use_quotearg_colon, &locale_results[0].group3);
313 set_quoting_style (NULL, clocale_quoting_style);
314 compare_strings (use_quotearg_buffer, &locale_results[1].group1);
315 compare_strings (use_quotearg, &locale_results[1].group2);
316 compare_strings (use_quotearg_colon, &locale_results[1].group3);
319 #endif /* ENABLE_NLS */