diff options
author | Mattias Andrée <maandree@kth.se> | 2018-11-23 20:38:54 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-11-23 20:38:54 +0100 |
commit | b5ca6729c7b52e8cade6d3808ad8280845ac32ea (patch) | |
tree | 0ddac006e8a6bad962c7c981449ba43fb4bc195f /memcmove.c | |
parent | Add a bunch of function and macros (diff) | |
download | libsimple-b5ca6729c7b52e8cade6d3808ad8280845ac32ea.tar.gz libsimple-b5ca6729c7b52e8cade6d3808ad8280845ac32ea.tar.bz2 libsimple-b5ca6729c7b52e8cade6d3808ad8280845ac32ea.tar.xz |
Some fixes and tests
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'memcmove.c')
-rw-r--r-- | memcmove.c | 53 |
1 files changed, 51 insertions, 2 deletions
@@ -4,11 +4,11 @@ void * -libsimple_memcmove(void *d_, const void *s_, int c_, size_t n) /* TODO test, man */ +libsimple_memcmove(void *d_, const void *s_, int c_, size_t n) /* TODO man */ { char *d = d_, c = (char)c_, *p; const char *s = s_; - if (d < s) { + if (d <= s) { for (; n; n--, s++) if ((*d++ = *s) == c) return d; @@ -36,6 +36,55 @@ libsimple_memcmove(void *d_, const void *s_, int c_, size_t n) /* TODO test, man int main(void) { + char buf[1024]; + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[5], &buf[5], 'o', 5) == &buf[5 + 5]); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-';; + assert(libsimple_memcmove(&buf[5], &buf[5], 'l', 5) == &buf[5 + 3]); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[5], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "-----hello-", 11)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[3], &buf[5], 'o', 5) == &buf[3 + 5]); + assert(!strncmp(buf, "---hellolo-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[3], &buf[5], 'l', 5) == &buf[3 + 3]); + assert(!strncmp(buf, "---helello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[3], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "---hellolo-", 11)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[8], &buf[5], 'o', 5) == &buf[8 + 5]); + assert(!strncmp(buf, "-----helhello-", 14)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[8], &buf[5], 'l', 5) == &buf[8 + 3]); + assert(!strncmp(buf, "-----helhel-", 12)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[8], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "-----helhello-", 14)); + return 0; } |