X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-memchr.c;h=a801614079eb8c7292e21a90ba885c285e22edcf;hb=880d107;hp=d108f1ae0426d968b6e42c436c2139d9f70566f4;hpb=bb037ad8926e663ad33a8a384f6d5c4f26af0fd4;p=gnulib.git diff --git a/tests/test-memchr.c b/tests/test-memchr.c index d108f1ae0..a80161407 100644 --- a/tests/test-memchr.c +++ b/tests/test-memchr.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Free Software Foundation + * Copyright (C) 2008-2010 Free Software Foundation, Inc. * Written by Eric Blake and Bruno Haible * * This program is free software: you can redistribute it and/or modify @@ -19,27 +19,20 @@ #include -#include +#include "signature.h" +SIGNATURE_CHECK (memchr, void *, (void const *, int, size_t)); + #include -#define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ - fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ - fflush (stderr); \ - abort (); \ - } \ - } \ - while (0) +#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 int -main () +main (void) { size_t n = 0x100000; char *input = malloc (n); @@ -56,7 +49,7 @@ main () ASSERT (MEMCHR (input, 'a', n) == input); ASSERT (MEMCHR (input, 'a', 0) == NULL); - ASSERT (MEMCHR (NULL, 'a', 0) == NULL); + ASSERT (MEMCHR (zerosize_ptr (), 'a', 0) == NULL); ASSERT (MEMCHR (input, 'b', n) == input + 1); ASSERT (MEMCHR (input, 'c', n) == input + 2); @@ -74,7 +67,7 @@ main () size_t repeat = 10000; for (; repeat > 0; repeat--) { - ASSERT (MEMCHR (input, 'c', n) == input + 2); + ASSERT (MEMCHR (input, 'c', n) == input + 2); } } @@ -83,12 +76,40 @@ main () int i, j; for (i = 0; i < 32; i++) { - for (j = 0; j < 256; j++) - input[i + j] = j; - for (j = 0; j < 256; j++) - { - ASSERT (MEMCHR (input + i, j, 256) == input + i + j); - } + for (j = 0; j < 256; j++) + input[i + j] = j; + for (j = 0; j < 256; j++) + { + ASSERT (MEMCHR (input + i, j, 256) == input + i + j); + } + } + } + + /* Check that memchr() does not read past the first occurrence of the + byte being searched. See the Austin Group's clarification + . */ + { + char *page_boundary = (char *) zerosize_ptr (); + + if (page_boundary != NULL) + { + for (n = 1; n <= 500; n++) + { + char *mem = page_boundary - n; + memset (mem, 'X', n); + ASSERT (MEMCHR (mem, 'U', n) == NULL); + + { + size_t i; + + for (i = 0; i < n; i++) + { + mem[i] = 'U'; + ASSERT (MEMCHR (mem, 'U', 4000) == mem + i); + mem[i] = 'X'; + } + } + } } }