From 699c50a375fb6755a4116c94e6068620d070bd98 Mon Sep 17 00:00:00 2001 From: Benjamin Feng Date: Fri, 31 Jan 2020 19:06:50 -0600 Subject: [PATCH] Switch a bunch of FBA to use testing.allocator --- lib/std/array_list.zig | 14 ++------- lib/std/ascii.zig | 5 ++-- lib/std/cstr.zig | 5 ++-- lib/std/fs/get_app_data_dir.zig | 6 ++-- lib/std/fs/path.zig | 10 +++---- lib/std/http/headers.zig | 48 ++++++++++++++---------------- lib/std/io.zig | 12 +++----- lib/std/io/test.zig | 13 +++----- lib/std/os/test.zig | 5 ++-- lib/std/process.zig | 6 ++-- lib/std/sort.zig | 8 ++--- lib/std/unicode.zig | 10 +++---- lib/std/zig/parser_test.zig | 3 +- test/stage1/behavior/bugs/1851.zig | 5 ++-- 14 files changed, 58 insertions(+), 92 deletions(-) diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index b028390465..f212ae8659 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 71f52bfd17..8bd959b46d 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 5194dc1a13..5d16e9387b 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 cb26e335de..35c0265435 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 f6f34585be..5d1c775629 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 dfe53fe750..a7a1464f99 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 341b73e33c..16bfffdcad 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 21ebd723c4..c4e9a1fc06 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/os/test.zig b/lib/std/os/test.zig index f33b5d5261..bd981521f9 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 ca5ca3a3bb..0178f6fa91 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 df9702b325..938c5e98f1 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 da0dbc3cb2..ed24f83c33 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 c31ef9c64b..03bb8d4222 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 679f4bf835..f3e7393b96 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;