Imported Upstream version 0.9.4
[platform/upstream/libunistring.git] / tests / unistr / test-chr.h
1 /* Test of uN_chr() functions.
2    Copyright (C) 2008-2014 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 size = 0x100000;
23   size_t i;
24   size_t length;
25   UNIT *input;
26   uint32_t *input32 = (uint32_t *) malloc (size * sizeof (uint32_t));
27   ASSERT (input32);
28
29   input32[0] = 'a';
30   input32[1] = 'b';
31   u32_set (input32 + 2, 'c', 1024);
32   for (i = 1026; i < size - 2; i += 63)
33     {
34       size_t last = i + 63 < size - 2 ? i + 63 : size - 2;
35       ucs4_t uc = 'd' | (i - 1026);
36       if (uc >= 0xd800 && uc <= 0xdfff)
37         uc |= 0x100000;
38       u32_set (input32 + i, uc, last - i);
39     }
40
41   input32[size - 2] = 'e';
42   input32[size - 1] = 'a';
43
44   input = U32_TO_U (input32, size, NULL, &length);
45   ASSERT (input);
46
47   /* Basic behavior tests.  */
48   ASSERT (U_CHR (input, length, 'a') == input);
49
50   ASSERT (U_CHR (input, 0, 'a') == NULL);
51   ASSERT (U_CHR (zerosize_ptr (), 0, 'a') == NULL);
52
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);
56
57   {
58     UNIT *exp = input + 1026;
59     UNIT *prev = input + 1;
60     for (i = 1026; i < size - 2; i += 63)
61       {
62         UNIT c[6];
63         size_t n;
64         ucs4_t uc = 'd' | (i - 1026);
65         if (uc >= 0xd800 && uc <= 0xdfff)
66           uc |= 0x100000;
67         n = U_UCTOMB (c, uc, 6);
68         ASSERT (exp < input + length - 1);
69         ASSERT (U_CHR (prev, (length - 1) - (prev - input), uc) == exp);
70         ASSERT (memcmp (exp, c, n * sizeof (UNIT)) == 0);
71         prev = exp;
72         exp += n * 63;
73       }
74   }
75
76   ASSERT (U_CHR (input + 1, length - 1, 'a') == input + length - 1);
77   ASSERT (U_CHR (input + 1, length - 1, 'e') == input + length - 2);
78
79   ASSERT (U_CHR (input, length, 'f') == NULL);
80   ASSERT (U_CHR (input, length, '\0') == NULL);
81
82   /* Check that a very long haystack is handled quickly if the byte is
83      found near the beginning.  */
84   {
85     size_t repeat = 10000;
86     for (; repeat > 0; repeat--)
87       {
88         ASSERT (U_CHR (input, length, 'c') == input + 2);
89       }
90   }
91
92   /* Alignment tests.  */
93   {
94     int i, j;
95     for (i = 0; i < 32; i++)
96       {
97         for (j = 0; j < 128; j++)
98           input[i + j] = j;
99         for (j = 0; j < 128; j++)
100           {
101             ASSERT (U_CHR (input + i, 128, j) == input + i + j);
102           }
103       }
104   }
105
106   /* Check that uN_chr() does not read past the first occurrence of the
107      byte being searched.  */
108   {
109     char *page_boundary = (char *) zerosize_ptr ();
110     size_t n;
111
112     if (page_boundary != NULL)
113       {
114         for (n = 1; n <= 500 / sizeof (UNIT); n++)
115           {
116             UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT));
117             U_SET (mem, 'X', n);
118             ASSERT (U_CHR (mem, n, 'U') == NULL);
119
120             {
121               size_t i;
122
123               for (i = 0; i < n; i++)
124                 {
125                   mem[i] = 'U';
126                   ASSERT (U_CHR (mem, 4000, 'U') == mem + i);
127                   mem[i] = 'X';
128                 }
129             }
130           }
131       }
132   }
133
134   free (input);
135
136   return 0;
137 }