scc

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

commit 75b05d85411ef3d37ef3ab460f1e12c2960cb6a8
parent f02ae1eb9ac2ebb1f12566363df5182e91d3b11b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 16 Sep 2018 06:40:08 +0100

[tests/libc] Add tests for str functions

Diffstat:
Mtests/libc/execute/0012-strchr.c | 4++++
Atests/libc/execute/0014-strcoll.c | 30++++++++++++++++++++++++++++++
Atests/libc/execute/0015-strcpy.c | 30++++++++++++++++++++++++++++++
Atests/libc/execute/0016-strcspn.c | 25+++++++++++++++++++++++++
Atests/libc/execute/0017-strerror.c | 16++++++++++++++++
Atests/libc/execute/0018-strlen.c | 23+++++++++++++++++++++++
Atests/libc/execute/0019-strncat.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/libc/execute/0020-strncmp.c | 30++++++++++++++++++++++++++++++
Atests/libc/execute/0021-strncpy.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Atests/libc/execute/0022-strnlen.c | 23+++++++++++++++++++++++
Atests/libc/execute/0023-strpbrk.c | 28++++++++++++++++++++++++++++
Atests/libc/execute/0024-strrchr.c | 27+++++++++++++++++++++++++++
Atests/libc/execute/0025-strspn.c | 25+++++++++++++++++++++++++
Atests/libc/execute/0026-strstr.c | 31+++++++++++++++++++++++++++++++
Atests/libc/execute/0027-strtok.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/libc/execute/0028-strxfrm.c | 26++++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 15+++++++++++++++
17 files changed, 495 insertions(+), 0 deletions(-)

