Mercurial > hg > octave-lojdl > gnulib-hg
annotate tests/test-memchr.c @ 14476:7e83803507fe
New module 'unictype/category-longname'.
* lib/unictype.in.h (uc_general_category_long_name): New declaration.
* lib/unictype/categ_longname.c: New file.
* modules/unictype/category-longname: New file.
* modules/unictype/category-all (Depends-on): Add it.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Fri, 25 Mar 2011 23:14:10 +0100 |
parents | 97fc9a21a8fb |
children | 8250f2777afc |
rev | line source |
---|---|
9974 | 1 /* |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
13678
diff
changeset
|
2 * Copyright (C) 2008-2011 Free Software Foundation, Inc. |
9974 | 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 | |
12489 | 22 #include "signature.h" |
23 SIGNATURE_CHECK (memchr, void *, (void const *, int, size_t)); | |
24 | |
9974 | 25 #include <stdlib.h> |
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 | 29 |
30 /* Calculating void * + int is not portable, so this wrapper converts | |
31 to char * to make the tests easier to write. */ | |
32 #define MEMCHR (char *) memchr | |
33 | |
34 int | |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11598
diff
changeset
|
35 main (void) |
9974 | 36 { |
37 size_t n = 0x100000; | |
38 char *input = malloc (n); | |
39 ASSERT (input); | |
40 | |
41 input[0] = 'a'; | |
42 input[1] = 'b'; | |
43 memset (input + 2, 'c', 1024); | |
44 memset (input + 1026, 'd', n - 1028); | |
45 input[n - 2] = 'e'; | |
46 input[n - 1] = 'a'; | |
47 | |
48 /* Basic behavior tests. */ | |
49 ASSERT (MEMCHR (input, 'a', n) == input); | |
50 | |
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 | 53 |
54 ASSERT (MEMCHR (input, 'b', n) == input + 1); | |
55 ASSERT (MEMCHR (input, 'c', n) == input + 2); | |
56 ASSERT (MEMCHR (input, 'd', n) == input + 1026); | |
57 | |
58 ASSERT (MEMCHR (input + 1, 'a', n - 1) == input + n - 1); | |
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 | 61 |
62 ASSERT (MEMCHR (input, 'f', n) == NULL); | |
63 ASSERT (MEMCHR (input, '\0', n) == NULL); | |
64 | |
65 /* Check that a very long haystack is handled quickly if the byte is | |
66 found near the beginning. */ | |
67 { | |
68 size_t repeat = 10000; | |
69 for (; repeat > 0; repeat--) | |
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 | 72 } |
73 } | |
74 | |
75 /* Alignment tests. */ | |
76 { | |
77 int i, j; | |
78 for (i = 0; i < 32; i++) | |
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 | 86 } |
87 } | |
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 | 129 free (input); |
130 | |
131 return 0; | |
132 } |