Blame support/tst-support_blob_repeat.c

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