robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | /* 01/02/2003 Port to LTP avenkat@us.ibm.com */ |
| 22 | /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ |
| 23 | |
| 24 | /* |
| 25 | * NAME |
| 26 | * string01.c - check string functions. |
| 27 | * |
| 28 | * CALLS |
| 29 | * strchr, strrchr, strcat, strcmp, strcpy, strlen, |
| 30 | * strncat, strncmp, strncpy |
| 31 | * |
| 32 | * ALGORITHM |
| 33 | * Test functionality of the string functions: |
| 34 | * (strchr, strrchr, strcat, strcmp, strcpy, strlen, |
| 35 | * strncat, strncmp, strncpy ) |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | /***** LTP Port *****/ |
| 44 | #include <errno.h> |
| 45 | #include <stdlib.h> |
| 46 | #include "test.h" |
| 47 | #include "usctest.h" |
| 48 | |
| 49 | #define FAILED 0 |
| 50 | #define PASSED 1 |
| 51 | |
| 52 | |
| 53 | char *TCID = "string01"; |
| 54 | |
| 55 | int local_flag = PASSED; |
| 56 | int block_number; |
| 57 | int errno; |
| 58 | FILE *temp; |
| 59 | int TST_TOTAL = 1; |
| 60 | extern int Tst_count; |
| 61 | /***** ** ** *****/ |
| 62 | |
| 63 | #define LONGSTR (96*1024-1) |
| 64 | /* #define LONGSTR (1024-1) */ |
| 65 | |
| 66 | /* |
| 67 | * Miscellaneous data strings for testing. |
| 68 | */ |
| 69 | |
| 70 | char tiat[] = "This is a test of the string functions. "; |
| 71 | char yat[] = "This is yet another test."; |
| 72 | char tiatyat[] = |
| 73 | "This is a test of the string functions. This is yet another test."; |
| 74 | |
| 75 | char longstr[LONGSTR+1]; /* a very long string */ |
| 76 | char dst0[LONGSTR+1]; /* place holders for various tests */ |
| 77 | char dst1[LONGSTR+1]; |
| 78 | char dst2[LONGSTR+1]; |
| 79 | |
| 80 | /* |
| 81 | * Data structures for testing. |
| 82 | */ |
| 83 | |
| 84 | /* Strlen (strlen( s ) == e_res) */ |
| 85 | struct t_strlen { |
| 86 | char *s; |
| 87 | int e_res; |
| 88 | } t_len[] = { |
| 89 | { "", 0 }, |
| 90 | { "12345", 5 }, |
| 91 | { tiat, 41 }, |
| 92 | { longstr, LONGSTR }, |
| 93 | {NULL,0} |
| 94 | }; |
| 95 | |
| 96 | /* Index (index( s, c ) == e_res) */ |
| 97 | struct t_index { |
| 98 | char *s; |
| 99 | char c; |
| 100 | char *e_res; |
| 101 | } t_index[] = { |
| 102 | { "", 'z', (char *) NULL }, |
| 103 | { tiat, 'a', tiat+8 }, |
| 104 | { tiat, 's', tiat+3 }, |
| 105 | { tiat, 'o', tiat+15 }, |
| 106 | { tiat, 'z', (char *) NULL }, |
| 107 | {NULL,0,NULL} |
| 108 | }; |
| 109 | |
| 110 | /* Rindex (rindex( s, c ) == e_res) */ |
| 111 | struct t_rindex { |
| 112 | char *s; |
| 113 | char c; |
| 114 | char *e_res; |
| 115 | } t_rindex[] = { |
| 116 | { "", 'z', (char *) NULL }, |
| 117 | { tiat, 'a', tiat+8 }, |
| 118 | { tiat, 's', tiat+37 }, |
| 119 | { tiat, 'o', tiat+35 }, |
| 120 | { tiat, 'z', (char *) NULL }, |
| 121 | {NULL,0,NULL} |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | /* Strcmp (strcmp( s1, s2 ) == e_res) */ |
| 126 | struct t_strcmp { |
| 127 | char *s1; |
| 128 | char *s2; |
| 129 | int e_res; |
| 130 | } t_cmp[] = { |
| 131 | { "", "", 0 }, |
| 132 | { "", tiat, -((int) 'T') }, |
| 133 | { tiat, "", 'T' }, |
| 134 | { tiat, tiat, 0 }, |
| 135 | { yat, tiat, 'y'-'a' }, |
| 136 | {NULL,NULL,0} |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | /* Strcat (strcmp( strcat(s1, s2), s1s2 ) == e_res) */ |
| 141 | /* ASSUMES strcmp is working -- it is tested prior to strcat */ |
| 142 | struct t_strcat { |
| 143 | char *s1; |
| 144 | char *s2; |
| 145 | char *s1s2; |
| 146 | int e_res; |
| 147 | } t_cat[] = { |
| 148 | { dst0, "", "", 0 }, |
| 149 | { dst0, tiat, tiat, 0 }, |
| 150 | { dst0, "", tiat, 0 }, |
| 151 | { dst0, yat, tiatyat, 0 }, |
| 152 | { dst1, longstr,longstr, 0 }, |
| 153 | {NULL,NULL,NULL,0} |
| 154 | }; |
| 155 | |
| 156 | /* Strcpy (strcmp( strcpy(s1, s2), s1s2 ) == e_res) */ |
| 157 | /* ASSUMES strcmp is working -- it is tested prior to strcpy */ |
| 158 | /* No overlapping copies are tested */ |
| 159 | struct t_strcpy { |
| 160 | char *s1; |
| 161 | char *s2; |
| 162 | int e_res; |
| 163 | } t_cpy[] = { |
| 164 | { dst0, "", 0 }, |
| 165 | { dst0, tiat, 0 }, |
| 166 | { dst0, longstr, 0 }, |
| 167 | {NULL,NULL,0} |
| 168 | }; |
| 169 | |
| 170 | /* Strncmp (strncmp( s1, s2 ) == e_res) */ |
| 171 | struct t_strncmp { |
| 172 | char *s1; |
| 173 | char *s2; |
| 174 | int n; |
| 175 | int e_res; |
| 176 | } t_ncmp[] = { |
| 177 | { "", "", 0, 0 }, |
| 178 | { "", "", 80, 0 }, |
| 179 | { tiat, "", 0, 0 }, |
| 180 | { "", tiat, 80, -((int) 'T') }, |
| 181 | { tiat, "", 80, 'T' }, |
| 182 | { tiat, tiat, 80, 0 }, |
| 183 | { yat, tiat, 80, 'y'-'a' }, |
| 184 | { yat, tiat, 8, 0 }, |
| 185 | { yat, tiat, 9, 'y'-'a' }, |
| 186 | {NULL,NULL,0,0} |
| 187 | |
| 188 | }; |
| 189 | |
| 190 | /* Strncat (strcmp( strncat(s1, s2, n), s1ns2 ) == e_res) */ |
| 191 | /* ASSUMES strcmp is working -- it is tested prior to strncat */ |
| 192 | /* dest is guaranteed to be all '\0' s at start of test */ |
| 193 | struct t_strncat { |
| 194 | char *s1; |
| 195 | char *s2; |
| 196 | int n; |
| 197 | char *s1ns2; |
| 198 | int e_res; |
| 199 | } t_ncat[] = { |
| 200 | /* Regular strcat stuff -- i.e., n is large enough */ |
| 201 | { dst0, "", LONGSTR, "", 0 }, |
| 202 | { dst0, tiat, LONGSTR, tiat, 0 }, |
| 203 | { dst0, "", LONGSTR, tiat, 0 }, |
| 204 | { dst0, yat, LONGSTR, tiatyat,0 }, |
| 205 | { dst1, longstr,LONGSTR, longstr,0 }, |
| 206 | |
| 207 | /* Restricted strcat stuff */ |
| 208 | { dst2, longstr,0, "", 0 }, |
| 209 | { dst2, longstr,1, "t", 0 }, |
| 210 | { dst2, longstr,LONGSTR-1, longstr,0 }, |
| 211 | {NULL,NULL,0,NULL,0} |
| 212 | |
| 213 | }; |
| 214 | |
| 215 | |
| 216 | /* Strncpy (strcmp( strncpy(s1, s2), s1n ) == e_res) */ |
| 217 | /* ASSUMES strcmp is working -- it is tested prior to strncpy */ |
| 218 | /* No overlapping copies are tested */ |
| 219 | struct t_strncpy { |
| 220 | char *s1; |
| 221 | char *s2; |
| 222 | int n; |
| 223 | char *s1n; |
| 224 | int e_res; |
| 225 | } t_ncpy[] = { |
| 226 | /* Regular strcpy stuff -- i.e., n is large enough */ |
| 227 | { dst0, "", LONGSTR, "", 0 }, |
| 228 | { dst0, tiat, LONGSTR, tiat, 0 }, |
| 229 | { dst0, longstr,LONGSTR, longstr,0 }, |
| 230 | |
| 231 | /* Restricted strcpy stuff */ |
| 232 | { dst1, tiat, 0, "", 0 }, |
| 233 | { dst1, longstr,5, "ttttt",0 }, |
| 234 | {NULL,NULL,0,NULL,0} |
| 235 | }; |
| 236 | |
| 237 | |
| 238 | /***** LTP Port *****/ |
| 239 | void setup(); |
| 240 | int blenter(); |
| 241 | int blexit(); |
| 242 | int anyfail(); |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | void setup() |
| 248 | { |
| 249 | temp = stderr; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | int blenter() |
| 254 | { |
| 255 | local_flag = PASSED; |
| 256 | return(0); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | int blexit() |
| 261 | { |
plars | 70dfdc7 | 2003-07-08 18:38:20 +0000 | [diff] [blame^] | 262 | (local_flag == PASSED ) ? tst_resm(TPASS, "Test passed") : tst_resm(TFAIL, "Test failed"); |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 263 | return(0); |
| 264 | } |
| 265 | |
| 266 | int anyfail() |
| 267 | { |
| 268 | tst_exit(); |
| 269 | return(0); |
| 270 | } |
| 271 | |
| 272 | /***** ** ** *****/ |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | /*--------------------------------------------------------------*/ |
| 280 | |
| 281 | int main (argc, argv) |
| 282 | int argc; |
| 283 | char *argv[]; |
| 284 | |
| 285 | { |
| 286 | register int n, i; |
| 287 | char *s, *pr; |
| 288 | |
| 289 | /* |
| 290 | * Init longstr |
| 291 | */ |
| 292 | |
| 293 | s = longstr; |
| 294 | n = LONGSTR; |
| 295 | while ( n-- ) |
| 296 | *s++ = 't'; |
| 297 | *s = '\0'; |
| 298 | |
| 299 | setup(); |
| 300 | /*--------------------------------------------------------------*/ |
| 301 | |
| 302 | /* |
| 303 | * Index |
| 304 | */ |
| 305 | //fprintf(temp, "\tStrchr\n" ); |
| 306 | i = 0; |
| 307 | while ( t_index[i].s ) { |
| 308 | if ((pr = strchr( t_index[i].s, t_index[i].c )) != t_index[i].e_res){ |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 309 | fprintf(temp, "(Strchr) test %d", |
| 310 | i); |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 311 | local_flag = FAILED; |
| 312 | } |
| 313 | i++; |
| 314 | } |
| 315 | /* |
| 316 | * Strrchr |
| 317 | */ |
| 318 | //fprintf(temp, "\tStrrchr\n" ); |
| 319 | i = 0; |
| 320 | while ( t_rindex[i].s ) { |
| 321 | if ((pr = strrchr( t_rindex[i].s, t_rindex[i].c )) |
| 322 | != t_rindex[i].e_res) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 323 | fprintf(temp, "(Strrchr) test %d", |
| 324 | i); |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 325 | local_flag = FAILED; |
| 326 | } |
| 327 | i++; |
| 328 | } |
| 329 | /* |
| 330 | * Strlen |
| 331 | */ |
| 332 | //fprintf(temp, "\tStrlen\n" ); |
| 333 | i = 0; |
| 334 | while ( t_len[i].s ) { |
| 335 | if ((n = strlen( t_len[i].s )) != t_len[i].e_res) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 336 | fprintf(temp, "(Strlen) test %d: expected %d, got %d", |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 337 | i, t_len[i].e_res, n ); |
| 338 | local_flag = FAILED; |
| 339 | } |
| 340 | i++; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Strcmp |
| 345 | */ |
| 346 | //fprintf(temp, "\tStrcmp\n" ); |
| 347 | i = 0; |
| 348 | #define sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0)) |
| 349 | while ( t_cmp[i].s1 ) { |
| 350 | n = strcmp( t_cmp[i].s1, t_cmp[i].s2 ); |
| 351 | if (sign(n) != sign(t_cmp[i].e_res)) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 352 | fprintf(temp, "(Strcmp) test %d: expected %d, got %d", |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 353 | i, sign(t_cmp[i].e_res), sign(n) ); |
| 354 | local_flag = FAILED; |
| 355 | } |
| 356 | i++; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Strcat |
| 361 | */ |
| 362 | //fprintf(temp, "\tStrcat\n" ); |
| 363 | memset( dst0, 0, LONGSTR+1 ); /* clean slate */ |
| 364 | memset( dst1, 0, LONGSTR+1 ); /* clean slate */ |
| 365 | i = 0; |
| 366 | while ( t_cat[i].s1 ) { |
| 367 | if ((n = strcmp( strcat(t_cat[i].s1, t_cat[i].s2), t_cat[i].s1s2)) |
| 368 | != t_cat[i].e_res) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 369 | fprintf(temp, "(Strcat) test %d: expected %d, got %d", |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 370 | i, t_cat[i].e_res, n ); |
| 371 | local_flag = FAILED; |
| 372 | } |
| 373 | i++; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Strcpy |
| 378 | */ |
| 379 | //fprintf(temp, "\tStrcpy\n" ); |
| 380 | i = 0; |
| 381 | while ( t_cpy[i].s1 ) { |
| 382 | if ((n = strcmp( strcpy(t_cpy[i].s1, t_cpy[i].s2), t_cpy[i].s2)) |
| 383 | != t_cpy[i].e_res) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 384 | fprintf(temp, "(Strcpy) test %d: expected %d, got %d", |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 385 | i, t_cpy[i].e_res, n ); |
| 386 | local_flag = FAILED; |
| 387 | } |
| 388 | i++; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Strncat |
| 393 | */ |
| 394 | //fprintf(temp, "\tStrncat\n" ); |
| 395 | memset( dst0, 0, LONGSTR+1 ); /* clean slate */ |
| 396 | memset( dst1, 0, LONGSTR+1 ); /* clean slate */ |
| 397 | memset( dst2, 0, LONGSTR+1 ); /* clean slate */ |
| 398 | i = 0; |
| 399 | while ( t_ncat[i].s1 ) { |
| 400 | if ((n = strcmp( strncat(t_ncat[i].s1,t_ncat[i].s2,t_ncat[i].n), |
| 401 | t_ncat[i].s1ns2)) != t_ncat[i].e_res) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 402 | fprintf(temp, "(Strncat) test %d: expected %d, got %d", |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 403 | i, t_ncat[i].e_res, n ); |
| 404 | local_flag = FAILED; |
| 405 | } |
| 406 | i++; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* |
| 411 | * Strncmp |
| 412 | */ |
| 413 | //fprintf(temp, "\tStrncmp\n" ); |
| 414 | i = 0; |
| 415 | while ( t_ncmp[i].s1 ) { |
| 416 | if ((n = strncmp( t_ncmp[i].s1, t_ncmp[i].s2, t_ncmp[i].n )) |
| 417 | != t_ncmp[i].e_res) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 418 | fprintf(temp, "(Strncmp) test %d: expected %d, got %d", |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 419 | i, t_ncmp[i].e_res, n ); |
| 420 | local_flag = FAILED; |
| 421 | } |
| 422 | i++; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Strncpy |
| 427 | */ |
| 428 | //fprintf(temp, "\tStrncpy\n" ); |
| 429 | i = 0; |
| 430 | memset( dst0, 0, LONGSTR+1 ); /* clean slate */ |
| 431 | memset( dst1, 0, LONGSTR+1 ); /* clean slate */ |
| 432 | while ( t_ncpy[i].s1 ) { |
| 433 | if ((n = strcmp( strncpy(t_ncpy[i].s1, t_ncpy[i].s2, t_ncpy[i].n), |
| 434 | t_ncpy[i].s1n)) != t_ncpy[i].e_res) { |
robbiew | 4c53b8f | 2003-04-04 16:46:57 +0000 | [diff] [blame] | 435 | fprintf(temp, "(Strncpy) test %d: expected %d, got %d", |
robbiew | c58f59a | 2002-12-31 22:43:14 +0000 | [diff] [blame] | 436 | i, t_ncpy[i].e_res, n ); |
| 437 | local_flag = FAILED; |
| 438 | } |
| 439 | i++; |
| 440 | } |
| 441 | |
| 442 | blexit(); |
| 443 | anyfail(); |
| 444 | return(0); |
| 445 | } |