aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.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/testing.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/testing.zig')
-rw-r--r--lib/std/testing.zig8
1 files changed, 4 insertions, 4 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 40a95a450b..17ac0d4b04 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -14,7 +14,7 @@ pub var allocator_instance = b: {
};
pub const failing_allocator = failing_allocator_instance.allocator();
-pub var failing_allocator_instance = FailingAllocator.init(base_allocator_instance.allocator(), 0);
+pub var failing_allocator_instance = FailingAllocator.init(base_allocator_instance.allocator(), .{ .fail_index = 0 });
pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
@@ -1081,16 +1081,16 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
// Try it once with unlimited memory, make sure it works
const needed_alloc_count = x: {
- var failing_allocator_inst = std.testing.FailingAllocator.init(backing_allocator, std.math.maxInt(usize));
+ var failing_allocator_inst = std.testing.FailingAllocator.init(backing_allocator, .{});
args.@"0" = failing_allocator_inst.allocator();
try @call(.auto, test_fn, args);
- break :x failing_allocator_inst.index;
+ break :x failing_allocator_inst.alloc_index;
};
var fail_index: usize = 0;
while (fail_index < needed_alloc_count) : (fail_index += 1) {
- var failing_allocator_inst = std.testing.FailingAllocator.init(backing_allocator, fail_index);
+ var failing_allocator_inst = std.testing.FailingAllocator.init(backing_allocator, .{ .fail_index = fail_index });
args.@"0" = failing_allocator_inst.allocator();
if (@call(.auto, test_fn, args)) |_| {