Blame support/tst-support_blob_repeat.c

Packit Service ada5e0
/* Tests for <support/blob_repeat.h>
Packit Service ada5e0
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service ada5e0
   This file is part of the GNU C Library.
Packit Service ada5e0
Packit Service ada5e0
   The GNU C Library is free software; you can redistribute it and/or
Packit Service ada5e0
   modify it under the terms of the GNU Lesser General Public
Packit Service ada5e0
   License as published by the Free Software Foundation; either
Packit Service ada5e0
   version 2.1 of the License, or (at your option) any later version.
Packit Service ada5e0
Packit Service ada5e0
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service ada5e0
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service ada5e0
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service ada5e0
   Lesser General Public License for more details.
Packit Service ada5e0
Packit Service ada5e0
   You should have received a copy of the GNU Lesser General Public
Packit Service ada5e0
   License along with the GNU C Library; if not, see
Packit Service ada5e0
   <http://www.gnu.org/licenses/>.  */
Packit Service ada5e0
Packit Service ada5e0
#include <stdio.h>
Packit Service ada5e0
#include <support/blob_repeat.h>
Packit Service ada5e0
#include <support/check.h>
Packit Service ada5e0
Packit Service ada5e0
static int
Packit Service ada5e0
do_test (void)
Packit Service ada5e0
{
Packit Service ada5e0
  struct support_blob_repeat repeat
Packit Service ada5e0
    = support_blob_repeat_allocate ("5", 1, 5);
Packit Service ada5e0
  TEST_COMPARE_BLOB (repeat.start, repeat.size, "55555", 5);
Packit Service ada5e0
  support_blob_repeat_free (&repeat);
Packit Service ada5e0
Packit Service ada5e0
  repeat = support_blob_repeat_allocate ("ABC", 3, 3);
Packit Service ada5e0
  TEST_COMPARE_BLOB (repeat.start, repeat.size, "ABCABCABC", 9);
Packit Service ada5e0
  support_blob_repeat_free (&repeat);
Packit Service ada5e0
Packit Service ada5e0
  repeat = support_blob_repeat_allocate ("abc", 4, 3);
Packit Service ada5e0
  TEST_COMPARE_BLOB (repeat.start, repeat.size, "abc\0abc\0abc", 12);
Packit Service ada5e0
  support_blob_repeat_free (&repeat);
Packit Service ada5e0
Packit Service ada5e0
  size_t gigabyte = 1U << 30;
Packit Service ada5e0
  repeat = support_blob_repeat_allocate ("X", 1, gigabyte + 1);
Packit Service ada5e0
  if (repeat.start == NULL)
Packit Service ada5e0
    puts ("warning: not enough memory for 1 GiB mapping");
Packit Service ada5e0
  else
Packit Service ada5e0
    {
Packit Service ada5e0
      TEST_COMPARE (repeat.size, gigabyte + 1);
Packit Service ada5e0
      {
Packit Service ada5e0
        unsigned char *p = repeat.start;
Packit Service ada5e0
        for (size_t i = 0; i < gigabyte + 1; ++i)
Packit Service ada5e0
          if (p[i] != 'X')
Packit Service ada5e0
            FAIL_EXIT1 ("invalid byte 0x%02x at %zu", p[i], i);
Packit Service ada5e0
Packit Service ada5e0
        /* Check that there is no sharing across the mapping.  */
Packit Service ada5e0
        p[0] = 'Y';
Packit Service ada5e0
        p[1U << 24] = 'Z';
Packit Service ada5e0
        for (size_t i = 0; i < gigabyte + 1; ++i)
Packit Service ada5e0
          if (i == 0)
Packit Service ada5e0
            TEST_COMPARE (p[i], 'Y');
Packit Service ada5e0
          else if (i == 1U << 24)
Packit Service ada5e0
            TEST_COMPARE (p[i], 'Z');
Packit Service ada5e0
          else if (p[i] != 'X')
Packit Service ada5e0
            FAIL_EXIT1 ("invalid byte 0x%02x at %zu", p[i], i);
Packit Service ada5e0
      }
Packit Service ada5e0
    }
Packit Service ada5e0
  support_blob_repeat_free (&repeat);
Packit Service ada5e0
Packit Service ada5e0
  repeat = support_blob_repeat_allocate ("012345678", 9, 10 * 1000 * 1000);
Packit Service ada5e0
  if (repeat.start == NULL)
Packit Service ada5e0
    puts ("warning: not enough memory for large mapping");
Packit Service ada5e0
  else
Packit Service ada5e0
    {
Packit Service ada5e0
      unsigned char *p = repeat.start;
Packit Service ada5e0
      for (int i = 0; i < 10 * 1000 * 1000; ++i)
Packit Service ada5e0
        for (int j = 0; j <= 8; ++j)
Packit Service ada5e0
          if (p[i * 9 + j] != '0' + j)
Packit Service ada5e0
            {
Packit Service ada5e0
              printf ("error: element %d index %d\n", i, j);
Packit Service ada5e0
              TEST_COMPARE (p[i * 9 + j], '0' + j);
Packit Service ada5e0
            }
Packit Service ada5e0
    }
Packit Service ada5e0
  support_blob_repeat_free (&repeat);
Packit Service ada5e0
Packit Service ada5e0
  return 0;
Packit Service ada5e0
}
Packit Service ada5e0
Packit Service ada5e0
#include <support/test-driver.c>