root/maint/gnulib/tests/test-scratch-buffer.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. byte_at
  2. main

   1 /* Test of scratch_buffer functions.
   2    Copyright (C) 2018-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Bruno Haible <bruno@clisp.org>, 2018.  */
  18 
  19 #include <config.h>
  20 
  21 #include <scratch_buffer.h>
  22 
  23 #include <string.h>
  24 #include "macros.h"
  25 
  26 static int
  27 byte_at (unsigned long long int i)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29   return ((i % 13) + ((i * i) % 251)) & 0xff;
  30 }
  31 
  32 int
  33 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35   /* Check scratch_buffer_set_array_size.  */
  36   {
  37     size_t sizes[] = { 100, 1000, 10000, 100000 };
  38     size_t s;
  39     for (s = 0; s < SIZEOF (sizes); s++)
  40       {
  41         size_t size = sizes[s];
  42         struct scratch_buffer buf;
  43         bool ok;
  44         size_t i;
  45 
  46         scratch_buffer_init (&buf);
  47 
  48         ok = scratch_buffer_set_array_size (&buf, size, 1);
  49         ASSERT (ok);
  50 
  51         for (i = 0; i < size; i++)
  52           ((unsigned char *) buf.data)[i] = byte_at (i);
  53 
  54         memset (buf.data, 'x', buf.length);
  55         memset (buf.data, 'y', size);
  56 
  57         scratch_buffer_free (&buf);
  58       }
  59   }
  60 
  61   /* Check scratch_buffer_grow.  */
  62   {
  63     size_t sizes[] = { 100, 1000, 10000, 100000 };
  64     size_t s;
  65     for (s = 0; s < SIZEOF (sizes); s++)
  66       {
  67         size_t size = sizes[s];
  68         struct scratch_buffer buf;
  69         bool ok;
  70         size_t i;
  71 
  72         scratch_buffer_init (&buf);
  73 
  74         while (buf.length < size)
  75           {
  76             ok = scratch_buffer_grow (&buf);
  77             ASSERT (ok);
  78           }
  79 
  80         for (i = 0; i < size; i++)
  81           ((unsigned char *) buf.data)[i] = byte_at (i);
  82 
  83         memset (buf.data, 'x', buf.length);
  84         memset (buf.data, 'y', size);
  85 
  86         scratch_buffer_free (&buf);
  87       }
  88   }
  89 
  90   /* Check scratch_buffer_grow_preserve.  */
  91   {
  92     size_t sizes[] = { 100, 1000, 10000, 100000 };
  93     struct scratch_buffer buf;
  94     size_t s;
  95     size_t size;
  96     bool ok;
  97     size_t i;
  98 
  99     scratch_buffer_init (&buf);
 100 
 101     s = 0;
 102     size = sizes[s];
 103     ok = scratch_buffer_set_array_size (&buf, size, 1);
 104     ASSERT (ok);
 105 
 106     for (i = 0; i < size; i++)
 107       ((unsigned char *) buf.data)[i] = byte_at (i);
 108 
 109     for (; s < SIZEOF (sizes); s++)
 110       {
 111         size_t oldsize = size;
 112         size = sizes[s];
 113 
 114         while (buf.length < size)
 115           {
 116             ok = scratch_buffer_grow_preserve (&buf);
 117             ASSERT (ok);
 118           }
 119 
 120         for (i = 0; i < oldsize; i++)
 121           ASSERT(((unsigned char *) buf.data)[i] == byte_at (i));
 122         for (i = oldsize; i < size; i++)
 123           ((unsigned char *) buf.data)[i] = byte_at (i);
 124       }
 125 
 126     scratch_buffer_free (&buf);
 127   }
 128 
 129   return 0;
 130 }

/* [previous][next][first][last][top][bottom][index][help] */