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