Back to index

glibc  2.9
tst-memstream2.c
Go to the documentation of this file.
00001 #include <mcheck.h>
00002 #include <stdio.h>
00003 
00004 
00005 #ifndef CHAR_T
00006 # define CHAR_T char
00007 # define W(o) o
00008 # define OPEN_MEMSTREAM open_memstream
00009 #endif
00010 
00011 #define S(s) S1 (s)
00012 #define S1(s) #s
00013 
00014 
00015 static void
00016 mcheck_abort (enum mcheck_status ev)
00017 {
00018   printf ("mecheck failed with status %d\n", (int) ev);
00019   exit (1);
00020 }
00021 
00022 
00023 static int
00024 do_test (void)
00025 {
00026   mcheck_pedantic (mcheck_abort);
00027 
00028   CHAR_T *buf = (CHAR_T *) 1l;
00029   size_t len = 12345;
00030   FILE *fp = OPEN_MEMSTREAM (&buf, &len);
00031   if (fp == NULL)
00032     {
00033       printf ("%s failed\n", S(OPEN_MEMSTREAM));
00034       return 1;
00035     }
00036 
00037   for (int outer = 0; outer < 800; ++outer)
00038     {
00039       for (int inner = 0; inner < 100; ++inner)
00040        if (fputc (W('a') + (outer * 100 + inner) % 26, fp) == EOF)
00041          {
00042            printf ("fputc at %d:%d failed\n", outer, inner);
00043            return 1;
00044          }
00045 
00046       if (fflush (fp) != 0)
00047        {
00048          puts ("fflush failed");
00049          return 1;
00050        }
00051 
00052       if (len != (outer + 1) * 100)
00053        {
00054          printf ("string in round %d not %d bytest long\n",
00055                 outer + 1, (outer + 1) * 100);
00056          return 1;
00057        }
00058       if (buf == (CHAR_T *) 1l)
00059        {
00060          printf ("round %d: buf not updated\n", outer + 1);
00061          return 1;
00062        }
00063       for (int inner = 0; inner < (outer + 1) * 100; ++inner)
00064        if (buf[inner] != W('a') + inner % 26)
00065          {
00066            printf ("round %d: buf[%d] != '%c'\n", outer + 1, inner,
00067                   (char) (W('a') + inner % 26));
00068            return 1;
00069          }
00070     }
00071 
00072   buf = (CHAR_T *) 1l;
00073   len = 12345;
00074   if (fclose (fp) != 0)
00075     {
00076       puts ("fclose failed");
00077       return 1;
00078     }
00079 
00080   if (len != 800 * 100)
00081     {
00082       puts ("string after close not 80000 bytes long");
00083       return 1;
00084     }
00085   if (buf == (CHAR_T *) 1l)
00086     {
00087       puts ("buf not updated");
00088       return 1;
00089     }
00090   for (int inner = 0; inner < 800 * 100; ++inner)
00091     if (buf[inner] != W('a') + inner % 26)
00092       {
00093        printf ("after close: buf[%d] != %c\n", inner,
00094               (char) (W('a') + inner % 26));
00095        return 1;
00096       }
00097 
00098   free (buf);
00099 
00100   return 0;
00101 }
00102 
00103 #define TEST_FUNCTION do_test ()
00104 #include "../test-skeleton.c"