(root)/
glibc-2.38/
malloc/
tst-scratch_buffer.c
       1  /*
       2     Copyright (C) 2015-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <array_length.h>
      20  #include <scratch_buffer.h>
      21  #include <support/check.h>
      22  #include <support/support.h>
      23  #include <stdbool.h>
      24  #include <stdio.h>
      25  #include <string.h>
      26  
      27  static bool
      28  unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b)
      29  {
      30    size_t old_length = buf->length;
      31    if (!scratch_buffer_set_array_size (buf, a, b))
      32      {
      33        printf ("scratch_buffer_set_array_size failed: %zu %zu\n",
      34  	      a, b);
      35        return false;
      36      }
      37    if (old_length != buf->length)
      38      {
      39        printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
      40  	      a, b);
      41        return false;
      42      }
      43    return true;
      44  }
      45  
      46  static bool
      47  array_size_must_fail (size_t a, size_t b)
      48  {
      49    for (int pass = 0; pass < 2; ++pass)
      50      {
      51        struct scratch_buffer buf;
      52        scratch_buffer_init (&buf);
      53        if (pass > 0)
      54  	if (!scratch_buffer_grow (&buf))
      55  	  {
      56  	    printf ("scratch_buffer_grow in array_size_must_fail failed\n");
      57  	    return false;
      58  	  }
      59        if (scratch_buffer_set_array_size (&buf, a, b))
      60  	{
      61  	  printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
      62  		  pass, a, b);
      63  	  return false;
      64  	}
      65        if (buf.data != buf.__space.__c)
      66  	{
      67  	  printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
      68  		  pass, a, b);
      69  	  return false;
      70  	}
      71      }
      72    return true;
      73  }
      74  
      75  static int
      76  do_test (void)
      77  {
      78    {
      79      struct scratch_buffer buf;
      80      scratch_buffer_init (&buf);
      81      memset (buf.data, ' ', buf.length);
      82      scratch_buffer_free (&buf);
      83    }
      84    {
      85      struct scratch_buffer buf;
      86      scratch_buffer_init (&buf);
      87      memset (buf.data, ' ', buf.length);
      88      size_t old_length = buf.length;
      89      scratch_buffer_grow (&buf);
      90      if (buf.length <= old_length)
      91        {
      92  	printf ("scratch_buffer_grow did not enlarge buffer\n");
      93  	return 1;
      94        }
      95      memset (buf.data, ' ', buf.length);
      96      scratch_buffer_free (&buf);
      97    }
      98    {
      99      struct scratch_buffer buf;
     100      scratch_buffer_init (&buf);
     101      memset (buf.data, '@', buf.length);
     102      strcpy (buf.data, "prefix");
     103      size_t old_length = buf.length;
     104      scratch_buffer_grow_preserve (&buf);
     105      if (buf.length <= old_length)
     106        {
     107  	printf ("scratch_buffer_grow_preserve did not enlarge buffer\n");
     108  	return 1;
     109        }
     110      if (strcmp (buf.data, "prefix") != 0)
     111        {
     112  	printf ("scratch_buffer_grow_preserve did not copy buffer\n");
     113  	return 1;
     114        }
     115      for (unsigned i = 7; i < old_length; ++i)
     116        if (((char *)buf.data)[i] != '@')
     117  	{
     118  	  printf ("scratch_buffer_grow_preserve did not copy buffer (%u)\n",
     119  		  i);
     120  	  return 1;
     121  	}
     122      scratch_buffer_free (&buf);
     123    }
     124    {
     125      struct scratch_buffer buf;
     126      scratch_buffer_init (&buf);
     127      for (int pass = 0; pass < 4; ++pass)
     128        {
     129  	if (!(unchanged_array_size (&buf, 0, 0)
     130  	      && unchanged_array_size (&buf, 1, 0)
     131  	      && unchanged_array_size (&buf, 0, 1)
     132  	      && unchanged_array_size (&buf, -1, 0)
     133  	      && unchanged_array_size (&buf, 0, -1)
     134  	      && unchanged_array_size (&buf, 1ULL << 16, 0)
     135  	      && unchanged_array_size (&buf, 0, 1ULL << 16)
     136  	      && unchanged_array_size (&buf, (size_t) (1ULL << 32), 0)
     137  	      && unchanged_array_size (&buf, 0, (size_t) (1ULL << 32))))
     138  	  return 1;
     139  	if (!scratch_buffer_grow (&buf))
     140  	  {
     141  	    printf ("scratch_buffer_grow_failed (pass %d)\n", pass);
     142  	  }
     143        }
     144      scratch_buffer_free (&buf);
     145    }
     146    {
     147      if (!(array_size_must_fail (-1, 1)
     148  	  && array_size_must_fail (-1, -1)
     149  	  && array_size_must_fail (1, -1)
     150  	  && array_size_must_fail (((size_t)-1) / 4, 4)
     151  	  && array_size_must_fail (4, ((size_t)-1) / 4)))
     152  	return 1;
     153    }
     154    return 0;
     155  }
     156  
     157  #include <support/test-driver.c>