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 (format: "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 (abortfunc: mcheck_abort); |
28 | |
29 | CHAR_T *buf = (CHAR_T *) 1l; |
30 | size_t len = 12345; |
31 | FILE *fp = OPEN_MEMSTREAM (bufloc: &buf, sizeloc: &len); |
32 | if (fp == NULL) |
33 | { |
34 | printf (format: "%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, stream: fp) == EOF) |
42 | { |
43 | printf (format: "fputc at %d:%d failed\n" , outer, inner); |
44 | return 1; |
45 | } |
46 | |
47 | if (fflush (fp) != 0) |
48 | { |
49 | puts (s: "fflush failed" ); |
50 | return 1; |
51 | } |
52 | |
53 | if (len != (outer + 1) * 100) |
54 | { |
55 | printf (format: "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 (format: "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 (format: "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 (s: "fclose failed" ); |
78 | return 1; |
79 | } |
80 | |
81 | if (len != 800 * 100) |
82 | { |
83 | puts (s: "string after close not 80000 bytes long" ); |
84 | return 1; |
85 | } |
86 | if (buf == (CHAR_T *) 1l) |
87 | { |
88 | puts (s: "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 (format: "after close: buf[%d] != %c\n" , inner, |
95 | (char) (W('a') + inner % 26)); |
96 | return 1; |
97 | } |
98 | |
99 | free (ptr: buf); |
100 | |
101 | return 0; |
102 | } |
103 | |
104 | #define TIMEOUT 100 |
105 | #define TEST_FUNCTION do_test () |
106 | #include "../test-skeleton.c" |
107 | |