about summary refs log tree commit diff
path: root/string/test-strcmp.c
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>2002-11-08 22:10:01 +0000
committerRoland McGrath <roland@gnu.org>2002-11-08 22:10:01 +0000
commite8c1660f7ba480c5a17103b557941fc6db7a5146 (patch)
tree71673df7864e35860bd8c7e8d2f3424fdb1dffc9 /string/test-strcmp.c
parente0bc9a8d13290de5328ae49311493246f406e0bd (diff)
downloadglibc-e8c1660f7ba480c5a17103b557941fc6db7a5146.tar.gz
glibc-e8c1660f7ba480c5a17103b557941fc6db7a5146.tar.xz
glibc-e8c1660f7ba480c5a17103b557941fc6db7a5146.zip
* string/test-strchr.c (stupid_strchr): New function.
	(do_random_tests): Make sure the string is zero terminated.
	* string/test-strpbrk.c (stupid_strpbrk): New function.
	(do_random_tests): Make sure the string is zero terminated.
	* string/test-strcmp.c (stupid_strcmp): New function.
	(do_random_tests): Make sure the strings are zero terminated.
	* string/test-strspn.c (stupid_strspn): New function.
	(simple_strspn): Rename rej argument to acc.
	(do_random_tests): Make sure the string is zero terminated.
	* string/test-strcspn.c (stupid_strcspn): New function.
	* string/test-strncpy.c (stupid_strncpy): New function.
	* string/test-stpncpy.c (stupid_stpncpy): New function.
	* string/test-strncmp.c (stupid_strncmp): New function.
	(do_random_tests): Make sure the strings are zero terminated.
	* string/test-string.h (impl_t): Change test into long.
	(IMPL): Add __attribute__((aligned (sizeof (void *)))).
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;
 	    }
 	}