1 /* Test of uN_chr() functions.
2 Copyright (C) 2008-2010 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 Eric Blake and Bruno Haible <bruno@clisp.org>, 2010. */
25 uint32_t *input32 = (uint32_t *) malloc (n * sizeof (uint32_t));
30 u32_set (input32 + 2, 'c', 1024);
31 u32_set (input32 + 1026, 'd', n - 1028);
35 input = U32_TO_U (input32, n, NULL, &length);
38 /* Basic behavior tests. */
39 ASSERT (U_CHR (input, n, 'a') == input);
41 ASSERT (U_CHR (input, 0, 'a') == NULL);
42 ASSERT (U_CHR (zerosize_ptr (), 0, 'a') == NULL);
44 ASSERT (U_CHR (input, n, 'b') == input + 1);
45 ASSERT (U_CHR (input, n, 'c') == input + 2);
46 ASSERT (U_CHR (input, n, 'd') == input + 1026);
48 ASSERT (U_CHR (input + 1, n - 1, 'a') == input + n - 1);
49 ASSERT (U_CHR (input + 1, n - 1, 'e') == input + n - 2);
51 ASSERT (U_CHR (input, n, 'f') == NULL);
52 ASSERT (U_CHR (input, n, '\0') == NULL);
54 /* Check that a very long haystack is handled quickly if the byte is
55 found near the beginning. */
57 size_t repeat = 10000;
58 for (; repeat > 0; repeat--)
60 ASSERT (U_CHR (input, n, 'c') == input + 2);
64 /* Alignment tests. */
67 for (i = 0; i < 32; i++)
69 for (j = 0; j < 128; j++)
71 for (j = 0; j < 128; j++)
73 ASSERT (U_CHR (input + i, 128, j) == input + i + j);
78 /* Check that uN_chr() does not read past the first occurrence of the
79 byte being searched. */
81 char *page_boundary = (char *) zerosize_ptr ();
83 if (page_boundary != NULL)
85 for (n = 1; n <= 500 / sizeof (UNIT); n++)
87 UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
89 ASSERT (U_CHR (mem, n, 'U') == NULL);
94 for (i = 0; i < n; i++)
97 ASSERT (U_CHR (mem, 4000, 'U') == mem + i);