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. */
26 uint32_t *input32 = (uint32_t *) malloc (n * sizeof (uint32_t));
31 u32_set (input32 + 2, 'c', 1024);
32 for (i = 1026; i < n - 2; i += 63)
34 size_t last = i + 63 < n - 2 ? i + 63 : n - 2;
35 ucs4_t uc = 'd' | (i - 1026);
36 if (uc >= 0xd800 && uc <= 0xdfff)
38 u32_set (input32 + i, uc, last - i);
44 input = U32_TO_U (input32, n, NULL, &length);
47 /* Basic behavior tests. */
48 ASSERT (U_CHR (input, length, 'a') == input);
50 ASSERT (U_CHR (input, 0, 'a') == NULL);
51 ASSERT (U_CHR (zerosize_ptr (), 0, 'a') == NULL);
53 ASSERT (U_CHR (input, length, 'b') == input + 1);
54 ASSERT (U_CHR (input, length, 'c') == input + 2);
55 ASSERT (U_CHR (input, length, 'd') == input + 1026);
57 ASSERT (U_CHR (input + 1, length - 1, 'a') == input + length - 1);
58 ASSERT (U_CHR (input + 1, length - 1, 'e') == input + length - 2);
60 ASSERT (U_CHR (input, length, 'f') == NULL);
61 ASSERT (U_CHR (input, length, '\0') == NULL);
63 /* Check that a very long haystack is handled quickly if the byte is
64 found near the beginning. */
66 size_t repeat = 10000;
67 for (; repeat > 0; repeat--)
69 ASSERT (U_CHR (input, length, 'c') == input + 2);
73 /* Alignment tests. */
76 for (i = 0; i < 32; i++)
78 for (j = 0; j < 128; j++)
80 for (j = 0; j < 128; j++)
82 ASSERT (U_CHR (input + i, 128, j) == input + i + j);
87 /* Check that uN_chr() does not read past the first occurrence of the
88 byte being searched. */
90 char *page_boundary = (char *) zerosize_ptr ();
92 if (page_boundary != NULL)
94 for (n = 1; n <= 500 / sizeof (UNIT); n++)
96 UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
98 ASSERT (U_CHR (mem, n, 'U') == NULL);
103 for (i = 0; i < n; i++)
106 ASSERT (U_CHR (mem, 4000, 'U') == mem + i);