summaryrefslogtreecommitdiff
path: root/tests/test-memchr.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff.email>2026-03-10 13:24:07 +0100
committerJörg Frings-Fürst <debian@jff.email>2026-03-10 13:24:07 +0100
commitcfd1f17f1a85d95ea12bca8dae42add7dad1ad11 (patch)
tree8016486f8ee7157213f2d09ff2491bfa9c94638a /tests/test-memchr.c
parent14e4d584d0121031ec40e6c35869745f1747ff29 (diff)
parent1403307d6e2fb4e7b5d97a35f40d1e95134561ab (diff)
Merge branch 'release/debian/1.4.2-1'HEADdebian/1.4.2-1master
Diffstat (limited to 'tests/test-memchr.c')
-rw-r--r--tests/test-memchr.c151
1 files changed, 78 insertions, 73 deletions
diff --git a/tests/test-memchr.c b/tests/test-memchr.c
index 9111371a..7d72dbac 100644
--- a/tests/test-memchr.c
+++ b/tests/test-memchr.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2024 Free Software Foundation, Inc.
+ * Copyright (C) 2008-2026 Free Software Foundation, Inc.
* Written by Eric Blake and Bruno Haible
*
* This program is free software: you can redistribute it and/or modify
@@ -27,68 +27,76 @@ SIGNATURE_CHECK (memchr, void *, (void const *, int, size_t));
#include "zerosize-ptr.h"
#include "macros.h"
-/* Calculating void * + int is not portable, so this wrapper converts
- to char * to make the tests easier to write. */
-#define MEMCHR (char *) memchr
+/* Test the library, not the compiler+library. */
+static void *
+lib_memchr (void const *s, int c, size_t n)
+{
+ return (void *) memchr (s, c, n);
+}
+static void *(*volatile volatile_memchr) (void const *, int, size_t)
+ = lib_memchr;
+#undef memchr
+#define memchr volatile_memchr
int
main (void)
{
- size_t n = 0x100000;
- char *input = malloc (n);
- ASSERT (input);
-
- input[0] = 'a';
- input[1] = 'b';
- memset (input + 2, 'c', 1024);
- memset (input + 1026, 'd', n - 1028);
- input[n - 2] = 'e';
- input[n - 1] = 'a';
-
- /* Basic behavior tests. */
- ASSERT (MEMCHR (input, 'a', n) == input);
-
- ASSERT (MEMCHR (input, 'a', 0) == NULL);
-
- {
- void *page_boundary = zerosize_ptr ();
- if (page_boundary)
- ASSERT (MEMCHR (page_boundary, 'a', 0) == NULL);
- }
-
- ASSERT (MEMCHR (input, 'b', n) == input + 1);
- ASSERT (MEMCHR (input, 'c', n) == input + 2);
- ASSERT (MEMCHR (input, 'd', n) == input + 1026);
-
- ASSERT (MEMCHR (input + 1, 'a', n - 1) == input + n - 1);
- ASSERT (MEMCHR (input + 1, 'e', n - 1) == input + n - 2);
- ASSERT (MEMCHR (input + 1, 0x789abc00 | 'e', n - 1) == input + n - 2);
-
- ASSERT (MEMCHR (input, 'f', n) == NULL);
- ASSERT (MEMCHR (input, '\0', n) == NULL);
-
- /* Check that a very long haystack is handled quickly if the byte is
- found near the beginning. */
{
- size_t repeat = 10000;
- for (; repeat > 0; repeat--)
+ size_t n = 0x100000;
+ char *input = malloc (n);
+ ASSERT (input);
+
+ input[0] = 'a';
+ input[1] = 'b';
+ memset (input + 2, 'c', 1024);
+ memset (input + 1026, 'd', n - 1028);
+ input[n - 2] = 'e';
+ input[n - 1] = 'a';
+
+ /* Basic behavior tests. */
+ ASSERT (memchr (input, 'a', n) == input);
+
+ ASSERT (memchr (input, 'a', 0) == NULL);
+
+ {
+ void *page_boundary = zerosize_ptr ();
+ if (page_boundary)
+ ASSERT (memchr (page_boundary, 'a', 0) == NULL);
+ }
+
+ ASSERT (memchr (input, 'b', n) == input + 1);
+ ASSERT (memchr (input, 'c', n) == input + 2);
+ ASSERT (memchr (input, 'd', n) == input + 1026);
+
+ ASSERT (memchr (input + 1, 'a', n - 1) == input + n - 1);
+ ASSERT (memchr (input + 1, 'e', n - 1) == input + n - 2);
+ ASSERT (memchr (input + 1, 0x789abc00 | 'e', n - 1) == input + n - 2);
+
+ ASSERT (memchr (input, 'f', n) == NULL);
+ ASSERT (memchr (input, '\0', n) == NULL);
+
+ /* Check that a very long haystack is handled quickly if the byte is
+ found near the beginning. */
+ {
+ size_t repeat = 10000;
+ for (; repeat > 0; repeat--)
+ {
+ ASSERT (memchr (input, 'c', n) == input + 2);
+ }
+ }
+
+ /* Alignment tests. */
+ for (int i = 0; i < 32; i++)
{
- ASSERT (MEMCHR (input, 'c', n) == input + 2);
- }
- }
-
- /* Alignment tests. */
- {
- int i, j;
- for (i = 0; i < 32; i++)
- {
- for (j = 0; j < 256; j++)
+ for (int j = 0; j < 256; j++)
input[i + j] = j;
- for (j = 0; j < 256; j++)
+ for (int j = 0; j < 256; j++)
{
- ASSERT (MEMCHR (input + i, j, 256) == input + i + j);
+ ASSERT (memchr (input + i, j, 256) == input + i + j);
}
}
+
+ free (input);
}
/* Check that memchr() does not read past the first occurrence of the
@@ -105,33 +113,30 @@ main (void)
if (page_boundary != NULL)
{
- for (n = 1; n <= limit; n++)
+ for (int n = 1; n <= limit; n++)
{
char *mem = page_boundary - n;
memset (mem, 'X', n);
- ASSERT (MEMCHR (mem, 'U', n) == NULL);
- ASSERT (MEMCHR (mem, 0, n) == NULL);
-
- {
- size_t i;
- size_t k;
-
- for (i = 0; i < n; i++)
- {
- mem[i] = 'U';
- for (k = i + 1; k < n + limit; k++)
- ASSERT (MEMCHR (mem, 'U', k) == mem + i);
- mem[i] = 0;
- for (k = i + 1; k < n + limit; k++)
- ASSERT (MEMCHR (mem, 0, k) == mem + i);
- mem[i] = 'X';
- }
- }
+ ASSERT (memchr (mem, 'U', n) == NULL);
+ ASSERT (memchr (mem, 0, n) == NULL);
+
+ for (size_t i = 0; i < n; i++)
+ {
+ mem[i] = 'U';
+ for (size_t k = i + 1; k < n + limit; k++)
+ ASSERT (memchr (mem, 'U', k) == mem + i);
+ mem[i] = 0;
+ for (size_t k = i + 1; k < n + limit; k++)
+ ASSERT (memchr (mem, 0, k) == mem + i);
+ mem[i] = 'X';
+ }
}
}
}
- free (input);
+ /* Test zero-length operations on NULL pointers, allowed by
+ <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf>. */
+ ASSERT (memchr (NULL, '?', 0) == NULL);
return test_exit_status;
}