usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-string-desc-quotearg.c
blob449533c09668b39c94a46ac787bcbf9e8d4c2384
1 /* Test of string descriptors.
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 #include "string-desc-quotearg.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include "macros.h"
28 int
29 main (void)
31 string_desc_t s1 = string_desc_from_c ("Hello world!");
32 string_desc_t s2 = string_desc_new_addr (21, "The\0quick\0brown\0\0fox");
34 /* Test string_desc_quotearg_buffer. */
36 char buf[80];
37 size_t n = string_desc_quotearg_buffer (buf, sizeof (buf), s2, NULL);
38 ASSERT (n == 21);
39 ASSERT (memcmp (buf, "The\0quick\0brown\0\0fox", n) == 0);
42 /* Test string_desc_quotearg_alloc. */
44 size_t n;
45 char *ret = string_desc_quotearg_alloc (s2, &n, NULL);
46 ASSERT (n == 21);
47 ASSERT (memcmp (ret, "The\0quick\0brown\0\0fox", n) == 0);
48 free (ret);
51 /* Test string_desc_quotearg_n. */
53 char *ret = string_desc_quotearg_n (1, s2);
54 ASSERT (memcmp (ret, "Thequickbrownfox", 16 + 1) == 0);
57 /* Test string_desc_quotearg. */
59 char *ret = string_desc_quotearg (s2);
60 ASSERT (memcmp (ret, "Thequickbrownfox", 16 + 1) == 0);
63 /* Test string_desc_quotearg_n_style. */
65 char *ret = string_desc_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 string_desc_quotearg_style. */
73 char *ret = string_desc_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 string_desc_quotearg_char. */
81 char *ret = string_desc_quotearg_char (s1, ' ');
82 ASSERT (memcmp (ret, "Hello world!", 12 + 1) == 0); /* ' ' not quoted?! */
85 /* Test string_desc_quotearg_colon. */
87 char *ret = string_desc_quotearg_colon (string_desc_from_c ("a:b"));
88 ASSERT (memcmp (ret, "a:b", 3 + 1) == 0); /* ':' not quoted?! */
91 /* Test string_desc_quotearg_n_custom. */
93 char *ret = string_desc_quotearg_n_custom (2, "<", ">", s1);
94 ASSERT (memcmp (ret, "<Hello world!>", 14 + 1) == 0);
97 /* Test string_desc_quotearg_n_custom. */
99 char *ret = string_desc_quotearg_custom ("[[", "]]", s1);
100 ASSERT (memcmp (ret, "[[Hello world!]]", 16 + 1) == 0);
103 return test_exit_status;