public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Noah Goldstein <nwg@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] string: Expand page cross tests in test-strcasecmp.c
Date: Fri, 25 Mar 2022 18:18:52 +0000 (GMT)	[thread overview]
Message-ID: <20220325181852.5556F388981B@sourceware.org> (raw)

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;
 }


                 reply	other threads:[~2022-03-25 18:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220325181852.5556F388981B@sourceware.org \
    --to=nwg@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).