annotate tests/test-memchr.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 10eb9086bea0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /*
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 * Copyright (C) 2008-2019 Free Software Foundation, Inc.
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 * Written by Eric Blake and Bruno Haible
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 *
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 * the Free Software Foundation; either version 3 of the License, or
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 * (at your option) any later version.
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 *
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 * GNU General Public License for more details.
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 *
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
16 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <string.h>
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
22 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
23 SIGNATURE_CHECK (memchr, void *, (void const *, int, size_t));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
24
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdlib.h>
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
11570
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 10442
diff changeset
27 #include "zerosize-ptr.h"
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
28 #include "macros.h"
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 /* Calculating void * + int is not portable, so this wrapper converts
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 to char * to make the tests easier to write. */
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #define MEMCHR (char *) memchr
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 int
12197
e45d9bb2233e tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents: 11598
diff changeset
35 main (void)
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 size_t n = 0x100000;
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 char *input = malloc (n);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 ASSERT (input);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 input[0] = 'a';
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 input[1] = 'b';
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 memset (input + 2, 'c', 1024);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 memset (input + 1026, 'd', n - 1028);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 input[n - 2] = 'e';
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 input[n - 1] = 'a';
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /* Basic behavior tests. */
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 ASSERT (MEMCHR (input, 'a', n) == input);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 ASSERT (MEMCHR (input, 'a', 0) == NULL);
11570
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 10442
diff changeset
52 ASSERT (MEMCHR (zerosize_ptr (), 'a', 0) == NULL);
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 ASSERT (MEMCHR (input, 'b', n) == input + 1);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 ASSERT (MEMCHR (input, 'c', n) == input + 2);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 ASSERT (MEMCHR (input, 'd', n) == input + 1026);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 ASSERT (MEMCHR (input + 1, 'a', n - 1) == input + n - 1);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 ASSERT (MEMCHR (input + 1, 'e', n - 1) == input + n - 2);
13678
794abd047acd test-rawmemchr: make more robust
Eric Blake <eblake@redhat.com>
parents: 13677
diff changeset
60 ASSERT (MEMCHR (input + 1, 0x789abc00 | 'e', n - 1) == input + n - 2);
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 ASSERT (MEMCHR (input, 'f', n) == NULL);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 ASSERT (MEMCHR (input, '\0', n) == NULL);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 /* Check that a very long haystack is handled quickly if the byte is
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 found near the beginning. */
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 {
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 size_t repeat = 10000;
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 for (; repeat > 0; repeat--)
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
71 ASSERT (MEMCHR (input, 'c', n) == input + 2);
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 }
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 /* Alignment tests. */
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 {
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 int i, j;
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 for (i = 0; i < 32; i++)
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
80 for (j = 0; j < 256; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
81 input[i + j] = j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
82 for (j = 0; j < 256; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
83 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
84 ASSERT (MEMCHR (input + i, j, 256) == input + i + j);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
85 }
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 }
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 }
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
11598
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
89 /* Check that memchr() does not read past the first occurrence of the
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
90 byte being searched. See the Austin Group's clarification
13677
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
91 <http://www.opengroup.org/austin/docs/austin_454.txt>.
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
92 Test both '\0' and something else, since some implementations
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
93 special-case searching for NUL.
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
94 */
11598
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
95 {
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
96 char *page_boundary = (char *) zerosize_ptr ();
13677
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
97 /* Too small, and we miss cache line boundary tests; too large,
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
98 and the test takes cubically longer to complete. */
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
99 int limit = 257;
11598
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
100
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
101 if (page_boundary != NULL)
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
102 {
13677
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
103 for (n = 1; n <= limit; n++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
104 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
105 char *mem = page_boundary - n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
106 memset (mem, 'X', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
107 ASSERT (MEMCHR (mem, 'U', n) == NULL);
13677
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
108 ASSERT (MEMCHR (mem, 0, n) == NULL);
11598
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
109
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
110 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
111 size_t i;
13677
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
112 size_t k;
11598
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
113
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
114 for (i = 0; i < n; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
115 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
116 mem[i] = 'U';
13677
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
117 for (k = i + 1; k < n + limit; k++)
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
118 ASSERT (MEMCHR (mem, 'U', k) == mem + i);
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
119 mem[i] = 0;
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
120 for (k = i + 1; k < n + limit; k++)
8ac4aae842ca memchr: detect glibc Alpha bug
Eric Blake <eblake@redhat.com>
parents: 12825
diff changeset
121 ASSERT (MEMCHR (mem, 0, k) == mem + i);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
122 mem[i] = 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
123 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
124 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12210
diff changeset
125 }
11598
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
126 }
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
127 }
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
128
9974
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 free (input);
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 return 0;
4e68e73d390a Tests for module 'memchr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 }