diff --git a/tests/libc/execute/0012-strchr.c b/tests/libc/execute/0012-strchr.c @@ -16,6 +16,10 @@ main() puts("testing"); + p = strchr(buf, 0); + assert(p == buf+5); + assert(*p == '\0'); + p = strchr(buf, 'a'); assert(p == buf); assert(*p == 'a'); diff --git a/tests/libc/execute/0014-strcoll.c b/tests/libc/execute/0014-strcoll.c @@ -0,0 +1,30 @@ +#include <assert.h> +#include <stdio.h> +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + puts("testing"); + + assert(strcoll("abcd", "abcd") == 0); + assert(strcoll("abcd", "a") > 0); + assert(strcoll("a", "abcd") < 0); + assert(strcoll("aa", "ab") < 0); + assert(strcoll("ab", "aa") > 0); + assert(strcoll("", "a") < 0); + assert(strcoll("a", "") > 0); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0015-strcpy.c b/tests/libc/execute/0015-strcpy.c @@ -0,0 +1,30 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char *s, buf[40]; + + puts("testing"); + + s = strcpy(buf, "test"); + assert(s == buf); + assert(!strcmp(s, "test")); + + s = strcpy(buf, ""); + assert(s == buf); + assert(!strcmp(s, "")); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0016-strcspn.c b/tests/libc/execute/0016-strcspn.c @@ -0,0 +1,25 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + puts("testing"); + + assert(strcspn("012", "56789") == 3); + assert(strcspn("", "56789") == 0); + assert(strcspn("01234", "") == 5); + assert(strcspn("", "") == 0); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0017-strerror.c b/tests/libc/execute/0017-strerror.c @@ -0,0 +1,16 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + return 0; +} diff --git a/tests/libc/execute/0018-strlen.c b/tests/libc/execute/0018-strlen.c @@ -0,0 +1,23 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + puts("testing"); + + assert(strlen("01234") == 5); + assert(strlen("") == 0); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0019-strncat.c b/tests/libc/execute/0019-strncat.c @@ -0,0 +1,58 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +test1 +test2 +test3 +test4 +test5 +done +end: +*/ + +int +main() +{ + char *s, buf[40], buf2[40]; + + puts("testing"); + + puts("test1"); + strcpy(buf, "01234"); + s = strncat(buf, "567", 8); + assert(s == buf); + assert(!strcmp(s, "01234567")); + + puts("test2"); + strcpy(buf, "01234"); + s = strncat(buf, "567", 2); + assert(s == buf); + assert(!strcmp(s, "0123456")); + + puts("test3"); + strcpy(buf, "01234"); + memcpy(buf2, "567", 3); + s = strncat(buf, buf2, 3); + assert(s == buf); + assert(!strcmp(s, "01234567")); + + puts("test4"); + strcpy(buf, "01234"); + s = strncat(buf, "", 7); + assert(s == buf); + assert(!strcmp(s, "01234")); + + puts("test5"); + strcpy(buf, "01234"); + s = strncat(buf, "", 0); + assert(s == buf); + assert(!strcmp(s, "01234")); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0020-strncmp.c b/tests/libc/execute/0020-strncmp.c @@ -0,0 +1,30 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + puts("testing"); + + assert(strncmp("abc", "abc", 3) == 0); + assert(strncmp("abcd", "abce", 3) == 0); + assert(strncmp("abc", "abc", 4) == 0); + assert(strncmp("abcd", "abef", 4) < 0); + assert(strncmp("abcf", "abcd", 4) > 0); + assert(strncmp("abc", "abe", 0) == 0); + assert(strncmp("", "", 1) == 0); + assert(strncmp("abc", "", 3) > 0); + assert(strncmp("", "abc", 3) < 0); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0021-strncpy.c b/tests/libc/execute/0021-strncpy.c @@ -0,0 +1,47 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +#define SIZ 6 + +/* +output: +testing +test1 +test2 +test3 +done +end: +*/ + +int +main() +{ + char *s, buf[SIZ]; + + puts("testing"); + + puts("test1"); + memset(buf, '0', SIZ); + s = strncpy(buf, "abc", sizeof(buf)); + assert(s == buf); + assert(!memcmp(s, (char[SIZ]) {"abc"}, sizeof(buf))); + + puts("test2"); + memset(buf, '0', SIZ); + s = strncpy(buf, "", sizeof(buf)); + assert(s == buf); + assert(!memcmp(s, (char[SIZ]) {'\0'}, sizeof(buf))); + + puts("test3"); + memset(buf, '0', SIZ); + s = strncpy(buf, "", 1); + assert(s == buf); + assert(!memcmp(s, + (char[SIZ]) {'\0', '0', '0', '0', '0', '0'}, + sizeof(buf))); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0022-strnlen.c b/tests/libc/execute/0022-strnlen.c @@ -0,0 +1,23 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + puts("testing"); + assert(strnlen("", 2) == 0); + assert(strnlen("abc", 10) == 3); + assert(strnlen((char[3]) {"abc"}, 3) == 3); + assert(strnlen("abc", 2) == 2); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0023-strpbrk.c b/tests/libc/execute/0023-strpbrk.c @@ -0,0 +1,28 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char buf[] = "abcdef"; + + puts("testing"); + assert(strpbrk(buf, "a") == buf); + assert(strpbrk(buf, "") == NULL); + assert(strpbrk("", "a") == NULL); + assert(strpbrk("", "") == NULL); + assert(strpbrk(buf, "1234") == NULL); + assert(strpbrk(buf, "f") == buf + 5); + assert(strpbrk(buf, "12345a") == buf); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0024-strrchr.c b/tests/libc/execute/0024-strrchr.c @@ -0,0 +1,27 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char buf[] = "012321"; + + puts("testing"); + assert(strrchr(buf, '1') == buf+5); + assert(strrchr(buf, '0') == buf); + assert(strrchr(buf, '3') == buf+3); + assert(strrchr("", '0') == NULL); + assert(strrchr(buf, 'a') == NULL); + assert(strrchr(buf, 0) == buf+6); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0025-strspn.c b/tests/libc/execute/0025-strspn.c @@ -0,0 +1,25 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + puts("testing"); + assert(strspn("abcdef", "cba") == 3); + assert(strspn("abc", "cba0") == 3); + assert(strspn("", "abc") == 0); + assert(strspn("abc", "") == 0); + assert(strspn("", "") == 0); + assert(strspn("abc", "def") == 0); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/0026-strstr.c b/tests/libc/execute/0026-strstr.c @@ -0,0 +1,31 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char buf[30] = "abc"; + + puts("testing"); + assert(strstr(buf, "abc") == buf); + assert(strstr(buf, "bc") == buf + 1); + assert(strstr(buf, "c") == buf + 2); + assert(strstr(buf, "d") == NULL); + strcpy(buf, "ababc"); + assert(strstr(buf, "abc") == buf+2); + assert(strstr("", "abc") == NULL); + assert(strstr("abc", "") == NULL); + assert(strstr("", "") == NULL); + puts("done"); + + return 0; +} + diff --git a/tests/libc/execute/0027-strtok.c b/tests/libc/execute/0027-strtok.c @@ -0,0 +1,57 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +test1 +one +two +three +four +test2 +one +three +done +end: +*/ + +void +test(char *msg, char *fmt) +{ + char *s, buff[50]; + + puts(msg); + + strcpy(buff, fmt); + for (s = strtok(buff, "-+"); s; s = strtok(NULL, "+-")) { + switch (atoi(s)) { + case 1: + puts("one"); + break; + case 2: + puts("two"); + break; + case 3: + puts("three"); + break; + case 4: + puts("four"); + break; + default: + puts("error"); + break; + } + } +} + +int +main() +{ + puts("testing"); + test("test1", "-+001--0002++3+-4"); + test("test2", "001--+-+-+-3+-"); + puts("done"); + return 0; +} diff --git a/tests/libc/execute/0028-strxfrm.c b/tests/libc/execute/0028-strxfrm.c @@ -0,0 +1,26 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + char buf[40]; + size_t n; + + puts("testing"); + + assert(strxfrm(buf, "test", 40) == 4); + assert(strxfrm(buf, "", 0) == 0); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -11,3 +11,18 @@ 0011-strcat 0012-strchr 0013-strcmp +0014-strcoll +0015-strcpy +0016-strcspn +0017-strerror [TODO] +0018-strlen +0019-strncat +0020-strncmp +0021-strncpy +0022-strnlen +0023-strpbrk +0024-strrchr +0025-strspn +0026-strstr +0027-strtok +0028-strxfrm