From: Paolo Bonzini Date: Tue, 20 Jul 2010 13:43:26 +0000 (+0200) Subject: unistr/u*-strchr: add tests X-Git-Tag: v0.1~4013 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=ad9a24a121ab3154f30d9d38d8243f41566d151e;hp=26947b67c9e1e45a836d81613d0c4fa2fe6cf51a;p=gnulib.git unistr/u*-strchr: add tests * 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. --- diff --git a/modules/unistr/u16-strchr-tests b/modules/unistr/u16-strchr-tests new file mode 100644 index 000000000..8ac007856 --- /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 000000000..f8278e921 --- /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 000000000..103671f97 --- /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 000000000..f280406eb --- /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 . */ + +/* Written by Paolo Bonzini , 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 000000000..c3d5111d8 --- /dev/null +++ b/tests/unistr/test-u16-strchr.c @@ -0,0 +1,33 @@ +/* Test of u16_strchr() 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 . */ + +/* Written by Paolo Bonzini , 2010. */ + +#include + +#include "unistr.h" + +#include + +#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 000000000..f2b3a1644 --- /dev/null +++ b/tests/unistr/test-u32-strchr.c @@ -0,0 +1,33 @@ +/* Test of u32_strchr() 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 . */ + +/* Written by Paolo Bonzini , 2010. */ + +#include + +#include "unistr.h" + +#include + +#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 000000000..5491a71cd --- /dev/null +++ b/tests/unistr/test-u8-strchr.c @@ -0,0 +1,33 @@ +/* Test of u8_strchr() 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 . */ + +/* Written by Paolo Bonzini , 2010. */ + +#include + +#include "unistr.h" + +#include + +#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"