aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_list.zig
diff options
context:
space:
mode:
authorGregory Anders <greg@gpanders.com>2023-08-28 20:25:05 -0500
committerVeikka Tuominen <git@vexu.eu>2023-09-06 19:06:32 +0300
commitcab9da35bd65c81e8efbac3a6c970ce17627e4c7 (patch)
tree6ffbf71d4d44490e554c355cc853fa0671dd3a2e /lib/std/array_list.zig
parent8976ad7ecbd6a0c70749e3bef9c508d0bfde02d2 (diff)
downloadzig-cab9da35bd65c81e8efbac3a6c970ce17627e4c7.tar.gz
zig-cab9da35bd65c81e8efbac3a6c970ce17627e4c7.zip
std: enable FailingAllocator to fail on resize
Now that allocator.resize() is allowed to fail, programs may wish to test code paths that handle resize() failure. The simplest way to do this now is to replace the vtable of the testing allocator with one that uses Allocator.noResize for the 'resize' function pointer. An alternative way to support this testing capability is to augment the FailingAllocator (which is already useful for testing allocation failure scenarios) to intentionally fail on calls to resize(). To do this, add a 'resize_fail_index' parameter to the FailingAllocator that causes resize() to fail after the given number of calls.
Diffstat (limited to 'lib/std/array_list.zig')
-rw-r--r--lib/std/array_list.zig22
1 files changed, 6 insertions, 16 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index ec38fad50b..a258475517 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -1587,13 +1587,8 @@ test "std.ArrayListUnmanaged(u8) implements writer" {
}
test "shrink still sets length when resizing is disabled" {
- // Use the testing allocator but with resize disabled.
- var a = testing.allocator;
- a.vtable = &.{
- .alloc = a.vtable.alloc,
- .resize = Allocator.noResize,
- .free = a.vtable.free,
- };
+ var failing_allocator = testing.FailingAllocator.init(testing.allocator, .{ .resize_fail_index = 0 });
+ const a = failing_allocator.allocator();
{
var list = ArrayList(i32).init(a);
@@ -1620,13 +1615,9 @@ test "shrink still sets length when resizing is disabled" {
}
test "shrinkAndFree with a copy" {
- // Use the testing allocator but with resize disabled.
- var a = testing.allocator;
- a.vtable = &.{
- .alloc = a.vtable.alloc,
- .resize = Allocator.noResize,
- .free = a.vtable.free,
- };
+ var failing_allocator = testing.FailingAllocator.init(testing.allocator, .{ .resize_fail_index = 0 });
+ const a = failing_allocator.allocator();
+
var list = ArrayList(i32).init(a);
defer list.deinit();
@@ -1748,8 +1739,7 @@ test "ArrayListAligned/ArrayListAlignedUnmanaged accepts unaligned slices" {
test "std.ArrayList(u0)" {
// An ArrayList on zero-sized types should not need to allocate
- var failing_allocator = testing.FailingAllocator.init(testing.allocator, 0);
- const a = failing_allocator.allocator();
+ const a = testing.failing_allocator;
var list = ArrayList(u0).init(a);
defer list.deinit();