From 3d6482b0949159c33d6ac2972a69868bdda766f2 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 12 Jun 2022 20:13:23 +0200 Subject: Move some test code to newline created .c files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- strnccpy.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'strnccpy.c') diff --git a/strnccpy.c b/strnccpy.c index 3d91c08..176c8a0 100644 --- a/strnccpy.c +++ b/strnccpy.c @@ -12,6 +12,76 @@ extern inline char *libsimple_strnccpy(char *restrict, const char *restrict, int int main(void) { + char buf[1024]; + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", '\0', 1024) == &buf[6]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 1024) == &buf[5]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'l', 1024) == &buf[3]); + assert(!strcmp(buf, "hel")); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'x', 1024) == NULL); + assert(!strcmp(buf, "hello")); + + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", '\0', 6) == &buf[6]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 6) == &buf[5]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'l', 6) == &buf[3]); + assert(!strcmp(buf, "hel")); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'x', 6) == NULL); + assert(!strcmp(buf, "hello")); + + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", '\0', 5) == NULL); + assert(!strncmp(buf, "hellox", 6)); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 5) == &buf[5]); + assert(!strncmp(buf, "hellox", 6)); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'l', 5) == &buf[3]); + assert(!strcmp(buf, "hel")); + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'x', 5) == NULL); + assert(!strncmp(buf, "hellox", 6)); + + + memset(buf, 'x', sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 3) == NULL); + assert(!strncmp(buf, "helx", 4)); + return 0; } -- cgit v1.2.3-70-g09d2