scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit b22f4577b97edea4a07fbcc0de1fac2e1732626b
parent 0b9f7df42d1d8871816be392295ef3a0ecafb8da
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 21 Sep 2018 15:54:47 +0100

[tests/libc] Add mem* tests

Diffstat:
Mlib/c/memcmp.c | 11++++++-----
Mlib/c/strncmp.c | 9+++------
Mtests/libc/execute/0027-strtok.c | 3+++
Mtests/libc/execute/0028-strxfrm.c | 1+
Atests/libc/execute/0029-memchr.c | 25+++++++++++++++++++++++++
Atests/libc/execute/0030-memcpy.c | 23+++++++++++++++++++++++
Atests/libc/execute/0031-memmove.c | 27+++++++++++++++++++++++++++
Atests/libc/execute/0032-memset.c | 30++++++++++++++++++++++++++++++
Atests/libc/execute/0033-memcmp.c | 27+++++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 5+++++
10 files changed, 150 insertions(+), 11 deletions(-)

diff --git a/lib/c/memcmp.c b/lib/c/memcmp.c @@ -4,10 +4,11 @@ int memcmp(const void *s1, const void *s2, size_t n) { - const unsigned char *s = (unsigned char *) s1; - const unsigned char *t = (unsigned char *) s2; + const char *s = s1; + const char *t = s2; - for ( ; n > 0 && *s++ == *t++; --n) - ; - return n ? (*s - *t) : 0; + for ( ; n > 0 && *s == *t; --n) + ++s, ++t; + + return (n > 0) ? *(unsigned char *) s - *(unsigned char *) t : 0; } diff --git a/lib/c/strncmp.c b/lib/c/strncmp.c @@ -6,12 +6,9 @@ strncmp(const char *s1, const char *s2, size_t n) { int c; + for ( ; n > 0 && (c = *s1) && c == *s2; --n) + ++s1, ++s2; if (n == 0) return 0; - while ((c = *s1) != '\0' && c == *s2) { - if (--n == 0) - return 0; - ++s1, ++s2; - } - return (*(unsigned char *) s1 - *(unsigned char *) s2); + return *(unsigned char *) s1 - *(unsigned char *) s2; } diff --git a/tests/libc/execute/0027-strtok.c b/tests/libc/execute/0027-strtok.c @@ -13,6 +13,8 @@ four test2 one three +test3 +one done end: */ @@ -52,6 +54,7 @@ main() puts("testing"); test("test1", "-+001--0002++3+-4"); test("test2", "001--+-+-+-3+-"); + test("test3", "001"); puts("done"); return 0; } diff --git a/tests/libc/execute/0028-strxfrm.c b/tests/libc/execute/0028-strxfrm.c @@ -19,6 +19,7 @@ main() assert(strxfrm(buf, "test", 40) == 4); assert(strxfrm(buf, "", 0) == 0); + assert(strxfrm(NULL, "abc", 0) > 0); puts("done"); diff --git a/tests/libc/execute/0029-memchr.c b/tests/libc/execute/0029-memchr.c @@ -0,0 +1,25 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char buf[] = {0, 1, 2, 3, 4, 160}; + + puts("testing"); + assert(memchr(buf, 2, 6) == buf+2); + assert(memchr(buf, 2, 0) == NULL); + assert(memchr(buf, 150, 6) == NULL); + assert(memchr(buf, 160, 6) == buf+5); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0030-memcpy.c b/tests/libc/execute/0030-memcpy.c @@ -0,0 +1,23 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char buf[40]; + + puts("testing"); + assert(!memcmp(memcpy(buf, "abc", 3), "abc", 3)); + assert(memcpy(buf, "abc", 0) == buf); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0031-memmove.c b/tests/libc/execute/0031-memmove.c @@ -0,0 +1,27 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char buf[30]; + + puts("testing"); + + memcpy(buf, "abcdef", 6); + assert(!memcmp(memmove(buf, buf+3, 3), "def", 3)); + memcpy(buf, "abcdef", 6); + assert(!memcmp(memmove(buf, buf+3, 0), "abc", 3)); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0032-memset.c b/tests/libc/execute/0032-memset.c @@ -0,0 +1,30 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char *p, buf[40]; + + puts("testing"); + + memset(buf, 2, sizeof(buf)); + for (p = buf; p < &buf[40]; ++p) + assert(*p == 2); + + memset(buf, 0, 0); + for (p = buf; p < &buf[40]; ++p) + assert(*p == 2); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0033-memcmp.c b/tests/libc/execute/0033-memcmp.c @@ -0,0 +1,27 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main(void) +{ + char buf[40] = {1, 2, 3, 4, 5}; + signed char buf2[40] = {-127}; + + puts("testing"); + assert(memcmp(buf, (char[]) {1, 2, 3, 4, 5}, 5) == 0); + assert(memcmp(buf, (char[]) {1, 1, 1, 1, 1}, 5) > 0); + assert(memcmp(buf, (char[]) {1, 3, 1, 1, 1}, 5) < 0); + assert(memcmp(buf, (char[]) {2, 3, 4, 5, 6}, 0) == 0); + assert(memcmp(buf2, (char[]) {-127}, 1) == 0); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -26,3 +26,8 @@ 0026-strstr 0027-strtok 0028-strxfrm +0029-memchr +0030-memcpy +0031-memmove +0032-memset +0033-memcmp