shithub: scc

Download patch

ref: 4bf844191abad6f3287b44f94123a171b5f8b8b0
parent: 071923624122ae0088073ca68a8dfc65c14c0c7d
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Sep 21 11:54:47 EDT 2018

[tests/libc] Add mem* tests

--- 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;
 }
--- a/lib/c/strncmp.c
+++ b/lib/c/strncmp.c
@@ -6,12 +6,9 @@
 {
 	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;
 }
--- a/tests/libc/execute/0027-strtok.c
+++ b/tests/libc/execute/0027-strtok.c
@@ -13,6 +13,8 @@
 test2
 one
 three
+test3
+one
 done
 end:
 */
@@ -52,6 +54,7 @@
 	puts("testing");
 	test("test1", "-+001--0002++3+-4");
 	test("test2", "001--+-+-+-3+-");
+	test("test3", "001");
 	puts("done");
 	return 0;
 }
--- a/tests/libc/execute/0028-strxfrm.c
+++ b/tests/libc/execute/0028-strxfrm.c
@@ -19,6 +19,7 @@
 
 	assert(strxfrm(buf, "test", 40) == 4);
 	assert(strxfrm(buf, "", 0) == 0);
+	assert(strxfrm(NULL, "abc", 0) > 0);
 
 	puts("done");
 
--- /dev/null
+++ b/tests/libc/execute/0029-memchr.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0030-memcpy.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0031-memmove.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0032-memset.c
@@ -1,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;
+}
--- /dev/null
+++ b/tests/libc/execute/0033-memcmp.c
@@ -1,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;
+}
--- 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