From 513e6b8a88df7b6f62e379886987041fa626ed34 Mon Sep 17 00:00:00 2001 From: Stewart Smith Date: Wed, 24 Jun 2015 13:59:42 +1000 Subject: add tests for libc memmove Signed-off-by: Stewart Smith --- libc/test/run-memops-test.c | 8 ++++++++ libc/test/run-memops.c | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'libc') diff --git a/libc/test/run-memops-test.c b/libc/test/run-memops-test.c index a4179eb..cb2d7f8 100644 --- a/libc/test/run-memops-test.c +++ b/libc/test/run-memops-test.c @@ -50,6 +50,7 @@ int test_strcmp(const void *ptr1, const void *ptr2, int expected); int test_strchr(const char *s, int c, char *expected); int test_strcasecmp(const char *s1, const char *s2, int expected); int test_strncasecmp(const char *s1, const char *s2, size_t n, int expected); +int test_memmove(void *dest, const void *src, size_t n, const void *r, const void *expected, size_t expected_n); int test_memset(char* buf, int c, size_t s) { @@ -93,3 +94,10 @@ int test_strncasecmp(const char *s1, const char *s2, size_t n, int expected) { return(expected == strncasecmp(s1, s2, n)); } + +int test_memmove(void *dest, const void *src, size_t n, const void *r, const void *expected, size_t expected_n) +{ + if (memmove(dest, src, n) != dest) + return -1; + return(memcmp(r, expected, expected_n) == 0); +} diff --git a/libc/test/run-memops.c b/libc/test/run-memops.c index 21f6ab6..5c425f2 100644 --- a/libc/test/run-memops.c +++ b/libc/test/run-memops.c @@ -28,10 +28,12 @@ int test_strcmp(const void *ptr1, const void *ptr2, int expected); int test_strchr(const char *s, int c, char *expected); int test_strcasecmp(const char *s1, const char *s2, int expected); int test_strncasecmp(const char *s1, const char *s2, size_t n, int expected); +int test_memmove(void *dest, const void *src, size_t n, const void *r, const void *expected, size_t expected_n); int main(void) { - char* buf; + char *buf; + char *buf2; buf = malloc(100); assert(test_memset(buf, 0x42, 100) == 0); @@ -77,9 +79,24 @@ int main(void) assert(test_strncasecmp(buf, "HeLLo WOrlc!", 2, 0)); assert(test_strncasecmp(buf, "HeLLp WOrlc!", 5, -1)); - free(buf); + buf = malloc(20); + buf2 = malloc(20); + strncpy(buf, "Hello", 20); + strncpy(buf2, " World!", 20); + + assert(test_memmove(buf + 5, buf2, strlen(buf2), buf, + "Hello World!", strlen("Hello World!"))); + + strncpy(buf, "HHello World!", 20); + assert(test_memmove(buf, buf+1, strlen("Hello World!"), buf, "Hello World!", strlen("Hello World!"))); + + strncpy(buf, "0123456789", 20); + assert(test_memmove(buf+1, buf , strlen("0123456789"), buf, "00123456789", strlen("00123456789"))); + + free(buf); + free(buf2); return 0; } -- cgit v1.1