1 /* Test program for string(3) routines. 2 * 3 * Slightly modified from Henry Spencer's original routine. 4 * - incorporates semantic changes per the ANSI standard (original tests 5 * can be recovered by defining the symbol NOT_ANSI while compiling, 6 * except for the change of memcpy() to memmove()). 7 * - makes additional tests of functions on unaligned buffers and strings. 8 */ 9 10 #include <sys/types.h> 11 #include <fcntl.h> 12 #include <string.h> 13 #include <stdlib.h> 14 #include <stdio.h> 15 16 17 #define STREQ(a, b) (strcmp((a), (b)) == 0) 18 19 char *it = "<UNSET>"; /* Routine name for message routines. */ 20 int waserror = 0; /* For exit status. */ 21 22 char uctest[] = "\004\203"; /* For testing signedness of chars. */ 23 int charsigned; /* Result. */ 24 25 int max_error = 2; 26 #include "common.h" 27 28 29 void check(int thing, int number); 30 void equal(char *a, char *b, int number); 31 int main(int argc, char *argv []); 32 void first(void); 33 void second(void); 34 35 /* 36 - check - complain if condition is not true 37 */ 38 void check(thing, number) 39 int thing; 40 int number; /* Test number for error message. */ 41 { 42 if (!thing) { 43 printf("%s flunked test %d\n", it, number); 44 waserror = 1; 45 errct++; 46 } 47 } 48 49 /* 50 - equal - complain if first two args don't strcmp as equal 51 */ 52 void equal(a, b, number) 53 char *a; 54 char *b; 55 int number; /* Test number for error message. */ 56 { 57 check(a != NULL && b != NULL && STREQ(a, b), number); 58 } 59 60 char one[50]; 61 char two[50]; 62 63 #ifdef UNIXERR 64 #define ERR 1 65 #endif 66 #ifdef BERKERR 67 #define ERR 1 68 #endif 69 #ifdef ERR 70 int f; 71 extern char *sys_errlist[]; 72 extern int sys_nerr; 73 #endif 74 75 /* ARGSUSED */ 76 int main(argc, argv) 77 int argc; 78 char *argv[]; 79 { 80 81 start(15); 82 83 /* First, establish whether chars are signed. */ 84 if (uctest[0] < uctest[1]) 85 charsigned = 0; 86 else 87 charsigned = 1; 88 89 /* Then, do the rest of the work. Split into two functions because 90 * some compilers get unhappy about a single immense function. */ 91 first(); 92 second(); 93 94 errct = waserror; 95 quit(); 96 return(-1); /* impossible */ 97 } 98 99 void first() 100 { 101 /* Test strcmp first because we use it to test other things. */ 102 it = "strcmp"; 103 check(strcmp("", "") == 0, 1);/* Trivial case. */ 104 check(strcmp("a", "a") == 0, 2); /* Identity. */ 105 check(strcmp("abc", "abc") == 0, 3); /* Multicharacter. */ 106 check(strcmp("abc", "abcd") < 0, 4); /* Length mismatches. */ 107 check(strcmp("abcd", "abc") > 0, 5); 108 check(strcmp("abcd", "abce") < 0, 6); /* Honest miscompares. */ 109 check(strcmp("abce", "abcd") > 0, 7); 110 check(strcmp("a\203", "a") > 0, 8); /* Tricky if char signed. */ 111 112 #ifdef NOT_ANSI 113 if (charsigned) 114 check(strcmp("a\203", "a\003") < 0, 9); 115 else 116 check(strcmp("a\203", "a\003") > 0, 9); 117 #else 118 check(strcmp("a\203", "a\003") > 0, 9); 119 #endif 120 121 check(strcmp("abcd" + 1, "abcd" + 1) == 0, 10); /* Unaligned tests. */ 122 check(strcmp("abcd" + 1, "abce" + 1) < 0, 11); 123 check(strcmp("abcd" + 1, "bcd") == 0, 12); 124 check(strcmp("abce" + 1, "bcd") > 0, 13); 125 check(strcmp("abcd" + 2, "bcd" + 1) == 0, 14); 126 check(strcmp("abcd" + 2, "bce" + 1) < 0, 15); 127 128 /* Test strcpy next because we need it to set up other tests. */ 129 it = "strcpy"; 130 check(strcpy(one, "abcd") == one, 1); /* Returned value. */ 131 equal(one, "abcd", 2); /* Basic test. */ 132 133 (void) strcpy(one, "x"); 134 equal(one, "x", 3); /* Writeover. */ 135 equal(one + 2, "cd", 4); /* Wrote too much? */ 136 137 (void) strcpy(two, "hi there"); 138 (void) strcpy(one, two); 139 equal(one, "hi there", 5); /* Basic test encore. */ 140 equal(two, "hi there", 6); /* Stomped on source? */ 141 142 (void) strcpy(one, ""); 143 equal(one, "", 7); /* Boundary condition. */ 144 145 (void) strcpy(one, "abcdef" + 1); /* Unaligned tests. */ 146 equal(one, "bcdef", 8); 147 (void) strcpy(one + 1, "*xy" + 1); 148 equal(one, "bxy", 9); 149 equal(one + 4, "f", 10); 150 151 /* Strcat */ 152 it = "strcat"; 153 (void) strcpy(one, "ijk"); 154 check(strcat(one, "lmn") == one, 1); /* Returned value. */ 155 equal(one, "ijklmn", 2); /* Basic test. */ 156 157 (void) strcpy(one, "x"); 158 (void) strcat(one, "yz"); 159 equal(one, "xyz", 3); /* Writeover. */ 160 equal(one + 4, "mn", 4); /* Wrote too much? */ 161 162 (void) strcpy(one, "gh"); 163 (void) strcpy(two, "ef"); 164 (void) strcat(one, two); 165 equal(one, "ghef", 5); /* Basic test encore. */ 166 equal(two, "ef", 6); /* Stomped on source? */ 167 168 (void) strcpy(one, ""); 169 (void) strcat(one, ""); 170 equal(one, "", 7); /* Boundary conditions. */ 171 (void) strcpy(one, "ab"); 172 (void) strcat(one, ""); 173 equal(one, "ab", 8); 174 (void) strcpy(one, ""); 175 (void) strcat(one, "cd"); 176 equal(one, "cd", 9); 177 178 /* Strncat - first test it as strcat, with big counts, then test the 179 * count mechanism. */ 180 it = "strncat"; 181 (void) strcpy(one, "ijk"); 182 check(strncat(one, "lmn", 99) == one, 1); /* Returned value. */ 183 equal(one, "ijklmn", 2); /* Basic test. */ 184 185 (void) strcpy(one, "x"); 186 (void) strncat(one, "yz", 99); 187 equal(one, "xyz", 3); /* Writeover. */ 188 equal(one + 4, "mn", 4); /* Wrote too much? */ 189 190 (void) strcpy(one, "gh"); 191 (void) strcpy(two, "ef"); 192 (void) strncat(one, two, 99); 193 equal(one, "ghef", 5); /* Basic test encore. */ 194 equal(two, "ef", 6); /* Stomped on source? */ 195 196 (void) strcpy(one, ""); 197 (void) strncat(one, "", 99); 198 equal(one, "", 7); /* Boundary conditions. */ 199 (void) strcpy(one, "ab"); 200 (void) strncat(one, "", 99); 201 equal(one, "ab", 8); 202 (void) strcpy(one, ""); 203 (void) strncat(one, "cd", 99); 204 equal(one, "cd", 9); 205 206 (void) strcpy(one, "ab"); 207 (void) strncat(one, "cdef", 2); 208 equal(one, "abcd", 10); /* Count-limited. */ 209 210 (void) strncat(one, "gh", 0); 211 equal(one, "abcd", 11); /* Zero count. */ 212 213 (void) strncat(one, "gh", 2); 214 equal(one, "abcdgh", 12); /* Count and length equal. */ 215 216 (void) strcpy(one, "abcdefghij"); /* Unaligned tests. */ 217 (void) strcpy(one, "abcd"); 218 (void) strcpy(one, "abc"); 219 (void) strncat(one, "de" + 1, 1); 220 equal(one, "abce", 13); 221 equal(one + 4, "", 14); 222 equal(one + 5, "fghij", 15); 223 224 /* Strncmp - first test as strcmp with big counts, then test count code. */ 225 it = "strncmp"; 226 check(strncmp("", "", 99) == 0, 1); /* Trivial case. */ 227 check(strncmp("a", "a", 99) == 0, 2); /* Identity. */ 228 check(strncmp("abc", "abc", 99) == 0, 3); /* Multicharacter. */ 229 check(strncmp("abc", "abcd", 99) < 0, 4); /* Length unequal. */ 230 check(strncmp("abcd", "abc", 99) > 0, 5); 231 check(strncmp("abcd", "abce", 99) < 0, 6); /* Honestly unequal. */ 232 check(strncmp("abce", "abcd", 99) > 0, 7); 233 check(strncmp("a\203", "a", 2) > 0, 8); /* Tricky if '\203' < 0 */ 234 235 #ifdef NOT_ANSI 236 if (charsigned) 237 check(strncmp("a\203", "a\003", 2) < 0, 9); 238 else 239 check(strncmp("a\203", "a\003", 2) > 0, 9); 240 #else 241 check(strncmp("a\203", "a\003", 2) > 0, 9); 242 #endif 243 244 check(strncmp("abce", "abcd", 3) == 0, 10); /* Count limited. */ 245 check(strncmp("abce", "abc", 3) == 0, 11); /* Count == length. */ 246 check(strncmp("abcd", "abce", 4) < 0, 12); /* Nudging limit. */ 247 check(strncmp("abc", "def", 0) == 0, 13); /* Zero count. */ 248 249 /* Strncpy - testing is a bit different because of odd semantics */ 250 it = "strncpy"; 251 check(strncpy(one, "abc", 4) == one, 1); /* Returned value. */ 252 equal(one, "abc", 2); /* Did the copy go right? */ 253 254 (void) strcpy(one, "abcdefgh"); 255 (void) strncpy(one, "xyz", 2); 256 equal(one, "xycdefgh", 3); /* Copy cut by count. */ 257 258 (void) strcpy(one, "abcdefgh"); 259 (void) strncpy(one, "xyz", 3);/* Copy cut just before NUL. */ 260 equal(one, "xyzdefgh", 4); 261 262 (void) strcpy(one, "abcdefgh"); 263 (void) strncpy(one, "xyz", 4);/* Copy just includes NUL. */ 264 equal(one, "xyz", 5); 265 equal(one + 4, "efgh", 6); /* Wrote too much? */ 266 267 (void) strcpy(one, "abcdefgh"); 268 (void) strncpy(one, "xyz", 5);/* Copy includes padding. */ 269 equal(one, "xyz", 7); 270 equal(one + 4, "", 8); 271 equal(one + 5, "fgh", 9); 272 273 (void) strcpy(one, "abc"); 274 (void) strncpy(one, "xyz", 0);/* Zero-length copy. */ 275 equal(one, "abc", 10); 276 277 (void) strncpy(one, "", 2); /* Zero-length source. */ 278 equal(one, "", 11); 279 equal(one + 1, "", 12); 280 equal(one + 2, "c", 13); 281 282 (void) strcpy(one, "hi there"); 283 (void) strncpy(two, one, 9); 284 equal(two, "hi there", 14); /* Just paranoia. */ 285 equal(one, "hi there", 15); /* Stomped on source? */ 286 287 /* Strlen */ 288 it = "strlen"; 289 check(strlen("") == 0, 1); /* Empty. */ 290 check(strlen("a") == 1, 2); /* Single char. */ 291 check(strlen("abcd") == 4, 3);/* Multiple chars. */ 292 check(strlen("abcd" + 1) == 3, 4); /* Unaligned test. */ 293 294 /* Strchr */ 295 it = "strchr"; 296 check(strchr("abcd", 'z') == NULL, 1); /* Not found. */ 297 (void) strcpy(one, "abcd"); 298 check(strchr(one, 'c') == one + 2, 2); /* Basic test. */ 299 check(strchr(one, 'd') == one + 3, 3); /* End of string. */ 300 check(strchr(one, 'a') == one, 4); /* Beginning. */ 301 check(strchr(one, '\0') == one + 4, 5); /* Finding NUL. */ 302 (void) strcpy(one, "ababa"); 303 check(strchr(one, 'b') == one + 1, 6); /* Finding first. */ 304 (void) strcpy(one, ""); 305 check(strchr(one, 'b') == NULL, 7); /* Empty string. */ 306 check(strchr(one, '\0') == one, 8); /* NUL in empty string. */ 307 308 /* Index - just like strchr */ 309 it = "index"; 310 check(index("abcd", 'z') == NULL, 1); /* Not found. */ 311 (void) strcpy(one, "abcd"); 312 check(index(one, 'c') == one + 2, 2); /* Basic test. */ 313 check(index(one, 'd') == one + 3, 3); /* End of string. */ 314 check(index(one, 'a') == one, 4); /* Beginning. */ 315 check(index(one, '\0') == one + 4, 5); /* Finding NUL. */ 316 (void) strcpy(one, "ababa"); 317 check(index(one, 'b') == one + 1, 6); /* Finding first. */ 318 (void) strcpy(one, ""); 319 check(index(one, 'b') == NULL, 7); /* Empty string. */ 320 check(index(one, '\0') == one, 8); /* NUL in empty string. */ 321 322 /* Strrchr */ 323 it = "strrchr"; 324 check(strrchr("abcd", 'z') == NULL, 1); /* Not found. */ 325 (void) strcpy(one, "abcd"); 326 check(strrchr(one, 'c') == one + 2, 2); /* Basic test. */ 327 check(strrchr(one, 'd') == one + 3, 3); /* End of string. */ 328 check(strrchr(one, 'a') == one, 4); /* Beginning. */ 329 check(strrchr(one, '\0') == one + 4, 5); /* Finding NUL. */ 330 (void) strcpy(one, "ababa"); 331 check(strrchr(one, 'b') == one + 3, 6); /* Finding last. */ 332 (void) strcpy(one, ""); 333 check(strrchr(one, 'b') == NULL, 7); /* Empty string. */ 334 check(strrchr(one, '\0') == one, 8); /* NUL in empty string. */ 335 336 /* Rindex - just like strrchr */ 337 it = "rindex"; 338 check(rindex("abcd", 'z') == NULL, 1); /* Not found. */ 339 (void) strcpy(one, "abcd"); 340 check(rindex(one, 'c') == one + 2, 2); /* Basic test. */ 341 check(rindex(one, 'd') == one + 3, 3); /* End of string. */ 342 check(rindex(one, 'a') == one, 4); /* Beginning. */ 343 check(rindex(one, '\0') == one + 4, 5); /* Finding NUL. */ 344 (void) strcpy(one, "ababa"); 345 check(rindex(one, 'b') == one + 3, 6); /* Finding last. */ 346 (void) strcpy(one, ""); 347 check(rindex(one, 'b') == NULL, 7); /* Empty string. */ 348 check(rindex(one, '\0') == one, 8); /* NUL in empty string. */ 349 } 350 351 void second() 352 { 353 /* Strpbrk - somewhat like strchr */ 354 it = "strpbrk"; 355 check(strpbrk("abcd", "z") == NULL, 1); /* Not found. */ 356 (void) strcpy(one, "abcd"); 357 check(strpbrk(one, "c") == one + 2, 2); /* Basic test. */ 358 check(strpbrk(one, "d") == one + 3, 3); /* End of string. */ 359 check(strpbrk(one, "a") == one, 4); /* Beginning. */ 360 check(strpbrk(one, "") == NULL, 5); /* Empty search list. */ 361 check(strpbrk(one, "cb") == one + 1, 6); /* Multiple search. */ 362 (void) strcpy(one, "abcabdea"); 363 check(strpbrk(one, "b") == one + 1, 7); /* Finding first. */ 364 check(strpbrk(one, "cb") == one + 1, 8); /* With multiple search. */ 365 check(strpbrk(one, "db") == one + 1, 9); /* Another variant. */ 366 (void) strcpy(one, ""); 367 check(strpbrk(one, "bc") == NULL, 10); /* Empty string. */ 368 check(strpbrk(one, "") == NULL, 11); /* Both strings empty. */ 369 370 /* Strstr - somewhat like strchr */ 371 it = "strstr"; 372 check(strstr("abcd", "z") == NULL, 1); /* Not found. */ 373 check(strstr("abcd", "abx") == NULL, 2); /* Dead end. */ 374 (void) strcpy(one, "abcd"); 375 check(strstr(one, "c") == one + 2, 3); /* Basic test. */ 376 check(strstr(one, "bc") == one + 1, 4); /* Multichar. */ 377 check(strstr(one, "d") == one + 3, 5); /* End of string. */ 378 check(strstr(one, "cd") == one + 2, 6); /* Tail of string. */ 379 check(strstr(one, "abc") == one, 7); /* Beginning. */ 380 check(strstr(one, "abcd") == one, 8); /* Exact match. */ 381 check(strstr(one, "abcde") == NULL, 9); /* Too long. */ 382 check(strstr(one, "de") == NULL, 10); /* Past end. */ 383 #ifdef NOT_ANSI 384 check(strstr(one, "") == one + 4, 11); /* Finding empty. */ 385 #else 386 check(strstr(one, "") == one, 11); /* Finding empty. */ 387 #endif 388 (void) strcpy(one, "ababa"); 389 check(strstr(one, "ba") == one + 1, 12); /* Finding first. */ 390 (void) strcpy(one, ""); 391 check(strstr(one, "b") == NULL, 13); /* Empty string. */ 392 check(strstr(one, "") == one, 14); /* Empty in empty string. */ 393 (void) strcpy(one, "bcbca"); 394 check(strstr(one, "bca") == one + 2, 15); /* False start. */ 395 (void) strcpy(one, "bbbcabbca"); 396 check(strstr(one, "bbca") == one + 1, 16); /* With overlap. */ 397 398 /* Strspn */ 399 it = "strspn"; 400 check(strspn("abcba", "abc") == 5, 1); /* Whole string. */ 401 check(strspn("abcba", "ab") == 2, 2); /* Partial. */ 402 check(strspn("abc", "qx") == 0, 3); /* None. */ 403 check(strspn("", "ab") == 0, 4); /* Null string. */ 404 check(strspn("abc", "") == 0, 5); /* Null search list. */ 405 406 /* Strcspn */ 407 it = "strcspn"; 408 check(strcspn("abcba", "qx") == 5, 1); /* Whole string. */ 409 check(strcspn("abcba", "cx") == 2, 2); /* Partial. */ 410 check(strcspn("abc", "abc") == 0, 3); /* None. */ 411 check(strcspn("", "ab") == 0, 4); /* Null string. */ 412 check(strcspn("abc", "") == 3, 5); /* Null search list. */ 413 414 /* Strtok - the hard one */ 415 it = "strtok"; 416 (void) strcpy(one, "first, second, third"); 417 equal(strtok(one, ", "), "first", 1); /* Basic test. */ 418 equal(one, "first", 2); 419 equal(strtok((char *) NULL, ", "), "second", 3); 420 equal(strtok((char *) NULL, ", "), "third", 4); 421 check(strtok((char *) NULL, ", ") == NULL, 5); 422 (void) strcpy(one, ", first, "); 423 equal(strtok(one, ", "), "first", 6); /* Extra delims, 1 tok. */ 424 check(strtok((char *) NULL, ", ") == NULL, 7); 425 (void) strcpy(one, "1a, 1b; 2a, 2b"); 426 equal(strtok(one, ", "), "1a", 8); /* Changing delim lists. */ 427 equal(strtok((char *) NULL, "; "), "1b", 9); 428 equal(strtok((char *) NULL, ", "), "2a", 10); 429 (void) strcpy(two, "x-y"); 430 equal(strtok(two, "-"), "x", 11); /* New string before done. */ 431 equal(strtok((char *) NULL, "-"), "y", 12); 432 check(strtok((char *) NULL, "-") == NULL, 13); 433 (void) strcpy(one, "a,b, c,, ,d"); 434 equal(strtok(one, ", "), "a", 14); /* Different separators. */ 435 equal(strtok((char *) NULL, ", "), "b", 15); 436 equal(strtok((char *) NULL, " ,"), "c", 16); /* Permute list too. */ 437 equal(strtok((char *) NULL, " ,"), "d", 17); 438 check(strtok((char *) NULL, ", ") == NULL, 18); 439 check(strtok((char *) NULL, ", ") == NULL, 19); /* Persistence. */ 440 (void) strcpy(one, ", "); 441 check(strtok(one, ", ") == NULL, 20); /* No tokens. */ 442 (void) strcpy(one, ""); 443 check(strtok(one, ", ") == NULL, 21); /* Empty string. */ 444 (void) strcpy(one, "abc"); 445 equal(strtok(one, ", "), "abc", 22); /* No delimiters. */ 446 check(strtok((char *) NULL, ", ") == NULL, 23); 447 (void) strcpy(one, "abc"); 448 equal(strtok(one, ""), "abc", 24); /* Empty delimiter list. */ 449 check(strtok((char *) NULL, "") == NULL, 25); 450 (void) strcpy(one, "abcdefgh"); 451 (void) strcpy(one, "a,b,c"); 452 equal(strtok(one, ","), "a", 26); /* Basics again... */ 453 equal(strtok((char *) NULL, ","), "b", 27); 454 equal(strtok((char *) NULL, ","), "c", 28); 455 check(strtok((char *) NULL, ",") == NULL, 29); 456 equal(one + 6, "gh", 30); /* Stomped past end? */ 457 equal(one, "a", 31); /* Stomped old tokens? */ 458 equal(one + 2, "b", 32); 459 equal(one + 4, "c", 33); 460 461 /* Memcmp */ 462 it = "memcmp"; 463 check(memcmp("a", "a", 1) == 0, 1); /* Identity. */ 464 check(memcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */ 465 check(memcmp("abcd", "abce", 4) < 0, 3); /* Honestly unequal. */ 466 check(memcmp("abce", "abcd", 4) > 0, 4); 467 check(memcmp("alph", "beta", 4) < 0, 5); 468 469 #ifdef NOT_ANSI 470 if (charsigned) 471 check(memcmp("a\203", "a\003", 2) < 0, 6); 472 else 473 check(memcmp("a\203", "a\003", 2) > 0, 6); 474 #else 475 check(memcmp("a\203", "a\003", 2) > 0, 6); 476 #endif 477 478 check(memcmp("abce", "abcd", 3) == 0, 7); /* Count limited. */ 479 check(memcmp("abc", "def", 0) == 0, 8); /* Zero count. */ 480 481 check(memcmp("a" + 1, "a" + 1, 1) == 0, 9); /* Unaligned tests. */ 482 check(memcmp("abc" + 1, "bc", 2) == 0, 10); 483 check(memcmp("bc", "abc" + 1, 2) == 0, 11); 484 check(memcmp("abcd" + 1, "abce" + 1, 3) < 0, 12); 485 check(memcmp("abce" + 1, "abcd" + 1, 3) > 0, 13); 486 /* 487 check(memcmp("a\203" + 1, "a\003" + 1, 1) > 0, 14); 488 */ 489 check(memcmp("abcde" + 1, "abcdf" + 1, 3) == 0, 15); 490 491 /* Memchr */ 492 it = "memchr"; 493 check(memchr("abcd", 'z', 4) == NULL, 1); /* Not found. */ 494 (void) strcpy(one, "abcd"); 495 check( (char *)memchr(one, 'c', 4) == one + 2, 2); /* Basic test. */ 496 check( (char *)memchr(one, 'd', 4) == one + 3, 3); /* End of string. */ 497 check( (char *)memchr(one, 'a', 4) == one, 4); /* Beginning. */ 498 check( (char *)memchr(one, '\0', 5) == one + 4, 5); /* Finding NUL. */ 499 (void) strcpy(one, "ababa"); 500 check( (char *)memchr(one, 'b', 5) == one + 1, 6); /* Finding first. */ 501 check( (char *)memchr(one, 'b', 0) == NULL, 7); /* Zero count. */ 502 check( (char *)memchr(one, 'a', 1) == one, 8); /* Singleton case. */ 503 (void) strcpy(one, "a\203b"); 504 check( (char *)memchr(one, 0203, 3) == one + 1, 9); /* Unsignedness. */ 505 506 /* Memcpy 507 * Note that X3J11 says memcpy may fail on overlap. */ 508 it = "memcpy"; 509 check( (char *)memcpy(one, "abc", 4) == one, 1); /* Returned value. */ 510 equal(one, "abc", 2); /* Did the copy go right? */ 511 512 (void) strcpy(one, "abcdefgh"); 513 (void) memcpy(one + 1, "xyz", 2); 514 equal(one, "axydefgh", 3); /* Basic test. */ 515 516 (void) strcpy(one, "abc"); 517 (void) memcpy(one, "xyz", 0); 518 equal(one, "abc", 4); /* Zero-length copy. */ 519 520 (void) strcpy(one, "hi there"); 521 (void) strcpy(two, "foo"); 522 (void) memcpy(two, one, 9); 523 equal(two, "hi there", 5); /* Just paranoia. */ 524 equal(one, "hi there", 6); /* Stomped on source? */ 525 526 (void) strcpy(one, "abcde"); /* Unaligned tests. */ 527 (void) memcpy(one + 1, "\0\0\0\0\0", 1); 528 equal(one, "a", 7); 529 equal(one + 2, "cde", 8); 530 (void) memcpy(one + 1, "xyz" + 1, 2); 531 equal(one, "ayzde", 9); 532 (void) memcpy(one + 1, "xyz" + 1, 3); 533 equal(one, "ayz", 10); 534 535 /* Memmove 536 * Note that X3J11 says memmove must work regardless of overlap. */ 537 it = "memmove"; 538 check( (char *)memmove(one, "abc", 4) == one, 1); /* Returned value. */ 539 equal(one, "abc", 2); /* Did the copy go right? */ 540 541 (void) strcpy(one, "abcdefgh"); 542 (void) memmove(one + 1, "xyz", 2); 543 equal(one, "axydefgh", 3); /* Basic test. */ 544 545 (void) strcpy(one, "abc"); 546 (void) memmove(one, "xyz", 0); 547 equal(one, "abc", 4); /* Zero-length copy. */ 548 549 (void) strcpy(one, "hi there"); 550 (void) strcpy(two, "foo"); 551 (void) memmove(two, one, 9); 552 equal(two, "hi there", 5); /* Just paranoia. */ 553 equal(one, "hi there", 6); /* Stomped on source? */ 554 555 (void) strcpy(one, "abcdefgh"); 556 (void) memmove(one + 1, one, 9); 557 equal(one, "aabcdefgh", 7); /* Overlap, right-to-left. */ 558 559 (void) strcpy(one, "abcdefgh"); 560 (void) memmove(one + 1, one + 2, 7); 561 equal(one, "acdefgh", 8); /* Overlap, left-to-right. */ 562 563 (void) strcpy(one, "abcdefgh"); 564 (void) memmove(one, one, 9); 565 equal(one, "abcdefgh", 9); /* 100% overlap. */ 566 567 (void) strcpy(one, "abcde"); /* Unaligned tests. */ 568 (void) memmove(one + 1, "\0\0\0\0\0", 1); 569 equal(one, "a", 10); 570 equal(one + 2, "cde", 11); 571 (void) memmove(one + 1, "xyz" + 1, 2); 572 equal(one, "ayzde", 12); 573 (void) memmove(one + 1, "xyz" + 1, 3); 574 equal(one, "ayz", 13); 575 (void) strcpy(one, "abcdefgh"); 576 (void) memmove(one + 2, one + 1, 8); 577 equal(one, "abbcdefgh", 14); 578 579 /* Memccpy - first test like memcpy, then the search part 580 * The SVID, the only place where memccpy is mentioned, says overlap 581 * might fail, so we don't try it. Besides, it's hard to see the 582 * rationale for a non-left-to-right memccpy. */ 583 it = "memccpy"; 584 check(memccpy(one, "abc", 'q', 4) == NULL, 1); /* Returned value. */ 585 equal(one, "abc", 2); /* Did the copy go right? */ 586 587 (void) strcpy(one, "abcdefgh"); 588 (void) memccpy(one + 1, "xyz", 'q', 2); 589 equal(one, "axydefgh", 3); /* Basic test. */ 590 591 (void) strcpy(one, "abc"); 592 (void) memccpy(one, "xyz", 'q', 0); 593 equal(one, "abc", 4); /* Zero-length copy. */ 594 595 (void) strcpy(one, "hi there"); 596 (void) strcpy(two, "foo"); 597 (void) memccpy(two, one, 'q', 9); 598 equal(two, "hi there", 5); /* Just paranoia. */ 599 equal(one, "hi there", 6); /* Stomped on source? */ 600 601 (void) strcpy(one, "abcdefgh"); 602 (void) strcpy(two, "horsefeathers"); 603 check( (char *)memccpy(two, one, 'f', 9) == two + 6, 7);/* Returned value. */ 604 equal(one, "abcdefgh", 8); /* Source intact? */ 605 equal(two, "abcdefeathers", 9); /* Copy correct? */ 606 607 (void) strcpy(one, "abcd"); 608 (void) strcpy(two, "bumblebee"); 609 check((char *)memccpy(two, one, 'a', 4) == two + 1, 10); /* First char. */ 610 equal(two, "aumblebee", 11); 611 check((char *)memccpy(two, one, 'd', 4) == two + 4, 12); /* Last char. */ 612 equal(two, "abcdlebee", 13); 613 (void) strcpy(one, "xyz"); 614 check((char *)memccpy(two, one, 'x', 1) == two + 1, 14); /* Singleton. */ 615 equal(two, "xbcdlebee", 15); 616 617 /* Memset */ 618 it = "memset"; 619 (void) strcpy(one, "abcdefgh"); 620 check( (char *) memset(one + 1, 'x', 3) == one + 1, 1); /* Return value. */ 621 equal(one, "axxxefgh", 2); /* Basic test. */ 622 623 (void) memset(one + 2, 'y', 0); 624 equal(one, "axxxefgh", 3); /* Zero-length set. */ 625 626 (void) memset(one + 5, 0, 1); 627 equal(one, "axxxe", 4); /* Zero fill. */ 628 equal(one + 6, "gh", 5); /* And the leftover. */ 629 630 (void) memset(one + 2, 010045, 1); 631 equal(one, "ax\045xe", 6); /* Unsigned char convert. */ 632 633 /* Bcopy - much like memcpy 634 * Berklix manual is silent about overlap, so don't test it. */ 635 it = "bcopy"; 636 (void) bcopy("abc", one, 4); 637 equal(one, "abc", 1); /* Simple copy. */ 638 639 (void) strcpy(one, "abcdefgh"); 640 (void) bcopy("xyz", one + 1, 2); 641 equal(one, "axydefgh", 2); /* Basic test. */ 642 643 (void) strcpy(one, "abc"); 644 (void) bcopy("xyz", one, 0); 645 equal(one, "abc", 3); /* Zero-length copy. */ 646 647 (void) strcpy(one, "hi there"); 648 (void) strcpy(two, "foo"); 649 (void) bcopy(one, two, 9); 650 equal(two, "hi there", 4); /* Just paranoia. */ 651 equal(one, "hi there", 5); /* Stomped on source? */ 652 653 /* Bzero */ 654 it = "bzero"; 655 (void) strcpy(one, "abcdef"); 656 bzero(one + 2, 2); 657 equal(one, "ab", 1); /* Basic test. */ 658 equal(one + 3, "", 2); 659 equal(one + 4, "ef", 3); 660 661 (void) strcpy(one, "abcdef"); 662 bzero(one + 2, 0); 663 equal(one, "abcdef", 4); /* Zero-length copy. */ 664 665 /* Bcmp - somewhat like memcmp */ 666 it = "bcmp"; 667 check(bcmp("a", "a", 1) == 0, 1); /* Identity. */ 668 check(bcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */ 669 check(bcmp("abcd", "abce", 4) != 0, 3); /* Honestly unequal. */ 670 check(bcmp("abce", "abcd", 4) != 0, 4); 671 check(bcmp("alph", "beta", 4) != 0, 5); 672 check(bcmp("abce", "abcd", 3) == 0, 6); /* Count limited. */ 673 check(bcmp("abc", "def", 0) == 0, 8); /* Zero count. */ 674 675 #ifdef ERR 676 /* Strerror - VERY system-dependent */ 677 it = "strerror"; 678 f = open("/", O_WRONLY); /* Should always fail. */ 679 check(f < 0 && errno > 0 && errno < sys_nerr, 1); 680 equal(strerror(errno), sys_errlist[errno], 2); 681 #ifdef UNIXERR 682 equal(strerror(errno), "Is a directory", 3); 683 #endif 684 #ifdef BERKERR 685 equal(strerror(errno), "Permission denied", 3); 686 #endif 687 #endif 688 } 689 690