1 /* Test of uN_strchr() functions.
2 Copyright (C) 2008-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paolo Bonzini <bonzini@gnu.org>, 2010.
18 Based on test-chr.h, by Eric Blake and Bruno Haible. */
23 size_t size = 0x100000;
27 uint32_t *input32 = (uint32_t *) malloc ((size + 1) * sizeof (uint32_t));
32 u32_set (input32 + 2, 'c', 1024);
33 for (i = 1026; i < size - 2; i += 63)
35 size_t last = i + 63 < size - 2 ? i + 63 : size - 2;
36 ucs4_t uc = 'd' | (i - 1026);
37 if (uc >= 0xd800 && uc <= 0xdfff)
39 u32_set (input32 + i, uc, last - i);
41 input32[size - 2] = 'e';
42 input32[size - 1] = 'a';
45 input = U32_TO_U (input32, size + 1, NULL, &length);
48 /* Basic behavior tests. */
49 ASSERT (U_STRCHR (input, 'a') == input);
50 ASSERT (U_STRCHR (input, 'b') == input + 1);
51 ASSERT (U_STRCHR (input, 'c') == input + 2);
54 UNIT *exp = input + 1026;
55 UNIT *prev = input + 2;
57 for (i = 1026; i < size - 2; i += 63)
61 ucs4_t uc = 'd' | (i - 1026);
62 if (uc >= 0xd800 && uc <= 0xdfff)
64 n = U_UCTOMB (c, uc, 6);
65 ASSERT (exp < input + length - 2);
66 ASSERT (U_STRCHR (prev, uc) == exp);
67 ASSERT (memcmp (exp, c, n * sizeof (UNIT)) == 0);
73 ASSERT (U_STRCHR (input + 1, 'a') == input + length - 2);
74 ASSERT (U_STRCHR (input + 1, 'e') == input + length - 3);
76 ASSERT (U_STRCHR (input, 'f') == NULL);
77 ASSERT (U_STRCHR (input, '\0') == input + length - 1);
79 /* Check that a very long haystack is handled quickly if the byte is
80 found near the beginning. */
82 size_t repeat = 10000;
83 for (; repeat > 0; repeat--)
85 ASSERT (U_STRCHR (input, 'c') == input + 2);
89 /* Alignment tests. */
92 for (i = 0; i < 32; i++)
94 for (j = 0; j < 127; j++)
97 for (j = 0; j < 127; j++)
99 ASSERT (U_STRCHR (input + i, j + 1) == input + i + j);
104 /* Check that uN_strchr() does not read past the end of the string. */
106 char *page_boundary = (char *) zerosize_ptr ();
109 if (page_boundary != NULL)
111 for (n = 2; n <= 500 / sizeof (UNIT); n++)
113 UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
114 U_SET (mem, 'X', n - 2);
116 ASSERT (U_STRCHR (mem, 'U') == NULL);
119 ASSERT (U_STRCHR (mem, 'U') == NULL);
125 /* This check is disabled, because uN_strchr() is allowed to read past the
126 first occurrence of the byte being searched. In fact, u8_strchr() does
127 so, on i586 glibc systems: u8_strchr calls strchr, which in
128 glibc/sysdeps/i386/i586/strchr.S loads the second word before the
129 handling of the first word has been completed. */
130 /* Check that uN_strchr() does not read past the first occurrence of the
131 byte being searched. */
133 char *page_boundary = (char *) zerosize_ptr ();
136 if (page_boundary != NULL)
138 for (n = 2; n <= 500 / sizeof (UNIT); n++)
140 UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
141 U_SET (mem, 'X', n - 1);
143 ASSERT (U_STRCHR (mem, 'U') == NULL);
148 for (i = 0; i < n; i++)
151 ASSERT (U_STRCHR (mem, 'U') == mem + i);