Bump to 1.14.1
[platform/upstream/augeas.git] / tests / unistr / test-u-strtok.h
1 /* Test of uN_strtok() functions.
2    Copyright (C) 2015-2016 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 static void
18 test_u_strtok (void)
19 {
20   {
21     UNIT input[] = { 'f', 'o', 'o', 0 };
22     const UNIT delim[] = { 0 };
23     UNIT *state;
24     const UNIT *result = U_STRTOK (input, delim, &state);
25     ASSERT (result == input);
26   }
27
28   {
29     UNIT input[] =
30       { 'A', 'B', 'C', ' ', 'A', 'B', 'C', 'D', 'A', 'B', ' ', '\t',
31         'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'D', 'E', 0
32       };
33     const UNIT delim[] = { ' ', '\t', 0 };
34     UNIT *state;
35     const UNIT *result;
36     result = U_STRTOK (input, delim, &state);
37     ASSERT (result == input);
38     result = U_STRTOK (NULL, delim, &state);
39     ASSERT (result == input + 4);
40     result = U_STRTOK (NULL, delim, &state);
41     ASSERT (result == input + 12);
42     result = U_STRTOK (NULL, delim, &state);
43     ASSERT (result == NULL);
44   }
45
46   /* Check for multibyte delimiters.  */
47   {
48     ucs4_t u_input[] =
49       { 'A', 'B', 'C', 0x3000, 'A', 'B', 'C', 'D', 'A', 'B', 0x3000, 0x3001,
50         'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'D', 'E', 0
51       };
52     ucs4_t u_delim[] = { 0x3000, 0x3001, 0 };
53     size_t input_len = 6 * SIZEOF (u_input);
54     UNIT *input = (UNIT *) malloc (input_len);
55     size_t delim_len = 6 * SIZEOF (u_delim);
56     UNIT *delim = (UNIT *) malloc (delim_len);
57     UNIT *state;
58     const UNIT *result;
59     UNIT *ptr, *first_ptr, *second_ptr;
60     size_t i;
61     for (i = 0, ptr = input; i < SIZEOF (u_input) && u_input[i] != 0; i++)
62       {
63         int ret = U_UCTOMB (ptr, u_input[i], input_len - (ptr - input));
64         if (i == 4)
65           first_ptr = ptr;
66         if (i == 12)
67           second_ptr = ptr;
68         ptr += ret;
69       }
70     *ptr = 0;
71     for (i = 0, ptr = delim; i < SIZEOF (u_delim) && u_delim[i] != 0; i++)
72       {
73         int ret = U_UCTOMB (ptr, u_delim[i], delim_len - (ptr - delim));
74         ptr += ret;
75       }
76     *ptr = 0;
77     result = U_STRTOK (input, delim, &state);
78     ASSERT (result == input);
79     result = U_STRTOK (NULL, delim, &state);
80     ASSERT (result == first_ptr);
81     result = U_STRTOK (NULL, delim, &state);
82     ASSERT (result == second_ptr);
83     result = U_STRTOK (NULL, delim, &state);
84     ASSERT (result == NULL);
85     free (input);
86     free (delim);
87   }
88 }