about summary refs log tree commit diff
path: root/string/test-strcmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'string/test-strcmp.c')
-rw-r--r--string/test-strcmp.c57
1 files changed, 36 insertions, 21 deletions
diff --git a/string/test-strcmp.c b/string/test-strcmp.c
index bfc147b701..dbbe3a4ff6 100644
--- a/string/test-strcmp.c
+++ b/string/test-strcmp.c
@@ -23,7 +23,9 @@
 
 typedef int (*proto_t) (const char *, const char *);
 int simple_strcmp (const char *, const char *);
+int stupid_strcmp (const char *, const char *);
 
+IMPL (stupid_strcmp, 0)
 IMPL (simple_strcmp, 0)
 IMPL (strcmp, 1)
 
@@ -37,6 +39,19 @@ simple_strcmp (const char *s1, const char *s2)
   return ret;
 }
 
+int
+stupid_strcmp (const char *s1, const char *s2)
+{
+  size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
+  size_t n = ns1 < ns2 ? ns1 : ns2;
+  int ret = 0;
+
+  while (n--)
+    if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
+      break;
+  return ret;
+}
+
 static void
 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
 {
@@ -110,7 +125,7 @@ do_test (size_t align1, size_t align2, size_t len, int max_char,
 static void
 do_random_tests (void)
 {
-  size_t i, j, n, align1, align2, pos, len;
+  size_t i, j, n, align1, align2, pos, len1, len2;
   int result, r;
   unsigned char *p1 = buf1 + page_size - 512;
   unsigned char *p2 = buf2 + page_size - 512;
@@ -123,22 +138,25 @@ do_random_tests (void)
       else
 	align2 = align1 + (random () & 24);
       pos = random () & 511;
-      j = align1;
-      if (align2 > j)
-	j = align2;
+      j = align1 > align2 ? align1 : align2;
       if (pos + j >= 511)
 	pos = 510 - j - (random () & 7);
-      len = random () & 511;
-      if (pos >= len)
-        len = pos + (random () & 7);
-      if (len + j >= 512)
-        len = 511 - j - (random () & 7);
-      j = len + align1 + 64;
-      if (j > 512) j = 512;
+      len1 = random () & 511;
+      if (pos >= len1 && (random () & 1))
+        len1 = pos + (random () & 7);
+      if (len1 + j >= 512)
+        len1 = 511 - j - (random () & 7);
+      if (pos >= len1)
+	len2 = len1;
+      else
+	len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
+      j = (pos > len2 ? pos : len2) + align1 + 64;
+      if (j > 512)
+	j = 512;
       for (i = 0; i < j; ++i)
 	{
 	  p1[i] = random () & 255;
-	  if (i < len + align1 && !p1[i])
+	  if (i < len1 + align1 && !p1[i])
 	    {
 	      p1[i] = random () & 255;
 	      if (!p1[i])
@@ -148,7 +166,7 @@ do_random_tests (void)
       for (i = 0; i < j; ++i)
 	{
 	  p2[i] = random () & 255;
-	  if (i < len + align2 && !p2[i])
+	  if (i < len2 + align2 && !p2[i])
 	    {
 	      p2[i] = random () & 255;
 	      if (!p2[i])
@@ -158,12 +176,7 @@ do_random_tests (void)
 
       result = 0;
       memcpy (p2 + align2, p1 + align1, pos);
-      if (pos >= len)
-	{
-	  p1[len + align1] = 0;
-	  p2[len + align2] = 0;
-	}
-      else
+      if (pos < len1)
 	{
 	  if (p2[align2 + pos] == p1[align1 + pos])
 	    {
@@ -177,6 +190,8 @@ do_random_tests (void)
 	  else
 	    result = 1;
 	}
+      p1[len1 + align1] = 0;
+      p2[len2 + align2] = 0;
 
       FOR_EACH_IMPL (impl, 1)
 	{
@@ -185,8 +200,8 @@ do_random_tests (void)
 	      || (r < 0 && result >= 0)
 	      || (r > 0 && result <= 0))
 	    {
-	      error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
-		     n, impl->name, align1, align2, len, pos, r, result, p1, p2);
+	      error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
+		     n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
 	      ret = 1;
 	    }
 	}