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. */
23 UNIT *input = (UNIT *) malloc (n * sizeof (UNIT));
28 U_SET (input + 2, 'c', 1024);
29 U_SET (input + 1026, 'd', n - 1028);
33 /* Basic behavior tests. */
34 ASSERT (U_CHR (input, n, 'a') == input);
36 ASSERT (U_CHR (input, 0, 'a') == NULL);
37 ASSERT (U_CHR (zerosize_ptr (), 0, 'a') == NULL);
39 ASSERT (U_CHR (input, n, 'b') == input + 1);
40 ASSERT (U_CHR (input, n, 'c') == input + 2);
41 ASSERT (U_CHR (input, n, 'd') == input + 1026);
43 ASSERT (U_CHR (input + 1, n - 1, 'a') == input + n - 1);
44 ASSERT (U_CHR (input + 1, n - 1, 'e') == input + n - 2);
46 ASSERT (U_CHR (input, n, 'f') == NULL);
47 ASSERT (U_CHR (input, n, '\0') == NULL);
49 /* Check that a very long haystack is handled quickly if the byte is
50 found near the beginning. */
52 size_t repeat = 10000;
53 for (; repeat > 0; repeat--)
55 ASSERT (U_CHR (input, n, 'c') == input + 2);
59 /* Alignment tests. */
62 for (i = 0; i < 32; i++)
64 for (j = 0; j < 128; j++)
66 for (j = 0; j < 128; j++)
68 ASSERT (U_CHR (input + i, 128, j) == input + i + j);
73 /* Check that uN_chr() does not read past the first occurrence of the
74 byte being searched. */
76 char *page_boundary = (char *) zerosize_ptr ();
78 if (page_boundary != NULL)
80 for (n = 1; n <= 500 / sizeof (UNIT); n++)
82 UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
84 ASSERT (U_CHR (mem, n, 'U') == NULL);
89 for (i = 0; i < n; i++)
92 ASSERT (U_CHR (mem, 4000, 'U') == mem + i);