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.
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.
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.
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/>. */
22 #include <support/check.h>
23 #include <support/support.h>
24 #include <support/xmemstream.h>
26 /* Returns a printable ASCII character based on INDEX. */
28 char_from_index (unsigned int index
)
30 return ' ' + (index
% 95);
33 enum { result_size
= 25000 };
36 run_one_size (unsigned int chunk_size
)
38 char *chunk
= xmalloc (chunk_size
+ 1);
40 struct xmemstream mem
;
41 xopen_memstream (&mem
);
42 unsigned int written
= 0;
43 for (unsigned int i
= 0; i
< result_size
; )
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. */
53 written
+= strlen(chunk
);
55 xfclose_memstream (&mem
);
57 TEST_COMPARE (written
, result_size
);
58 TEST_COMPARE (mem
.length
, result_size
);
59 TEST_COMPARE (strlen (mem
.buffer
), result_size
);
61 for (unsigned int i
= 0; i
< result_size
; ++i
)
62 TEST_COMPARE (mem
.buffer
[i
], char_from_index (i
));
71 for (unsigned int chunk_size
= 1; chunk_size
<= 30; ++ chunk_size
)
72 run_one_size (chunk_size
);
77 #include <support/test-driver.c>