usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-string-desc.c
blobc39077c567870c0d5e8eb131e942b418579a8acf
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.h"
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "macros.h"
30 int
31 main (int argc, char *argv[])
33 ASSERT (argc > 1);
34 int fd3 = open (argv[1], O_RDWR | O_TRUNC | O_CREAT, 0600);
35 ASSERT (fd3 >= 0);
37 string_desc_t s0 = string_desc_new_empty ();
38 string_desc_t s1 = string_desc_from_c ("Hello world!");
39 string_desc_t s2 = string_desc_new_addr (21, "The\0quick\0brown\0\0fox");
41 /* Test string_desc_length. */
42 ASSERT (string_desc_length (s0) == 0);
43 ASSERT (string_desc_length (s1) == 12);
44 ASSERT (string_desc_length (s2) == 21);
46 /* Test string_desc_char_at. */
47 ASSERT (string_desc_char_at (s1, 0) == 'H');
48 ASSERT (string_desc_char_at (s1, 11) == '!');
49 ASSERT (string_desc_char_at (s2, 0) == 'T');
50 ASSERT (string_desc_char_at (s2, 1) == 'h');
51 ASSERT (string_desc_char_at (s2, 2) == 'e');
52 ASSERT (string_desc_char_at (s2, 3) == '\0');
53 ASSERT (string_desc_char_at (s2, 4) == 'q');
54 ASSERT (string_desc_char_at (s2, 15) == '\0');
55 ASSERT (string_desc_char_at (s2, 16) == '\0');
57 /* Test string_desc_data. */
58 (void) string_desc_data (s0);
59 ASSERT (memcmp (string_desc_data (s1), "Hello world!", 12) == 0);
60 ASSERT (memcmp (string_desc_data (s2), "The\0quick\0brown\0\0fox", 21) == 0);
62 /* Test string_desc_is_empty. */
63 ASSERT (string_desc_is_empty (s0));
64 ASSERT (!string_desc_is_empty (s1));
65 ASSERT (!string_desc_is_empty (s2));
67 /* Test string_desc_startswith. */
68 ASSERT (string_desc_startswith (s1, s0));
69 ASSERT (!string_desc_startswith (s0, s1));
70 ASSERT (!string_desc_startswith (s1, s2));
71 ASSERT (!string_desc_startswith (s2, s1));
72 ASSERT (string_desc_startswith (s2, string_desc_from_c ("The")));
73 ASSERT (string_desc_startswith (s2, string_desc_new_addr (9, "The\0quick")));
74 ASSERT (!string_desc_startswith (s2, string_desc_new_addr (9, "The\0quirk")));
76 /* Test string_desc_endswith. */
77 ASSERT (string_desc_endswith (s1, s0));
78 ASSERT (!string_desc_endswith (s0, s1));
79 ASSERT (!string_desc_endswith (s1, s2));
80 ASSERT (!string_desc_endswith (s2, s1));
81 ASSERT (!string_desc_endswith (s2, string_desc_from_c ("fox")));
82 ASSERT (string_desc_endswith (s2, string_desc_new_addr (4, "fox")));
83 ASSERT (string_desc_endswith (s2, string_desc_new_addr (6, "\0\0fox")));
84 ASSERT (!string_desc_endswith (s2, string_desc_new_addr (5, "\0\0ox")));
86 /* Test string_desc_cmp. */
87 ASSERT (string_desc_cmp (s0, s0) == 0);
88 ASSERT (string_desc_cmp (s0, s1) < 0);
89 ASSERT (string_desc_cmp (s0, s2) < 0);
90 ASSERT (string_desc_cmp (s1, s0) > 0);
91 ASSERT (string_desc_cmp (s1, s1) == 0);
92 ASSERT (string_desc_cmp (s1, s2) < 0);
93 ASSERT (string_desc_cmp (s2, s0) > 0);
94 ASSERT (string_desc_cmp (s2, s1) > 0);
95 ASSERT (string_desc_cmp (s2, s2) == 0);
97 /* Test string_desc_index. */
98 ASSERT (string_desc_index (s0, 'o') == -1);
99 ASSERT (string_desc_index (s2, 'o') == 12);
101 /* Test string_desc_last_index. */
102 ASSERT (string_desc_last_index (s0, 'o') == -1);
103 ASSERT (string_desc_last_index (s2, 'o') == 18);
105 /* Test string_desc_contains. */
106 ASSERT (string_desc_contains (s0, string_desc_from_c ("ll")) == -1);
107 ASSERT (string_desc_contains (s1, string_desc_from_c ("ll")) == 2);
108 ASSERT (string_desc_contains (s1, string_desc_new_addr (1, "")) == -1);
109 ASSERT (string_desc_contains (s2, string_desc_new_addr (1, "")) == 3);
110 ASSERT (string_desc_contains (s1, string_desc_new_addr (2, "\0")) == -1);
111 ASSERT (string_desc_contains (s2, string_desc_new_addr (2, "\0")) == 15);
113 /* Test string_desc_substring. */
114 ASSERT (string_desc_cmp (string_desc_substring (s1, 2, 5),
115 string_desc_from_c ("llo")) == 0);
117 /* Test string_desc_write. */
118 ASSERT (string_desc_write (fd3, s0) == 0);
119 ASSERT (string_desc_write (fd3, s1) == 0);
120 ASSERT (string_desc_write (fd3, s2) == 0);
122 /* Test string_desc_fwrite. */
123 ASSERT (string_desc_fwrite (stdout, s0) == 0);
124 ASSERT (string_desc_fwrite (stdout, s1) == 0);
125 ASSERT (string_desc_fwrite (stdout, s2) == 0);
127 /* Test string_desc_new, string_desc_set_char_at, string_desc_fill. */
128 string_desc_t s4;
129 ASSERT (string_desc_new (&s4, 5) == 0);
130 string_desc_set_char_at (s4, 0, 'H');
131 string_desc_set_char_at (s4, 4, 'o');
132 string_desc_set_char_at (s4, 1, 'e');
133 string_desc_fill (s4, 2, 4, 'l');
134 ASSERT (string_desc_length (s4) == 5);
135 ASSERT (string_desc_startswith (s1, s4));
137 /* Test string_desc_new_filled, string_desc_set_char_at. */
138 string_desc_t s5;
139 ASSERT (string_desc_new_filled (&s5, 5, 'l') == 0);
140 string_desc_set_char_at (s5, 0, 'H');
141 string_desc_set_char_at (s5, 4, 'o');
142 string_desc_set_char_at (s5, 1, 'e');
143 ASSERT (string_desc_length (s5) == 5);
144 ASSERT (string_desc_startswith (s1, s5));
146 /* Test string_desc_equals. */
147 ASSERT (!string_desc_equals (s1, s5));
148 ASSERT (string_desc_equals (s4, s5));
150 /* Test string_desc_copy, string_desc_free. */
152 string_desc_t s6;
153 ASSERT (string_desc_copy (&s6, s0) == 0);
154 ASSERT (string_desc_is_empty (s6));
155 string_desc_free (s6);
158 string_desc_t s6;
159 ASSERT (string_desc_copy (&s6, s2) == 0);
160 ASSERT (string_desc_equals (s6, s2));
161 string_desc_free (s6);
164 /* Test string_desc_overwrite. */
166 string_desc_t s7;
167 ASSERT (string_desc_copy (&s7, s2) == 0);
168 string_desc_overwrite (s7, 4, s1);
169 ASSERT (string_desc_equals (s7, string_desc_new_addr (21, "The\0Hello world!\0fox")));
172 /* Test string_desc_concat. */
174 string_desc_t s8;
175 ASSERT (string_desc_concat (&s8, 3, string_desc_new_addr (10, "The\0quick"),
176 string_desc_new_addr (7, "brown\0"),
177 string_desc_new_addr (4, "fox"),
178 string_desc_new_addr (7, "unused")) == 0);
179 ASSERT (string_desc_equals (s8, s2));
180 string_desc_free (s8);
183 /* Test string_desc_c. */
185 char *ptr = string_desc_c (s2);
186 ASSERT (ptr != NULL);
187 ASSERT (memcmp (ptr, "The\0quick\0brown\0\0fox\0", 22) == 0);
188 free (ptr);
191 close (fd3);
193 return test_exit_status;