libcstl
Loading...
Searching...
No Matches
string.c
Go to the documentation of this file.
1/*!
2 * @file
3 *
4 * Much like string.h, this file serves only to include the actual
5 * implementation file after setting certain macros to enable the
6 * "templatization" in that file to work. This file mus set the
7 * @p %cstl_STRING and @p %STDSTRPFX macros as described in string.h. In
8 * addition it must set @p %STRNUL to the NUL character as represented
9 * by the string's character type.
10 *
11 * This file also contains the unit tests for the string object(s)
12 *
13 * @see string.h for a description of necessary macros
14 * @see _string.c for the templatized string object implementation
15 */
16
17#include "cstl/string.h"
18
19#include <stdlib.h>
20#include <string.h>
21
22#ifndef NO_DOC
23#define STRNUL '\0'
24#define cstl_STRING cstl_string
25#define STDSTRPFX str
26#include "_string.c"
27#undef STDSTRPFX
28#undef cstl_STRING
29#undef STRNUL
30#endif
31
32#ifndef NO_DOC
33#define STRNUL L'\0'
34#define cstl_STRING cstl_wstring
35#define STDSTRPFX wcs
36#include "_string.c"
37#undef STDSTRPFX
38#undef cstl_STRING
39#undef STRNUL
40#endif
41
42#ifdef __cfg_test__
43// GCOV_EXCL_START
44#include "internal/check.h"
45
46START_TEST(erase)
47{
48 DECLARE_CSTL_STRING(string, s);
49
50 ck_assert_str_eq(cstl_string_str(&s), "");
51
52 /* insertion beyond the end fails */
53 ck_assert_signal(SIGABRT, cstl_string_insert_ch(&s, 1, 20, 'a'));
54
55 /* insertion at the end succeeds */
56 cstl_string_insert_ch(&s, 0, 3, 'a');
57 ck_assert_str_eq(cstl_string_str(&s), "aaa");
58 cstl_string_erase(&s, 0, 3);
59
60 cstl_string_set_str(&s, "abc");
61 cstl_string_erase(&s, 1, 1);
62 ck_assert_str_eq(cstl_string_str(&s), "ac");
63
64 cstl_string_set_str(&s, "abc");
65 cstl_string_erase(&s, 1, 12);
66 ck_assert_str_eq(cstl_string_str(&s), "a");
67
68 /* erase beyond the end fails */
69 ck_assert_signal(SIGABRT, cstl_string_erase(&s, 1, 1));
70
71 cstl_string_clear(&s);
72}
73END_TEST
74
75START_TEST(substr)
76{
77 DECLARE_CSTL_STRING(string, s);
78 DECLARE_CSTL_STRING(string, sub);
79
80 cstl_string_set_str(&s, "abcdefg");
81 cstl_string_substr(&s, 3, 3, &sub);
82 ck_assert_str_eq(cstl_string_str(&sub), "def");
83
84 cstl_string_set_str(&s, "abcdefg");
85 cstl_string_substr(&s, 2, 12, &sub);
86 ck_assert_str_eq(cstl_string_str(&sub), "cdefg");
87
88 cstl_string_clear(&sub);
89 cstl_string_clear(&s);
90}
91END_TEST
92
93START_TEST(find)
94{
95 DECLARE_CSTL_STRING(string, s);
96
97 cstl_string_set_str(&s, "abcdefghijk");
98
99 ck_assert(*cstl_string_at(&s, 0) == 'a');
100 ck_assert(*cstl_string_at(&s, 1) == 'b');
101 ck_assert(*cstl_string_at(&s, 2) == 'c');
102 ck_assert(*cstl_string_at_const(&s, 3) == 'd');
103 ck_assert(*cstl_string_at_const(&s, 4) == 'e');
104 ck_assert(*cstl_string_at_const(&s, 5) == 'f');
105 /* access beyond the end aborts */
106 ck_assert_signal(SIGABRT, cstl_string_at(&s, 20));
107
108 ck_assert_int_eq(cstl_string_find_ch(&s, 'd', 0), 3);
109 ck_assert_int_eq(cstl_string_find_ch(&s, 'e', 0), 4);
110 ck_assert_int_eq(cstl_string_find_ch(&s, cstl_string_nul, 0), -1);
111 ck_assert_int_eq(cstl_string_find_ch(&s, 'd', 3), 3);
112 ck_assert_int_eq(cstl_string_find_ch(&s, 'e', 3), 4);
113 ck_assert_int_eq(cstl_string_find_ch(&s, 'z', 3), -1);
114 /* find beyond the end aborts */
115 ck_assert_signal(SIGABRT, cstl_string_find_ch(&s, 'a', 20));
116
117 ck_assert_int_eq(cstl_string_find_str(&s, "xyz", 0), -1);
118 ck_assert_int_eq(cstl_string_find_str(&s, "abc", 0), 0);
119 ck_assert_int_eq(cstl_string_find_str(&s, "ghikj", 0), -1);
120 ck_assert_int_eq(cstl_string_find_str(&s, "efghij", 0), 4);
121 ck_assert_int_eq(cstl_string_find_str(&s, "xyz", 4), -1);
122 ck_assert_int_eq(cstl_string_find_str(&s, "abc", 4), -1);
123 ck_assert_int_eq(cstl_string_find_str(&s, "ghikj", 4), -1);
124 ck_assert_int_eq(cstl_string_find_str(&s, "efghij", 4), 4);
125 /* find beyond the end aborts */
126 ck_assert_signal(SIGABRT, cstl_string_find_str(&s, "abc", 20));
127
128 cstl_string_clear(&s);
129}
130END_TEST
131
132START_TEST(swap)
133{
134 DECLARE_CSTL_STRING(string, s1);
135 DECLARE_CSTL_STRING(string, s2);
136
137 cstl_string_set_str(&s1, "hello");
138 cstl_string_set_str(&s2, "world");
139
140 cstl_string_swap(&s1, &s2);
141
142 ck_assert_str_eq(cstl_string_str(&s1), "world");
143 ck_assert_str_eq(cstl_string_str(&s2), "hello");
144
145 cstl_string_clear(&s2);
146 cstl_string_clear(&s1);
147}
148END_TEST
149
150Suite * string_suite(void)
151{
152 Suite * const s = suite_create("string");
153
154 TCase * tc;
155
156 tc = tcase_create("string");
157 tcase_add_test(tc, erase);
158 tcase_add_test(tc, substr);
159 tcase_add_test(tc, find);
160 tcase_add_test(tc, swap);
161 suite_add_tcase(s, tc);
162
163 return s;
164}
165
166// GCOV_EXCL_STOP
167#endif
#define DECLARE_CSTL_STRING(TYPE, NAME)
(Statically) declare and initialize a vector
Definition string.h:101