bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 4/5] unistr/u*-strchr: add tests


From: bonzini
Subject: [PATCH v2 4/5] unistr/u*-strchr: add tests
Date: Fri, 23 Jul 2010 08:53:51 +0200

From: Paolo Bonzini <address@hidden>

* modules/unistr/u16-strchr-tests, modules/unistr/u32-strchr-tests,
modules/unistr/u8-strchr-tests, tests/unistr/test-strchr.h,
tests/unistr/test-u16-strchr.c, tests/unistr/test-u32-strchr.c,
tests/unistr/test-u8-strchr.c: New files.
---
 modules/unistr/u16-strchr-tests |   22 +++++++
 modules/unistr/u32-strchr-tests |   22 +++++++
 modules/unistr/u8-strchr-tests  |   22 +++++++
 tests/unistr/test-strchr.h      |  128 +++++++++++++++++++++++++++++++++++++++
 tests/unistr/test-u16-strchr.c  |   33 ++++++++++
 tests/unistr/test-u32-strchr.c  |   33 ++++++++++
 tests/unistr/test-u8-strchr.c   |   33 ++++++++++
 7 files changed, 293 insertions(+), 0 deletions(-)
 create mode 100644 modules/unistr/u16-strchr-tests
 create mode 100644 modules/unistr/u32-strchr-tests
 create mode 100644 modules/unistr/u8-strchr-tests
 create mode 100644 tests/unistr/test-strchr.h
 create mode 100644 tests/unistr/test-u16-strchr.c
 create mode 100644 tests/unistr/test-u32-strchr.c
 create mode 100644 tests/unistr/test-u8-strchr.c

