Commit 699c50a375

Benjamin Feng <benjamin.feng@glassdoor.com>
2020-02-01 02:06:50
Switch a bunch of FBA to use testing.allocator
1 parent ab4ea5d
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);
 }
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);
 }
 
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);
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;
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);
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);
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
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));
 }
 
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);
 }
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
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.
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;
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);
     }
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;