1 /* Test of scratch_buffer functions.
2 Copyright (C) 2018 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. */
21 #include <scratch_buffer.h>
29 return ((i
% 13) + ((i
* i
) % 251)) & 0xff;
35 /* Check scratch_buffer_set_array_size. */
37 size_t sizes
[] = { 100, 1000, 10000, 100000 };
39 for (s
= 0; s
< SIZEOF (sizes
); s
++)
41 size_t size
= sizes
[s
];
42 struct scratch_buffer buf
;
46 scratch_buffer_init (&buf
);
48 ok
= scratch_buffer_set_array_size (&buf
, size
, 1);
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 };
65 for (s
= 0; s
< SIZEOF (sizes
); s
++)
67 size_t size
= sizes
[s
];
68 struct scratch_buffer buf
;
72 scratch_buffer_init (&buf
);
74 while (buf
.length
< size
)
76 ok
= scratch_buffer_grow (&buf
);
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
;
99 scratch_buffer_init (&buf
);
103 ok
= scratch_buffer_set_array_size (&buf
, size
, 1);
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
;
114 while (buf
.length
< size
)
116 ok
= scratch_buffer_grow_preserve (&buf
);
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
);