Debian Codegraph @

ext/safestringlib/unittests/test_strcspn_s.c

raw
1 /*------------------------------------------------------------------
2 * test_strcspn_s
3 *
4 *
5 *------------------------------------------------------------------
6 */
7
8 #include "test_private.h"
9 #include "safe_str_lib.h"
10
11 #define LEN ( 128 )
12 #define SHORT_LEN ( 5 )
13
14 static char str1[LEN];
15 static char str2[LEN];
16
17 int test_strcspn_s (void)
18 {
19 errno_t rc;
20 rsize_t count;
21 int std_count;
22 unsigned int testno = 0;
23
24
25
26 /*--------------------------------------------------*/
27 /* 1 Test for NULL destination check */
28 printf("Test #%d: NULL Scan String Check\n", ++testno);
29
30 rc = strcspn_s(NULL, LEN, str2, LEN, &count);
31 if (rc != ESNULLP) {
32 printf("%s %u Error rc=%d \n",
33 __FUNCTION__, __LINE__, rc);
34 }
35
36 if (count != 0) {
37 printf("%s %u Error count=%zu rc=%d \n",
38 __FUNCTION__, __LINE__, count, rc);
39 }
40
41 /*--------------------------------------------------*/
42 /* 2 Test for NULL source check */
43 printf("Test #%d: NULL Exclusion String Check: \n", ++testno);
44
45 rc = strcspn_s(str1, LEN, NULL, LEN, &count);
46 if (rc != ESNULLP) {
47 printf("%s %u Error rc=%d \n",
48 __FUNCTION__, __LINE__, rc);
49 }
50
51 if (count != 0) {
52 printf("%s %u Error count=%zu rc=%d \n",
53 __FUNCTION__, __LINE__, count, rc);
54 }
55
56 /*--------------------------------------------------*/
57 /* 3 Test for NULL Count ouput check */
58 printf("Test #%d: NULL Count parameter Check: \n", ++testno);
59
60 rc = strcspn_s(str1, LEN, str2, LEN, NULL);
61 if (rc != ESNULLP) {
62 printf("%s %u Error rc=%d \n",
63 __FUNCTION__, __LINE__, rc);
64 }
65
66 /*--------------------------------------------------*/
67 /* 4 Test for zero length destination */
68 printf("Test #%d:Zero Length Scan String Check\n", ++testno);
69
70 rc = strcspn_s(str1, 0, str2, LEN, &count);
71 if (rc != ESZEROL) {
72 printf("%s %u Error rc=%d \n",
73 __FUNCTION__, __LINE__, rc);
74 }
75
76 if (count != 0) {
77 printf("%s %u Error count=%zu rc=%d \n",
78 __FUNCTION__, __LINE__, count, rc);
79 }
80
81 /*--------------------------------------------------*/
82 /* 5 Test for zero length source */
83 printf("Test #%d:Zero Length Exclusion String Check\n", ++testno);
84
85 rc = strcspn_s(str1, LEN, str2, 0, &count);
86 if (rc != ESZEROL) {
87 printf("%s %u Error rc=%d \n",
88 __FUNCTION__, __LINE__, rc);
89 }
90
91 if (count != 0) {
92 printf("%s %u Error count=%zu rc=%d \n",
93 __FUNCTION__, __LINE__, count, rc);
94 }
95
96 /*--------------------------------------------------*/
97 /* 6 Test for too large length destination */
98 printf("Test #%d:Exceed Max Length Scan String Check\n", ++testno);
99
100 rc = strcspn_s(str1, RSIZE_MAX_STR+1, str2, LEN, &count);
101 if (rc != ESLEMAX) {
102 printf("%s %u Error rc=%d \n",
103 __FUNCTION__, __LINE__, rc);
104 }
105
106 if (count != 0) {
107 printf("%s %u Error count=%zu rc=%d \n",
108 __FUNCTION__, __LINE__, count, rc);
109 }
110
111 /*--------------------------------------------------*/
112 /* 7 Test for too large length destination */
113 printf("Test #%d:Exceed Max Length Exclusion String Check\n", ++testno);
114
115 rc = strcspn_s(str1, RSIZE_MAX_STR, str2, RSIZE_MAX_STR+1, &count);
116 if (rc != ESLEMAX) {
117 printf("%s %u Error rc=%d \n",
118 __FUNCTION__, __LINE__, rc);
119 }
120
121 if (count != 0) {
122 printf("%s %u Error count=%zu rc=%d \n",
123 __FUNCTION__, __LINE__, count, rc);
124 }
125
126 /*--------------------------------------------------*/
127 /* 8 Test for */
128 printf("Test #%d:\n", ++testno);
129
130 str1[0] = '\0';
131 str2[0] = '\0';
132
133 rc = strcspn_s(str1, LEN, str2, LEN, &count);
134 if (rc != EOK) {
135 printf("%s %u Error rc=%d \n",
136 __FUNCTION__, __LINE__, rc);
137 }
138
139 if (count != 0) {
140 printf("%s %u Error count=%zu rc=%d \n",
141 __FUNCTION__, __LINE__, count, rc);
142 }
143
144 std_count = strcspn(str1, str2);
145 if (count != std_count) {
146 printf("%s %u count=%zu std_count=%d rc=%d \n",
147 __FUNCTION__, __LINE__, count, std_count, rc);
148 }
149
150 /*--------------------------------------------------*/
151 /* 9 Test for */
152 printf("Test #%d:\n", ++testno);
153
154
155 strcpy (str1, "keep it simple");
156 strcpy (str2, "KEEP");
157
158 rc = strcspn_s(str1, 1, str2, LEN, &count);
159 if (rc != EOK) {
160 printf("%s %u Error rc=%d \n",
161 __FUNCTION__, __LINE__, rc);
162 }
163
164 if (count != 1) {
165 printf("%s %u Error count=%zu rc=%d \n",
166 __FUNCTION__, __LINE__, count, rc);
167 }
168
169 /*--------------------------------------------------*/
170 /* 10 Test for */
171 printf("Test #%d:\n", ++testno);
172
173
174 strcpy (str1, "keep it simple");
175 strcpy (str2, "KEEP");
176
177 rc = strcspn_s(str1, 2, str2, LEN, &count);
178 if (rc != EOK) {
179 printf("%s %u Error rc=%d \n",
180 __FUNCTION__, __LINE__, rc);
181 }
182
183 if (count != 2) {
184 printf("%s %u Error count=%zu rc=%d \n",
185 __FUNCTION__, __LINE__, count, rc);
186 }
187
188 /*--------------------------------------------------*/
189 /* 11 Test for */
190 printf("Test #%d:\n", ++testno);
191
192
193 strcpy (str1, "keep it simple");
194 strcpy (str2, "KEEP");
195
196 rc = strcspn_s(str1, 3, str2, LEN, &count);
197 if (rc != EOK) {
198 printf("%s %u Error rc=%d \n",
199 __FUNCTION__, __LINE__, rc);
200 }
201
202 if (count != 3) {
203 printf("%s %u Error count=%zu rc=%d \n",
204 __FUNCTION__, __LINE__, count, rc);
205 }
206
207
208 /*--------------------------------------------------*/
209 /* 12 Test for */
210 printf("Test #%d:\n", ++testno);
211
212
213 strcpy (str1, "keep it simple");
214 strcpy (str2, "KEEP");
215
216 rc = strcspn_s(str1, 6, str2, LEN, &count);
217 if (rc != EOK) {
218 printf("%s %u Error rc=%d \n",
219 __FUNCTION__, __LINE__, rc);
220 }
221
222 if (count != 6) {
223 printf("%s %u Error count=%zu rc=%d \n",
224 __FUNCTION__, __LINE__, count, rc);
225 }
226
227 /*--------------------------------------------------*/
228 /* 13 Test for */
229 printf("Test #%d:\n", ++testno);
230
231
232 strcpy (str1, "keep it simple");
233 strcpy (str2, "ABCDEF");
234
235 rc = strcspn_s(str1, 6, str2, 2, &count);
236 if (rc != EOK) {
237 printf("%s %u Error rc=%d \n",
238 __FUNCTION__, __LINE__, rc);
239 }
240
241 if (count != 6) {
242 printf("%s %u count=%zu std_count=%d rc=%d \n",
243 __FUNCTION__, __LINE__, count, std_count, rc);
244 }
245
246 /*--------------------------------------------------*/
247 /* 14 Test for */
248 printf("Test #%d:\n", ++testno);
249
250
251 strcpy (str1, "keep it simple");
252 strcpy (str2, "keep");
253
254 rc = strcspn_s(str1, LEN, str2, LEN, &count);
255 if (rc != EOK) {
256 printf("%s %u Error rc=%d \n",
257 __FUNCTION__, __LINE__, rc);
258 }
259
260 std_count = strcspn(str1, str2);
261 if (count != std_count) {
262 printf("%s %u count=%zu std_count=%d rc=%d \n",
263 __FUNCTION__, __LINE__, count, std_count, rc);
264 }
265
266 /*--------------------------------------------------*/
267 /* 15 Test for */
268 printf("Test #%d:\n", ++testno);
269
270
271 strcpy (str1, "keep it simple");
272 strcpy (str2, "notincluded");
273
274 rc = strcspn_s(str1, LEN, str2, LEN, &count);
275 if (rc != EOK) {
276 printf("%s %u Error rc=%d \n",
277 __FUNCTION__, __LINE__, rc);
278 }
279
280 std_count = strcspn(str1, str2);
281 if (count != std_count) {
282 printf("%s %u count=%zu std_count=%d rc=%d \n",
283 __FUNCTION__, __LINE__, count, std_count, rc);
284 }
285
286 /*--------------------------------------------------*/
287 /* 16 Test for */
288 printf("Test #%d:\n", ++testno);
289
290
291 strcpy (str1, "keep it simple");
292 strcpy (str2, "1234567890");
293
294 rc = strcspn_s(str1, LEN, str2, LEN, &count);
295 if (rc != EOK) {
296 printf("%s %u Error rc=%d \n",
297 __FUNCTION__, __LINE__, rc);
298 }
299
300 std_count = strcspn(str1, str2);
301 if (count != std_count) {
302 printf("%s %u count=%zu std_count=%d rc=%d \n",
303 __FUNCTION__, __LINE__, count, std_count, rc);
304 }
305
306 /*--------------------------------------------------*/
307
308 return (0);
309 }
310