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-strncasecmp.c | 166 +++++++++++++++++++++++++++++++++++---
> 1 file changed, 154 insertions(+), 12 deletions(-)
>
> diff --git a/string/test-strncasecmp.c b/string/test-strncasecmp.c
> index a3c848165a..b86c630bf6 100644
> --- a/string/test-strncasecmp.c
> +++ b/string/test-strncasecmp.c
> @@ -18,6 +18,10 @@
>
> #include <locale.h>
> #include <ctype.h>
> +
> +#define TEST_LEN (getpagesize () * 3)
> +#define MIN_PAGE_SIZE (TEST_LEN + 2 * getpagesize ())
> +
> #define TEST_MAIN
> #define TEST_NAME "strncasecmp"
> #include "test-string.h"
> @@ -106,14 +110,15 @@ do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
> if (len == 0)
> return;
>
> - align1 &= 7;
> - if (align1 + len + 1 >= page_size)
> + align1 &= getpagesize () - 1;
> + if (align1 + (len + 2) >= page_size)
> return;
>
> - align2 &= 7;
> - if (align2 + len + 1 >= page_size)
> + align2 &= getpagesize () - 1;
> + if (align2 + (len + 2) >= page_size)
> return;
>
> +
> s1 = (char *) (buf1 + align1);
> s2 = (char *) (buf2 + align2);
>
> @@ -126,12 +131,33 @@ do_test (size_t align1, size_t align2, size_t n, 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, n, exp_result);
> }
> @@ -299,10 +325,10 @@ bz14195 (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);
> @@ -374,6 +400,122 @@ test_locale (const char *locale)
> do_test (2 * i, i, (8 << i) + 100, 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 + 1, i, 127, 0);
> + do_test (getpagesize () - j - 1, 0, i + 1, i, 127, 1);
> + do_test (getpagesize () - j - 1, 0, i + 1, i, 127, -1);
> +
> + do_test (getpagesize () - j - 1, 0, i, i, 127, 0);
> + do_test (getpagesize () - j - 1, 0, i - 1, i, 127, 0);
> +
> + do_test (getpagesize () - j - 1, 0, ULONG_MAX, i, 127, 0);
> + do_test (getpagesize () - j - 1, 0, ULONG_MAX, i, 127, 1);
> + do_test (getpagesize () - j - 1, 0, ULONG_MAX, i, 127, -1);
> +
> + do_test (getpagesize () - j - 1, 0, ULONG_MAX - i, i, 127, 0);
> + do_test (getpagesize () - j - 1, 0, ULONG_MAX - i, i, 127, 1);
> + do_test (getpagesize () - j - 1, 0, ULONG_MAX - i, i, 127, -1);
> +
> + do_test (getpagesize () - j - 1, j, i + 1, i, 127, 0);
> + do_test (getpagesize () - j - 1, j, i + 1, i, 127, 1);
> + do_test (getpagesize () - j - 1, j, i + 1, i, 127, -1);
> +
> + do_test (getpagesize () - j - 1, j, i, i, 127, 0);
> + do_test (getpagesize () - j - 1, j, i - 1, i, 127, 0);
> +
> + do_test (getpagesize () - j - 1, j, ULONG_MAX, i, 127, 0);
> + do_test (getpagesize () - j - 1, j, ULONG_MAX, i, 127, 1);
> + do_test (getpagesize () - j - 1, j, ULONG_MAX, i, 127, -1);
> +
> + do_test (getpagesize () - j - 1, j, ULONG_MAX - i, i, 127, 0);
> + do_test (getpagesize () - j - 1, j, ULONG_MAX - i, i, 127, 1);
> + do_test (getpagesize () - j - 1, j, ULONG_MAX - i, i, 127, -1);
> +
> + do_test (0, getpagesize () - j - 1, i + 1, i, 127, 0);
> + do_test (0, getpagesize () - j - 1, i + 1, i, 127, 1);
> + do_test (0, getpagesize () - j - 1, i + 1, i, 127, -1);
> +
> + do_test (0, getpagesize () - j - 1, i, i, 127, 0);
> + do_test (0, getpagesize () - j - 1, i - 1, i, 127, 0);
> +
> + do_test (0, getpagesize () - j - 1, ULONG_MAX, i, 127, 0);
> + do_test (0, getpagesize () - j - 1, ULONG_MAX, i, 127, 1);
> + do_test (0, getpagesize () - j - 1, ULONG_MAX, i, 127, -1);
> +
> + do_test (0, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 0);
> + do_test (0, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 1);
> + do_test (0, getpagesize () - j - 1, ULONG_MAX - i, i, 127, -1);
> +
> + do_test (j, getpagesize () - j - 1, i + 1, i, 127, 0);
> + do_test (j, getpagesize () - j - 1, i + 1, i, 127, 1);
> + do_test (j, getpagesize () - j - 1, i + 1, i, 127, -1);
> +
> + do_test (j, getpagesize () - j - 1, i, i, 127, 0);
> + do_test (j, getpagesize () - j - 1, i - 1, i, 127, 0);
> +
> + do_test (j, getpagesize () - j - 1, ULONG_MAX, i, 127, 0);
> + do_test (j, getpagesize () - j - 1, ULONG_MAX, i, 127, 1);
> + do_test (j, getpagesize () - j - 1, ULONG_MAX, i, 127, -1);
> +
> + do_test (j, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 0);
> + do_test (j, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 1);
> + do_test (j, getpagesize () - j - 1, ULONG_MAX - i, i, 127, -1);
> +
> + for (k = 2; k <= 128; k += k)
> + {
> + do_test (getpagesize () - k, getpagesize () - j - 1, i - 1, i,
> + 127, 0);
> + do_test (getpagesize () - k - 1, getpagesize () - j - 1, i - 1,
> + i, 127, 0);
> + do_test (getpagesize () - k, getpagesize () - j - 1, i + 1, i,
> + 127, 0);
> + do_test (getpagesize () - k - 1, getpagesize () - j - 1, i + 1,
> + i, 127, 0);
> + do_test (getpagesize () - k, getpagesize () - j - 1, i, i, 127,
> + 0);
> + do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, i,
> + 127, 0);
> + do_test (getpagesize () - k, getpagesize () - j - 1, i + 1, i,
> + 127, -1);
> + do_test (getpagesize () - k - 1, getpagesize () - j - 1, i + 1,
> + i, 127, -1);
> + do_test (getpagesize () - k, getpagesize () - j - 1, i + 1, i,
> + 127, 1);
> + do_test (getpagesize () - k - 1, getpagesize () - j - 1, i + 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 ();
> do_page_tests ();
> }
> @@ -383,11 +525,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.
@@ -18,6 +18,10 @@
#include <locale.h>
#include <ctype.h>
+
+#define TEST_LEN (getpagesize () * 3)
+#define MIN_PAGE_SIZE (TEST_LEN + 2 * getpagesize ())
+
#define TEST_MAIN
#define TEST_NAME "strncasecmp"
#include "test-string.h"
@@ -106,14 +110,15 @@ do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
if (len == 0)
return;
- align1 &= 7;
- if (align1 + len + 1 >= page_size)
+ align1 &= getpagesize () - 1;
+ if (align1 + (len + 2) >= page_size)
return;
- align2 &= 7;
- if (align2 + len + 1 >= page_size)
+ align2 &= getpagesize () - 1;
+ if (align2 + (len + 2) >= page_size)
return;
+
s1 = (char *) (buf1 + align1);
s2 = (char *) (buf2 + align2);
@@ -126,12 +131,33 @@ do_test (size_t align1, size_t align2, size_t n, 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, n, exp_result);
}
@@ -299,10 +325,10 @@ bz14195 (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);
@@ -374,6 +400,122 @@ test_locale (const char *locale)
do_test (2 * i, i, (8 << i) + 100, 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 + 1, i, 127, 0);
+ do_test (getpagesize () - j - 1, 0, i + 1, i, 127, 1);
+ do_test (getpagesize () - j - 1, 0, i + 1, i, 127, -1);
+
+ do_test (getpagesize () - j - 1, 0, i, i, 127, 0);
+ do_test (getpagesize () - j - 1, 0, i - 1, i, 127, 0);
+
+ do_test (getpagesize () - j - 1, 0, ULONG_MAX, i, 127, 0);
+ do_test (getpagesize () - j - 1, 0, ULONG_MAX, i, 127, 1);
+ do_test (getpagesize () - j - 1, 0, ULONG_MAX, i, 127, -1);
+
+ do_test (getpagesize () - j - 1, 0, ULONG_MAX - i, i, 127, 0);
+ do_test (getpagesize () - j - 1, 0, ULONG_MAX - i, i, 127, 1);
+ do_test (getpagesize () - j - 1, 0, ULONG_MAX - i, i, 127, -1);
+
+ do_test (getpagesize () - j - 1, j, i + 1, i, 127, 0);
+ do_test (getpagesize () - j - 1, j, i + 1, i, 127, 1);
+ do_test (getpagesize () - j - 1, j, i + 1, i, 127, -1);
+
+ do_test (getpagesize () - j - 1, j, i, i, 127, 0);
+ do_test (getpagesize () - j - 1, j, i - 1, i, 127, 0);
+
+ do_test (getpagesize () - j - 1, j, ULONG_MAX, i, 127, 0);
+ do_test (getpagesize () - j - 1, j, ULONG_MAX, i, 127, 1);
+ do_test (getpagesize () - j - 1, j, ULONG_MAX, i, 127, -1);
+
+ do_test (getpagesize () - j - 1, j, ULONG_MAX - i, i, 127, 0);
+ do_test (getpagesize () - j - 1, j, ULONG_MAX - i, i, 127, 1);
+ do_test (getpagesize () - j - 1, j, ULONG_MAX - i, i, 127, -1);
+
+ do_test (0, getpagesize () - j - 1, i + 1, i, 127, 0);
+ do_test (0, getpagesize () - j - 1, i + 1, i, 127, 1);
+ do_test (0, getpagesize () - j - 1, i + 1, i, 127, -1);
+
+ do_test (0, getpagesize () - j - 1, i, i, 127, 0);
+ do_test (0, getpagesize () - j - 1, i - 1, i, 127, 0);
+
+ do_test (0, getpagesize () - j - 1, ULONG_MAX, i, 127, 0);
+ do_test (0, getpagesize () - j - 1, ULONG_MAX, i, 127, 1);
+ do_test (0, getpagesize () - j - 1, ULONG_MAX, i, 127, -1);
+
+ do_test (0, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 0);
+ do_test (0, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 1);
+ do_test (0, getpagesize () - j - 1, ULONG_MAX - i, i, 127, -1);
+
+ do_test (j, getpagesize () - j - 1, i + 1, i, 127, 0);
+ do_test (j, getpagesize () - j - 1, i + 1, i, 127, 1);
+ do_test (j, getpagesize () - j - 1, i + 1, i, 127, -1);
+
+ do_test (j, getpagesize () - j - 1, i, i, 127, 0);
+ do_test (j, getpagesize () - j - 1, i - 1, i, 127, 0);
+
+ do_test (j, getpagesize () - j - 1, ULONG_MAX, i, 127, 0);
+ do_test (j, getpagesize () - j - 1, ULONG_MAX, i, 127, 1);
+ do_test (j, getpagesize () - j - 1, ULONG_MAX, i, 127, -1);
+
+ do_test (j, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 0);
+ do_test (j, getpagesize () - j - 1, ULONG_MAX - i, i, 127, 1);
+ do_test (j, getpagesize () - j - 1, ULONG_MAX - i, i, 127, -1);
+
+ for (k = 2; k <= 128; k += k)
+ {
+ do_test (getpagesize () - k, getpagesize () - j - 1, i - 1, i,
+ 127, 0);
+ do_test (getpagesize () - k - 1, getpagesize () - j - 1, i - 1,
+ i, 127, 0);
+ do_test (getpagesize () - k, getpagesize () - j - 1, i + 1, i,
+ 127, 0);
+ do_test (getpagesize () - k - 1, getpagesize () - j - 1, i + 1,
+ i, 127, 0);
+ do_test (getpagesize () - k, getpagesize () - j - 1, i, i, 127,
+ 0);
+ do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, i,
+ 127, 0);
+ do_test (getpagesize () - k, getpagesize () - j - 1, i + 1, i,
+ 127, -1);
+ do_test (getpagesize () - k - 1, getpagesize () - j - 1, i + 1,
+ i, 127, -1);
+ do_test (getpagesize () - k, getpagesize () - j - 1, i + 1, i,
+ 127, 1);
+ do_test (getpagesize () - k - 1, getpagesize () - j - 1, i + 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 ();
do_page_tests ();
}
@@ -383,11 +525,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;
}