|
nsz |
73214b |
#define _BSD_SOURCE
|
|
nsz |
73214b |
#include <stdio.h>
|
|
nsz |
73214b |
#include <string.h>
|
|
nsz |
73214b |
#include "test.h"
|
|
nsz |
73214b |
|
|
nsz |
73214b |
/* r = place to store result
|
|
nsz |
73214b |
* f = function call to test (or any expression)
|
|
nsz |
73214b |
* x = expected result
|
|
nsz |
73214b |
* m = message to print on failure (with formats for r & x)
|
|
nsz |
73214b |
**/
|
|
nsz |
73214b |
|
|
nsz |
73214b |
#define TEST(r, f, x, m) ( \
|
|
nsz |
73214b |
((r) = (f)) == (x) || \
|
|
nsz |
73214b |
(error("%s failed (" m ")\n", #f, r, x), 0) )
|
|
nsz |
73214b |
|
|
nsz |
73214b |
#define TEST_S(s, x, m) ( \
|
|
nsz |
73214b |
!strcmp((s),(x)) || \
|
|
nsz |
73214b |
(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
|
|
nsz |
73214b |
|
|
nsz |
73214b |
void test_string(void) {
|
|
nsz |
73214b |
char b[32];
|
|
nsz |
73214b |
char *s;
|
|
nsz |
73214b |
int i;
|
|
nsz |
73214b |
|
|
nsz |
73214b |
b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0;
|
|
nsz |
73214b |
TEST(s, strcpy(b, b+16), b, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST_S(s, "abc", "strcpy gave incorrect string");
|
|
nsz |
73214b |
TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST_S(s, "abc", "strcpy gave incorrect string");
|
|
nsz |
73214b |
TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST_S(s, "abc", "strcpy gave incorrect string");
|
|
nsz |
73214b |
TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST_S(s, "abc", "strcpy gave incorrect string");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST_S(s, "bc", "strcpy gave incorrect string");
|
|
nsz |
73214b |
TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST_S(s, "c", "strcpy gave incorrect string");
|
|
nsz |
73214b |
TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST_S(s, "", "strcpy gave incorrect string");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p");
|
|
nsz |
73214b |
TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest");
|
|
nsz |
73214b |
TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
b[3] = 'x'; b[4] = 0;
|
|
nsz |
73214b |
strncpy(b, "abc", 3);
|
|
nsz |
73214b |
TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu");
|
|
nsz |
73214b |
TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n");
|
|
nsz |
73214b |
TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
strcpy(b, "abc");
|
|
nsz |
73214b |
TEST(s, strncat(b, "123456", 3), b, "%p != %p");
|
|
nsz |
73214b |
TEST(i, b[6], 0, "strncat failed to null-terminate (%d)");
|
|
nsz |
73214b |
TEST_S(s, "abc123", "strncat gave incorrect string");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
strcpy(b, "aaababccdd0001122223");
|
|
nsz |
73214b |
TEST(s, strchr(b, 'b'), b+3, "%p != %p");
|
|
nsz |
73214b |
TEST(s, strrchr(b, 'b'), b+5, "%p != %p");
|
|
nsz |
73214b |
TEST(i, strspn(b, "abcd"), 10, "%d != %d");
|
|
nsz |
73214b |
TEST(i, strcspn(b, "0123"), 10, "%d != %d");
|
|
nsz |
73214b |
TEST(s, strpbrk(b, "0123"), b+10, "%d != %d");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
strcpy(b, "abc 123; xyz; foo");
|
|
nsz |
73214b |
TEST(s, strtok(b, " "), b, "%p != %p");
|
|
nsz |
73214b |
TEST_S(s, "abc", "strtok result");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
TEST(s, strtok(NULL, ";"), b+4, "%p != %p");
|
|
nsz |
73214b |
TEST_S(s, " 123", "strtok result");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
TEST(s, strtok(NULL, " ;"), b+11, "%p != %p");
|
|
nsz |
73214b |
TEST_S(s, "xyz", "strtok result");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
TEST(s, strtok(NULL, " ;"), b+16, "%p != %p");
|
|
nsz |
73214b |
TEST_S(s, "foo", "strtok result");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
#ifdef HAVE_BSD_STRL
|
|
nsz |
73214b |
memset(b, 'x', sizeof b);
|
|
nsz |
73214b |
TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d");
|
|
nsz |
73214b |
TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)");
|
|
nsz |
73214b |
TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
memset(b, 'x', sizeof b);
|
|
nsz |
73214b |
TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d");
|
|
nsz |
73214b |
TEST(i, b[0], 'a', "strlcpy did not copy character %d");
|
|
nsz |
73214b |
TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
memset(b, 'x', sizeof b);
|
|
nsz |
73214b |
TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d");
|
|
nsz |
73214b |
TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
memcpy(b, "abc\0\0\0x\0", 8);
|
|
nsz |
73214b |
TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d");
|
|
nsz |
73214b |
TEST_S(b, "abc123", "strlcat result");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
memcpy(b, "abc\0\0\0x\0", 8);
|
|
nsz |
73214b |
TEST(i, strlcat(b, "123", 6), 6, "length %d != %d");
|
|
nsz |
73214b |
TEST_S(b, "abc12", "strlcat result");
|
|
nsz |
73214b |
TEST(i, b[6], 'x', "strlcat wrote past string %d != %d");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
memcpy(b, "abc\0\0\0x\0", 8);
|
|
nsz |
73214b |
TEST(i, strlcat(b, "123", 4), 6, "length %d != %d");
|
|
nsz |
73214b |
TEST_S(b, "abc", "strlcat result");
|
|
nsz |
73214b |
|
|
nsz |
73214b |
memcpy(b, "abc\0\0\0x\0", 8);
|
|
nsz |
73214b |
TEST(i, strlcat(b, "123", 3), 6, "length %d != %d");
|
|
nsz |
73214b |
TEST_S(b, "abc", "strlcat result");
|
|
nsz |
73214b |
#endif
|
|
nsz |
73214b |
}
|