(root)/
glibc-2.38/
libio/
tst-memstream2.c
       1  #include <mcheck.h>
       2  #include <stdio.h>
       3  #include <stdlib.h>
       4  
       5  
       6  #ifndef CHAR_T
       7  # define CHAR_T char
       8  # define W(o) o
       9  # define OPEN_MEMSTREAM open_memstream
      10  #endif
      11  
      12  #define S(s) S1 (s)
      13  #define S1(s) #s
      14  
      15  
      16  static void
      17  mcheck_abort (enum mcheck_status ev)
      18  {
      19    printf ("mecheck failed with status %d\n", (int) ev);
      20    exit (1);
      21  }
      22  
      23  
      24  static int
      25  do_test (void)
      26  {
      27    mcheck_pedantic (mcheck_abort);
      28  
      29    CHAR_T *buf = (CHAR_T *) 1l;
      30    size_t len = 12345;
      31    FILE *fp = OPEN_MEMSTREAM (&buf, &len);
      32    if (fp == NULL)
      33      {
      34        printf ("%s failed\n", S(OPEN_MEMSTREAM));
      35        return 1;
      36      }
      37  
      38    for (int outer = 0; outer < 800; ++outer)
      39      {
      40        for (int inner = 0; inner < 100; ++inner)
      41  	if (fputc (W('a') + (outer * 100 + inner) % 26, fp) == EOF)
      42  	  {
      43  	    printf ("fputc at %d:%d failed\n", outer, inner);
      44  	    return 1;
      45  	  }
      46  
      47        if (fflush (fp) != 0)
      48  	{
      49  	  puts ("fflush failed");
      50  	  return 1;
      51  	}
      52  
      53        if (len != (outer + 1) * 100)
      54  	{
      55  	  printf ("string in round %d not %d bytest long\n",
      56  		  outer + 1, (outer + 1) * 100);
      57  	  return 1;
      58  	}
      59        if (buf == (CHAR_T *) 1l)
      60  	{
      61  	  printf ("round %d: buf not updated\n", outer + 1);
      62  	  return 1;
      63  	}
      64        for (int inner = 0; inner < (outer + 1) * 100; ++inner)
      65  	if (buf[inner] != W('a') + inner % 26)
      66  	  {
      67  	    printf ("round %d: buf[%d] != '%c'\n", outer + 1, inner,
      68  		    (char) (W('a') + inner % 26));
      69  	    return 1;
      70  	  }
      71      }
      72  
      73    buf = (CHAR_T *) 1l;
      74    len = 12345;
      75    if (fclose (fp) != 0)
      76      {
      77        puts ("fclose failed");
      78        return 1;
      79      }
      80  
      81    if (len != 800 * 100)
      82      {
      83        puts ("string after close not 80000 bytes long");
      84        return 1;
      85      }
      86    if (buf == (CHAR_T *) 1l)
      87      {
      88        puts ("buf not updated");
      89        return 1;
      90      }
      91    for (int inner = 0; inner < 800 * 100; ++inner)
      92      if (buf[inner] != W('a') + inner % 26)
      93        {
      94  	printf ("after close: buf[%d] != %c\n", inner,
      95  		(char) (W('a') + inner % 26));
      96  	return 1;
      97        }
      98  
      99    free (buf);
     100  
     101    return 0;
     102  }
     103  
     104  #define TIMEOUT 100
     105  #define TEST_FUNCTION do_test ()
     106  #include "../test-skeleton.c"