1 | /* |
2 | Copyright (C) 2015-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
19 | #include <array_length.h> |
20 | #include <scratch_buffer.h> |
21 | #include <support/check.h> |
22 | #include <support/support.h> |
23 | #include <stdbool.h> |
24 | #include <stdio.h> |
25 | #include <string.h> |
26 | |
27 | static bool |
28 | unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b) |
29 | { |
30 | size_t old_length = buf->length; |
31 | if (!scratch_buffer_set_array_size (buffer: buf, nelem: a, size: b)) |
32 | { |
33 | printf (format: "scratch_buffer_set_array_size failed: %zu %zu\n" , |
34 | a, b); |
35 | return false; |
36 | } |
37 | if (old_length != buf->length) |
38 | { |
39 | printf (format: "scratch_buffer_set_array_size did not preserve size: %zu %zu\n" , |
40 | a, b); |
41 | return false; |
42 | } |
43 | return true; |
44 | } |
45 | |
46 | static bool |
47 | array_size_must_fail (size_t a, size_t b) |
48 | { |
49 | for (int pass = 0; pass < 2; ++pass) |
50 | { |
51 | struct scratch_buffer buf; |
52 | scratch_buffer_init (buffer: &buf); |
53 | if (pass > 0) |
54 | if (!scratch_buffer_grow (buffer: &buf)) |
55 | { |
56 | printf (format: "scratch_buffer_grow in array_size_must_fail failed\n" ); |
57 | return false; |
58 | } |
59 | if (scratch_buffer_set_array_size (buffer: &buf, nelem: a, size: b)) |
60 | { |
61 | printf (format: "scratch_buffer_set_array_size passed: %d %zu %zu\n" , |
62 | pass, a, b); |
63 | return false; |
64 | } |
65 | if (buf.data != buf.__space.__c) |
66 | { |
67 | printf (format: "scratch_buffer_set_array_size did not free: %d %zu %zu\n" , |
68 | pass, a, b); |
69 | return false; |
70 | } |
71 | } |
72 | return true; |
73 | } |
74 | |
75 | static int |
76 | do_test (void) |
77 | { |
78 | { |
79 | struct scratch_buffer buf; |
80 | scratch_buffer_init (buffer: &buf); |
81 | memset (buf.data, ' ', buf.length); |
82 | scratch_buffer_free (buffer: &buf); |
83 | } |
84 | { |
85 | struct scratch_buffer buf; |
86 | scratch_buffer_init (buffer: &buf); |
87 | memset (buf.data, ' ', buf.length); |
88 | size_t old_length = buf.length; |
89 | scratch_buffer_grow (buffer: &buf); |
90 | if (buf.length <= old_length) |
91 | { |
92 | printf (format: "scratch_buffer_grow did not enlarge buffer\n" ); |
93 | return 1; |
94 | } |
95 | memset (buf.data, ' ', buf.length); |
96 | scratch_buffer_free (buffer: &buf); |
97 | } |
98 | { |
99 | struct scratch_buffer buf; |
100 | scratch_buffer_init (buffer: &buf); |
101 | memset (buf.data, '@', buf.length); |
102 | strcpy (buf.data, "prefix" ); |
103 | size_t old_length = buf.length; |
104 | scratch_buffer_grow_preserve (buffer: &buf); |
105 | if (buf.length <= old_length) |
106 | { |
107 | printf (format: "scratch_buffer_grow_preserve did not enlarge buffer\n" ); |
108 | return 1; |
109 | } |
110 | if (strcmp (buf.data, "prefix" ) != 0) |
111 | { |
112 | printf (format: "scratch_buffer_grow_preserve did not copy buffer\n" ); |
113 | return 1; |
114 | } |
115 | for (unsigned i = 7; i < old_length; ++i) |
116 | if (((char *)buf.data)[i] != '@') |
117 | { |
118 | printf (format: "scratch_buffer_grow_preserve did not copy buffer (%u)\n" , |
119 | i); |
120 | return 1; |
121 | } |
122 | scratch_buffer_free (buffer: &buf); |
123 | } |
124 | { |
125 | struct scratch_buffer buf; |
126 | scratch_buffer_init (buffer: &buf); |
127 | for (int pass = 0; pass < 4; ++pass) |
128 | { |
129 | if (!(unchanged_array_size (buf: &buf, a: 0, b: 0) |
130 | && unchanged_array_size (buf: &buf, a: 1, b: 0) |
131 | && unchanged_array_size (buf: &buf, a: 0, b: 1) |
132 | && unchanged_array_size (buf: &buf, a: -1, b: 0) |
133 | && unchanged_array_size (buf: &buf, a: 0, b: -1) |
134 | && unchanged_array_size (buf: &buf, a: 1ULL << 16, b: 0) |
135 | && unchanged_array_size (buf: &buf, a: 0, b: 1ULL << 16) |
136 | && unchanged_array_size (buf: &buf, a: (size_t) (1ULL << 32), b: 0) |
137 | && unchanged_array_size (buf: &buf, a: 0, b: (size_t) (1ULL << 32)))) |
138 | return 1; |
139 | if (!scratch_buffer_grow (buffer: &buf)) |
140 | { |
141 | printf (format: "scratch_buffer_grow_failed (pass %d)\n" , pass); |
142 | } |
143 | } |
144 | scratch_buffer_free (buffer: &buf); |
145 | } |
146 | { |
147 | if (!(array_size_must_fail (a: -1, b: 1) |
148 | && array_size_must_fail (a: -1, b: -1) |
149 | && array_size_must_fail (a: 1, b: -1) |
150 | && array_size_must_fail (a: ((size_t)-1) / 4, b: 4) |
151 | && array_size_must_fail (a: 4, b: ((size_t)-1) / 4))) |
152 | return 1; |
153 | } |
154 | return 0; |
155 | } |
156 | |
157 | #include <support/test-driver.c> |
158 | |