1 /* Test of string descriptors.
2 Copyright (C) 2023-2025 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. */
21 #include "string-desc-quotearg.h"
31 string_desc_t s1
= sd_from_c ("Hello world!");
32 string_desc_t s2
= sd_new_addr (21, "The\0quick\0brown\0\0fox");
34 /* Test sd_quotearg_buffer. */
37 size_t n
= sd_quotearg_buffer (buf
, sizeof (buf
), s2
, NULL
);
39 ASSERT (memcmp (buf
, "The\0quick\0brown\0\0fox", n
) == 0);
42 /* Test sd_quotearg_alloc. */
45 char *ret
= sd_quotearg_alloc (s2
, &n
, NULL
);
47 ASSERT (memcmp (ret
, "The\0quick\0brown\0\0fox", n
) == 0);
51 /* Test sd_quotearg_n. */
53 char *ret
= sd_quotearg_n (1, s2
);
54 ASSERT (memcmp (ret
, "Thequickbrownfox", 16 + 1) == 0);
57 /* Test sd_quotearg. */
59 char *ret
= sd_quotearg (s2
);
60 ASSERT (memcmp (ret
, "Thequickbrownfox", 16 + 1) == 0);
63 /* Test sd_quotearg_n_style. */
65 char *ret
= sd_quotearg_n_style (1, clocale_quoting_style
, s2
);
66 ASSERT (memcmp (ret
, "\"The\\0quick\\0brown\\0\\0fox\\0\"", 28 + 1) == 0
67 || /* if the locale has UTF-8 encoding */
68 memcmp (ret
, "\342\200\230The\\0quick\\0brown\\0\\0fox\\0\342\200\231", 32 + 1) == 0);
71 /* Test sd_quotearg_style. */
73 char *ret
= sd_quotearg_style (clocale_quoting_style
, s2
);
74 ASSERT (memcmp (ret
, "\"The\\0quick\\0brown\\0\\0fox\\0\"", 28 + 1) == 0
75 || /* if the locale has UTF-8 encoding */
76 memcmp (ret
, "\342\200\230The\\0quick\\0brown\\0\\0fox\\0\342\200\231", 32 + 1) == 0);
79 /* Test sd_quotearg_char. */
81 char *ret
= sd_quotearg_char (s1
, ' ');
82 ASSERT (memcmp (ret
, "Hello world!", 12 + 1) == 0); /* ' ' not quoted?! */
85 /* Test sd_quotearg_colon. */
87 char *ret
= sd_quotearg_colon (sd_from_c ("a:b"));
88 ASSERT (memcmp (ret
, "a:b", 3 + 1) == 0); /* ':' not quoted?! */
91 /* Test sd_quotearg_n_custom. */
93 char *ret
= sd_quotearg_n_custom (2, "<", ">", s1
);
94 ASSERT (memcmp (ret
, "<Hello world!>", 14 + 1) == 0);
97 /* Test sd_quotearg_n_custom. */
99 char *ret
= sd_quotearg_custom ("[[", "]]", s1
);
100 ASSERT (memcmp (ret
, "[[Hello world!]]", 16 + 1) == 0);
103 return test_exit_status
;