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