Linux: Assume and consolidate bind wire-up syscall
[glibc.git] / stdio-common / tst-memstream-string.c
blob0a31e0c12c0a03d5cf934ea79c19eb9724061806
1 /* Test writing differently sized strings to a memstream.
2 Copyright (C) 2022 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/>. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <support/check.h>
23 #include <support/support.h>
24 #include <support/xmemstream.h>
26 /* Returns a printable ASCII character based on INDEX. */
27 static inline char
28 char_from_index (unsigned int index)
30 return ' ' + (index % 95);
33 enum { result_size = 25000 };
35 static void
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. */
52 i += to_print;
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));
64 free (mem.buffer);
65 free (chunk);
68 static int
69 do_test (void)
71 for (unsigned int chunk_size = 1; chunk_size <= 30; ++ chunk_size)
72 run_one_size (chunk_size);
74 return 0;
77 #include <support/test-driver.c>