1  /* Test writing differently sized strings to a memstream.
       2     Copyright (C) 2022-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 <stdio.h>
      20  #include <stdlib.h>
      21  #include <string.h>
      22  #include <support/check.h>
      23  #include <support/support.h>
      24  #include <support/xmemstream.h>
      25  
      26  /* Returns a printable ASCII character based on INDEX.   */
      27  static inline char
      28  char_from_index (unsigned int index)
      29  {
      30    return ' ' + (index % 95);
      31  }
      32  
      33  enum { result_size = 25000 };
      34  
      35  static void
      36  run_one_size (unsigned int chunk_size)
      37  {
      38    char *chunk = xmalloc (chunk_size + 1);
      39  
      40    struct xmemstream mem;
      41    xopen_memstream (&mem);
      42    unsigned int written = 0;
      43    for (unsigned int i = 0; i < result_size; )
      44      {
      45        unsigned int to_print = result_size - i;
      46        if (to_print > chunk_size)
      47          to_print = chunk_size;
      48        for (unsigned int j = 0; j < to_print; ++j)
      49          chunk[j] = char_from_index(i + j);
      50        chunk[to_print] = '\0';
      51        fprintf (mem.out, "%s", chunk); /* Needs -fno-builtin-fprintf.  */
      52        i += to_print;
      53        written += strlen(chunk);
      54      }
      55    xfclose_memstream (&mem);
      56  
      57    TEST_COMPARE (written, result_size);
      58    TEST_COMPARE (mem.length, result_size);
      59    TEST_COMPARE (strlen (mem.buffer), result_size);
      60  
      61    for (unsigned int i = 0; i < result_size; ++i)
      62      TEST_COMPARE (mem.buffer[i], char_from_index (i));
      63  
      64    free (mem.buffer);
      65    free (chunk);
      66  }
      67  
      68  static int
      69  do_test (void)
      70  {
      71    for (unsigned int chunk_size = 1; chunk_size <= 30; ++ chunk_size)
      72      run_one_size (chunk_size);
      73  
      74    return 0;
      75  }
      76  
      77  #include <support/test-driver.c>