Whitespace cleanup.
[gnulib.git] / tests / test-strchrnul.c
1 /*
2  * Copyright (C) 2008 Free Software Foundation
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 <stdio.h>
23 #include <stdlib.h>
24
25 #define ASSERT(expr) \
26   do                                                                        \
27     {                                                                       \
28       if (!(expr))                                                      \
29         {                                                               \
30           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
31           fflush (stderr);                                              \
32           abort ();                                                     \
33         }                                                               \
34     }                                                                   \
35   while (0)
36
37 int
38 main ()
39 {
40   size_t n = 0x100000;
41   char *input = malloc (n + 1);
42   ASSERT (input);
43
44   input[0] = 'a';
45   input[1] = 'b';
46   memset (input + 2, 'c', 1024);
47   memset (input + 1026, 'd', n - 1028);
48   input[n - 2] = 'e';
49   input[n - 1] = 'a';
50   input[n] = '\0';
51
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);
57
58   ASSERT (strchrnul (input + 1, 'a') == input + n - 1);
59   ASSERT (strchrnul (input + 1, 'e') == input + n - 2);
60
61   ASSERT (strchrnul (input, 'f') == input + n);
62   ASSERT (strchrnul (input, '\0') == input + n);
63
64   /* Check that a very long haystack is handled quickly if the byte is
65      found near the beginning.  */
66   {
67     size_t repeat = 10000;
68     for (; repeat > 0; repeat--)
69       {
70         ASSERT (strchrnul (input, 'c') == input + 2);
71       }
72   }
73
74   /* Alignment tests.  */
75   {
76     int i, j;
77     for (i = 0; i < 32; i++)
78       {
79         for (j = 0; j < 256; j++)
80           input[i + j] = (j + 1) & 0xff;
81         for (j = 1; j < 256; j++)
82           {
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);
86             input[i + j - 1] = j;
87           }
88       }
89   }
90
91   free (input);
92
93   return 0;
94 }