More tests for unistr/u8-strchr.
authorBruno Haible <bruno@clisp.org>
Sat, 31 Jul 2010 19:26:01 +0000 (21:26 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 31 Jul 2010 19:26:01 +0000 (21:26 +0200)
ChangeLog
tests/unistr/test-strchr.h
tests/unistr/test-u16-strchr.c
tests/unistr/test-u32-strchr.c
tests/unistr/test-u8-strchr.c

index b0d7104..2649458 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2010-07-31  Bruno Haible  <bruno@clisp.org>
 
+       More tests for unistr/u8-strchr.
+       * tests/unistr/test-strchr.h (test_strchr): Renamed from main. Check
+       that the function does not read past the first occurrence of the byte
+       being searched.
+       * tests/unistr/test-u8-strchr.c (main): New function, with more tests.
+       * tests/unistr/test-u16-strchr.c (main): New function.
+       * tests/unistr/test-u32-strchr.c (main): New function.
+
+2010-07-31  Bruno Haible  <bruno@clisp.org>
+
        posix-modules: Ignore backup files of documentation files.
        * posix-modules: grep only through files named *.texi.
 
index f280406..9a22fb7 100644 (file)
@@ -1,4 +1,4 @@
-/* Test of uN_chr() functions.
+/* Test of uN_strchr() functions.
    Copyright (C) 2008-2010 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
@@ -17,8 +17,8 @@
 /* Written by Paolo Bonzini <bonzini@gnu.org>, 2010.
    Based on test-chr.h, by Eric Blake and Bruno Haible.  */
 
-int
-main (void)
+static void
+test_strchr (void)
 {
   size_t size = 0x100000;
   size_t length;
@@ -101,8 +101,7 @@ main (void)
       }
   }
 
-  /* Check that uN_chr() does not read past the first occurrence of the
-     byte being searched.  */
+  /* Check that uN_strchr() does not read past the end of the string.  */
   {
     char *page_boundary = (char *) zerosize_ptr ();
     size_t n;
@@ -122,7 +121,34 @@ main (void)
       }
   }
 
-  free (input);
+  /* Check that uN_strchr() does not read past the first occurrence of the
+     byte being searched.  */
+  {
+    char *page_boundary = (char *) zerosize_ptr ();
+    size_t n;
+
+    if (page_boundary != NULL)
+      {
+        for (n = 2; n <= 500 / sizeof (UNIT); n++)
+          {
+            UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
+            U_SET (mem, 'X', n - 1);
+            mem[n - 1] = 0;
+            ASSERT (U_STRCHR (mem, 'U') == NULL);
+
+            {
+              size_t i;
 
-  return 0;
+              for (i = 0; i < n; i++)
+                {
+                  mem[i] = 'U';
+                  ASSERT (U_STRCHR (mem, 'U') == mem + i);
+                  mem[i] = 'X';
+                }
+            }
+          }
+      }
+  }
+
+  free (input);
 }
index 24cab3a..7de8c64 100644 (file)
 #define U_STRCHR u16_strchr
 #define U_SET u16_set
 #include "test-strchr.h"
+
+int
+main (void)
+{
+  test_strchr ();
+
+  return 0;
+}
index fdbdc0d..74903d9 100644 (file)
 #define U_STRCHR u32_strchr
 #define U_SET u32_set
 #include "test-strchr.h"
+
+int
+main (void)
+{
+  test_strchr ();
+
+  return 0;
+}
index ee8b9f2..2a289fb 100644 (file)
 #define U_STRCHR u8_strchr
 #define U_SET u8_set
 #include "test-strchr.h"
+
+int
+main (void)
+{
+  test_strchr ();
+
+  /* Check that u8_strchr() does not read past the end of the string.  */
+  {
+    char *page_boundary = (char *) zerosize_ptr ();
+
+    if (page_boundary != NULL)
+      {
+        UNIT *mem;
+
+        mem = (UNIT *) (page_boundary - 1 * sizeof (UNIT));
+        mem[0] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 2 * sizeof (UNIT));
+        mem[0] = 0x50;
+        mem[1] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 3 * sizeof (UNIT));
+        mem[0] = 0x50;
+        mem[1] = 0x50;
+        mem[2] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 3 * sizeof (UNIT));
+        mem[0] = 0xC4; mem[1] = 0xA0; /* U+0120 */
+        mem[2] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 3 * sizeof (UNIT));
+        mem[0] = 0xC5; mem[1] = 0xA3; /* U+0163 */
+        mem[2] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
+        mem[0] = 0x50;
+        mem[1] = 0x50;
+        mem[2] = 0x50;
+        mem[3] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
+        mem[0] = 0x50;
+        mem[1] = 0xC5; mem[2] = 0xA3; /* U+0163 */
+        mem[3] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+        ASSERT (u8_strchr (mem, 0x163) == mem + 1);
+
+        mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
+        mem[0] = 0xE3; mem[1] = 0x91; mem[2] = 0x00; /* U+3450 */
+        mem[3] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
+        mem[0] = 0xE3; mem[1] = 0x92; mem[2] = 0x96; /* U+3496 */
+        mem[3] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 5 * sizeof (UNIT));
+        mem[0] = 0x50;
+        mem[1] = 0x50;
+        mem[2] = 0x50;
+        mem[3] = 0x50;
+        mem[4] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+
+        mem = (UNIT *) (page_boundary - 5 * sizeof (UNIT));
+        mem[0] = 0x50;
+        mem[1] = 0xE3; mem[2] = 0x92; mem[3] = 0x96; /* U+3496 */
+        mem[4] = 0;
+        ASSERT (u8_strchr (mem, 0x55) == NULL);
+        ASSERT (u8_strchr (mem, 0x123) == NULL);
+        ASSERT (u8_strchr (mem, 0x3456) == NULL);
+        ASSERT (u8_strchr (mem, 0x23456) == NULL);
+        ASSERT (u8_strchr (mem, 0x3496) == mem + 1);
+      }
+  }
+
+  return 0;
+}