[v1,15/23] string: Expand page cross tests in test-strcasecmp.c

Message ID 20220323215734.3927131-15-goldstein.w.n@gmail.com (mailing list archive)
State Accepted, archived
Headers
Series [v1,01/23] benchtests: Use json-lib in bench-strchr.c |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Noah Goldstein March 23, 2022, 9:57 p.m. UTC
  Add more robust tests that cover all the page cross edge cases.
---
 string/test-strcasecmp.c | 112 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 100 insertions(+), 12 deletions(-)
  

Comments

H.J. Lu March 24, 2022, 7:01 p.m. UTC | #1
On Wed, Mar 23, 2022 at 3:01 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
>
> Add more robust tests that cover all the page cross edge cases.
> ---
>  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;
>  }
> --
> 2.25.1
>

LGTM.

Reviewed-by: H.J. Lu <hjl.tools@gmail.com>

Thanks.
  

Patch

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