diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index b028390465f3f08d2a84db14c42698c34cf23c4b..f212ae8659dbb7f2f340e189c87e6386196267e5 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -244,10 +244,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { } test "std.ArrayList.init" { - var bytes: [1024]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - - var list = ArrayList(i32).init(allocator); + var list = ArrayList(i32).init(testing.allocator); defer list.deinit(); testing.expect(list.len == 0); @@ -255,19 +252,14 @@ test "std.ArrayList.init" { } test "std.ArrayList.initCapacity" { - var bytes: [1024]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - var list = try ArrayList(i8).initCapacity(allocator, 200); + var list = try ArrayList(i8).initCapacity(testing.allocator, 200); defer list.deinit(); testing.expect(list.len == 0); testing.expect(list.capacity() >= 200); } test "std.ArrayList.basic" { - var bytes: [1024]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - - var list = ArrayList(i32).init(allocator); + var list = ArrayList(i32).init(testing.allocator); defer list.deinit(); // setting on empty list is out of bounds diff --git a/lib/std/ascii.zig b/lib/std/ascii.zig index 71f52bfd17473b1282eb3e5c38bb4f056b2f5dfe..8bd959b46d577bc4e9d5f0b4d735d4a5f6c3282d 100644 --- a/lib/std/ascii.zig +++ b/lib/std/ascii.zig @@ -236,9 +236,8 @@ pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) } test "allocLowerString" { - var buf: [100]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator; - const result = try allocLowerString(allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); + const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); + defer std.testing.allocator.free(result); std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result)); } diff --git a/lib/std/cstr.zig b/lib/std/cstr.zig index 5194dc1a13b45a8a42560751dc746a54dcbca487..5d16e9387be9e1a6143d29a2d4a329133b604092 100644 --- a/lib/std/cstr.zig +++ b/lib/std/cstr.zig @@ -41,9 +41,8 @@ pub fn addNullByte(allocator: *mem.Allocator, slice: []const u8) ![:0]u8 { } test "addNullByte" { - var buf: [30]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator; - const slice = try addNullByte(allocator, "hello"[0..4]); + const slice = try addNullByte(std.testing.allocator, "hello"[0..4]); + defer std.testing.allocator.free(slice); testing.expect(slice.len == 4); testing.expect(slice[4] == 0); } diff --git a/lib/std/fs/get_app_data_dir.zig b/lib/std/fs/get_app_data_dir.zig index cb26e335de70e954f423c7c0dd5794c58869aa63..35c02654353ed134cdb37a6817afdea051bff1a2 100644 --- a/lib/std/fs/get_app_data_dir.zig +++ b/lib/std/fs/get_app_data_dir.zig @@ -56,9 +56,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD } test "getAppDataDir" { - var buf: [512]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator; - // We can't actually validate the result - _ = getAppDataDir(allocator, "zig") catch return; + const dir = getAppDataDir(std.testing.allocator, "zig") catch return; + defer std.testing.allocator.free(dir); } diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index f6f34585bebbdadedd248b44f80a3b4324b4228e..5d1c775629175c0bd0fe06ce6977b5fd46041e33 100644 --- a/lib/std/fs/path.zig +++ b/lib/std/fs/path.zig @@ -89,16 +89,14 @@ pub fn joinPosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { } fn testJoinWindows(paths: []const []const u8, expected: []const u8) void { - var buf: [1024]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - const actual = joinWindows(a, paths) catch @panic("fail"); + const actual = joinWindows(testing.allocator, paths) catch @panic("fail"); + defer testing.allocator.free(actual); testing.expectEqualSlices(u8, expected, actual); } fn testJoinPosix(paths: []const []const u8, expected: []const u8) void { - var buf: [1024]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - const actual = joinPosix(a, paths) catch @panic("fail"); + const actual = joinPosix(testing.allocator, paths) catch @panic("fail"); + defer testing.allocator.free(actual); testing.expectEqualSlices(u8, expected, actual); } diff --git a/lib/std/http/headers.zig b/lib/std/http/headers.zig index dfe53fe7501a9e0f7e203bb1594e8cd3eeb25aeb..a7a1464f990ace60913f1d2b666bc2ebb31e14fd 100644 --- a/lib/std/http/headers.zig +++ b/lib/std/http/headers.zig @@ -83,12 +83,8 @@ const HeaderEntry = struct { } }; -var test_memory: [32 * 1024]u8 = undefined; -var test_fba_state = std.heap.FixedBufferAllocator.init(&test_memory); -const test_allocator = &test_fba_state.allocator; - test "HeaderEntry" { - var e = try HeaderEntry.init(test_allocator, "foo", "bar", null); + var e = try HeaderEntry.init(testing.allocator, "foo", "bar", null); defer e.deinit(); testing.expectEqualSlices(u8, "foo", e.name); testing.expectEqualSlices(u8, "bar", e.value); @@ -368,7 +364,7 @@ pub const Headers = struct { }; test "Headers.iterator" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("cookie", "somevalue", null); @@ -390,7 +386,7 @@ test "Headers.iterator" { } test "Headers.contains" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("cookie", "somevalue", null); @@ -400,7 +396,7 @@ test "Headers.contains" { } test "Headers.delete" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("baz", "qux", null); @@ -428,7 +424,7 @@ test "Headers.delete" { } test "Headers.orderedRemove" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("baz", "qux", null); @@ -451,7 +447,7 @@ test "Headers.orderedRemove" { } test "Headers.swapRemove" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("baz", "qux", null); @@ -474,7 +470,7 @@ test "Headers.swapRemove" { } test "Headers.at" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("cookie", "somevalue", null); @@ -494,7 +490,7 @@ test "Headers.at" { } test "Headers.getIndices" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("set-cookie", "x=1", null); @@ -506,27 +502,27 @@ test "Headers.getIndices" { } test "Headers.get" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("set-cookie", "x=1", null); try h.append("set-cookie", "y=2", null); { - const v = try h.get(test_allocator, "not-present"); + const v = try h.get(testing.allocator, "not-present"); testing.expect(null == v); } { - const v = (try h.get(test_allocator, "foo")).?; - defer test_allocator.free(v); + const v = (try h.get(testing.allocator, "foo")).?; + defer testing.allocator.free(v); const e = v[0]; testing.expectEqualSlices(u8, "foo", e.name); testing.expectEqualSlices(u8, "bar", e.value); testing.expectEqual(false, e.never_index); } { - const v = (try h.get(test_allocator, "set-cookie")).?; - defer test_allocator.free(v); + const v = (try h.get(testing.allocator, "set-cookie")).?; + defer testing.allocator.free(v); { const e = v[0]; testing.expectEqualSlices(u8, "set-cookie", e.name); @@ -543,30 +539,30 @@ test "Headers.get" { } test "Headers.getCommaSeparated" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("set-cookie", "x=1", null); try h.append("set-cookie", "y=2", null); { - const v = try h.getCommaSeparated(test_allocator, "not-present"); + const v = try h.getCommaSeparated(testing.allocator, "not-present"); testing.expect(null == v); } { - const v = (try h.getCommaSeparated(test_allocator, "foo")).?; - defer test_allocator.free(v); + const v = (try h.getCommaSeparated(testing.allocator, "foo")).?; + defer testing.allocator.free(v); testing.expectEqualSlices(u8, "bar", v); } { - const v = (try h.getCommaSeparated(test_allocator, "set-cookie")).?; - defer test_allocator.free(v); + const v = (try h.getCommaSeparated(testing.allocator, "set-cookie")).?; + defer testing.allocator.free(v); testing.expectEqualSlices(u8, "x=1,y=2", v); } } test "Headers.sort" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("cookie", "somevalue", null); @@ -587,7 +583,7 @@ test "Headers.sort" { } test "Headers.format" { - var h = Headers.init(test_allocator); + var h = Headers.init(testing.allocator); defer h.deinit(); try h.append("foo", "bar", null); try h.append("cookie", "somevalue", null); diff --git a/lib/std/io.zig b/lib/std/io.zig index 341b73e33ca8d84f9a2a4b9030f6906bf46805b4..16bfffdcad4745d2cc33d752b616426ee0a22b59 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -223,15 +223,13 @@ test "io.BufferedInStream" { } }; - var buf: [100]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator; - const str = "This is a test"; var one_byte_stream = OneByteReadInStream.init(str); var buf_in_stream = BufferedInStream(OneByteReadInStream.Error).init(&one_byte_stream.stream); const stream = &buf_in_stream.stream; - const res = try stream.readAllAlloc(allocator, str.len + 1); + const res = try stream.readAllAlloc(testing.allocator, str.len + 1); + defer testing.allocator.free(res); testing.expectEqualSlices(u8, str, res); } @@ -874,10 +872,8 @@ pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 { } test "io.readLineFrom" { - var bytes: [128]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - - var buf = try std.Buffer.initSize(allocator, 0); + var buf = try std.Buffer.initSize(testing.allocator, 0); + defer buf.deinit(); var mem_stream = SliceInStream.init( \\Line 1 \\Line 22 diff --git a/lib/std/io/test.zig b/lib/std/io/test.zig index 21ebd723c46ef915a67176b05c35786ac399d76e..c4e9a1fc06b32c118558436b3486997bf168cb8d 100644 --- a/lib/std/io/test.zig +++ b/lib/std/io/test.zig @@ -11,9 +11,6 @@ const fs = std.fs; const File = std.fs.File; test "write a file, read it, then delete it" { - var raw_bytes: [200 * 1024]u8 = undefined; - var allocator = &std.heap.FixedBufferAllocator.init(raw_bytes[0..]).allocator; - const cwd = fs.cwd(); var data: [1024]u8 = undefined; @@ -53,8 +50,8 @@ test "write a file, read it, then delete it" { var file_in_stream = file.inStream(); var buf_stream = io.BufferedInStream(File.ReadError).init(&file_in_stream.stream); const st = &buf_stream.stream; - const contents = try st.readAllAlloc(allocator, 2 * 1024); - defer allocator.free(contents); + const contents = try st.readAllAlloc(std.testing.allocator, 2 * 1024); + defer std.testing.allocator.free(contents); expect(mem.eql(u8, contents[0.."begin".len], "begin")); expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], &data)); @@ -64,10 +61,8 @@ test "write a file, read it, then delete it" { } test "BufferOutStream" { - var bytes: [100]u8 = undefined; - var allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - - var buffer = try std.Buffer.initSize(allocator, 0); + var buffer = try std.Buffer.initSize(std.testing.allocator, 0); + defer buffer.deinit(); var buf_stream = &std.io.BufferOutStream.init(&buffer).stream; const x: i32 = 42; diff --git a/lib/std/json.zig b/lib/std/json.zig index 1b10be07632cf142545e45555f971438cde03d70..1e4f19fda8e4a4fab90dac122f3e6d41b9451fd5 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1495,10 +1495,7 @@ fn unescapeString(output: []u8, input: []const u8) !void { } test "json.parser.dynamic" { - var memory: [1024 * 16]u8 = undefined; - var buf_alloc = std.heap.FixedBufferAllocator.init(&memory); - - var p = Parser.init(&buf_alloc.allocator, false); + var p = Parser.init(testing.allocator, false); defer p.deinit(); const s = @@ -1588,10 +1585,10 @@ test "write json then parse it" { try jw.endObject(); - var mem_buffer: [1024 * 20]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator; - var parser = Parser.init(allocator, false); - const tree = try parser.parse(slice_out_stream.getWritten()); + var parser = Parser.init(testing.allocator, false); + defer parser.deinit(); + var tree = try parser.parse(slice_out_stream.getWritten()); + defer tree.deinit(); testing.expect(tree.root.Object.get("f").?.value.Bool == false); testing.expect(tree.root.Object.get("t").?.value.Bool == true); diff --git a/lib/std/json/test.zig b/lib/std/json/test.zig index 28fdca1b0f81ec8a13d46d1543d6f0e1b2376847..6873fa038a627e57c65f4aa339f91cacf2361d91 100644 --- a/lib/std/json/test.zig +++ b/lib/std/json/test.zig @@ -8,19 +8,18 @@ const std = @import("../std.zig"); fn ok(comptime s: []const u8) void { std.testing.expect(std.json.validate(s)); - var mem_buffer: [1024 * 20]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator; - var p = std.json.Parser.init(allocator, false); + var p = std.json.Parser.init(std.testing.allocator, false); + defer p.deinit(); - _ = p.parse(s) catch unreachable; + var tree = p.parse(s) catch unreachable; + defer tree.deinit(); } fn err(comptime s: []const u8) void { std.testing.expect(!std.json.validate(s)); - var mem_buffer: [1024 * 20]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator; - var p = std.json.Parser.init(allocator, false); + var p = std.json.Parser.init(std.testing.allocator, false); + defer p.deinit(); if (p.parse(s)) |_| { unreachable; @@ -30,9 +29,8 @@ fn err(comptime s: []const u8) void { fn utf8Error(comptime s: []const u8) void { std.testing.expect(!std.json.validate(s)); - var mem_buffer: [1024 * 20]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator; - var p = std.json.Parser.init(allocator, false); + var p = std.json.Parser.init(std.testing.allocator, false); + defer p.deinit(); if (p.parse(s)) |_| { unreachable; @@ -44,19 +42,18 @@ fn utf8Error(comptime s: []const u8) void { fn any(comptime s: []const u8) void { _ = std.json.validate(s); - var mem_buffer: [1024 * 20]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator; - var p = std.json.Parser.init(allocator, false); + var p = std.json.Parser.init(std.testing.allocator, false); + defer p.deinit(); - _ = p.parse(s) catch {}; + var tree = p.parse(s) catch return; + defer tree.deinit(); } fn anyStreamingErrNonStreaming(comptime s: []const u8) void { _ = std.json.validate(s); - var mem_buffer: [1024 * 20]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator; - var p = std.json.Parser.init(allocator, false); + var p = std.json.Parser.init(std.testing.allocator, false); + defer p.deinit(); if (p.parse(s)) |_| { unreachable; diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index 989653116c576c5ae17746ce12be649641593df6..9b967955ec75333e8e7d3780654c352c79300b80 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -95,8 +95,6 @@ test "cpu count" { } test "AtomicFile" { - var buffer: [1024]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(buffer[0..]).allocator; const test_out_file = "tmp_atomic_file_test_dest.txt"; const test_content = \\ hello! @@ -108,7 +106,8 @@ test "AtomicFile" { try af.file.write(test_content); try af.finish(); } - const content = try io.readFileAlloc(allocator, test_out_file); + const content = try io.readFileAlloc(testing.allocator, test_out_file); + defer testing.allocator.free(content); expect(mem.eql(u8, content, test_content)); try fs.cwd().deleteFile(test_out_file); diff --git a/lib/std/process.zig b/lib/std/process.zig index ca5ca3a3bb0da2cc47597f336f15a085aa2e8a80..0178f6fa916b5c35b70e476e072a2ba3d5c8065f 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -27,10 +27,8 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { } test "getCwdAlloc" { - // at least call it so it gets compiled - var buf: [1000]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator; - _ = getCwdAlloc(allocator) catch undefined; + const cwd = try getCwdAlloc(testing.allocator); + testing.allocator.free(cwd); } /// Caller must free result when done. diff --git a/lib/std/sort.zig b/lib/std/sort.zig index df9702b3251da2a38fb34463e65add9aad2d7be8..938c5e98f1b2191205d2efa063318d1f9d1bc8ee 100644 --- a/lib/std/sort.zig +++ b/lib/std/sort.zig @@ -1220,16 +1220,16 @@ test "sort fuzz testing" { const test_case_count = 10; var i: usize = 0; while (i < test_case_count) : (i += 1) { - fuzzTest(&prng.random); + try fuzzTest(&prng.random); } } var fixed_buffer_mem: [100 * 1024]u8 = undefined; -fn fuzzTest(rng: *std.rand.Random) void { +fn fuzzTest(rng: *std.rand.Random) !void { const array_size = rng.range(usize, 0, 1000); - var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); - var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable; + var array = try testing.allocator.alloc(IdAndValue, array_size); + defer testing.allocator.free(array); // populate with random data for (array) |*item, index| { item.id = index; diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index da0dbc3cb24da2894955476bda40eeba8aab858a..ed24f83c3392af23a16d3b0dd093acb7059baee1 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -617,16 +617,14 @@ test "utf8ToUtf16Le" { test "utf8ToUtf16LeWithNull" { { - var bytes: [128]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - const utf16 = try utf8ToUtf16LeWithNull(allocator, "𐐷"); + const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "𐐷"); + defer testing.allocator.free(utf16); testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..])); testing.expect(utf16[2] == 0); } { - var bytes: [128]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - const utf16 = try utf8ToUtf16LeWithNull(allocator, "\u{10FFFF}"); + const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "\u{10FFFF}"); + defer testing.allocator.free(utf16); testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..])); testing.expect(utf16[2] == 0); } diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index c31ef9c64b4e85e4796213fc558effefa814e320..03bb8d42228874afe28cd34eeef90af1b1968bd8 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -2886,8 +2886,7 @@ fn testCanonical(source: []const u8) !void { } fn testError(source: []const u8) !void { - var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); - const tree = try std.zig.parse(&fixed_allocator.allocator, source); + const tree = try std.zig.parse(std.testing.allocator, source); defer tree.deinit(); std.testing.expect(tree.errors.len != 0); diff --git a/test/stage1/behavior/bugs/1851.zig b/test/stage1/behavior/bugs/1851.zig index 679f4bf835c054e5287e9c1a023526774cd86baf..f3e7393b962d732aa1a216b64af8d871e2311880 100644 --- a/test/stage1/behavior/bugs/1851.zig +++ b/test/stage1/behavior/bugs/1851.zig @@ -6,10 +6,9 @@ test "allocation and looping over 3-byte integer" { expect(@sizeOf([1]u24) == 4); expect(@alignOf(u24) == 4); expect(@alignOf([1]u24) == 4); - var buffer: [100]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buffer).allocator; - var x = a.alloc(u24, 2) catch unreachable; + var x = try std.testing.allocator.alloc(u24, 2); + defer std.testing.allocator.free(x); expect(x.len == 2); x[0] = 0xFFFFFF; x[1] = 0xFFFFFF;