blob: 9dcf3ecdac3320bd6a74179569107360224d21b0 [file] [log] [blame]
robbiewc58f59a2002-12-31 22:43:14 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewc58f59a2002-12-31 22:43:14 +000018 */
19
robbiewc58f59a2002-12-31 22:43:14 +000020/* 01/02/2003 Port to LTP avenkat@us.ibm.com */
21/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
22
23/*
24 * NAME
25 * string01.c - check string functions.
26 *
27 * CALLS
28 * strchr, strrchr, strcat, strcmp, strcpy, strlen,
29 * strncat, strncmp, strncpy
30 *
31 * ALGORITHM
32 * Test functionality of the string functions:
33 * (strchr, strrchr, strcat, strcmp, strcpy, strlen,
34 * strncat, strncmp, strncpy )
35 *
36 */
37
subrata_modak56207ce2009-03-23 13:35:39 +000038#include <stdio.h>
robbiewc58f59a2002-12-31 22:43:14 +000039#include <sys/types.h>
40#include <string.h>
41
42/***** LTP Port *****/
43#include <errno.h>
44#include <stdlib.h>
45#include "test.h"
robbiewc58f59a2002-12-31 22:43:14 +000046
47#define FAILED 0
48#define PASSED 1
49
robbiewc58f59a2002-12-31 22:43:14 +000050char *TCID = "string01";
51
52int local_flag = PASSED;
53int block_number;
robbiewc58f59a2002-12-31 22:43:14 +000054FILE *temp;
55int TST_TOTAL = 1;
robbiewc58f59a2002-12-31 22:43:14 +000056/***** ** ** *****/
57
58#define LONGSTR (96*1024-1)
59/* #define LONGSTR (1024-1) */
60
61/*
62 * Miscellaneous data strings for testing.
63 */
64
65char tiat[] = "This is a test of the string functions. ";
66char yat[] = "This is yet another test.";
67char tiatyat[] =
subrata_modak56207ce2009-03-23 13:35:39 +000068 "This is a test of the string functions. This is yet another test.";
robbiewc58f59a2002-12-31 22:43:14 +000069
subrata_modak56207ce2009-03-23 13:35:39 +000070char longstr[LONGSTR + 1]; /* a very long string */
71char dst0[LONGSTR + 1]; /* place holders for various tests */
72char dst1[LONGSTR + 1];
73char dst2[LONGSTR + 1];
robbiewc58f59a2002-12-31 22:43:14 +000074
75/*
76 * Data structures for testing.
77 */
78
79/* Strlen (strlen( s ) == e_res) */
80struct t_strlen {
81 char *s;
82 int e_res;
83} t_len[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000084 {
85 "", 0}, {
86 "12345", 5}, {
87 tiat, 41}, {
88 longstr, LONGSTR}, {
89 NULL, 0}
robbiewc58f59a2002-12-31 22:43:14 +000090};
91
92/* Index (index( s, c ) == e_res) */
93struct t_index {
94 char *s;
95 char c;
96 char *e_res;
97} t_index[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000098 {
Garrett Cooper45e285d2010-11-22 12:19:25 -080099 "", 'z', NULL}, {
subrata_modak56207ce2009-03-23 13:35:39 +0000100 tiat, 'a', tiat + 8}, {
101 tiat, 's', tiat + 3}, {
102 tiat, 'o', tiat + 15}, {
Garrett Cooper45e285d2010-11-22 12:19:25 -0800103 tiat, 'z', NULL}, {
subrata_modak56207ce2009-03-23 13:35:39 +0000104 NULL, 0, NULL}
robbiewc58f59a2002-12-31 22:43:14 +0000105};
106
107/* Rindex (rindex( s, c ) == e_res) */
108struct t_rindex {
109 char *s;
110 char c;
111 char *e_res;
112} t_rindex[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000113 {
Garrett Cooper45e285d2010-11-22 12:19:25 -0800114 "", 'z', NULL}, {
subrata_modak56207ce2009-03-23 13:35:39 +0000115 tiat, 'a', tiat + 8}, {
116 tiat, 's', tiat + 37}, {
117 tiat, 'o', tiat + 35}, {
Garrett Cooper45e285d2010-11-22 12:19:25 -0800118 tiat, 'z', NULL}, {
subrata_modak56207ce2009-03-23 13:35:39 +0000119 NULL, 0, NULL}
robbiewc58f59a2002-12-31 22:43:14 +0000120};
121
robbiewc58f59a2002-12-31 22:43:14 +0000122/* Strcmp (strcmp( s1, s2 ) == e_res) */
123struct t_strcmp {
124 char *s1;
125 char *s2;
126 int e_res;
127} t_cmp[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000128 {
129 "", "", 0}, {
130 "", tiat, -((int)'T')}, {
131 tiat, "", 'T'}, {
132 tiat, tiat, 0}, {
133 yat, tiat, 'y' - 'a'}, {
134 NULL, NULL, 0}
robbiewc58f59a2002-12-31 22:43:14 +0000135};
136
robbiewc58f59a2002-12-31 22:43:14 +0000137/* Strcat (strcmp( strcat(s1, s2), s1s2 ) == e_res) */
138/* ASSUMES strcmp is working -- it is tested prior to strcat */
139struct t_strcat {
140 char *s1;
141 char *s2;
142 char *s1s2;
143 int e_res;
144} t_cat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000145 {
146 dst0, "", "", 0}, {
147 dst0, tiat, tiat, 0}, {
148 dst0, "", tiat, 0}, {
149 dst0, yat, tiatyat, 0}, {
150 dst1, longstr, longstr, 0}, {
151 NULL, NULL, NULL, 0}
robbiewc58f59a2002-12-31 22:43:14 +0000152};
153
154/* Strcpy (strcmp( strcpy(s1, s2), s1s2 ) == e_res) */
155/* ASSUMES strcmp is working -- it is tested prior to strcpy */
156/* No overlapping copies are tested */
157struct t_strcpy {
158 char *s1;
159 char *s2;
160 int e_res;
161} t_cpy[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000162 {
163 dst0, "", 0}, {
164 dst0, tiat, 0}, {
165 dst0, longstr, 0}, {
166 NULL, NULL, 0}
robbiewc58f59a2002-12-31 22:43:14 +0000167};
168
169/* Strncmp (strncmp( s1, s2 ) == e_res) */
170struct t_strncmp {
171 char *s1;
172 char *s2;
173 int n;
174 int e_res;
subrata_modak56207ce2009-03-23 13:35:39 +0000175 int a_res; /* Allowable results, some platforms only return 1 or -1 */
robbiewc58f59a2002-12-31 22:43:14 +0000176} t_ncmp[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000177 {
178 "", "", 0, 0, 0}, {
179 "", "", 80, 0, 0}, {
180 tiat, "", 0, 0, 0}, {
181 "", tiat, 80, -((int)'T'), -1}, {
182 tiat, "", 80, 'T', 1}, {
183 tiat, tiat, 80, 0, 0}, {
184 yat, tiat, 80, 'y' - 'a', 1}, {
185 yat, tiat, 8, 0, 1}, {
186 yat, tiat, 9, 'y' - 'a', 1}, {
187 NULL, NULL, 0, 0, 0}
subrata_modakbdbaec52009-02-26 12:14:51 +0000188
robbiewc58f59a2002-12-31 22:43:14 +0000189};
190
191/* Strncat (strcmp( strncat(s1, s2, n), s1ns2 ) == e_res) */
192/* ASSUMES strcmp is working -- it is tested prior to strncat */
193/* dest is guaranteed to be all '\0' s at start of test */
194struct t_strncat {
195 char *s1;
196 char *s2;
197 int n;
198 char *s1ns2;
199 int e_res;
200} t_ncat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000201 /* Regular strcat stuff -- i.e., n is large enough */
202 {
203 dst0, "", LONGSTR, "", 0}, {
204 dst0, tiat, LONGSTR, tiat, 0}, {
205 dst0, "", LONGSTR, tiat, 0}, {
206 dst0, yat, LONGSTR, tiatyat, 0}, {
207 dst1, longstr, LONGSTR, longstr, 0},
208 /* Restricted strcat stuff */
209 {
210 dst2, longstr, 0, "", 0}, {
211 dst2, longstr, 1, "t", 0}, {
212 dst2, longstr, LONGSTR - 1, longstr, 0}, {
213 NULL, NULL, 0, NULL, 0}
subrata_modakbdbaec52009-02-26 12:14:51 +0000214
robbiewc58f59a2002-12-31 22:43:14 +0000215};
216
robbiewc58f59a2002-12-31 22:43:14 +0000217/* Strncpy (strcmp( strncpy(s1, s2), s1n ) == e_res) */
218/* ASSUMES strcmp is working -- it is tested prior to strncpy */
219/* No overlapping copies are tested */
220struct t_strncpy {
221 char *s1;
222 char *s2;
223 int n;
224 char *s1n;
225 int e_res;
226} t_ncpy[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000227 /* Regular strcpy stuff -- i.e., n is large enough */
228 {
229 dst0, "", LONGSTR, "", 0}, {
230 dst0, tiat, LONGSTR, tiat, 0}, {
231 dst0, longstr, LONGSTR, longstr, 0},
232 /* Restricted strcpy stuff */
233 {
234 dst1, tiat, 0, "", 0}, {
235 dst1, longstr, 5, "ttttt", 0}, {
236 NULL, NULL, 0, NULL, 0}
robbiewc58f59a2002-12-31 22:43:14 +0000237};
238
robbiewc58f59a2002-12-31 22:43:14 +0000239/***** LTP Port *****/
240void setup();
241int blenter();
242int blexit();
243int anyfail();
244
Mike Frysingerc57fba52014-04-09 18:56:30 -0400245void setup(void)
robbiewc58f59a2002-12-31 22:43:14 +0000246{
subrata_modak56207ce2009-03-23 13:35:39 +0000247 temp = stderr;
robbiewc58f59a2002-12-31 22:43:14 +0000248}
249
Mike Frysingerc57fba52014-04-09 18:56:30 -0400250int blenter(void)
robbiewc58f59a2002-12-31 22:43:14 +0000251{
subrata_modak56207ce2009-03-23 13:35:39 +0000252 local_flag = PASSED;
253 return 0;
robbiewc58f59a2002-12-31 22:43:14 +0000254}
255
Mike Frysingerc57fba52014-04-09 18:56:30 -0400256int blexit(void)
robbiewc58f59a2002-12-31 22:43:14 +0000257{
subrata_modak56207ce2009-03-23 13:35:39 +0000258 (local_flag == PASSED) ? tst_resm(TPASS,
259 "Test passed") : tst_resm(TFAIL,
260 "Test failed");
261 return 0;
robbiewc58f59a2002-12-31 22:43:14 +0000262}
263
Mike Frysingerc57fba52014-04-09 18:56:30 -0400264int anyfail(void)
robbiewc58f59a2002-12-31 22:43:14 +0000265{
subrata_modak56207ce2009-03-23 13:35:39 +0000266 tst_exit();
robbiewc58f59a2002-12-31 22:43:14 +0000267}
268
269/***** ** ** *****/
270
robbiewc58f59a2002-12-31 22:43:14 +0000271/*--------------------------------------------------------------*/
272
Mike Frysingerc57fba52014-04-09 18:56:30 -0400273int main(int argc, char *argv[])
robbiewc58f59a2002-12-31 22:43:14 +0000274{
275 register int n, i;
276 char *s, *pr;
277
278 /*
279 * Init longstr
280 */
281
282 s = longstr;
283 n = LONGSTR;
subrata_modak56207ce2009-03-23 13:35:39 +0000284 while (n--)
robbiewc58f59a2002-12-31 22:43:14 +0000285 *s++ = 't';
286 *s = '\0';
287
288 setup();
289/*--------------------------------------------------------------*/
290
291 /*
292 * Index
293 */
294 //fprintf(temp, "\tStrchr\n" );
295 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000296 while (t_index[i].s) {
297 if ((pr =
298 strchr(t_index[i].s, t_index[i].c)) != t_index[i].e_res) {
299 fprintf(temp, "(Strchr) test %d", i);
300 local_flag = FAILED;
robbiewc58f59a2002-12-31 22:43:14 +0000301 }
subrata_modak56207ce2009-03-23 13:35:39 +0000302 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000303 }
304 /*
305 * Strrchr
306 */
307 //fprintf(temp, "\tStrrchr\n" );
308 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000309 while (t_rindex[i].s) {
310 if ((pr = strrchr(t_rindex[i].s, t_rindex[i].c))
311 != t_rindex[i].e_res) {
312 fprintf(temp, "(Strrchr) test %d", i);
313 local_flag = FAILED;
robbiewc58f59a2002-12-31 22:43:14 +0000314 }
subrata_modak56207ce2009-03-23 13:35:39 +0000315 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000316 }
317 /*
318 * Strlen
319 */
320 //fprintf(temp, "\tStrlen\n" );
321 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000322 while (t_len[i].s) {
323 if ((n = strlen(t_len[i].s)) != t_len[i].e_res) {
324 fprintf(temp, "(Strlen) test %d: expected %d, got %d",
325 i, t_len[i].e_res, n);
326 local_flag = FAILED;
robbiewc58f59a2002-12-31 22:43:14 +0000327 }
subrata_modak56207ce2009-03-23 13:35:39 +0000328 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000329 }
330
331 /*
332 * Strcmp
333 */
334 //fprintf(temp, "\tStrcmp\n" );
335 i = 0;
336#define sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
subrata_modak56207ce2009-03-23 13:35:39 +0000337 while (t_cmp[i].s1) {
338 n = strcmp(t_cmp[i].s1, t_cmp[i].s2);
339 if (sign(n) != sign(t_cmp[i].e_res)) {
340 fprintf(temp, "(Strcmp) test %d: expected %d, got %d",
341 i, sign(t_cmp[i].e_res), sign(n));
342 local_flag = FAILED;
343 }
344 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000345 }
346
347 /*
348 * Strcat
349 */
350 //fprintf(temp, "\tStrcat\n" );
subrata_modak56207ce2009-03-23 13:35:39 +0000351 memset(dst0, 0, LONGSTR + 1); /* clean slate */
352 memset(dst1, 0, LONGSTR + 1); /* clean slate */
robbiewc58f59a2002-12-31 22:43:14 +0000353 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000354 while (t_cat[i].s1) {
355 if ((n =
356 strcmp(strcat(t_cat[i].s1, t_cat[i].s2), t_cat[i].s1s2))
357 != t_cat[i].e_res) {
358 fprintf(temp, "(Strcat) test %d: expected %d, got %d",
359 i, t_cat[i].e_res, n);
360 local_flag = FAILED;
robbiewc58f59a2002-12-31 22:43:14 +0000361 }
subrata_modak56207ce2009-03-23 13:35:39 +0000362 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000363 }
364
365 /*
366 * Strcpy
367 */
368 //fprintf(temp, "\tStrcpy\n" );
369 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000370 while (t_cpy[i].s1) {
371 if ((n = strcmp(strcpy(t_cpy[i].s1, t_cpy[i].s2), t_cpy[i].s2))
372 != t_cpy[i].e_res) {
373 fprintf(temp, "(Strcpy) test %d: expected %d, got %d",
374 i, t_cpy[i].e_res, n);
375 local_flag = FAILED;
robbiewc58f59a2002-12-31 22:43:14 +0000376 }
subrata_modak56207ce2009-03-23 13:35:39 +0000377 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000378 }
379
380 /*
381 * Strncat
382 */
383 //fprintf(temp, "\tStrncat\n" );
subrata_modak56207ce2009-03-23 13:35:39 +0000384 memset(dst0, 0, LONGSTR + 1); /* clean slate */
385 memset(dst1, 0, LONGSTR + 1); /* clean slate */
386 memset(dst2, 0, LONGSTR + 1); /* clean slate */
robbiewc58f59a2002-12-31 22:43:14 +0000387 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000388 while (t_ncat[i].s1) {
389 if ((n =
390 strcmp(strncat(t_ncat[i].s1, t_ncat[i].s2, t_ncat[i].n),
391 t_ncat[i].s1ns2)) != t_ncat[i].e_res) {
392 fprintf(temp, "(Strncat) test %d: expected %d, got %d",
393 i, t_ncat[i].e_res, n);
394 local_flag = FAILED;
robbiewc58f59a2002-12-31 22:43:14 +0000395 }
subrata_modak56207ce2009-03-23 13:35:39 +0000396 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000397 }
398
robbiewc58f59a2002-12-31 22:43:14 +0000399 /*
400 * Strncmp
401 */
402 //fprintf(temp, "\tStrncmp\n" );
403 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000404 while (t_ncmp[i].s1) {
405 if ((n = strncmp(t_ncmp[i].s1, t_ncmp[i].s2, t_ncmp[i].n))
406 != t_ncmp[i].e_res) {
407 if ((t_ncmp[i].a_res < 0 && n > t_ncmp[i].a_res)
408 || (t_ncmp[i].a_res > 0 && n < t_ncmp[i].a_res)) {
409 fprintf(temp,
410 "(Strncmp) test %d: expected %d, got %d",
411 i, t_ncmp[i].e_res, n);
412 local_flag = FAILED;
413 }
robbiewc58f59a2002-12-31 22:43:14 +0000414 }
subrata_modak56207ce2009-03-23 13:35:39 +0000415 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000416 }
417
418 /*
419 * Strncpy
420 */
421 //fprintf(temp, "\tStrncpy\n" );
422 i = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000423 memset(dst0, 0, LONGSTR + 1); /* clean slate */
424 memset(dst1, 0, LONGSTR + 1); /* clean slate */
425 while (t_ncpy[i].s1) {
426 if ((n =
427 strcmp(strncpy(t_ncpy[i].s1, t_ncpy[i].s2, t_ncpy[i].n),
428 t_ncpy[i].s1n)) != t_ncpy[i].e_res) {
429 fprintf(temp, "(Strncpy) test %d: expected %d, got %d",
430 i, t_ncpy[i].e_res, n);
431 local_flag = FAILED;
robbiewc58f59a2002-12-31 22:43:14 +0000432 }
subrata_modak56207ce2009-03-23 13:35:39 +0000433 i++;
robbiewc58f59a2002-12-31 22:43:14 +0000434 }
435
436 blexit();
437 anyfail();
Garrett Cooper2c282152010-12-16 00:55:50 -0800438 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700439}