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. */
21 #include "xstring-desc.h"
31 string_desc_t s0
= string_desc_new_empty ();
32 string_desc_t s1
= string_desc_from_c ("Hello world!");
33 string_desc_t s2
= string_desc_new_addr (21, "The\0quick\0brown\0\0fox");
35 /* Test xstring_desc_new. */
36 string_desc_t s4
= xstring_desc_new (5);
37 string_desc_set_char_at (s4
, 0, 'H');
38 string_desc_set_char_at (s4
, 4, 'o');
39 string_desc_set_char_at (s4
, 1, 'e');
40 string_desc_fill (s4
, 2, 4, 'l');
41 ASSERT (string_desc_length (s4
) == 5);
42 ASSERT (string_desc_startswith (s1
, s4
));
44 /* Test xstring_desc_new_filled. */
45 string_desc_t s5
= xstring_desc_new_filled (5, 'l');
46 string_desc_set_char_at (s5
, 0, 'H');
47 string_desc_set_char_at (s5
, 4, 'o');
48 string_desc_set_char_at (s5
, 1, 'e');
49 ASSERT (string_desc_length (s5
) == 5);
50 ASSERT (string_desc_startswith (s1
, s5
));
52 /* Test xstring_desc_copy. */
54 string_desc_t s6
= xstring_desc_copy (s0
);
55 ASSERT (string_desc_is_empty (s6
));
56 string_desc_free (s6
);
59 string_desc_t s6
= xstring_desc_copy (s2
);
60 ASSERT (string_desc_equals (s6
, s2
));
61 string_desc_free (s6
);
64 /* Test xstring_desc_concat. */
67 xstring_desc_concat (3, string_desc_new_addr (10, "The\0quick"),
68 string_desc_new_addr (7, "brown\0"),
69 string_desc_new_addr (4, "fox"),
70 string_desc_new_addr (7, "unused"));
71 ASSERT (string_desc_equals (s8
, s2
));
72 string_desc_free (s8
);
75 /* Test xstring_desc_c. */
77 char *ptr
= xstring_desc_c (s2
);
79 ASSERT (memcmp (ptr
, "The\0quick\0brown\0\0fox\0", 22) == 0);
83 return test_exit_status
;