usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-strchrnul.c
blobfdb48c9bfaca0c09e3c767b09f1b2377ee251119
1 /*
2 * Copyright (C) 2008-2024 Free Software Foundation, Inc.
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 <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include <string.h>
22 #include "signature.h"
23 SIGNATURE_CHECK (strchrnul, char *, (char const *, int));
25 #include <stdlib.h>
27 #include "macros.h"
29 int
30 main (void)
32 size_t n = 0x100000;
33 char *input = malloc (n + 1);
34 ASSERT (input);
36 input[0] = 'a';
37 input[1] = 'b';
38 memset (input + 2, 'c', 1024);
39 memset (input + 1026, 'd', n - 1028);
40 input[n - 2] = 'e';
41 input[n - 1] = 'a';
42 input[n] = '\0';
44 /* Basic behavior tests. */
45 ASSERT (strchrnul (input, 'a') == input);
46 ASSERT (strchrnul (input, 'b') == input + 1);
47 ASSERT (strchrnul (input, 'c') == input + 2);
48 ASSERT (strchrnul (input, 'd') == input + 1026);
50 ASSERT (strchrnul (input + 1, 'a') == input + n - 1);
51 ASSERT (strchrnul (input + 1, 'e') == input + n - 2);
53 ASSERT (strchrnul (input, 'f') == input + n);
54 ASSERT (strchrnul (input, '\0') == input + n);
56 /* Check that a very long haystack is handled quickly if the byte is
57 found near the beginning. */
59 size_t repeat = 10000;
60 for (; repeat > 0; repeat--)
62 ASSERT (strchrnul (input, 'c') == input + 2);
66 /* Alignment tests. */
68 int i, j;
69 for (i = 0; i < 32; i++)
71 for (j = 0; j < 256; j++)
72 input[i + j] = (j + 1) & 0xff;
73 for (j = 1; j < 256; j++)
75 ASSERT (strchrnul (input + i, j) == input + i + j - 1);
76 input[i + j - 1] = (j == 1 ? 2 : 1);
77 ASSERT (strchrnul (input + i, j) == input + i + 255);
78 input[i + j - 1] = j;
83 free (input);
85 return test_exit_status;