Blame support/tst-support_blob_repeat.c

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