maint: update copyright
[gnulib.git] / tests / test-strchrnul.c
1 /*
2  * Copyright (C) 2008-2014 Free Software Foundation, Inc.
3  * Written by Eric Blake and Bruno Haible
4  *
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.
9  *
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.
14  *
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/>.  */
17
18 #include <config.h>
19
20 #include <string.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (strchrnul, char *, (char const *, int));
24
25 #include <stdlib.h>
26
27 #include "macros.h"
28
29 int
30 main (void)
31 {
32   size_t n = 0x100000;
33   char *input = malloc (n + 1);
34   ASSERT (input);
35
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';
43
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);
49
50   ASSERT (strchrnul (input + 1, 'a') == input + n - 1);
51   ASSERT (strchrnul (input + 1, 'e') == input + n - 2);
52
53   ASSERT (strchrnul (input, 'f') == input + n);
54   ASSERT (strchrnul (input, '\0') == input + n);
55
56   /* Check that a very long haystack is handled quickly if the byte is
57      found near the beginning.  */
58   {
59     size_t repeat = 10000;
60     for (; repeat > 0; repeat--)
61       {
62         ASSERT (strchrnul (input, 'c') == input + 2);
63       }
64   }
65
66   /* Alignment tests.  */
67   {
68     int i, j;
69     for (i = 0; i < 32; i++)
70       {
71         for (j = 0; j < 256; j++)
72           input[i + j] = (j + 1) & 0xff;
73         for (j = 1; j < 256; j++)
74           {
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;
79           }
80       }
81   }
82
83   free (input);
84
85   return 0;
86 }