|
Packit Service |
82fcde |
/*
|
|
Packit Service |
82fcde |
Copyright (C) 2015-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <scratch_buffer.h>
|
|
Packit Service |
82fcde |
#include <stdbool.h>
|
|
Packit Service |
82fcde |
#include <stdio.h>
|
|
Packit Service |
82fcde |
#include <string.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static bool
|
|
Packit Service |
82fcde |
unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
size_t old_length = buf->length;
|
|
Packit Service |
82fcde |
if (!scratch_buffer_set_array_size (buf, a, b))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_set_array_size failed: %zu %zu\n",
|
|
Packit Service |
82fcde |
a, b);
|
|
Packit Service |
82fcde |
return false;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (old_length != buf->length)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
|
|
Packit Service |
82fcde |
a, b);
|
|
Packit Service |
82fcde |
return false;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
return true;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static bool
|
|
Packit Service |
82fcde |
array_size_must_fail (size_t a, size_t b)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
for (int pass = 0; pass < 2; ++pass)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
struct scratch_buffer buf;
|
|
Packit Service |
82fcde |
scratch_buffer_init (&buf;;
|
|
Packit Service |
82fcde |
if (pass > 0)
|
|
Packit Service |
82fcde |
if (!scratch_buffer_grow (&buf))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_grow in array_size_must_fail failed\n");
|
|
Packit Service |
82fcde |
return false;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (scratch_buffer_set_array_size (&buf, a, b))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
|
|
Packit Service |
82fcde |
pass, a, b);
|
|
Packit Service |
82fcde |
return false;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (buf.data != buf.__space.__c)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
|
|
Packit Service |
82fcde |
pass, a, b);
|
|
Packit Service |
82fcde |
return false;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
return true;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static int
|
|
Packit Service |
82fcde |
do_test (void)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
struct scratch_buffer buf;
|
|
Packit Service |
82fcde |
scratch_buffer_init (&buf;;
|
|
Packit Service |
82fcde |
memset (buf.data, ' ', buf.length);
|
|
Packit Service |
82fcde |
scratch_buffer_free (&buf;;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
struct scratch_buffer buf;
|
|
Packit Service |
82fcde |
scratch_buffer_init (&buf;;
|
|
Packit Service |
82fcde |
memset (buf.data, ' ', buf.length);
|
|
Packit Service |
82fcde |
size_t old_length = buf.length;
|
|
Packit Service |
82fcde |
scratch_buffer_grow (&buf;;
|
|
Packit Service |
82fcde |
if (buf.length <= old_length)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_grow did not enlarge buffer\n");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
memset (buf.data, ' ', buf.length);
|
|
Packit Service |
82fcde |
scratch_buffer_free (&buf;;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
struct scratch_buffer buf;
|
|
Packit Service |
82fcde |
scratch_buffer_init (&buf;;
|
|
Packit Service |
82fcde |
memset (buf.data, '@', buf.length);
|
|
Packit Service |
82fcde |
strcpy (buf.data, "prefix");
|
|
Packit Service |
82fcde |
size_t old_length = buf.length;
|
|
Packit Service |
82fcde |
scratch_buffer_grow_preserve (&buf;;
|
|
Packit Service |
82fcde |
if (buf.length <= old_length)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_grow_preserve did not enlarge buffer\n");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (strcmp (buf.data, "prefix") != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_grow_preserve did not copy buffer\n");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
for (unsigned i = 7; i < old_length; ++i)
|
|
Packit Service |
82fcde |
if (((char *)buf.data)[i] != '@')
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_grow_preserve did not copy buffer (%u)\n",
|
|
Packit Service |
82fcde |
i);
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
scratch_buffer_free (&buf;;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
struct scratch_buffer buf;
|
|
Packit Service |
82fcde |
scratch_buffer_init (&buf;;
|
|
Packit Service |
82fcde |
for (int pass = 0; pass < 4; ++pass)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (!(unchanged_array_size (&buf, 0, 0)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, 1, 0)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, 0, 1)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, -1, 0)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, 0, -1)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, 1ULL << 16, 0)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, 0, 1ULL << 16)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, (size_t) (1ULL << 32), 0)
|
|
Packit Service |
82fcde |
&& unchanged_array_size (&buf, 0, (size_t) (1ULL << 32))))
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
if (!scratch_buffer_grow (&buf))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("scratch_buffer_grow_failed (pass %d)\n", pass);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
scratch_buffer_free (&buf;;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (!(array_size_must_fail (-1, 1)
|
|
Packit Service |
82fcde |
&& array_size_must_fail (-1, -1)
|
|
Packit Service |
82fcde |
&& array_size_must_fail (1, -1)
|
|
Packit Service |
82fcde |
&& array_size_must_fail (((size_t)-1) / 4, 4)
|
|
Packit Service |
82fcde |
&& array_size_must_fail (4, ((size_t)-1) / 4)))
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
return 0;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define TEST_FUNCTION do_test ()
|
|
Packit Service |
82fcde |
#include "../test-skeleton.c"
|