public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc] string: Expand page cross tests in test-strcasecmp.c
@ 2022-03-25 18:18 Noah Goldstein
  0 siblings, 0 replies; only message in thread
From: Noah Goldstein @ 2022-03-25 18:18 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=1edbcc82b581c17fb9ebd233f8374c0825e4b12c

commit 1edbcc82b581c17fb9ebd233f8374c0825e4b12c
Author: Noah Goldstein <goldstein.w.n@gmail.com>
Date:   Wed Mar 23 16:57:33 2022 -0500

    string: Expand page cross tests in test-strcasecmp.c
    
    Add more robust tests that cover all the page cross edge cases.
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>

Diff:
---
 string/test-strcasecmp.c | 112 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 100 insertions(+), 12 deletions(-)

diff --git a/string/test-strcasecmp.c b/string/test-strcasecmp.c
index 3d994f9d64..438a9713ac 100644
--- a/string/test-strcasecmp.c
+++ b/string/test-strcasecmp.c
@@ -18,6 +18,10 @@
 
 #include <locale.h>
 #include <ctype.h>
+#include <assert.h>
+#define TEST_LEN (getpagesize () * 3)
+#define MIN_PAGE_SIZE (TEST_LEN + 2 * getpagesize ())
+
 #define TEST_MAIN
 #define TEST_NAME "strcasecmp"
 #include "test-string.h"
@@ -85,12 +89,13 @@ do_test (size_t align1, size_t align2, size_t len, int max_char,
   if (len == 0)
     return;
 
-  align1 &= 7;
-  if (align1 + len + 1 >= page_size)
+
+  align1 &= getpagesize () - 1;
+  if (align1 + (len + 1) >= page_size)
     return;
 
-  align2 &= 7;
-  if (align2 + len + 1 >= page_size)
+  align2 &= getpagesize () - 1;
+  if (align2 + (len + 1) >= page_size)
     return;
 
   s1 = (char *) (buf1 + align1);
@@ -105,12 +110,33 @@ do_test (size_t align1, size_t align2, size_t len, int max_char,
   s1[len] = s2[len] = 0;
   s1[len + 1] = 23;
   s2[len + 1] = 24 + exp_result;
+
   if ((s2[len - 1] == 'z' && exp_result == -1)
       || (s2[len - 1] == 'a' && exp_result == 1))
     s1[len - 1] += exp_result;
+  else if ((s1[len - 1] == 'Z' + 1 && exp_result == 1)
+           || (s1[len - 1] == 'A' - 1 && exp_result == -1))
+    s1[len - 1] = tolower (s2[len - 1]) + exp_result;
   else
     s2[len - 1] -= exp_result;
 
+  /* For some locals this is not guranteed yet.  */
+  if (tolower (s1[len - 1]) - tolower (s2[len - 1]) != exp_result)
+    {
+      if (exp_result == -1)
+        {
+          s1[len - 1] = tolower ('a');
+          s2[len - 1] = toupper (tolower ('a') - 1);
+        }
+      else if (exp_result == 0)
+        s1[len - 1] = toupper (s2[len - 1]);
+      else
+        {
+          s1[len - 1] = tolower ('a');
+          s2[len - 1] = toupper (tolower ('a') + 1);
+        }
+    }
+
   FOR_EACH_IMPL (impl, 0)
     do_one_test (impl, s1, s2, exp_result);
 }
@@ -207,10 +233,10 @@ do_random_tests (void)
 }
 
 static void
-test_locale (const char *locale)
+test_locale (const char *locale, int extra_tests)
 {
-  size_t i;
-
+  size_t i, j, k;
+  const size_t test_len = MIN(TEST_LEN, 3 * 4096);
   if (setlocale (LC_CTYPE, locale) == NULL)
     {
       error (0, 0, "cannot set locale \"%s\"", locale);
@@ -249,6 +275,68 @@ test_locale (const char *locale)
       do_test (2 * i, i, 8 << i, 254, -1);
     }
 
+  for (j = 0; extra_tests && j < 160; ++j)
+    {
+      for (i = 0; i < test_len;)
+        {
+          do_test (getpagesize () - j - 1, 0, i, 127, 0);
+          do_test (getpagesize () - j - 1, 0, i, 127, 1);
+          do_test (getpagesize () - j - 1, 0, i, 127, -1);
+
+          do_test (getpagesize () - j - 1, j, i, 127, 0);
+          do_test (getpagesize () - j - 1, j, i, 127, 1);
+          do_test (getpagesize () - j - 1, j, i, 127, -1);
+
+          do_test (0, getpagesize () - j - 1, i, 127, 0);
+          do_test (0, getpagesize () - j - 1, i, 127, 1);
+          do_test (0, getpagesize () - j - 1, i, 127, -1);
+
+          do_test (j, getpagesize () - j - 1, i, 127, 0);
+          do_test (j, getpagesize () - j - 1, i, 127, 1);
+          do_test (j, getpagesize () - j - 1, i, 127, -1);
+
+          for (k = 2; k <= 128; k += k)
+            {
+              do_test (getpagesize () - k, getpagesize () - j - 1, i, 127, 0);
+              do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, 127,
+                       0);
+              do_test (getpagesize () - k, getpagesize () - j - 1, i, 127, 1);
+              do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, 127,
+                       1);
+              do_test (getpagesize () - k, getpagesize () - j - 1, i, 127, -1);
+              do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, 127,
+                       -1);
+            }
+
+          if (i < 32)
+            {
+              i += 1;
+            }
+          else if (i < 161)
+            {
+              i += 7;
+            }
+          else if (i + 161 < test_len)
+            {
+              i += 31;
+              i *= 17;
+              i /= 16;
+              if (i + 161 > test_len)
+                {
+                  i = test_len - 160;
+                }
+            }
+          else if (i + 32 < test_len)
+            {
+              i += 7;
+            }
+          else
+            {
+              i += 1;
+            }
+        }
+    }
+
   do_random_tests ();
 }
 
@@ -257,11 +345,11 @@ test_main (void)
 {
   test_init ();
 
-  test_locale ("C");
-  test_locale ("en_US.ISO-8859-1");
-  test_locale ("en_US.UTF-8");
-  test_locale ("tr_TR.ISO-8859-9");
-  test_locale ("tr_TR.UTF-8");
+  test_locale ("C", 1);
+  test_locale ("en_US.ISO-8859-1", 0);
+  test_locale ("en_US.UTF-8", 0);
+  test_locale ("tr_TR.ISO-8859-9", 0);
+  test_locale ("tr_TR.UTF-8", 0);
 
   return ret;
 }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-25 18:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-25 18:18 [glibc] string: Expand page cross tests in test-strcasecmp.c Noah Goldstein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).