shithub: scc

Download patch

ref: 51b5a24e09ca25aa34dfb8cc50299ee75cfd06c1
parent: 5310badb8f39560835f0db678d6784217c92766f
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Sep 16 02:40:08 EDT 2018

[tests/libc] Add tests for str functions

--- a/tests/libc/execute/0012-strchr.c
+++ b/tests/libc/execute/0012-strchr.c
@@ -16,6 +16,10 @@
 
 	puts("testing");
 
+	p = strchr(buf, 0);
+	assert(p == buf+5);
+	assert(*p == '\0');
+
 	p = strchr(buf, 'a');
 	assert(p == buf);
 	assert(*p == 'a');
--- /dev/null
+++ b/tests/libc/execute/0014-strcoll.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0015-strcpy.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0016-strcspn.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0017-strerror.c
@@ -1,0 +1,16 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+/*
+output:
+testing
+done
+end:
+*/
+
+int
+main()
+{
+	return 0;
+}
--- /dev/null
+++ b/tests/libc/execute/0018-strlen.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0019-strncat.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0020-strncmp.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0021-strncpy.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0022-strnlen.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0023-strpbrk.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0024-strrchr.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0025-strspn.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0026-strstr.c
@@ -1,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;
+}
+
--- /dev/null
+++ b/tests/libc/execute/0027-strtok.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0028-strxfrm.c
@@ -1,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;
+}
--- 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