2 * Copyright (C) 2008, 2009 Free Software Foundation
3 * Written by Eric Blake and Bruno Haible
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #define ASSERT(expr) \
30 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 char *input = malloc (n + 1);
46 memset (input + 2, 'c', 1024);
47 memset (input + 1026, 'd', n - 1028);
52 /* Basic behavior tests. */
53 ASSERT (strchrnul (input, 'a') == input);
54 ASSERT (strchrnul (input, 'b') == input + 1);
55 ASSERT (strchrnul (input, 'c') == input + 2);
56 ASSERT (strchrnul (input, 'd') == input + 1026);
58 ASSERT (strchrnul (input + 1, 'a') == input + n - 1);
59 ASSERT (strchrnul (input + 1, 'e') == input + n - 2);
61 ASSERT (strchrnul (input, 'f') == input + n);
62 ASSERT (strchrnul (input, '\0') == input + n);
64 /* Check that a very long haystack is handled quickly if the byte is
65 found near the beginning. */
67 size_t repeat = 10000;
68 for (; repeat > 0; repeat--)
70 ASSERT (strchrnul (input, 'c') == input + 2);
74 /* Alignment tests. */
77 for (i = 0; i < 32; i++)
79 for (j = 0; j < 256; j++)
80 input[i + j] = (j + 1) & 0xff;
81 for (j = 1; j < 256; j++)
83 ASSERT (strchrnul (input + i, j) == input + i + j - 1);
84 input[i + j - 1] = (j == 1 ? 2 : 1);
85 ASSERT (strchrnul (input + i, j) == input + i + 255);