usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-scratch-buffer.c
blob190061efc2f8f7fd6fa852e09dd3c7bbad78536d
1 /* Test of scratch_buffer functions.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2018. */
19 #include <config.h>
21 #include <scratch_buffer.h>
23 #include <string.h>
24 #include "macros.h"
26 static int
27 byte_at (unsigned long long int i)
29 return ((i % 13) + ((i * i) % 251)) & 0xff;
32 int
33 main ()
35 /* Check scratch_buffer_set_array_size. */
37 size_t sizes[] = { 100, 1000, 10000, 100000 };
38 size_t s;
39 for (s = 0; s < SIZEOF (sizes); s++)
41 size_t size = sizes[s];
42 struct scratch_buffer buf;
43 bool ok;
44 size_t i;
46 scratch_buffer_init (&buf);
48 ok = scratch_buffer_set_array_size (&buf, size, 1);
49 ASSERT (ok);
51 for (i = 0; i < size; i++)
52 ((unsigned char *) buf.data)[i] = byte_at (i);
54 memset (buf.data, 'x', buf.length);
55 memset (buf.data, 'y', size);
57 scratch_buffer_free (&buf);
61 /* Check scratch_buffer_grow. */
63 size_t sizes[] = { 100, 1000, 10000, 100000 };
64 size_t s;
65 for (s = 0; s < SIZEOF (sizes); s++)
67 size_t size = sizes[s];
68 struct scratch_buffer buf;
69 bool ok;
70 size_t i;
72 scratch_buffer_init (&buf);
74 while (buf.length < size)
76 ok = scratch_buffer_grow (&buf);
77 ASSERT (ok);
80 for (i = 0; i < size; i++)
81 ((unsigned char *) buf.data)[i] = byte_at (i);
83 memset (buf.data, 'x', buf.length);
84 memset (buf.data, 'y', size);
86 scratch_buffer_free (&buf);
90 /* Check scratch_buffer_grow_preserve. */
92 size_t sizes[] = { 100, 1000, 10000, 100000 };
93 struct scratch_buffer buf;
94 size_t s;
95 size_t size;
96 bool ok;
97 size_t i;
99 scratch_buffer_init (&buf);
101 s = 0;
102 size = sizes[s];
103 ok = scratch_buffer_set_array_size (&buf, size, 1);
104 ASSERT (ok);
106 for (i = 0; i < size; i++)
107 ((unsigned char *) buf.data)[i] = byte_at (i);
109 for (; s < SIZEOF (sizes); s++)
111 size_t oldsize = size;
112 size = sizes[s];
114 while (buf.length < size)
116 ok = scratch_buffer_grow_preserve (&buf);
117 ASSERT (ok);
120 for (i = 0; i < oldsize; i++)
121 ASSERT(((unsigned char *) buf.data)[i] == byte_at (i));
122 for (i = oldsize; i < size; i++)
123 ((unsigned char *) buf.data)[i] = byte_at (i);
126 scratch_buffer_free (&buf);
129 return test_exit_status;