unistr/u*-chr: prepare for multibyte tests
[gnulib.git] / tests / unistr / test-chr.h
1 /* Test of uN_chr() functions.
2    Copyright (C) 2008-2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Eric Blake and Bruno Haible <bruno@clisp.org>, 2010.  */
18
19 int
20 main (void)
21 {
22   size_t n = 0x100000;
23   size_t length;
24   UNIT *input;
25   uint32_t *input32 = (uint32_t *) malloc (n * sizeof (uint32_t));
26   ASSERT (input32);
27
28   input32[0] = 'a';
29   input32[1] = 'b';
30   u32_set (input32 + 2, 'c', 1024);
31   u32_set (input32 + 1026, 'd', n - 1028);
32   input32[n - 2] = 'e';
33   input32[n - 1] = 'a';
34
35   input = U32_TO_U (input32, n, NULL, &length);
36   ASSERT (input);
37
38   /* Basic behavior tests.  */
39   ASSERT (U_CHR (input, n, 'a') == input);
40
41   ASSERT (U_CHR (input, 0, 'a') == NULL);
42   ASSERT (U_CHR (zerosize_ptr (), 0, 'a') == NULL);
43
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);
47
48   ASSERT (U_CHR (input + 1, n - 1, 'a') == input + n - 1);
49   ASSERT (U_CHR (input + 1, n - 1, 'e') == input + n - 2);
50
51   ASSERT (U_CHR (input, n, 'f') == NULL);
52   ASSERT (U_CHR (input, n, '\0') == NULL);
53
54   /* Check that a very long haystack is handled quickly if the byte is
55      found near the beginning.  */
56   {
57     size_t repeat = 10000;
58     for (; repeat > 0; repeat--)
59       {
60         ASSERT (U_CHR (input, n, 'c') == input + 2);
61       }
62   }
63
64   /* Alignment tests.  */
65   {
66     int i, j;
67     for (i = 0; i < 32; i++)
68       {
69         for (j = 0; j < 128; j++)
70           input[i + j] = j;
71         for (j = 0; j < 128; j++)
72           {
73             ASSERT (U_CHR (input + i, 128, j) == input + i + j);
74           }
75       }
76   }
77
78   /* Check that uN_chr() does not read past the first occurrence of the
79      byte being searched.  */
80   {
81     char *page_boundary = (char *) zerosize_ptr ();
82
83     if (page_boundary != NULL)
84       {
85         for (n = 1; n <= 500 / sizeof (UNIT); n++)
86           {
87             UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
88             U_SET (mem, 'X', n);
89             ASSERT (U_CHR (mem, n, 'U') == NULL);
90
91             {
92               size_t i;
93
94               for (i = 0; i < n; i++)
95                 {
96                   mem[i] = 'U';
97                   ASSERT (U_CHR (mem, 4000, 'U') == mem + i);
98                   mem[i] = 'X';
99                 }
100             }
101           }
102       }
103   }
104
105   free (input);
106
107   return 0;
108 }