Просмотр исходного кода

Add tests for memcmp and fix a teeny tiny bug

jD91mZM2 6 лет назад
Родитель
Сommit
0469c0c2c6
3 измененных файлов с 37 добавлено и 18 удалено
  1. 1 1
      src/header/string/mod.rs
  2. 1 0
      tests/expected/string/mem.stdout
  3. 35 17
      tests/string/mem.c

+ 1 - 1
src/header/string/mod.rs

@@ -95,7 +95,7 @@ pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: usize)
     let mut b = b as *const u8;
     for _ in 0..rem {
         if *a != *b {
-            return a as c_int - b as c_int;
+            return *a as c_int - *b as c_int;
         }
         a = a.offset(1);
         b = b.offset(1);

+ 1 - 0
tests/expected/string/mem.stdout

@@ -1,3 +1,4 @@
 # mem #
 Correct memchr
 Correct memccpy
+Correct memcmp

+ 35 - 17
tests/string/mem.c

@@ -3,22 +3,40 @@
 #include <string.h>
 
 int main(int argc, char ** argv) {
-	printf("# mem #\n");
-	char arr[100];
-	memset(arr, 0, 100); // Compiler builtin, should work
-	arr[50] = 1;
-	if ((size_t)memchr((void *)arr, 1, 100) - (size_t)arr != 50) {
-		printf("Incorrect memchr\n");
-		exit(1);
-	}
-	printf("Correct memchr\n");
-	char arr2[51];
-	memset(arr2, 0, 51); // Compiler builtin, should work
-	memccpy((void *)arr2, (void *)arr, 1, 100);
-	if (arr[50] != 1) {
-		printf("Incorrect memccpy\n");
-		exit(1);
-	}
-	printf("Correct memccpy\n");
+    puts("# mem #");
+    char arr[100];
+    memset(arr, 0, 100); // Compiler builtin, should work
+    arr[50] = 1;
+    if ((size_t)memchr((void *)arr, 1, 100) - (size_t)arr != 50) {
+        puts("Incorrect memchr");
+        exit(1);
+    }
+    puts("Correct memchr");
+    char arr2[51];
+    memset(arr2, 0, 51); // Compiler builtin, should work
+    memccpy((void *)arr2, (void *)arr, 1, 100);
+    if (arr[50] != 1) {
+        puts("Incorrect memccpy");
+        exit(1);
+    }
+    puts("Correct memccpy");
+    int res;
+    if ((res = memcmp("hello world", "hello world", 11))) {
+        printf("Incorrect memcmp (1), expected 0 found %d\n", res);
+        exit(1);
+    }
+    if ((res = memcmp("hello world", "hello worlt", 11)) >= 0) {
+        printf("Incorrect memcmp (2), expected -, found %d\n", res);
+        exit(1);
+    }
+    if ((res = memcmp("hello world", "hallo world", 5)) <= 0) {
+        printf("Incorrect memcmp (3), expected +, found %d\n", res);
+        exit(1);
+    }
+    if ((res = memcmp("hello world", "henlo world", 5)) >= 0) {
+        printf("Incorrect memcmp (4), expected -, found %d\n", res);
+        exit(1);
+    }
+    puts("Correct memcmp");
     return 0;
 }