diff --git a/doc/docgen.zig b/doc/docgen.zig
index 0218f504199ff9d1ddaa228e95d7f8b9baf4bb3f..e94f3500e78ba546fafecbd45275a58acee8908e 100644
--- a/doc/docgen.zig
+++ b/doc/docgen.zig
@@ -672,7 +672,8 @@ const TermState = enum {
test "term color" {
const input_bytes = "A\x1b[32;1mgreen\x1b[0mB";
- const result = try termColor(std.debug.global_allocator, input_bytes);
+ const result = try termColor(std.testing.allocator, input_bytes);
+ defer std.testing.allocator.free(result);
testing.expectEqualSlices(u8, "AgreenB", result);
}
diff --git a/doc/langref.html.in b/doc/langref.html.in
index 6789b25a63df7972861d202fb107de3fac268627..47f13f9e058e45a8a69b9c46e0d3693f20682c94 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -5359,7 +5359,7 @@ const std = @import("std");
const assert = std.debug.assert;
test "turn HashMap into a set with void" {
- var map = std.HashMap(i32, void, hash_i32, eql_i32).init(std.debug.global_allocator);
+ var map = std.HashMap(i32, void, hash_i32, eql_i32).init(std.testing.allocator);
defer map.deinit();
_ = try map.put(1, {});
@@ -9281,9 +9281,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
In the above example, 100 bytes of stack memory are used to initialize a
{#syntax#}FixedBufferAllocator{#endsyntax#}, which is then passed to a function.
As a convenience there is a global {#syntax#}FixedBufferAllocator{#endsyntax#}
- available for quick tests at {#syntax#}std.debug.global_allocator{#endsyntax#},
- however it is deprecated and should be avoided in favor of directly using a
- {#syntax#}FixedBufferAllocator{#endsyntax#} as in the example above.
+ available for quick tests at {#syntax#}std.testing.allocator{#endsyntax#},
+ which will also do perform basic leak detection.
Currently Zig has no general purpose allocator, but there is
@@ -9341,7 +9340,7 @@ pub fn main() !void {
Are you writing a test, and you want to make sure {#syntax#}error.OutOfMemory{#endsyntax#}
- is handled correctly? In this case, use {#syntax#}std.debug.FailingAllocator{#endsyntax#}.
+ is handled correctly? In this case, use {#syntax#}std.testing.FailingAllocator{#endsyntax#}.
Finally, if none of the above apply, you need a general purpose allocator. Zig does not
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index 64f13eff9b1be4b13ecbebd557ad6d2dfed7e3b7..b028390465f3f08d2a84db14c42698c34cf23c4b 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -320,7 +320,7 @@ test "std.ArrayList.basic" {
}
test "std.ArrayList.orderedRemove" {
- var list = ArrayList(i32).init(debug.global_allocator);
+ var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
try list.append(1);
@@ -347,7 +347,7 @@ test "std.ArrayList.orderedRemove" {
}
test "std.ArrayList.swapRemove" {
- var list = ArrayList(i32).init(debug.global_allocator);
+ var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
try list.append(1);
@@ -374,7 +374,7 @@ test "std.ArrayList.swapRemove" {
}
test "std.ArrayList.swapRemoveOrError" {
- var list = ArrayList(i32).init(debug.global_allocator);
+ var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
// Test just after initialization
@@ -402,7 +402,7 @@ test "std.ArrayList.swapRemoveOrError" {
}
test "std.ArrayList.insert" {
- var list = ArrayList(i32).init(debug.global_allocator);
+ var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
try list.append(1);
@@ -416,7 +416,7 @@ test "std.ArrayList.insert" {
}
test "std.ArrayList.insertSlice" {
- var list = ArrayList(i32).init(debug.global_allocator);
+ var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
try list.append(1);
@@ -443,7 +443,8 @@ const Item = struct {
};
test "std.ArrayList: ArrayList(T) of struct T" {
- var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(debug.global_allocator) };
- try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(debug.global_allocator) });
+ var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(testing.allocator) };
+ defer root.sub_items.deinit();
+ try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) });
testing.expect(root.sub_items.items[0].integer == 42);
}
diff --git a/lib/std/buf_map.zig b/lib/std/buf_map.zig
index 06f00608bb0247b79832eeecb948e3827d7a9db7..e8bc735b572c45b3bed6efc05ee88054e2816987 100644
--- a/lib/std/buf_map.zig
+++ b/lib/std/buf_map.zig
@@ -43,9 +43,10 @@ pub const BufMap = struct {
pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {
const value_copy = try self.copy(value);
errdefer self.free(value_copy);
- // Avoid copying key if it already exists
const get_or_put = try self.hash_map.getOrPut(key);
- if (!get_or_put.found_existing) {
+ if (get_or_put.found_existing) {
+ self.free(get_or_put.kv.value);
+ } else {
get_or_put.kv.key = self.copy(key) catch |err| {
_ = self.hash_map.remove(key);
return err;
@@ -83,7 +84,7 @@ pub const BufMap = struct {
};
test "BufMap" {
- var bufmap = BufMap.init(std.heap.page_allocator);
+ var bufmap = BufMap.init(std.testing.allocator);
defer bufmap.deinit();
try bufmap.set("x", "1");
diff --git a/lib/std/buf_set.zig b/lib/std/buf_set.zig
index 7ac8136e7dd41ef86c8303836000797d41187ceb..89df0478ff91437873b56d3ce2d782091105e048 100644
--- a/lib/std/buf_set.zig
+++ b/lib/std/buf_set.zig
@@ -65,7 +65,7 @@ pub const BufSet = struct {
};
test "BufSet" {
- var bufset = BufSet.init(std.heap.page_allocator);
+ var bufset = BufSet.init(std.testing.allocator);
defer bufset.deinit();
try bufset.put("x");
diff --git a/lib/std/buffer.zig b/lib/std/buffer.zig
index 42bf8e8142e8b0055efe057489e15fd6b2d1a41d..077b2b615fdace5b457e7e7202adaee9b9cdc8f7 100644
--- a/lib/std/buffer.zig
+++ b/lib/std/buffer.zig
@@ -150,7 +150,9 @@ pub const Buffer = struct {
};
test "simple Buffer" {
- var buf = try Buffer.init(debug.global_allocator, "");
+ var buf = try Buffer.init(testing.allocator, "");
+ defer buf.deinit();
+
testing.expect(buf.len() == 0);
try buf.append("hello");
try buf.append(" ");
@@ -159,6 +161,7 @@ test "simple Buffer" {
testing.expect(mem.eql(u8, mem.toSliceConst(u8, buf.toSliceConst().ptr), buf.toSliceConst()));
var buf2 = try Buffer.initFromBuffer(buf);
+ defer buf2.deinit();
testing.expect(buf.eql(buf2.toSliceConst()));
testing.expect(buf.startsWith("hell"));
@@ -169,14 +172,16 @@ test "simple Buffer" {
}
test "Buffer.initSize" {
- var buf = try Buffer.initSize(debug.global_allocator, 3);
+ var buf = try Buffer.initSize(testing.allocator, 3);
+ defer buf.deinit();
testing.expect(buf.len() == 3);
try buf.append("hello");
testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello"));
}
test "Buffer.initCapacity" {
- var buf = try Buffer.initCapacity(debug.global_allocator, 10);
+ var buf = try Buffer.initCapacity(testing.allocator, 10);
+ defer buf.deinit();
testing.expect(buf.len() == 0);
testing.expect(buf.capacity() >= 10);
const old_cap = buf.capacity();
diff --git a/lib/std/build.zig b/lib/std/build.zig
index f535b022af0c52e50f7ff42b6bcf830af7c8f2f4..2b9a8ddcd62b73a95c71a1cf23526b81f4c55a74 100644
--- a/lib/std/build.zig
+++ b/lib/std/build.zig
@@ -1059,7 +1059,10 @@ pub const Builder = struct {
};
test "builder.findProgram compiles" {
+ // TODO: uncomment and fix the leak
+ // const builder = try Builder.create(std.testing.allocator, "zig", "zig-cache", "zig-cache");
const builder = try Builder.create(std.heap.page_allocator, "zig", "zig-cache", "zig-cache");
+ defer builder.destroy();
_ = builder.findProgram(&[_][]const u8{}, &[_][]const u8{}) catch null;
}
diff --git a/lib/std/debug.zig b/lib/std/debug.zig
index 28340fede2088d977acffed0706bd94f230c2bb4..3af2a5c417df993812c8426157235f51c2ef7c79 100644
--- a/lib/std/debug.zig
+++ b/lib/std/debug.zig
@@ -19,8 +19,8 @@ const windows = std.os.windows;
pub const leb = @import("debug/leb128.zig");
-pub const FailingAllocator = @import("debug/failing_allocator.zig").FailingAllocator;
-pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator;
+pub const global_allocator = @compileError("Please switch to std.testing.allocator.");
+pub const failing_allocator = @compileError("Please switch to std.testing.failing_allocator.");
pub const runtime_safety = switch (builtin.mode) {
.Debug, .ReleaseSafe => true,
@@ -2196,11 +2196,6 @@ fn readInitialLength(comptime E: type, in_stream: *io.InStream(E), is_64: *bool)
}
}
-/// This should only be used in temporary test programs.
-pub const global_allocator = &global_fixed_allocator.allocator;
-var global_fixed_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(global_allocator_mem[0..]);
-var global_allocator_mem: [100 * 1024]u8 = undefined;
-
/// TODO multithreaded awareness
var debug_info_allocator: ?*mem.Allocator = null;
var debug_info_arena_allocator: std.heap.ArenaAllocator = undefined;
diff --git a/lib/std/debug/failing_allocator.zig b/lib/std/debug/failing_allocator.zig
deleted file mode 100644
index 081a29cd97576db857804eb553c95e1f2c7136a6..0000000000000000000000000000000000000000
--- a/lib/std/debug/failing_allocator.zig
+++ /dev/null
@@ -1,81 +0,0 @@
-const std = @import("../std.zig");
-const mem = std.mem;
-
-/// Allocator that fails after N allocations, useful for making sure out of
-/// memory conditions are handled correctly.
-///
-/// To use this, first initialize it and get an allocator with
-///
-/// `const failing_allocator = &FailingAllocator.init(,
-/// ).allocator;`
-///
-/// Then use `failing_allocator` anywhere you would have used a
-/// different allocator.
-pub const FailingAllocator = struct {
- allocator: mem.Allocator,
- index: usize,
- fail_index: usize,
- internal_allocator: *mem.Allocator,
- allocated_bytes: usize,
- freed_bytes: usize,
- allocations: usize,
- deallocations: usize,
-
- /// `fail_index` is the number of successful allocations you can
- /// expect from this allocator. The next allocation will fail.
- /// For example, if this is called with `fail_index` equal to 2,
- /// the following test will pass:
- ///
- /// var a = try failing_alloc.create(i32);
- /// var b = try failing_alloc.create(i32);
- /// testing.expectError(error.OutOfMemory, failing_alloc.create(i32));
- pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator {
- return FailingAllocator{
- .internal_allocator = allocator,
- .fail_index = fail_index,
- .index = 0,
- .allocated_bytes = 0,
- .freed_bytes = 0,
- .allocations = 0,
- .deallocations = 0,
- .allocator = mem.Allocator{
- .reallocFn = realloc,
- .shrinkFn = shrink,
- },
- };
- }
-
- fn realloc(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
- const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
- if (self.index == self.fail_index) {
- return error.OutOfMemory;
- }
- const result = try self.internal_allocator.reallocFn(
- self.internal_allocator,
- old_mem,
- old_align,
- new_size,
- new_align,
- );
- if (new_size < old_mem.len) {
- self.freed_bytes += old_mem.len - new_size;
- if (new_size == 0)
- self.deallocations += 1;
- } else if (new_size > old_mem.len) {
- self.allocated_bytes += new_size - old_mem.len;
- if (old_mem.len == 0)
- self.allocations += 1;
- }
- self.index += 1;
- return result;
- }
-
- fn shrink(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
- const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
- const r = self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align);
- self.freed_bytes += old_mem.len - r.len;
- if (new_size == 0)
- self.deallocations += 1;
- return r;
- }
-};
diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig
index 7870979abb6f127912df32d1cee625576d40b23e..ac8589f2ce9156428a370dd95d36c37a1e4d422a 100644
--- a/lib/std/fifo.zig
+++ b/lib/std/fifo.zig
@@ -347,7 +347,7 @@ pub fn LinearFifo(
}
test "LinearFifo(u8, .Dynamic)" {
- var fifo = LinearFifo(u8, .Dynamic).init(debug.global_allocator);
+ var fifo = LinearFifo(u8, .Dynamic).init(testing.allocator);
defer fifo.deinit();
try fifo.write("HELLO");
@@ -422,7 +422,7 @@ test "LinearFifo" {
var fifo = switch (bt) {
.Static => FifoType.init(),
.Slice => FifoType.init(buf[0..]),
- .Dynamic => FifoType.init(debug.global_allocator),
+ .Dynamic => FifoType.init(testing.allocator),
};
defer fifo.deinit();
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
index cf3322a9952b09a09d287d1171c3856020dc2e75..627d8bc7aba4bcffdf4c8c177a0a6c1a34c1de6f 100644
--- a/lib/std/fmt.zig
+++ b/lib/std/fmt.zig
@@ -1598,7 +1598,8 @@ test "hexToBytes" {
test "formatIntValue with comptime_int" {
const value: comptime_int = 123456789123456789;
- var buf = try std.Buffer.init(std.debug.global_allocator, "");
+ var buf = try std.Buffer.init(std.testing.allocator, "");
+ defer buf.deinit();
try formatIntValue(value, "", FormatOptions{}, &buf, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append);
std.testing.expect(mem.eql(u8, buf.toSlice(), "123456789123456789"));
}
@@ -1652,19 +1653,23 @@ test "formatType max_depth" {
inst.a = &inst;
inst.tu.ptr = &inst.tu;
- var buf0 = try std.Buffer.init(std.debug.global_allocator, "");
+ var buf0 = try std.Buffer.init(std.testing.allocator, "");
+ defer buf0.deinit();
try formatType(inst, "", FormatOptions{}, &buf0, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 0);
std.testing.expect(mem.eql(u8, buf0.toSlice(), "S{ ... }"));
- var buf1 = try std.Buffer.init(std.debug.global_allocator, "");
+ var buf1 = try std.Buffer.init(std.testing.allocator, "");
+ defer buf1.deinit();
try formatType(inst, "", FormatOptions{}, &buf1, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 1);
std.testing.expect(mem.eql(u8, buf1.toSlice(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }"));
- var buf2 = try std.Buffer.init(std.debug.global_allocator, "");
+ var buf2 = try std.Buffer.init(std.testing.allocator, "");
+ defer buf2.deinit();
try formatType(inst, "", FormatOptions{}, &buf2, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 2);
std.testing.expect(mem.eql(u8, buf2.toSlice(), "S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }"));
- var buf3 = try std.Buffer.init(std.debug.global_allocator, "");
+ var buf3 = try std.Buffer.init(std.testing.allocator, "");
+ defer buf3.deinit();
try formatType(inst, "", FormatOptions{}, &buf3, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 3);
std.testing.expect(mem.eql(u8, buf3.toSlice(), "S{ .a = S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ .ptr = TU{ ... } } }, .e = E.Two, .vec = (10.200,2.220) }"));
}
diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig
index b3ad3e9f7a2eed91cdaf79465a603bd97d695048..66e24a4805acc3b10f59f6721f50292e22224733 100644
--- a/lib/std/fs/path.zig
+++ b/lib/std/fs/path.zig
@@ -665,15 +665,16 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
}
test "resolve" {
- const cwd = try process.getCwdAlloc(debug.global_allocator);
+ const cwd = try process.getCwdAlloc(testing.allocator);
+ defer testing.allocator.free(cwd);
if (builtin.os == .windows) {
if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
cwd[0] = asciiUpper(cwd[0]);
}
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{"."}), cwd));
+ try testResolveWindows(&[_][]const u8{"."}, cwd);
} else {
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }), cwd));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{"."}), cwd));
+ try testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }, cwd);
+ try testResolvePosix(&[_][]const u8{"."}, cwd);
}
}
@@ -683,66 +684,71 @@ test "resolveWindows" {
return error.SkipZigTest;
}
if (builtin.os == .windows) {
- const cwd = try process.getCwdAlloc(debug.global_allocator);
+ const cwd = try process.getCwdAlloc(testing.allocator);
+ defer testing.allocator.free(cwd);
const parsed_cwd = windowsParsePath(cwd);
{
- const result = testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" });
- const expected = try join(debug.global_allocator, &[_][]const u8{
+ const expected = try join(testing.allocator, &[_][]const u8{
parsed_cwd.disk_designator,
"usr\\local\\lib\\zig\\std\\array_list.zig",
});
+ defer testing.allocator.free(expected);
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
}
- testing.expect(mem.eql(u8, result, expected));
+ try testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }, expected);
}
{
- const result = testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" });
- const expected = try join(debug.global_allocator, &[_][]const u8{
+ const expected = try join(testing.allocator, &[_][]const u8{
cwd,
"usr\\local\\lib\\zig",
});
+ defer testing.allocator.free(expected);
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
}
- testing.expect(mem.eql(u8, result, expected));
+ try testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" }, expected);
}
}
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }), "C:\\hi\\ok"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }), "C:\\blah\\a"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }), "C:\\blah\\a"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }), "D:\\e.exe"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/ignore", "c:/some/file" }), "C:\\some\\file"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "d:/ignore", "d:some/dir//" }), "D:\\ignore\\some\\dir"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "//server/share", "..", "relative\\" }), "\\\\server\\share\\relative"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/", "//" }), "C:\\"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/", "//dir" }), "C:\\dir"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }), "\\\\server\\share\\"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/", "//server//share" }), "\\\\server\\share\\"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "c:/", "///some//dir" }), "C:\\some\\dir"));
- testing.expect(mem.eql(u8, testResolveWindows(&[_][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }), "C:\\foo\\tmp.3\\cycles\\root.js"));
+ try testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }, "C:\\hi\\ok");
+ try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }, "C:\\blah\\a");
+ try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }, "C:\\blah\\a");
+ try testResolveWindows(&[_][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }, "D:\\e.exe");
+ try testResolveWindows(&[_][]const u8{ "c:/ignore", "c:/some/file" }, "C:\\some\\file");
+ try testResolveWindows(&[_][]const u8{ "d:/ignore", "d:some/dir//" }, "D:\\ignore\\some\\dir");
+ try testResolveWindows(&[_][]const u8{ "//server/share", "..", "relative\\" }, "\\\\server\\share\\relative");
+ try testResolveWindows(&[_][]const u8{ "c:/", "//" }, "C:\\");
+ try testResolveWindows(&[_][]const u8{ "c:/", "//dir" }, "C:\\dir");
+ try testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }, "\\\\server\\share\\");
+ try testResolveWindows(&[_][]const u8{ "c:/", "//server//share" }, "\\\\server\\share\\");
+ try testResolveWindows(&[_][]const u8{ "c:/", "///some//dir" }, "C:\\some\\dir");
+ try testResolveWindows(&[_][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }, "C:\\foo\\tmp.3\\cycles\\root.js");
}
test "resolvePosix" {
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/a/b", "c" }), "/a/b/c"));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/a/b", "c", "//d", "e///" }), "/d/e"));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/a/b/c", "..", "../" }), "/a"));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/", "..", ".." }), "/"));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{"/a/b/c/"}), "/a/b/c"));
+ try testResolvePosix(&[_][]const u8{ "/a/b", "c" }, "/a/b/c");
+ try testResolvePosix(&[_][]const u8{ "/a/b", "c", "//d", "e///" }, "/d/e");
+ try testResolvePosix(&[_][]const u8{ "/a/b/c", "..", "../" }, "/a");
+ try testResolvePosix(&[_][]const u8{ "/", "..", ".." }, "/");
+ try testResolvePosix(&[_][]const u8{"/a/b/c/"}, "/a/b/c");
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/var/lib", "../", "file/" }), "/var/file"));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/var/lib", "/../", "file/" }), "/file"));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/some/dir", ".", "/absolute/" }), "/absolute"));
- testing.expect(mem.eql(u8, testResolvePosix(&[_][]const u8{ "/foo/tmp.3/", "../tmp.3/cycles/root.js" }), "/foo/tmp.3/cycles/root.js"));
+ try testResolvePosix(&[_][]const u8{ "/var/lib", "../", "file/" }, "/var/file");
+ try testResolvePosix(&[_][]const u8{ "/var/lib", "/../", "file/" }, "/file");
+ try testResolvePosix(&[_][]const u8{ "/some/dir", ".", "/absolute/" }, "/absolute");
+ try testResolvePosix(&[_][]const u8{ "/foo/tmp.3/", "../tmp.3/cycles/root.js" }, "/foo/tmp.3/cycles/root.js");
}
-fn testResolveWindows(paths: []const []const u8) []u8 {
- return resolveWindows(debug.global_allocator, paths) catch unreachable;
+fn testResolveWindows(paths: []const []const u8, expected: []const u8) !void {
+ const actual = try resolveWindows(testing.allocator, paths);
+ defer testing.allocator.free(actual);
+ return testing.expect(mem.eql(u8, actual, expected));
}
-fn testResolvePosix(paths: []const []const u8) []u8 {
- return resolvePosix(debug.global_allocator, paths) catch unreachable;
+fn testResolvePosix(paths: []const []const u8, expected: []const u8) !void {
+ const actual = try resolvePosix(testing.allocator, paths);
+ defer testing.allocator.free(actual);
+ return testing.expect(mem.eql(u8, actual, expected));
}
/// If the path is a file in the current directory (no directory component)
@@ -1126,51 +1132,53 @@ test "relative" {
// TODO https://github.com/ziglang/zig/issues/3288
return error.SkipZigTest;
}
- testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");
- testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");
- testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");
- testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/bbbb", "");
- testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc");
- testRelativeWindows("c:/aaaa/", "c:/aaaa/cccc", "cccc");
- testRelativeWindows("c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb");
- testRelativeWindows("c:/aaaa/bbbb", "d:\\", "D:\\");
- testRelativeWindows("c:/AaAa/bbbb", "c:/aaaa/bbbb", "");
- testRelativeWindows("c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc");
- testRelativeWindows("C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\..");
- testRelativeWindows("C:\\foo\\test", "C:\\foo\\test\\bar\\package.json", "bar\\package.json");
- testRelativeWindows("C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz");
- testRelativeWindows("C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux");
- testRelativeWindows("\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz");
- testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar", "..");
- testRelativeWindows("\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz");
- testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux");
- testRelativeWindows("C:\\baz-quux", "C:\\baz", "..\\baz");
- testRelativeWindows("C:\\baz", "C:\\baz-quux", "..\\baz-quux");
- testRelativeWindows("\\\\foo\\baz-quux", "\\\\foo\\baz", "..\\baz");
- testRelativeWindows("\\\\foo\\baz", "\\\\foo\\baz-quux", "..\\baz-quux");
- testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz");
- testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz");
+ try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");
+ try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");
+ try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");
+ try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/bbbb", "");
+ try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc");
+ try testRelativeWindows("c:/aaaa/", "c:/aaaa/cccc", "cccc");
+ try testRelativeWindows("c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb");
+ try testRelativeWindows("c:/aaaa/bbbb", "d:\\", "D:\\");
+ try testRelativeWindows("c:/AaAa/bbbb", "c:/aaaa/bbbb", "");
+ try testRelativeWindows("c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc");
+ try testRelativeWindows("C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\..");
+ try testRelativeWindows("C:\\foo\\test", "C:\\foo\\test\\bar\\package.json", "bar\\package.json");
+ try testRelativeWindows("C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz");
+ try testRelativeWindows("C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux");
+ try testRelativeWindows("\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz");
+ try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar", "..");
+ try testRelativeWindows("\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz");
+ try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux");
+ try testRelativeWindows("C:\\baz-quux", "C:\\baz", "..\\baz");
+ try testRelativeWindows("C:\\baz", "C:\\baz-quux", "..\\baz-quux");
+ try testRelativeWindows("\\\\foo\\baz-quux", "\\\\foo\\baz", "..\\baz");
+ try testRelativeWindows("\\\\foo\\baz", "\\\\foo\\baz-quux", "..\\baz-quux");
+ try testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz");
+ try testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz");
- testRelativePosix("/var/lib", "/var", "..");
- testRelativePosix("/var/lib", "/bin", "../../bin");
- testRelativePosix("/var/lib", "/var/lib", "");
- testRelativePosix("/var/lib", "/var/apache", "../apache");
- testRelativePosix("/var/", "/var/lib", "lib");
- testRelativePosix("/", "/var/lib", "var/lib");
- testRelativePosix("/foo/test", "/foo/test/bar/package.json", "bar/package.json");
- testRelativePosix("/Users/a/web/b/test/mails", "/Users/a/web/b", "../..");
- testRelativePosix("/foo/bar/baz-quux", "/foo/bar/baz", "../baz");
- testRelativePosix("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux");
- testRelativePosix("/baz-quux", "/baz", "../baz");
- testRelativePosix("/baz", "/baz-quux", "../baz-quux");
+ try testRelativePosix("/var/lib", "/var", "..");
+ try testRelativePosix("/var/lib", "/bin", "../../bin");
+ try testRelativePosix("/var/lib", "/var/lib", "");
+ try testRelativePosix("/var/lib", "/var/apache", "../apache");
+ try testRelativePosix("/var/", "/var/lib", "lib");
+ try testRelativePosix("/", "/var/lib", "var/lib");
+ try testRelativePosix("/foo/test", "/foo/test/bar/package.json", "bar/package.json");
+ try testRelativePosix("/Users/a/web/b/test/mails", "/Users/a/web/b", "../..");
+ try testRelativePosix("/foo/bar/baz-quux", "/foo/bar/baz", "../baz");
+ try testRelativePosix("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux");
+ try testRelativePosix("/baz-quux", "/baz", "../baz");
+ try testRelativePosix("/baz", "/baz-quux", "../baz-quux");
}
-fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) void {
- const result = relativePosix(debug.global_allocator, from, to) catch unreachable;
+fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) !void {
+ const result = try relativePosix(testing.allocator, from, to);
+ defer testing.allocator.free(result);
testing.expectEqualSlices(u8, expected_output, result);
}
-fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) void {
- const result = relativeWindows(debug.global_allocator, from, to) catch unreachable;
+fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) !void {
+ const result = try relativeWindows(testing.allocator, from, to);
+ defer testing.allocator.free(result);
testing.expectEqualSlices(u8, expected_output, result);
}
diff --git a/lib/std/hash/auto_hash.zig b/lib/std/hash/auto_hash.zig
index e60a1c10552077875eb957cc064d0347e4f2fe7c..adde71875eae1d1bcc863648a5adb1935c61e10c 100644
--- a/lib/std/hash/auto_hash.zig
+++ b/lib/std/hash/auto_hash.zig
@@ -234,8 +234,8 @@ test "hash pointer" {
test "hash slice shallow" {
// Allocate one array dynamically so that we're assured it is not merged
// with the other by the optimization passes.
- const array1 = try std.heap.page_allocator.create([6]u32);
- defer std.heap.page_allocator.destroy(array1);
+ const array1 = try std.testing.allocator.create([6]u32);
+ defer std.testing.allocator.destroy(array1);
array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };
const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 };
const a = array1[0..];
@@ -250,8 +250,8 @@ test "hash slice shallow" {
test "hash slice deep" {
// Allocate one array dynamically so that we're assured it is not merged
// with the other by the optimization passes.
- const array1 = try std.heap.page_allocator.create([6]u32);
- defer std.heap.page_allocator.destroy(array1);
+ const array1 = try std.testing.allocator.create([6]u32);
+ defer std.testing.allocator.destroy(array1);
array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };
const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 };
const a = array1[0..];
@@ -278,7 +278,7 @@ test "hash struct deep" {
}
};
- const allocator = std.heap.page_allocator;
+ const allocator = std.testing.allocator;
const foo = try Foo.init(allocator, 123, 1.0, true);
const bar = try Foo.init(allocator, 123, 1.0, true);
const baz = try Foo.init(allocator, 123, 1.0, false);
diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig
index 57a2f58b92f57aafcd2ff94e57a2eedbf5767ccb..80ca218df63b75796f37b6ea430a6e4765660272 100644
--- a/lib/std/hash_map.zig
+++ b/lib/std/hash_map.zig
@@ -419,7 +419,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
}
test "basic hash map usage" {
- var map = AutoHashMap(i32, i32).init(std.heap.page_allocator);
+ var map = AutoHashMap(i32, i32).init(std.testing.allocator);
defer map.deinit();
testing.expect((try map.put(1, 11)) == null);
@@ -463,7 +463,7 @@ test "basic hash map usage" {
}
test "iterator hash map" {
- var reset_map = AutoHashMap(i32, i32).init(std.heap.page_allocator);
+ var reset_map = AutoHashMap(i32, i32).init(std.testing.allocator);
defer reset_map.deinit();
try reset_map.putNoClobber(1, 11);
@@ -509,7 +509,7 @@ test "iterator hash map" {
}
test "ensure capacity" {
- var map = AutoHashMap(i32, i32).init(std.heap.page_allocator);
+ var map = AutoHashMap(i32, i32).init(std.testing.allocator);
defer map.deinit();
try map.ensureCapacity(20);
diff --git a/lib/std/heap.zig b/lib/std/heap.zig
index f25d2f0172d6bb92f43cdff94cdad47a499965a6..e7196f82f260b650766364f9da1e6fe3e3e43a09 100644
--- a/lib/std/heap.zig
+++ b/lib/std/heap.zig
@@ -711,6 +711,10 @@ pub const ThreadSafeFixedBufferAllocator = blk: {
fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
return old_mem[0..new_size];
}
+
+ pub fn reset(self: *ThreadSafeFixedBufferAllocator) void {
+ self.end_index = 0;
+ }
};
}
};
diff --git a/lib/std/heap/logging_allocator.zig b/lib/std/heap/logging_allocator.zig
index d1b7f4776d3498ea91780567096f4dd0effbabfd..7dce7bc20a5a1b5d98edd044285314322466bc79 100644
--- a/lib/std/heap/logging_allocator.zig
+++ b/lib/std/heap/logging_allocator.zig
@@ -57,7 +57,7 @@ test "LoggingAllocator" {
var slice_stream = std.io.SliceOutStream.init(buf[0..]);
const stream = &slice_stream.stream;
- const allocator = &LoggingAllocator.init(std.heap.page_allocator, @ptrCast(*AnyErrorOutStream, stream)).allocator;
+ const allocator = &LoggingAllocator.init(std.testing.allocator, @ptrCast(*AnyErrorOutStream, stream)).allocator;
const ptr = try allocator.alloc(u8, 10);
allocator.free(ptr);
diff --git a/lib/std/io.zig b/lib/std/io.zig
index 93f2ae7680da78b976b430c21f3225c6e9af889a..a0e58c373dd32e202018697075709a542176094a 100644
--- a/lib/std/io.zig
+++ b/lib/std/io.zig
@@ -891,7 +891,7 @@ pub fn readLineSlice(slice: []u8) ![]u8 {
pub fn readLineSliceFrom(stream: var, slice: []u8) ![]u8 {
// We cannot use Buffer.fromOwnedSlice, as it wants to append a null byte
// after taking ownership, which would always require an allocation.
- var buf = std.Buffer{ .list = std.ArrayList(u8).fromOwnedSlice(debug.failing_allocator, slice) };
+ var buf = std.Buffer{ .list = std.ArrayList(u8).fromOwnedSlice(testing.failing_allocator, slice) };
try buf.resize(0);
return try readLineFrom(stream, &buf);
}
diff --git a/lib/std/linked_list.zig b/lib/std/linked_list.zig
index 6495653150f3238e011c950738c34c70ee8a0da5..a21c9a83eb7026e85ce9f2d923236bd481e0b01e 100644
--- a/lib/std/linked_list.zig
+++ b/lib/std/linked_list.zig
@@ -143,7 +143,7 @@ pub fn SinglyLinkedList(comptime T: type) type {
}
test "basic SinglyLinkedList test" {
- const allocator = debug.global_allocator;
+ const allocator = testing.allocator;
var list = SinglyLinkedList(u32).init();
var one = try list.createNode(1, allocator);
@@ -404,7 +404,7 @@ pub fn TailQueue(comptime T: type) type {
}
test "basic TailQueue test" {
- const allocator = debug.global_allocator;
+ const allocator = testing.allocator;
var list = TailQueue(u32).init();
var one = try list.createNode(1, allocator);
@@ -456,7 +456,7 @@ test "basic TailQueue test" {
}
test "TailQueue concatenation" {
- const allocator = debug.global_allocator;
+ const allocator = testing.allocator;
var list1 = TailQueue(u32).init();
var list2 = TailQueue(u32).init();
diff --git a/lib/std/mutex.zig b/lib/std/mutex.zig
index 7fbe4fde1842aaa0e7698e2f5790eceeb220c309..30ac835f9fcb5d645c9909d5d7883350b29e758a 100644
--- a/lib/std/mutex.zig
+++ b/lib/std/mutex.zig
@@ -306,12 +306,6 @@ const TestContext = struct {
};
test "std.Mutex" {
- var plenty_of_memory = try std.heap.page_allocator.alloc(u8, 300 * 1024);
- defer std.heap.page_allocator.free(plenty_of_memory);
-
- var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
- var a = &fixed_buffer_allocator.allocator;
-
var mutex = Mutex.init();
defer mutex.deinit();
diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig
index f71e80c7d76b0c78d7ff349e6dea003e43a3376f..a872c03fde18e087a4ed60cde526e01030cd2dbf 100644
--- a/lib/std/os/test.zig
+++ b/lib/std/os/test.zig
@@ -9,7 +9,7 @@ const elf = std.elf;
const File = std.fs.File;
const Thread = std.Thread;
-const a = std.debug.global_allocator;
+const a = std.testing.allocator;
const builtin = @import("builtin");
const AtomicRmwOp = builtin.AtomicRmwOp;
@@ -235,8 +235,8 @@ test "pipe" {
}
test "argsAlloc" {
- var args = try std.process.argsAlloc(std.heap.page_allocator);
- std.process.argsFree(std.heap.page_allocator, args);
+ var args = try std.process.argsAlloc(std.testing.allocator);
+ std.process.argsFree(std.testing.allocator, args);
}
test "memfd_create" {
diff --git a/lib/std/packed_int_array.zig b/lib/std/packed_int_array.zig
index bc29e985b5edd41da4ebcefc932fe36daa7e2e46..3dfa55e74b6bc0e28802f1cdb08ec783096614ac 100644
--- a/lib/std/packed_int_array.zig
+++ b/lib/std/packed_int_array.zig
@@ -604,7 +604,7 @@ test "PackedIntArray at end of available memory" {
p: PackedArray,
};
- const allocator = std.heap.page_allocator;
+ const allocator = std.testing.allocator;
var pad = try allocator.create(Padded);
defer allocator.destroy(pad);
@@ -618,7 +618,7 @@ test "PackedIntSlice at end of available memory" {
}
const PackedSlice = PackedIntSlice(u11);
- const allocator = std.heap.page_allocator;
+ const allocator = std.testing.allocator;
var page = try allocator.alloc(u8, std.mem.page_size);
defer allocator.free(page);
diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig
index 6c56f469f960a372c701bffa0fa0ffeb1eaf9aa9..e726a07a88538f8fbbe395eb6954a228be09146d 100644
--- a/lib/std/priority_queue.zig
+++ b/lib/std/priority_queue.zig
@@ -1,10 +1,10 @@
const std = @import("std.zig");
const Allocator = std.mem.Allocator;
-const debug = std.debug;
-const assert = debug.assert;
-const expect = std.testing.expect;
-const expectEqual = std.testing.expectEqual;
-const expectError = std.testing.expectError;
+const assert = std.debug.assert;
+const testing = std.testing;
+const expect = testing.expect;
+const expectEqual = testing.expectEqual;
+const expectError = testing.expectError;
/// Priority queue for storing generic data. Initialize with `init`.
pub fn PriorityQueue(comptime T: type) type {
@@ -239,7 +239,7 @@ fn greaterThan(a: u32, b: u32) bool {
const PQ = PriorityQueue(u32);
test "std.PriorityQueue: add and remove min heap" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
try queue.add(54);
@@ -257,7 +257,7 @@ test "std.PriorityQueue: add and remove min heap" {
}
test "std.PriorityQueue: add and remove same min heap" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
try queue.add(1);
@@ -275,14 +275,14 @@ test "std.PriorityQueue: add and remove same min heap" {
}
test "std.PriorityQueue: removeOrNull on empty" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
expect(queue.removeOrNull() == null);
}
test "std.PriorityQueue: edge case 3 elements" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
try queue.add(9);
@@ -294,7 +294,7 @@ test "std.PriorityQueue: edge case 3 elements" {
}
test "std.PriorityQueue: peek" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
expect(queue.peek() == null);
@@ -306,7 +306,7 @@ test "std.PriorityQueue: peek" {
}
test "std.PriorityQueue: sift up with odd indices" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
for (items) |e| {
@@ -320,7 +320,7 @@ test "std.PriorityQueue: sift up with odd indices" {
}
test "std.PriorityQueue: addSlice" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
try queue.addSlice(items[0..]);
@@ -333,8 +333,8 @@ test "std.PriorityQueue: addSlice" {
test "std.PriorityQueue: fromOwnedSlice" {
const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
- const heap_items = try std.mem.dupe(debug.global_allocator, u32, items[0..]);
- var queue = PQ.fromOwnedSlice(debug.global_allocator, lessThan, heap_items[0..]);
+ const heap_items = try std.mem.dupe(testing.allocator, u32, items[0..]);
+ var queue = PQ.fromOwnedSlice(testing.allocator, lessThan, heap_items[0..]);
defer queue.deinit();
const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 };
@@ -344,7 +344,7 @@ test "std.PriorityQueue: fromOwnedSlice" {
}
test "std.PriorityQueue: add and remove max heap" {
- var queue = PQ.init(debug.global_allocator, greaterThan);
+ var queue = PQ.init(testing.allocator, greaterThan);
defer queue.deinit();
try queue.add(54);
@@ -362,7 +362,7 @@ test "std.PriorityQueue: add and remove max heap" {
}
test "std.PriorityQueue: add and remove same max heap" {
- var queue = PQ.init(debug.global_allocator, greaterThan);
+ var queue = PQ.init(testing.allocator, greaterThan);
defer queue.deinit();
try queue.add(1);
@@ -380,8 +380,8 @@ test "std.PriorityQueue: add and remove same max heap" {
}
test "std.PriorityQueue: iterator" {
- var queue = PQ.init(debug.global_allocator, lessThan);
- var map = std.AutoHashMap(u32, void).init(debug.global_allocator);
+ var queue = PQ.init(testing.allocator, lessThan);
+ var map = std.AutoHashMap(u32, void).init(testing.allocator);
defer {
queue.deinit();
map.deinit();
@@ -402,7 +402,7 @@ test "std.PriorityQueue: iterator" {
}
test "std.PriorityQueue: remove at index" {
- var queue = PQ.init(debug.global_allocator, lessThan);
+ var queue = PQ.init(testing.allocator, lessThan);
defer queue.deinit();
try queue.add(3);
diff --git a/lib/std/process.zig b/lib/std/process.zig
index 0ce3cabc199c3dcb47da14bd97675370c9daf4ce..ca5ca3a3bb0da2cc47597f336f15a085aa2e8a80 100644
--- a/lib/std/process.zig
+++ b/lib/std/process.zig
@@ -79,7 +79,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
// https://github.com/WebAssembly/WASI/issues/27
var environ = try allocator.alloc(?[*:0]u8, environ_count + 1);
defer allocator.free(environ);
- var environ_buf = try std.heap.page_allocator.alloc(u8, environ_buf_size);
+ var environ_buf = try allocator.alloc(u8, environ_buf_size);
defer allocator.free(environ_buf);
const environ_get_ret = os.wasi.environ_get(environ.ptr, environ_buf.ptr);
@@ -114,7 +114,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
}
test "os.getEnvMap" {
- var env = try getEnvMap(std.debug.global_allocator);
+ var env = try getEnvMap(std.testing.allocator);
defer env.deinit();
}
@@ -165,7 +165,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned
}
test "os.getEnvVarOwned" {
- var ga = std.debug.global_allocator;
+ var ga = std.testing.allocator;
testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV"));
}
@@ -492,10 +492,11 @@ test "windows arg parsing" {
fn testWindowsCmdLine(input_cmd_line: [*]const u8, expected_args: []const []const u8) void {
var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);
for (expected_args) |expected_arg| {
- const arg = it.next(std.debug.global_allocator).? catch unreachable;
+ const arg = it.next(std.testing.allocator).? catch unreachable;
+ defer std.testing.allocator.free(arg);
testing.expectEqualSlices(u8, expected_arg, arg);
}
- testing.expect(it.next(std.debug.global_allocator) == null);
+ testing.expect(it.next(std.testing.allocator) == null);
}
pub const UserInfo = struct {
diff --git a/lib/std/segmented_list.zig b/lib/std/segmented_list.zig
index 80e4666b6ac10aa51bebc3ffa82d0a137c243e2c..d7d137175c232203ea2a9ccbcba00d03eed8ed71 100644
--- a/lib/std/segmented_list.zig
+++ b/lib/std/segmented_list.zig
@@ -339,7 +339,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
test "std.SegmentedList" {
- var a = std.heap.page_allocator;
+ var a = std.testing.allocator;
try testSegmentedList(0, a);
try testSegmentedList(1, a);
diff --git a/lib/std/special/test_runner.zig b/lib/std/special/test_runner.zig
index 0335c8562d909daffeeedcf77b17e4b226f4f5a0..07c46ee43c5b2f6a49f5f2e0e6f73c1aa71e512d 100644
--- a/lib/std/special/test_runner.zig
+++ b/lib/std/special/test_runner.zig
@@ -13,6 +13,8 @@ pub fn main() anyerror!void {
};
for (test_fn_list) |test_fn, i| {
+ std.testing.base_allocator_instance.reset();
+
var test_node = root_node.start(test_fn.name, null);
test_node.activate();
progress.refresh();
@@ -22,6 +24,10 @@ pub fn main() anyerror!void {
if (test_fn.func()) |_| {
ok_count += 1;
test_node.end();
+ std.testing.allocator_instance.validate() catch |err| switch (err) {
+ error.Leak => std.debug.panic("", .{}),
+ else => std.debug.panic("error.{}", .{@errorName(err)}),
+ };
if (progress.terminal == null) std.debug.warn("OK\n", .{});
} else |err| switch (err) {
error.SkipZigTest => {
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 7a261e0751d4fbb208cd174cb4a387dbb850d8e3..f8247b5a9de1967db5ac37a803c6850718a0cebb 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -2,6 +2,18 @@ const builtin = @import("builtin");
const TypeId = builtin.TypeId;
const std = @import("std.zig");
+pub const LeakCountAllocator = @import("testing/leak_count_allocator.zig").LeakCountAllocator;
+pub const FailingAllocator = @import("testing/failing_allocator.zig").FailingAllocator;
+
+/// This should only be used in temporary test programs.
+pub const allocator = &allocator_instance.allocator;
+pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.allocator);
+
+pub const failing_allocator = &FailingAllocator.init(&base_allocator_instance.allocator, 0).allocator;
+
+pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
+var allocator_mem: [512 * 1024]u8 = undefined;
+
/// This function is intended to be used only in tests. It prints diagnostics to stderr
/// and then aborts when actual_error_union is not expected_error.
pub fn expectError(expected_error: anyerror, actual_error_union: var) void {
diff --git a/lib/std/testing/failing_allocator.zig b/lib/std/testing/failing_allocator.zig
new file mode 100644
index 0000000000000000000000000000000000000000..081a29cd97576db857804eb553c95e1f2c7136a6
--- /dev/null
+++ b/lib/std/testing/failing_allocator.zig
@@ -0,0 +1,81 @@
+const std = @import("../std.zig");
+const mem = std.mem;
+
+/// Allocator that fails after N allocations, useful for making sure out of
+/// memory conditions are handled correctly.
+///
+/// To use this, first initialize it and get an allocator with
+///
+/// `const failing_allocator = &FailingAllocator.init(,
+/// ).allocator;`
+///
+/// Then use `failing_allocator` anywhere you would have used a
+/// different allocator.
+pub const FailingAllocator = struct {
+ allocator: mem.Allocator,
+ index: usize,
+ fail_index: usize,
+ internal_allocator: *mem.Allocator,
+ allocated_bytes: usize,
+ freed_bytes: usize,
+ allocations: usize,
+ deallocations: usize,
+
+ /// `fail_index` is the number of successful allocations you can
+ /// expect from this allocator. The next allocation will fail.
+ /// For example, if this is called with `fail_index` equal to 2,
+ /// the following test will pass:
+ ///
+ /// var a = try failing_alloc.create(i32);
+ /// var b = try failing_alloc.create(i32);
+ /// testing.expectError(error.OutOfMemory, failing_alloc.create(i32));
+ pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator {
+ return FailingAllocator{
+ .internal_allocator = allocator,
+ .fail_index = fail_index,
+ .index = 0,
+ .allocated_bytes = 0,
+ .freed_bytes = 0,
+ .allocations = 0,
+ .deallocations = 0,
+ .allocator = mem.Allocator{
+ .reallocFn = realloc,
+ .shrinkFn = shrink,
+ },
+ };
+ }
+
+ fn realloc(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
+ const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
+ if (self.index == self.fail_index) {
+ return error.OutOfMemory;
+ }
+ const result = try self.internal_allocator.reallocFn(
+ self.internal_allocator,
+ old_mem,
+ old_align,
+ new_size,
+ new_align,
+ );
+ if (new_size < old_mem.len) {
+ self.freed_bytes += old_mem.len - new_size;
+ if (new_size == 0)
+ self.deallocations += 1;
+ } else if (new_size > old_mem.len) {
+ self.allocated_bytes += new_size - old_mem.len;
+ if (old_mem.len == 0)
+ self.allocations += 1;
+ }
+ self.index += 1;
+ return result;
+ }
+
+ fn shrink(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
+ const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
+ const r = self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align);
+ self.freed_bytes += old_mem.len - r.len;
+ if (new_size == 0)
+ self.deallocations += 1;
+ return r;
+ }
+};
diff --git a/lib/std/testing/leak_count_allocator.zig b/lib/std/testing/leak_count_allocator.zig
new file mode 100644
index 0000000000000000000000000000000000000000..65244e529bcd91510ba4c1979f4d85218a35497c
--- /dev/null
+++ b/lib/std/testing/leak_count_allocator.zig
@@ -0,0 +1,50 @@
+const std = @import("../std.zig");
+
+/// This allocator is used in front of another allocator and counts the numbers of allocs and frees.
+/// The test runner asserts every alloc has a corresponding free at the end of each test.
+///
+/// The detection algorithm is incredibly primitive and only accounts for number of calls.
+/// This should be replaced by the general purpose debug allocator.
+pub const LeakCountAllocator = struct {
+ count: usize,
+ allocator: std.mem.Allocator,
+ internal_allocator: *std.mem.Allocator,
+
+ pub fn init(allocator: *std.mem.Allocator) LeakCountAllocator {
+ return .{
+ .count = 0,
+ .allocator = .{
+ .reallocFn = realloc,
+ .shrinkFn = shrink,
+ },
+ .internal_allocator = allocator,
+ };
+ }
+
+ fn realloc(allocator: *std.mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
+ const self = @fieldParentPtr(LeakCountAllocator, "allocator", allocator);
+ var data = try self.internal_allocator.reallocFn(self.internal_allocator, old_mem, old_align, new_size, new_align);
+ if (old_mem.len == 0) {
+ self.count += 1;
+ }
+ return data;
+ }
+
+ fn shrink(allocator: *std.mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
+ const self = @fieldParentPtr(LeakCountAllocator, "allocator", allocator);
+ if (new_size == 0) {
+ if (self.count == 0) {
+ std.debug.panic("error - too many calls to free, most likely double free", .{});
+ }
+ self.count -= 1;
+ }
+ return self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align);
+ }
+
+ pub fn validate(self: LeakCountAllocator) !void {
+ if (self.count > 0) {
+ std.debug.warn("error - detected leaked allocations without matching free: {}\n", .{self.count});
+ return error.Leak;
+ }
+ }
+};
diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig
index 3d16748447a53b2164d8c512a9c56f4acdd0db75..77783c3eddac3f55889c164cd8189d5c79a512ae 100644
--- a/lib/std/unicode.zig
+++ b/lib/std/unicode.zig
@@ -501,14 +501,16 @@ test "utf16leToUtf8" {
{
mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 'A');
mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 'a');
- const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);
+ const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
+ defer std.testing.allocator.free(utf8);
testing.expect(mem.eql(u8, utf8, "Aa"));
}
{
mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0x80);
mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xffff);
- const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);
+ const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
+ defer std.testing.allocator.free(utf8);
testing.expect(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf"));
}
@@ -516,7 +518,8 @@ test "utf16leToUtf8" {
// the values just outside the surrogate half range
mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd7ff);
mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xe000);
- const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);
+ const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
+ defer std.testing.allocator.free(utf8);
testing.expect(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80"));
}
@@ -524,7 +527,8 @@ test "utf16leToUtf8" {
// smallest surrogate pair
mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd800);
mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);
- const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);
+ const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
+ defer std.testing.allocator.free(utf8);
testing.expect(mem.eql(u8, utf8, "\xf0\x90\x80\x80"));
}
@@ -532,14 +536,16 @@ test "utf16leToUtf8" {
// largest surrogate pair
mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);
mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdfff);
- const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);
+ const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
+ defer std.testing.allocator.free(utf8);
testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf"));
}
{
mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);
mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);
- const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);
+ const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
+ defer std.testing.allocator.free(utf8);
testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80"));
}
}
diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig
index dc9a6bc7ca39ed9017c0edb2ff5c14bfbe5ed952..e339343823b75fefe0d886a55f2da1e0460078de 100644
--- a/lib/std/zig/ast.zig
+++ b/lib/std/zig/ast.zig
@@ -2287,7 +2287,7 @@ pub const Node = struct {
test "iterate" {
var root = Node.Root{
.base = Node{ .id = Node.Id.Root },
- .decls = Node.Root.DeclList.init(std.debug.global_allocator),
+ .decls = Node.Root.DeclList.init(std.testing.allocator),
.eof_token = 0,
};
var base = &root.base;
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index c57540ade97b9599aa7542a07e9005e12cbba36d..1640d09992c19f1c2579bad6d7e7b16ed11dd443 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -2773,7 +2773,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
const needed_alloc_count = x: {
// Try it once with unlimited memory, make sure it works
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
- var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize));
+ var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize));
var anything_changed: bool = undefined;
const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed);
if (!mem.eql(u8, result_source, expected_source)) {
@@ -2797,7 +2797,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
var fail_index: usize = 0;
while (fail_index < needed_alloc_count) : (fail_index += 1) {
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
- var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index);
+ var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, fail_index);
var anything_changed: bool = undefined;
if (testParse(source, &failing_allocator.allocator, &anything_changed)) |_| {
return error.NondeterministicMemoryUsage;
diff --git a/src-self-hosted/c_tokenizer.zig b/src-self-hosted/c_tokenizer.zig
index 7cff969c355ce2900c7de576d5e5e24340916849..7e085bcf7854b6db3ebd6e705758c9fc531262ed 100644
--- a/src-self-hosted/c_tokenizer.zig
+++ b/src-self-hosted/c_tokenizer.zig
@@ -866,7 +866,7 @@ fn expectTokens(tl: *TokenList, src: [*:0]const u8, expected: []CToken) void {
}
test "tokenize macro" {
- var tl = TokenList.init(std.heap.page_allocator);
+ var tl = TokenList.init(std.testing.allocator);
defer tl.deinit();
expectTokens(&tl, "TEST(0\n", &[_]CToken{
@@ -904,7 +904,7 @@ test "tokenize macro" {
}
test "tokenize macro ops" {
- var tl = TokenList.init(std.heap.page_allocator);
+ var tl = TokenList.init(std.testing.allocator);
defer tl.deinit();
expectTokens(&tl, "ADD A + B", &[_]CToken{
diff --git a/test/cli.zig b/test/cli.zig
index 0527b5c923196d9cd07cd55ad6cebec264f1a5c9..b7d03d9e21f4b76bd3c8ab1c313554c1b6e3b611 100644
--- a/test/cli.zig
+++ b/test/cli.zig
@@ -8,7 +8,7 @@ const ChildProcess = std.ChildProcess;
var a: *std.mem.Allocator = undefined;
pub fn main() !void {
- var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
var arg_it = process.args();
diff --git a/test/compare_output.zig b/test/compare_output.zig
index db20709afa21663a0ec773b5b1e8157827199678..7a41d46f5423c926e80d0053737ecd22553b16b0 100644
--- a/test/compare_output.zig
+++ b/test/compare_output.zig
@@ -445,7 +445,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\const std = @import("std");
\\const io = std.io;
\\const os = std.os;
- \\const allocator = std.debug.global_allocator;
+ \\const allocator = std.testing.allocator;
\\
\\pub fn main() !void {
\\ var args_it = std.process.args();
@@ -486,7 +486,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\const std = @import("std");
\\const io = std.io;
\\const os = std.os;
- \\const allocator = std.debug.global_allocator;
+ \\const allocator = std.testing.allocator;
\\
\\pub fn main() !void {
\\ var args_it = std.process.args();
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 89e187aed2f37b284c2088b68b616eb18c3ff5d0..341062e161f5d26dfbc04c76e3e4bf43caef0c0d 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -5765,7 +5765,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\
\\export fn entry() void {
\\ const a = MdNode.Header {
- \\ .text = MdText.init(&std.debug.global_allocator),
+ \\ .text = MdText.init(&std.testing.allocator),
\\ .weight = HeaderWeight.H1,
\\ };
\\}
diff --git a/test/stage1/behavior/async_fn.zig b/test/stage1/behavior/async_fn.zig
index 87e7fca383d34dad710fe2badaef3b6e94c9a1cd..e0d1ec286697084bb363dff7d62804c90480e080 100644
--- a/test/stage1/behavior/async_fn.zig
+++ b/test/stage1/behavior/async_fn.zig
@@ -410,8 +410,8 @@ test "heap allocated async function frame" {
var x: i32 = 42;
fn doTheTest() !void {
- const frame = try std.heap.page_allocator.create(@Frame(someFunc));
- defer std.heap.page_allocator.destroy(frame);
+ const frame = try std.testing.allocator.create(@Frame(someFunc));
+ defer std.testing.allocator.destroy(frame);
expect(x == 42);
frame.* = async someFunc();
@@ -671,7 +671,7 @@ fn testAsyncAwaitTypicalUsage(
}
fn amain() !void {
- const allocator = std.heap.page_allocator; // TODO once we have the debug allocator, use that, so that this can detect leaks
+ const allocator = std.testing.allocator;
var download_frame = async fetchUrl(allocator, "https://example.com/");
var download_awaited = false;
errdefer if (!download_awaited) {
@@ -935,12 +935,12 @@ fn recursiveAsyncFunctionTest(comptime suspending_implementation: bool) type {
_ = async amain(&result);
return result;
} else {
- return fib(std.heap.page_allocator, 10) catch unreachable;
+ return fib(std.testing.allocator, 10) catch unreachable;
}
}
fn amain(result: *u32) void {
- var x = async fib(std.heap.page_allocator, 10);
+ var x = async fib(std.testing.allocator, 10);
result.* = (await x) catch unreachable;
}
};
diff --git a/test/stage1/behavior/const_slice_child.zig b/test/stage1/behavior/const_slice_child.zig
index 6720667187c5434de124fa0e0e1ebc96c2393140..92e5121026d84778866db9667ce4f5f24fd555b4 100644
--- a/test/stage1/behavior/const_slice_child.zig
+++ b/test/stage1/behavior/const_slice_child.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const debug = std.debug;
-const expect = std.testing.expect;
+const testing = std.testing;
+const expect = testing.expect;
var argv: [*]const [*]const u8 = undefined;
@@ -22,7 +23,8 @@ fn foo(args: [][]const u8) void {
}
fn bar(argc: usize) void {
- const args = debug.global_allocator.alloc([]const u8, argc) catch unreachable;
+ const args = testing.allocator.alloc([]const u8, argc) catch unreachable;
+ defer testing.allocator.free(args);
for (args) |_, i| {
const ptr = argv[i];
args[i] = ptr[0..strlen(ptr)];
diff --git a/test/standalone/brace_expansion/main.zig b/test/standalone/brace_expansion/main.zig
index cbb328541e023acf201f05d5936d4f573d6306ac..e1d2ee66738156224be5b5a3434714b2fae52a7e 100644
--- a/test/standalone/brace_expansion/main.zig
+++ b/test/standalone/brace_expansion/main.zig
@@ -201,7 +201,10 @@ pub fn main() !void {
}
test "invalid inputs" {
- global_allocator = std.debug.global_allocator;
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+
+ global_allocator = &arena.allocator;
expectError("}ABC", error.InvalidInput);
expectError("{ABC", error.InvalidInput);
@@ -222,7 +225,10 @@ fn expectError(test_input: []const u8, expected_err: anyerror) void {
}
test "valid inputs" {
- global_allocator = std.debug.global_allocator;
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+
+ global_allocator = &arena.allocator;
expectExpansion("{x,y,z}", "x y z");
expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");
diff --git a/test/standalone/cat/main.zig b/test/standalone/cat/main.zig
index f0cd9728ab4c0862fa4617a4e05650d313c199f6..34439f9c242d86301d10d72559fa682fc69ddf6c 100644
--- a/test/standalone/cat/main.zig
+++ b/test/standalone/cat/main.zig
@@ -4,7 +4,7 @@ const process = std.process;
const fs = std.fs;
const mem = std.mem;
const warn = std.debug.warn;
-const allocator = std.debug.global_allocator;
+const allocator = std.testing.allocator;
pub fn main() !void {
var args_it = process.args();
diff --git a/test/standalone/empty_env/main.zig b/test/standalone/empty_env/main.zig
index d6d5ecb5af7b21a46fea44ca66e350a057ec4aaf..f4ebf56136bb256b45eb8894bbe9bad41e25c6c5 100644
--- a/test/standalone/empty_env/main.zig
+++ b/test/standalone/empty_env/main.zig
@@ -1,6 +1,6 @@
const std = @import("std");
pub fn main() void {
- const env_map = std.process.getEnvMap(std.debug.global_allocator) catch @panic("unable to get env map");
+ const env_map = std.process.getEnvMap(std.testing.allocator) catch @panic("unable to get env map");
std.testing.expect(env_map.count() == 0);
}
diff --git a/test/standalone/load_dynamic_library/main.zig b/test/standalone/load_dynamic_library/main.zig
index 197e3ca47c968b2be08bddede5092cb5329b6f52..70fc4986ac3e6ef715d39bd84c7efed28c41baee 100644
--- a/test/standalone/load_dynamic_library/main.zig
+++ b/test/standalone/load_dynamic_library/main.zig
@@ -1,8 +1,8 @@
const std = @import("std");
pub fn main() !void {
- const args = try std.process.argsAlloc(std.debug.global_allocator);
- defer std.process.argsFree(std.debug.global_allocator, args);
+ const args = try std.process.argsAlloc(std.testing.allocator);
+ defer std.process.argsFree(std.testing.allocator, args);
const dynlib_name = args[1];