authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-01-29 12:21:29-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-01-29 12:21:29-06:00
log4d134a01f5a8baae346783f19d9b5db8c8256d32
treeaa75eac95360a47502c3a278329d2d775d53d571
parent34706dad3ff22f73197fbf5e9cbf4628f698a74d

Move debug.global_allocator to testing.allocator


23 files changed, 84 insertions(+), 73 deletions(-)

doc/docgen.zig+1-1
...@@ -672,7 +672,7 @@ const TermState = enum {...@@ -672,7 +672,7 @@ const TermState = enum {
672672
673test "term color" {673test "term color" {
674 const input_bytes = "A\x1b[32;1mgreen\x1b[0mB";674 const input_bytes = "A\x1b[32;1mgreen\x1b[0mB";
675 const result = try termColor(std.debug.global_allocator, input_bytes);675 const result = try termColor(std.testing.allocator, input_bytes);
676 testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result);676 testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result);
677}677}
678678
lib/std/array_list.zig+7-7
...@@ -320,7 +320,7 @@ test "std.ArrayList.basic" {...@@ -320,7 +320,7 @@ test "std.ArrayList.basic" {
320}320}
321321
322test "std.ArrayList.orderedRemove" {322test "std.ArrayList.orderedRemove" {
323 var list = ArrayList(i32).init(debug.global_allocator);323 var list = ArrayList(i32).init(testing.allocator);
324 defer list.deinit();324 defer list.deinit();
325325
326 try list.append(1);326 try list.append(1);
...@@ -347,7 +347,7 @@ test "std.ArrayList.orderedRemove" {...@@ -347,7 +347,7 @@ test "std.ArrayList.orderedRemove" {
347}347}
348348
349test "std.ArrayList.swapRemove" {349test "std.ArrayList.swapRemove" {
350 var list = ArrayList(i32).init(debug.global_allocator);350 var list = ArrayList(i32).init(testing.allocator);
351 defer list.deinit();351 defer list.deinit();
352352
353 try list.append(1);353 try list.append(1);
...@@ -374,7 +374,7 @@ test "std.ArrayList.swapRemove" {...@@ -374,7 +374,7 @@ test "std.ArrayList.swapRemove" {
374}374}
375375
376test "std.ArrayList.swapRemoveOrError" {376test "std.ArrayList.swapRemoveOrError" {
377 var list = ArrayList(i32).init(debug.global_allocator);377 var list = ArrayList(i32).init(testing.allocator);
378 defer list.deinit();378 defer list.deinit();
379379
380 // Test just after initialization380 // Test just after initialization
...@@ -402,7 +402,7 @@ test "std.ArrayList.swapRemoveOrError" {...@@ -402,7 +402,7 @@ test "std.ArrayList.swapRemoveOrError" {
402}402}
403403
404test "std.ArrayList.insert" {404test "std.ArrayList.insert" {
405 var list = ArrayList(i32).init(debug.global_allocator);405 var list = ArrayList(i32).init(testing.allocator);
406 defer list.deinit();406 defer list.deinit();
407407
408 try list.append(1);408 try list.append(1);
...@@ -416,7 +416,7 @@ test "std.ArrayList.insert" {...@@ -416,7 +416,7 @@ test "std.ArrayList.insert" {
416}416}
417417
418test "std.ArrayList.insertSlice" {418test "std.ArrayList.insertSlice" {
419 var list = ArrayList(i32).init(debug.global_allocator);419 var list = ArrayList(i32).init(testing.allocator);
420 defer list.deinit();420 defer list.deinit();
421421
422 try list.append(1);422 try list.append(1);
...@@ -443,7 +443,7 @@ const Item = struct {...@@ -443,7 +443,7 @@ const Item = struct {
443};443};
444444
445test "std.ArrayList: ArrayList(T) of struct T" {445test "std.ArrayList: ArrayList(T) of struct T" {
446 var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(debug.global_allocator) };446 var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(testing.allocator) };
447 try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(debug.global_allocator) });447 try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) });
448 testing.expect(root.sub_items.items[0].integer == 42);448 testing.expect(root.sub_items.items[0].integer == 42);
449}449}
lib/std/buffer.zig+3-3
...@@ -150,7 +150,7 @@ pub const Buffer = struct {...@@ -150,7 +150,7 @@ pub const Buffer = struct {
150};150};
151151
152test "simple Buffer" {152test "simple Buffer" {
153 var buf = try Buffer.init(debug.global_allocator, "");153 var buf = try Buffer.init(testing.allocator, "");
154 testing.expect(buf.len() == 0);154 testing.expect(buf.len() == 0);
155 try buf.append("hello");155 try buf.append("hello");
156 try buf.append(" ");156 try buf.append(" ");
...@@ -169,14 +169,14 @@ test "simple Buffer" {...@@ -169,14 +169,14 @@ test "simple Buffer" {
169}169}
170170
171test "Buffer.initSize" {171test "Buffer.initSize" {
172 var buf = try Buffer.initSize(debug.global_allocator, 3);172 var buf = try Buffer.initSize(testing.allocator, 3);
173 testing.expect(buf.len() == 3);173 testing.expect(buf.len() == 3);
174 try buf.append("hello");174 try buf.append("hello");
175 testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello"));175 testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello"));
176}176}
177177
178test "Buffer.initCapacity" {178test "Buffer.initCapacity" {
179 var buf = try Buffer.initCapacity(debug.global_allocator, 10);179 var buf = try Buffer.initCapacity(testing.allocator, 10);
180 testing.expect(buf.len() == 0);180 testing.expect(buf.len() == 0);
181 testing.expect(buf.capacity() >= 10);181 testing.expect(buf.capacity() >= 10);
182 const old_cap = buf.capacity();182 const old_cap = buf.capacity();
lib/std/debug.zig+4-3
...@@ -20,7 +20,7 @@ const windows = std.os.windows;...@@ -20,7 +20,7 @@ const windows = std.os.windows;
20pub const leb = @import("debug/leb128.zig");20pub const leb = @import("debug/leb128.zig");
2121
22pub const FailingAllocator = @import("debug/failing_allocator.zig").FailingAllocator;22pub const FailingAllocator = @import("debug/failing_allocator.zig").FailingAllocator;
23pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator;23pub const failing_allocator = &FailingAllocator.init(&global_fixed_allocator.allocator, 0).allocator;
2424
25pub const runtime_safety = switch (builtin.mode) {25pub const runtime_safety = switch (builtin.mode) {
26 .Debug, .ReleaseSafe => true,26 .Debug, .ReleaseSafe => true,
...@@ -2192,8 +2192,9 @@ fn readInitialLength(comptime E: type, in_stream: *io.InStream(E), is_64: *bool)...@@ -2192,8 +2192,9 @@ fn readInitialLength(comptime E: type, in_stream: *io.InStream(E), is_64: *bool)
2192 }2192 }
2193}2193}
21942194
2195/// This should only be used in temporary test programs.2195pub const global_allocator = blk: {
2196pub const global_allocator = &global_fixed_allocator.allocator;2196 @compileError("Please switch to std.testing.allocator.");
2197};
2197var global_fixed_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(global_allocator_mem[0..]);2198var global_fixed_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(global_allocator_mem[0..]);
2198var global_allocator_mem: [100 * 1024]u8 = undefined;2199var global_allocator_mem: [100 * 1024]u8 = undefined;
21992200
lib/std/fifo.zig+2-2
...@@ -347,7 +347,7 @@ pub fn LinearFifo(...@@ -347,7 +347,7 @@ pub fn LinearFifo(
347}347}
348348
349test "LinearFifo(u8, .Dynamic)" {349test "LinearFifo(u8, .Dynamic)" {
350 var fifo = LinearFifo(u8, .Dynamic).init(debug.global_allocator);350 var fifo = LinearFifo(u8, .Dynamic).init(testing.allocator);
351 defer fifo.deinit();351 defer fifo.deinit();
352352
353 try fifo.write("HELLO");353 try fifo.write("HELLO");
...@@ -422,7 +422,7 @@ test "LinearFifo" {...@@ -422,7 +422,7 @@ test "LinearFifo" {
422 var fifo = switch (bt) {422 var fifo = switch (bt) {
423 .Static => FifoType.init(),423 .Static => FifoType.init(),
424 .Slice => FifoType.init(buf[0..]),424 .Slice => FifoType.init(buf[0..]),
425 .Dynamic => FifoType.init(debug.global_allocator),425 .Dynamic => FifoType.init(testing.allocator),
426 };426 };
427 defer fifo.deinit();427 defer fifo.deinit();
428428
lib/std/fmt.zig+5-5
...@@ -1598,7 +1598,7 @@ test "hexToBytes" {...@@ -1598,7 +1598,7 @@ test "hexToBytes" {
1598test "formatIntValue with comptime_int" {1598test "formatIntValue with comptime_int" {
1599 const value: comptime_int = 123456789123456789;1599 const value: comptime_int = 123456789123456789;
16001600
1601 var buf = try std.Buffer.init(std.debug.global_allocator, "");1601 var buf = try std.Buffer.init(std.testing.allocator, "");
1602 try formatIntValue(value, "", FormatOptions{}, &buf, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append);1602 try formatIntValue(value, "", FormatOptions{}, &buf, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append);
1603 std.testing.expect(mem.eql(u8, buf.toSlice(), "123456789123456789"));1603 std.testing.expect(mem.eql(u8, buf.toSlice(), "123456789123456789"));
1604}1604}
...@@ -1652,19 +1652,19 @@ test "formatType max_depth" {...@@ -1652,19 +1652,19 @@ test "formatType max_depth" {
1652 inst.a = &inst;1652 inst.a = &inst;
1653 inst.tu.ptr = &inst.tu;1653 inst.tu.ptr = &inst.tu;
16541654
1655 var buf0 = try std.Buffer.init(std.debug.global_allocator, "");1655 var buf0 = try std.Buffer.init(std.testing.allocator, "");
1656 try formatType(inst, "", FormatOptions{}, &buf0, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 0);1656 try formatType(inst, "", FormatOptions{}, &buf0, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 0);
1657 std.testing.expect(mem.eql(u8, buf0.toSlice(), "S{ ... }"));1657 std.testing.expect(mem.eql(u8, buf0.toSlice(), "S{ ... }"));
16581658
1659 var buf1 = try std.Buffer.init(std.debug.global_allocator, "");1659 var buf1 = try std.Buffer.init(std.testing.allocator, "");
1660 try formatType(inst, "", FormatOptions{}, &buf1, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 1);1660 try formatType(inst, "", FormatOptions{}, &buf1, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 1);
1661 std.testing.expect(mem.eql(u8, buf1.toSlice(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }"));1661 std.testing.expect(mem.eql(u8, buf1.toSlice(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }"));
16621662
1663 var buf2 = try std.Buffer.init(std.debug.global_allocator, "");1663 var buf2 = try std.Buffer.init(std.testing.allocator, "");
1664 try formatType(inst, "", FormatOptions{}, &buf2, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 2);1664 try formatType(inst, "", FormatOptions{}, &buf2, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 2);
1665 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) }"));1665 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) }"));
16661666
1667 var buf3 = try std.Buffer.init(std.debug.global_allocator, "");1667 var buf3 = try std.Buffer.init(std.testing.allocator, "");
1668 try formatType(inst, "", FormatOptions{}, &buf3, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 3);1668 try formatType(inst, "", FormatOptions{}, &buf3, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 3);
1669 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) }"));1669 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) }"));
1670}1670}
lib/std/fs/path.zig+8-8
...@@ -665,7 +665,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -665,7 +665,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
665}665}
666666
667test "resolve" {667test "resolve" {
668 const cwd = try process.getCwdAlloc(debug.global_allocator);668 const cwd = try process.getCwdAlloc(testing.allocator);
669 if (builtin.os == .windows) {669 if (builtin.os == .windows) {
670 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {670 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
671 cwd[0] = asciiUpper(cwd[0]);671 cwd[0] = asciiUpper(cwd[0]);
...@@ -683,11 +683,11 @@ test "resolveWindows" {...@@ -683,11 +683,11 @@ test "resolveWindows" {
683 return error.SkipZigTest;683 return error.SkipZigTest;
684 }684 }
685 if (builtin.os == .windows) {685 if (builtin.os == .windows) {
686 const cwd = try process.getCwdAlloc(debug.global_allocator);686 const cwd = try process.getCwdAlloc(testing.allocator);
687 const parsed_cwd = windowsParsePath(cwd);687 const parsed_cwd = windowsParsePath(cwd);
688 {688 {
689 const result = testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" });689 const result = testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" });
690 const expected = try join(debug.global_allocator, &[_][]const u8{690 const expected = try join(testing.allocator, &[_][]const u8{
691 parsed_cwd.disk_designator,691 parsed_cwd.disk_designator,
692 "usr\\local\\lib\\zig\\std\\array_list.zig",692 "usr\\local\\lib\\zig\\std\\array_list.zig",
693 });693 });
...@@ -698,7 +698,7 @@ test "resolveWindows" {...@@ -698,7 +698,7 @@ test "resolveWindows" {
698 }698 }
699 {699 {
700 const result = testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" });700 const result = testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" });
701 const expected = try join(debug.global_allocator, &[_][]const u8{701 const expected = try join(testing.allocator, &[_][]const u8{
702 cwd,702 cwd,
703 "usr\\local\\lib\\zig",703 "usr\\local\\lib\\zig",
704 });704 });
...@@ -738,11 +738,11 @@ test "resolvePosix" {...@@ -738,11 +738,11 @@ test "resolvePosix" {
738}738}
739739
740fn testResolveWindows(paths: []const []const u8) []u8 {740fn testResolveWindows(paths: []const []const u8) []u8 {
741 return resolveWindows(debug.global_allocator, paths) catch unreachable;741 return resolveWindows(testing.allocator, paths) catch unreachable;
742}742}
743743
744fn testResolvePosix(paths: []const []const u8) []u8 {744fn testResolvePosix(paths: []const []const u8) []u8 {
745 return resolvePosix(debug.global_allocator, paths) catch unreachable;745 return resolvePosix(testing.allocator, paths) catch unreachable;
746}746}
747747
748/// If the path is a file in the current directory (no directory component)748/// If the path is a file in the current directory (no directory component)
...@@ -1166,11 +1166,11 @@ test "relative" {...@@ -1166,11 +1166,11 @@ test "relative" {
1166}1166}
11671167
1168fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) void {1168fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) void {
1169 const result = relativePosix(debug.global_allocator, from, to) catch unreachable;1169 const result = relativePosix(testing.allocator, from, to) catch unreachable;
1170 testing.expectEqualSlices(u8, expected_output, result);1170 testing.expectEqualSlices(u8, expected_output, result);
1171}1171}
11721172
1173fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) void {1173fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) void {
1174 const result = relativeWindows(debug.global_allocator, from, to) catch unreachable;1174 const result = relativeWindows(testing.allocator, from, to) catch unreachable;
1175 testing.expectEqualSlices(u8, expected_output, result);1175 testing.expectEqualSlices(u8, expected_output, result);
1176}1176}
lib/std/heap.zig+4
...@@ -711,6 +711,10 @@ pub const ThreadSafeFixedBufferAllocator = blk: {...@@ -711,6 +711,10 @@ pub const ThreadSafeFixedBufferAllocator = blk: {
711 fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {711 fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
712 return old_mem[0..new_size];712 return old_mem[0..new_size];
713 }713 }
714
715 pub fn reset(self: *ThreadSafeFixedBufferAllocator) void {
716 self.end_index = 0;
717 }
714 };718 };
715 }719 }
716};720};
lib/std/linked_list.zig+3-3
...@@ -143,7 +143,7 @@ pub fn SinglyLinkedList(comptime T: type) type {...@@ -143,7 +143,7 @@ pub fn SinglyLinkedList(comptime T: type) type {
143}143}
144144
145test "basic SinglyLinkedList test" {145test "basic SinglyLinkedList test" {
146 const allocator = debug.global_allocator;146 const allocator = testing.allocator;
147 var list = SinglyLinkedList(u32).init();147 var list = SinglyLinkedList(u32).init();
148148
149 var one = try list.createNode(1, allocator);149 var one = try list.createNode(1, allocator);
...@@ -404,7 +404,7 @@ pub fn TailQueue(comptime T: type) type {...@@ -404,7 +404,7 @@ pub fn TailQueue(comptime T: type) type {
404}404}
405405
406test "basic TailQueue test" {406test "basic TailQueue test" {
407 const allocator = debug.global_allocator;407 const allocator = testing.allocator;
408 var list = TailQueue(u32).init();408 var list = TailQueue(u32).init();
409409
410 var one = try list.createNode(1, allocator);410 var one = try list.createNode(1, allocator);
...@@ -456,7 +456,7 @@ test "basic TailQueue test" {...@@ -456,7 +456,7 @@ test "basic TailQueue test" {
456}456}
457457
458test "TailQueue concatenation" {458test "TailQueue concatenation" {
459 const allocator = debug.global_allocator;459 const allocator = testing.allocator;
460 var list1 = TailQueue(u32).init();460 var list1 = TailQueue(u32).init();
461 var list2 = TailQueue(u32).init();461 var list2 = TailQueue(u32).init();
462462
lib/std/os/test.zig+1-1
...@@ -9,7 +9,7 @@ const elf = std.elf;...@@ -9,7 +9,7 @@ const elf = std.elf;
9const File = std.fs.File;9const File = std.fs.File;
10const Thread = std.Thread;10const Thread = std.Thread;
1111
12const a = std.debug.global_allocator;12const a = std.testing.allocator;
1313
14const builtin = @import("builtin");14const builtin = @import("builtin");
15const AtomicRmwOp = builtin.AtomicRmwOp;15const AtomicRmwOp = builtin.AtomicRmwOp;
lib/std/priority_queue.zig+19-19
...@@ -1,10 +1,10 @@...@@ -1,10 +1,10 @@
1const std = @import("std.zig");1const std = @import("std.zig");
2const Allocator = std.mem.Allocator;2const Allocator = std.mem.Allocator;
3const debug = std.debug;3const assert = std.debug.assert;
4const assert = debug.assert;4const testing = std.testing;
5const expect = std.testing.expect;5const expect = testing.expect;
6const expectEqual = std.testing.expectEqual;6const expectEqual = testing.expectEqual;
7const expectError = std.testing.expectError;7const expectError = testing.expectError;
88
9/// Priority queue for storing generic data. Initialize with `init`.9/// Priority queue for storing generic data. Initialize with `init`.
10pub fn PriorityQueue(comptime T: type) type {10pub fn PriorityQueue(comptime T: type) type {
...@@ -239,7 +239,7 @@ fn greaterThan(a: u32, b: u32) bool {...@@ -239,7 +239,7 @@ fn greaterThan(a: u32, b: u32) bool {
239const PQ = PriorityQueue(u32);239const PQ = PriorityQueue(u32);
240240
241test "std.PriorityQueue: add and remove min heap" {241test "std.PriorityQueue: add and remove min heap" {
242 var queue = PQ.init(debug.global_allocator, lessThan);242 var queue = PQ.init(testing.allocator, lessThan);
243 defer queue.deinit();243 defer queue.deinit();
244244
245 try queue.add(54);245 try queue.add(54);
...@@ -257,7 +257,7 @@ test "std.PriorityQueue: add and remove min heap" {...@@ -257,7 +257,7 @@ test "std.PriorityQueue: add and remove min heap" {
257}257}
258258
259test "std.PriorityQueue: add and remove same min heap" {259test "std.PriorityQueue: add and remove same min heap" {
260 var queue = PQ.init(debug.global_allocator, lessThan);260 var queue = PQ.init(testing.allocator, lessThan);
261 defer queue.deinit();261 defer queue.deinit();
262262
263 try queue.add(1);263 try queue.add(1);
...@@ -275,14 +275,14 @@ test "std.PriorityQueue: add and remove same min heap" {...@@ -275,14 +275,14 @@ test "std.PriorityQueue: add and remove same min heap" {
275}275}
276276
277test "std.PriorityQueue: removeOrNull on empty" {277test "std.PriorityQueue: removeOrNull on empty" {
278 var queue = PQ.init(debug.global_allocator, lessThan);278 var queue = PQ.init(testing.allocator, lessThan);
279 defer queue.deinit();279 defer queue.deinit();
280280
281 expect(queue.removeOrNull() == null);281 expect(queue.removeOrNull() == null);
282}282}
283283
284test "std.PriorityQueue: edge case 3 elements" {284test "std.PriorityQueue: edge case 3 elements" {
285 var queue = PQ.init(debug.global_allocator, lessThan);285 var queue = PQ.init(testing.allocator, lessThan);
286 defer queue.deinit();286 defer queue.deinit();
287287
288 try queue.add(9);288 try queue.add(9);
...@@ -294,7 +294,7 @@ test "std.PriorityQueue: edge case 3 elements" {...@@ -294,7 +294,7 @@ test "std.PriorityQueue: edge case 3 elements" {
294}294}
295295
296test "std.PriorityQueue: peek" {296test "std.PriorityQueue: peek" {
297 var queue = PQ.init(debug.global_allocator, lessThan);297 var queue = PQ.init(testing.allocator, lessThan);
298 defer queue.deinit();298 defer queue.deinit();
299299
300 expect(queue.peek() == null);300 expect(queue.peek() == null);
...@@ -306,7 +306,7 @@ test "std.PriorityQueue: peek" {...@@ -306,7 +306,7 @@ test "std.PriorityQueue: peek" {
306}306}
307307
308test "std.PriorityQueue: sift up with odd indices" {308test "std.PriorityQueue: sift up with odd indices" {
309 var queue = PQ.init(debug.global_allocator, lessThan);309 var queue = PQ.init(testing.allocator, lessThan);
310 defer queue.deinit();310 defer queue.deinit();
311 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };311 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
312 for (items) |e| {312 for (items) |e| {
...@@ -320,7 +320,7 @@ test "std.PriorityQueue: sift up with odd indices" {...@@ -320,7 +320,7 @@ test "std.PriorityQueue: sift up with odd indices" {
320}320}
321321
322test "std.PriorityQueue: addSlice" {322test "std.PriorityQueue: addSlice" {
323 var queue = PQ.init(debug.global_allocator, lessThan);323 var queue = PQ.init(testing.allocator, lessThan);
324 defer queue.deinit();324 defer queue.deinit();
325 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };325 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
326 try queue.addSlice(items[0..]);326 try queue.addSlice(items[0..]);
...@@ -333,8 +333,8 @@ test "std.PriorityQueue: addSlice" {...@@ -333,8 +333,8 @@ test "std.PriorityQueue: addSlice" {
333333
334test "std.PriorityQueue: fromOwnedSlice" {334test "std.PriorityQueue: fromOwnedSlice" {
335 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };335 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
336 const heap_items = try std.mem.dupe(debug.global_allocator, u32, items[0..]);336 const heap_items = try std.mem.dupe(testing.allocator, u32, items[0..]);
337 var queue = PQ.fromOwnedSlice(debug.global_allocator, lessThan, heap_items[0..]);337 var queue = PQ.fromOwnedSlice(testing.allocator, lessThan, heap_items[0..]);
338 defer queue.deinit();338 defer queue.deinit();
339339
340 const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 };340 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" {...@@ -344,7 +344,7 @@ test "std.PriorityQueue: fromOwnedSlice" {
344}344}
345345
346test "std.PriorityQueue: add and remove max heap" {346test "std.PriorityQueue: add and remove max heap" {
347 var queue = PQ.init(debug.global_allocator, greaterThan);347 var queue = PQ.init(testing.allocator, greaterThan);
348 defer queue.deinit();348 defer queue.deinit();
349349
350 try queue.add(54);350 try queue.add(54);
...@@ -362,7 +362,7 @@ test "std.PriorityQueue: add and remove max heap" {...@@ -362,7 +362,7 @@ test "std.PriorityQueue: add and remove max heap" {
362}362}
363363
364test "std.PriorityQueue: add and remove same max heap" {364test "std.PriorityQueue: add and remove same max heap" {
365 var queue = PQ.init(debug.global_allocator, greaterThan);365 var queue = PQ.init(testing.allocator, greaterThan);
366 defer queue.deinit();366 defer queue.deinit();
367367
368 try queue.add(1);368 try queue.add(1);
...@@ -380,8 +380,8 @@ test "std.PriorityQueue: add and remove same max heap" {...@@ -380,8 +380,8 @@ test "std.PriorityQueue: add and remove same max heap" {
380}380}
381381
382test "std.PriorityQueue: iterator" {382test "std.PriorityQueue: iterator" {
383 var queue = PQ.init(debug.global_allocator, lessThan);383 var queue = PQ.init(testing.allocator, lessThan);
384 var map = std.AutoHashMap(u32, void).init(debug.global_allocator);384 var map = std.AutoHashMap(u32, void).init(testing.allocator);
385 defer {385 defer {
386 queue.deinit();386 queue.deinit();
387 map.deinit();387 map.deinit();
...@@ -402,7 +402,7 @@ test "std.PriorityQueue: iterator" {...@@ -402,7 +402,7 @@ test "std.PriorityQueue: iterator" {
402}402}
403403
404test "std.PriorityQueue: remove at index" {404test "std.PriorityQueue: remove at index" {
405 var queue = PQ.init(debug.global_allocator, lessThan);405 var queue = PQ.init(testing.allocator, lessThan);
406 defer queue.deinit();406 defer queue.deinit();
407407
408 try queue.add(3);408 try queue.add(3);
lib/std/process.zig+4-4
...@@ -114,7 +114,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {...@@ -114,7 +114,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
114}114}
115115
116test "os.getEnvMap" {116test "os.getEnvMap" {
117 var env = try getEnvMap(std.debug.global_allocator);117 var env = try getEnvMap(std.testing.allocator);
118 defer env.deinit();118 defer env.deinit();
119}119}
120120
...@@ -165,7 +165,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned...@@ -165,7 +165,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned
165}165}
166166
167test "os.getEnvVarOwned" {167test "os.getEnvVarOwned" {
168 var ga = std.debug.global_allocator;168 var ga = std.testing.allocator;
169 testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV"));169 testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV"));
170}170}
171171
...@@ -492,10 +492,10 @@ test "windows arg parsing" {...@@ -492,10 +492,10 @@ test "windows arg parsing" {
492fn testWindowsCmdLine(input_cmd_line: [*]const u8, expected_args: []const []const u8) void {492fn testWindowsCmdLine(input_cmd_line: [*]const u8, expected_args: []const []const u8) void {
493 var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);493 var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);
494 for (expected_args) |expected_arg| {494 for (expected_args) |expected_arg| {
495 const arg = it.next(std.debug.global_allocator).? catch unreachable;495 const arg = it.next(std.testing.allocator).? catch unreachable;
496 testing.expectEqualSlices(u8, expected_arg, arg);496 testing.expectEqualSlices(u8, expected_arg, arg);
497 }497 }
498 testing.expect(it.next(std.debug.global_allocator) == null);498 testing.expect(it.next(std.testing.allocator) == null);
499}499}
500500
501pub const UserInfo = struct {501pub const UserInfo = struct {
lib/std/special/test_runner.zig+1
...@@ -13,6 +13,7 @@ pub fn main() anyerror!void {...@@ -13,6 +13,7 @@ pub fn main() anyerror!void {
13 };13 };
1414
15 for (test_fn_list) |test_fn, i| {15 for (test_fn_list) |test_fn, i| {
16 std.testing.allocator_instance.reset();
16 var test_node = root_node.start(test_fn.name, null);17 var test_node = root_node.start(test_fn.name, null);
17 test_node.activate();18 test_node.activate();
18 progress.refresh();19 progress.refresh();
lib/std/testing.zig+5
...@@ -2,6 +2,11 @@ const builtin = @import("builtin");...@@ -2,6 +2,11 @@ const builtin = @import("builtin");
2const TypeId = builtin.TypeId;2const TypeId = builtin.TypeId;
3const std = @import("std.zig");3const std = @import("std.zig");
44
5/// This should only be used in temporary test programs.
6pub const allocator = &allocator_instance.allocator;
7pub var allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
8var allocator_mem: [100 * 1024]u8 = undefined;
9
5/// This function is intended to be used only in tests. It prints diagnostics to stderr10/// This function is intended to be used only in tests. It prints diagnostics to stderr
6/// and then aborts when actual_error_union is not expected_error.11/// and then aborts when actual_error_union is not expected_error.
7pub fn expectError(expected_error: anyerror, actual_error_union: var) void {12pub fn expectError(expected_error: anyerror, actual_error_union: var) void {
lib/std/unicode.zig+6-6
...@@ -501,14 +501,14 @@ test "utf16leToUtf8" {...@@ -501,14 +501,14 @@ test "utf16leToUtf8" {
501 {501 {
502 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 'A');502 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 'A');
503 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 'a');503 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 'a');
504 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);504 const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
505 testing.expect(mem.eql(u8, utf8, "Aa"));505 testing.expect(mem.eql(u8, utf8, "Aa"));
506 }506 }
507507
508 {508 {
509 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0x80);509 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0x80);
510 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xffff);510 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xffff);
511 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);511 const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
512 testing.expect(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf"));512 testing.expect(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf"));
513 }513 }
514514
...@@ -516,7 +516,7 @@ test "utf16leToUtf8" {...@@ -516,7 +516,7 @@ test "utf16leToUtf8" {
516 // the values just outside the surrogate half range516 // the values just outside the surrogate half range
517 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd7ff);517 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd7ff);
518 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xe000);518 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xe000);
519 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);519 const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
520 testing.expect(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80"));520 testing.expect(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80"));
521 }521 }
522522
...@@ -524,7 +524,7 @@ test "utf16leToUtf8" {...@@ -524,7 +524,7 @@ test "utf16leToUtf8" {
524 // smallest surrogate pair524 // smallest surrogate pair
525 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd800);525 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd800);
526 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);526 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);
527 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);527 const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
528 testing.expect(mem.eql(u8, utf8, "\xf0\x90\x80\x80"));528 testing.expect(mem.eql(u8, utf8, "\xf0\x90\x80\x80"));
529 }529 }
530530
...@@ -532,14 +532,14 @@ test "utf16leToUtf8" {...@@ -532,14 +532,14 @@ test "utf16leToUtf8" {
532 // largest surrogate pair532 // largest surrogate pair
533 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);533 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);
534 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdfff);534 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdfff);
535 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);535 const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
536 testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf"));536 testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf"));
537 }537 }
538538
539 {539 {
540 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);540 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);
541 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);541 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);
542 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, &utf16le);542 const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le);
543 testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80"));543 testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80"));
544 }544 }
545}545}
lib/std/zig/ast.zig+1-1
...@@ -2287,7 +2287,7 @@ pub const Node = struct {...@@ -2287,7 +2287,7 @@ pub const Node = struct {
2287test "iterate" {2287test "iterate" {
2288 var root = Node.Root{2288 var root = Node.Root{
2289 .base = Node{ .id = Node.Id.Root },2289 .base = Node{ .id = Node.Id.Root },
2290 .decls = Node.Root.DeclList.init(std.debug.global_allocator),2290 .decls = Node.Root.DeclList.init(std.testing.allocator),
2291 .eof_token = 0,2291 .eof_token = 0,
2292 };2292 };
2293 var base = &root.base;2293 var base = &root.base;
test/compare_output.zig+2-2
...@@ -445,7 +445,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -445,7 +445,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
445 \\const std = @import("std");445 \\const std = @import("std");
446 \\const io = std.io;446 \\const io = std.io;
447 \\const os = std.os;447 \\const os = std.os;
448 \\const allocator = std.debug.global_allocator;448 \\const allocator = std.testing.allocator;
449 \\449 \\
450 \\pub fn main() !void {450 \\pub fn main() !void {
451 \\ var args_it = std.process.args();451 \\ var args_it = std.process.args();
...@@ -486,7 +486,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -486,7 +486,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
486 \\const std = @import("std");486 \\const std = @import("std");
487 \\const io = std.io;487 \\const io = std.io;
488 \\const os = std.os;488 \\const os = std.os;
489 \\const allocator = std.debug.global_allocator;489 \\const allocator = std.testing.allocator;
490 \\490 \\
491 \\pub fn main() !void {491 \\pub fn main() !void {
492 \\ var args_it = std.process.args();492 \\ var args_it = std.process.args();
test/compile_errors.zig+1-1
...@@ -5765,7 +5765,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5765,7 +5765,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5765 \\5765 \\
5766 \\export fn entry() void {5766 \\export fn entry() void {
5767 \\ const a = MdNode.Header {5767 \\ const a = MdNode.Header {
5768 \\ .text = MdText.init(&std.debug.global_allocator),5768 \\ .text = MdText.init(&std.testing.allocator),
5769 \\ .weight = HeaderWeight.H1,5769 \\ .weight = HeaderWeight.H1,
5770 \\ };5770 \\ };
5771 \\}5771 \\}
test/stage1/behavior/const_slice_child.zig+1-1
...@@ -22,7 +22,7 @@ fn foo(args: [][]const u8) void {...@@ -22,7 +22,7 @@ fn foo(args: [][]const u8) void {
22}22}
2323
24fn bar(argc: usize) void {24fn bar(argc: usize) void {
25 const args = debug.global_allocator.alloc([]const u8, argc) catch unreachable;25 const args = testing.allocator.alloc([]const u8, argc) catch unreachable;
26 for (args) |_, i| {26 for (args) |_, i| {
27 const ptr = argv[i];27 const ptr = argv[i];
28 args[i] = ptr[0..strlen(ptr)];28 args[i] = ptr[0..strlen(ptr)];
test/standalone/brace_expansion/main.zig+2-2
...@@ -201,7 +201,7 @@ pub fn main() !void {...@@ -201,7 +201,7 @@ pub fn main() !void {
201}201}
202202
203test "invalid inputs" {203test "invalid inputs" {
204 global_allocator = std.debug.global_allocator;204 global_allocator = std.testing.allocator;
205205
206 expectError("}ABC", error.InvalidInput);206 expectError("}ABC", error.InvalidInput);
207 expectError("{ABC", error.InvalidInput);207 expectError("{ABC", error.InvalidInput);
...@@ -222,7 +222,7 @@ fn expectError(test_input: []const u8, expected_err: anyerror) void {...@@ -222,7 +222,7 @@ fn expectError(test_input: []const u8, expected_err: anyerror) void {
222}222}
223223
224test "valid inputs" {224test "valid inputs" {
225 global_allocator = std.debug.global_allocator;225 global_allocator = std.testing.allocator;
226226
227 expectExpansion("{x,y,z}", "x y z");227 expectExpansion("{x,y,z}", "x y z");
228 expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");228 expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");
test/standalone/cat/main.zig+1-1
...@@ -4,7 +4,7 @@ const process = std.process;...@@ -4,7 +4,7 @@ const process = std.process;
4const fs = std.fs;4const fs = std.fs;
5const mem = std.mem;5const mem = std.mem;
6const warn = std.debug.warn;6const warn = std.debug.warn;
7const allocator = std.debug.global_allocator;7const allocator = std.testing.allocator;
88
9pub fn main() !void {9pub fn main() !void {
10 var args_it = process.args();10 var args_it = process.args();
test/standalone/empty_env/main.zig+1-1
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1const std = @import("std");1const std = @import("std");
22
3pub fn main() void {3pub fn main() void {
4 const env_map = std.process.getEnvMap(std.debug.global_allocator) catch @panic("unable to get env map");4 const env_map = std.process.getEnvMap(std.testing.allocator) catch @panic("unable to get env map");
5 std.testing.expect(env_map.count() == 0);5 std.testing.expect(env_map.count() == 0);
6}6}
test/standalone/load_dynamic_library/main.zig+2-2
...@@ -1,8 +1,8 @@...@@ -1,8 +1,8 @@
1const std = @import("std");1const std = @import("std");
22
3pub fn main() !void {3pub fn main() !void {
4 const args = try std.process.argsAlloc(std.debug.global_allocator);4 const args = try std.process.argsAlloc(std.testing.allocator);
5 defer std.process.argsFree(std.debug.global_allocator, args);5 defer std.process.argsFree(std.testing.allocator, args);
66
7 const dynlib_name = args[1];7 const dynlib_name = args[1];
88