diff --git a/modules/unistr/u16-strchr-tests b/modules/unistr/u16-strchr-tests
new file mode 100644
index 0000000..8ac0078
--- /dev/null
+++ b/modules/unistr/u16-strchr-tests
@@ -0,0 +1,22 @@
+Files:
+tests/unistr/test-u16-strchr.c
+tests/unistr/test-strchr.h
+tests/zerosize-ptr.h
+tests/macros.h
+m4/mmap-anon.m4
+
+Depends-on:
+extensions
+getpagesize
+unistr/u16-set
+
+configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_HEADERS_ONCE([sys/mman.h])
+AC_CHECK_FUNCS_ONCE([mprotect])
+
+Makefile.am:
+TESTS += test-u16-strchr
+check_PROGRAMS += test-u16-strchr
+test_u16_strchr_SOURCES = unistr/test-u16-strchr.c
+test_u16_strchr_LDADD = $(LDADD) $(LIBUNISTRING)
diff --git a/modules/unistr/u32-strchr-tests b/modules/unistr/u32-strchr-tests
new file mode 100644
index 0000000..f8278e9
--- /dev/null
+++ b/modules/unistr/u32-strchr-tests
@@ -0,0 +1,22 @@
+Files:
+tests/unistr/test-u32-strchr.c
+tests/unistr/test-strchr.h
+tests/zerosize-ptr.h
+tests/macros.h
+m4/mmap-anon.m4
+
+Depends-on:
+extensions
+getpagesize
+unistr/u32-set
+
+configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_HEADERS_ONCE([sys/mman.h])
+AC_CHECK_FUNCS_ONCE([mprotect])
+
+Makefile.am:
+TESTS += test-u32-strchr
+check_PROGRAMS += test-u32-strchr
+test_u32_strchr_SOURCES = unistr/test-u32-strchr.c
+test_u32_strchr_LDADD = $(LDADD) $(LIBUNISTRING)
diff --git a/modules/unistr/u8-strchr-tests b/modules/unistr/u8-strchr-tests
new file mode 100644
index 0000000..103671f
--- /dev/null
+++ b/modules/unistr/u8-strchr-tests
@@ -0,0 +1,22 @@
+Files:
+tests/unistr/test-u8-strchr.c
+tests/unistr/test-strchr.h
+tests/zerosize-ptr.h
+tests/macros.h
+m4/mmap-anon.m4
+
+Depends-on:
+extensions
+getpagesize
+unistr/u8-set
+
+configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_HEADERS_ONCE([sys/mman.h])
+AC_CHECK_FUNCS_ONCE([mprotect])
+
+Makefile.am:
+TESTS += test-u8-strchr
+check_PROGRAMS += test-u8-strchr
+test_u8_strchr_SOURCES = unistr/test-u8-strchr.c
+test_u8_strchr_LDADD = $(LDADD) $(LIBUNISTRING)
diff --git a/tests/unistr/test-strchr.h b/tests/unistr/test-strchr.h
new file mode 100644
index 0000000..8f90dc1
--- /dev/null
+++ b/tests/unistr/test-strchr.h
@@ -0,0 +1,128 @@
+/* Test of uN_chr() functions.
+   Copyright (C) 2008-2010 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paolo Bonzini <address@hidden>, 2010.
+   Based on test-chr.h, by Eric Blake and Bruno Haible.  */
+
+int
+main (void)
+{
+  size_t size = 0x100000;
+  size_t length;
+  size_t i;
+  UNIT *input;
+  uint32_t *input32 = (uint32_t *) malloc ((size + 1) * sizeof (uint32_t));
+  ASSERT (input32);
+
+  input32[0] = 'a';
+  input32[1] = 'b';
+  u32_set (input32 + 2, 'c', 1024);
+  for (i = 1026; i < size - 2; i += 63)
+    {
+      size_t last = i + 63 < size - 2 ? i + 63 : size - 2;
+      ucs4_t uc = 'd' | (i - 1026);
+      if (uc >= 0xd800 && uc <= 0xdfff)
+        uc |= 0x100000;
+      u32_set (input32 + i, uc, last - i);
+    }
+  input32[size - 2] = 'e';
+  input32[size - 1] = 'a';
+  input32[size] = 0;
+
+  input = U32_TO_U (input32, size + 1, NULL, &length);
+  ASSERT (input);
+
+  /* Basic behavior tests.  */
+  ASSERT (U_STRCHR (input, 'a') == input);
+  ASSERT (U_STRCHR (input, 'b') == input + 1);
+  ASSERT (U_STRCHR (input, 'c') == input + 2);
+
+  {
+    UNIT *exp = input + 1026;
+    UNIT *prev = input + 2;
+
+    for (i = 1026; i < size - 2; i += 63)
+      {
+        UNIT c[6];
+        size_t n;
+        ucs4_t uc = 'd' | (i - 1026);
+        if (uc >= 0xd800 && uc <= 0xdfff)
+          uc |= 0x100000;
+        n = U_UCTOMB(c, uc, 6);
+        ASSERT (exp < input + length - 2);
+        ASSERT (U_STRCHR (prev, uc) == exp);
+        ASSERT (memcmp (exp, c, n * sizeof (UNIT)) == 0);
+        prev = exp;
+        exp += n * 63;
+      }
+  }
+
+  ASSERT (U_STRCHR (input + 1, 'a') == input + length - 2);
+  ASSERT (U_STRCHR (input + 1, 'e') == input + length - 3);
+
+  ASSERT (U_STRCHR (input, 'f') == NULL);
+  ASSERT (U_STRCHR (input, '\0') == input + length - 1);
+
+  /* Check that a very long haystack is handled quickly if the byte is
+     found near the beginning.  */
+  {
+    size_t repeat = 10000;
+    for (; repeat > 0; repeat--)
+      {
+        ASSERT (U_STRCHR (input, 'c') == input + 2);
+      }
+  }
+
+  /* Alignment tests.  */
+  {
+    int i, j;
+    for (i = 0; i < 32; i++)
+      {
+        for (j = 0; j < 127; j++)
+          input[i + j] = j + 1;
+        input[i + 128] = 0;
+        for (j = 0; j < 127; j++)
+          {
+            ASSERT (U_STRCHR (input + i, j + 1) == input + i + j);
+          }
+      }
+  }
+
+  /* Check that uN_chr() 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 - 2);
+            mem[n - 2] = 0;
+            ASSERT (U_STRCHR (mem, 'U') == NULL);
+            mem[n - 2] = 'X';
+            mem[n - 1] = 0;
+            ASSERT (U_STRCHR (mem, 'U') == NULL);
+          }
+      }
+  }
+
+  free (input);
+
+  return 0;
+}
diff --git a/tests/unistr/test-u16-strchr.c b/tests/unistr/test-u16-strchr.c
new file mode 100644
index 0000000..bb10fee
--- /dev/null
+++ b/tests/unistr/test-u16-strchr.c
@@ -0,0 +1,33 @@
+/* Test of u16_chr() function.
+   Copyright (C) 2010 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paolo Bonzini <address@hidden>, 2010.  */
+
+#include <config.h>
+
+#include "unistr.h"
+
+#include <stdlib.h>
+
+#include "zerosize-ptr.h"
+#include "macros.h"
+
+#define UNIT uint16_t
+#define U_UCTOMB u16_uctomb
+#define U32_TO_U u32_to_u16
+#define U_STRCHR u16_strchr
+#define U_SET u16_set
+#include "test-strchr.h"
diff --git a/tests/unistr/test-u32-strchr.c b/tests/unistr/test-u32-strchr.c
new file mode 100644
index 0000000..94c7937
--- /dev/null
+++ b/tests/unistr/test-u32-strchr.c
@@ -0,0 +1,33 @@
+/* Test of u32_chr() function.
+   Copyright (C) 2010 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paolo Bonzini <address@hidden>, 2010.  */
+
+#include <config.h>
+
+#include "unistr.h"
+
+#include <stdlib.h>
+
+#include "zerosize-ptr.h"
+#include "macros.h"
+
+#define UNIT uint32_t
+#define U_UCTOMB(s, uc, n) (*(s) = (uc), 1)
+#define U32_TO_U(s, n, result, length) (*(length) = (n), (s))
+#define U_STRCHR u32_strchr
+#define U_SET u32_set
+#include "test-strchr.h"
diff --git a/tests/unistr/test-u8-strchr.c b/tests/unistr/test-u8-strchr.c
new file mode 100644
index 0000000..a4dfa07
--- /dev/null
+++ b/tests/unistr/test-u8-strchr.c
@@ -0,0 +1,33 @@
+/* Test of u8_chr() function.
+   Copyright (C) 2010 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paolo Bonzini <address@hidden>, 2010.  */
+
+#include <config.h>
+
+#include "unistr.h"
+
+#include <stdlib.h>
+
+#include "zerosize-ptr.h"
+#include "macros.h"
+
+#define UNIT uint8_t
+#define U_UCTOMB u8_uctomb
+#define U32_TO_U u32_to_u8
+#define U_STRCHR u8_strchr
+#define U_SET u8_set
+#include "test-strchr.h"
-- 
1.7.1





reply via email to

[Prev in Thread] Current Thread [Next in Thread]