Blame gnulib-tests/test-rawmemchr.c

Packit Service fdd496
/*
Packit Service fdd496
 * Copyright (C) 2008-2017 Free Software Foundation, Inc.
Packit Service fdd496
 * Written by Eric Blake and Bruno Haible
Packit Service fdd496
 *
Packit Service fdd496
 * This program is free software: you can redistribute it and/or modify
Packit Service fdd496
 * it under the terms of the GNU General Public License as published by
Packit Service fdd496
 * the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
 * (at your option) any later version.
Packit Service fdd496
 *
Packit Service fdd496
 * This program is distributed in the hope that it will be useful,
Packit Service fdd496
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
 * GNU General Public License for more details.
Packit Service fdd496
 *
Packit Service fdd496
 * You should have received a copy of the GNU General Public License
Packit Service fdd496
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#include <string.h>
Packit Service fdd496
Packit Service fdd496
#include "signature.h"
Packit Service fdd496
SIGNATURE_CHECK (rawmemchr, void *, (void const *, int));
Packit Service fdd496
Packit Service fdd496
#include <stdlib.h>
Packit Service fdd496
Packit Service fdd496
#include "zerosize-ptr.h"
Packit Service fdd496
#include "macros.h"
Packit Service fdd496
Packit Service fdd496
/* Calculating void * + int is not portable, so this wrapper converts
Packit Service fdd496
   to char * to make the tests easier to write.  */
Packit Service fdd496
#define RAWMEMCHR (char *) rawmemchr
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
main (void)
Packit Service fdd496
{
Packit Service fdd496
  size_t n = 0x100000;
Packit Service fdd496
  char *input = malloc (n + 1);
Packit Service fdd496
  ASSERT (input);
Packit Service fdd496
Packit Service fdd496
  input[0] = 'a';
Packit Service fdd496
  input[1] = 'b';
Packit Service fdd496
  memset (input + 2, 'c', 1024);
Packit Service fdd496
  memset (input + 1026, 'd', n - 1028);
Packit Service fdd496
  input[n - 2] = 'e';
Packit Service fdd496
  input[n - 1] = 'a';
Packit Service fdd496
  input[n] = '\0';
Packit Service fdd496
Packit Service fdd496
  /* Basic behavior tests.  */
Packit Service fdd496
  ASSERT (RAWMEMCHR (input, 'a') == input);
Packit Service fdd496
  ASSERT (RAWMEMCHR (input, 'b') == input + 1);
Packit Service fdd496
  ASSERT (RAWMEMCHR (input, 'c') == input + 2);
Packit Service fdd496
  ASSERT (RAWMEMCHR (input, 'd') == input + 1026);
Packit Service fdd496
Packit Service fdd496
  ASSERT (RAWMEMCHR (input + 1, 'a') == input + n - 1);
Packit Service fdd496
  ASSERT (RAWMEMCHR (input + 1, 'e') == input + n - 2);
Packit Service fdd496
  ASSERT (RAWMEMCHR (input + 1, 0x789abc00 | 'e') == input + n - 2);
Packit Service fdd496
Packit Service fdd496
  ASSERT (RAWMEMCHR (input, '\0') == input + n);
Packit Service fdd496
Packit Service fdd496
  /* Alignment tests.  */
Packit Service fdd496
  {
Packit Service fdd496
    int i, j;
Packit Service fdd496
    for (i = 0; i < 32; i++)
Packit Service fdd496
      {
Packit Service fdd496
        for (j = 0; j < 256; j++)
Packit Service fdd496
          input[i + j] = j;
Packit Service fdd496
        for (j = 0; j < 256; j++)
Packit Service fdd496
          {
Packit Service fdd496
            ASSERT (RAWMEMCHR (input + i, j) == input + i + j);
Packit Service fdd496
          }
Packit Service fdd496
      }
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Ensure that no unaligned oversized reads occur.  */
Packit Service fdd496
  {
Packit Service fdd496
    char *page_boundary = (char *) zerosize_ptr ();
Packit Service fdd496
    size_t i;
Packit Service fdd496
Packit Service fdd496
    if (!page_boundary)
Packit Service fdd496
      page_boundary = input + 4096;
Packit Service fdd496
    memset (page_boundary - 512, '1', 511);
Packit Service fdd496
    page_boundary[-1] = '2';
Packit Service fdd496
    for (i = 1; i <= 512; i++)
Packit Service fdd496
      ASSERT (RAWMEMCHR (page_boundary - i, (i * 0x01010100) | '2')
Packit Service fdd496
              == page_boundary - 1);
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  free (input);
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
}