diff --git a/lib/std/json.zig b/lib/std/json.zig index 1e4f19fda..74e008d76 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1598,21 +1598,21 @@ test "write json then parse it" { testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello")); } -fn test_parse(memory: []u8, json_str: []const u8) !Value { - // buf_alloc goes out of scope, but we don't use it after parsing - var buf_alloc = std.heap.FixedBufferAllocator.init(memory); - var p = Parser.init(&buf_alloc.allocator, false); +fn test_parse(arena_allocator: *std.mem.Allocator, json_str: []const u8) !Value { + var p = Parser.init(arena_allocator, false); return (try p.parse(json_str)).root; } test "parsing empty string gives appropriate error" { - var memory: [1024 * 4]u8 = undefined; - testing.expectError(error.UnexpectedEndOfJson, test_parse(&memory, "")); + var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_allocator.deinit(); + testing.expectError(error.UnexpectedEndOfJson, test_parse(&arena_allocator.allocator, "")); } test "integer after float has proper type" { - var memory: [1024 * 8]u8 = undefined; - const json = try test_parse(&memory, + var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_allocator.deinit(); + const json = try test_parse(&arena_allocator.allocator, \\{ \\ "float": 3.14, \\ "ints": [1, 2, 3] @@ -1622,7 +1622,8 @@ test "integer after float has proper type" { } test "escaped characters" { - var memory: [1024 * 16]u8 = undefined; + var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_allocator.deinit(); const input = \\{ \\ "backslash": "\\", @@ -1638,7 +1639,7 @@ test "escaped characters" { \\} ; - const obj = (try test_parse(&memory, input)).Object; + const obj = (try test_parse(&arena_allocator.allocator, input)).Object; testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\"); testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/"); @@ -1662,13 +1663,13 @@ test "string copy option" { \\} ; - var mem_buffer: [1024 * 16]u8 = undefined; - var buf_alloc = std.heap.FixedBufferAllocator.init(&mem_buffer); + var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_allocator.deinit(); - const tree_nocopy = try Parser.init(&buf_alloc.allocator, false).parse(input); + const tree_nocopy = try Parser.init(&arena_allocator.allocator, false).parse(input); const obj_nocopy = tree_nocopy.root.Object; - const tree_copy = try Parser.init(&buf_alloc.allocator, true).parse(input); + const tree_copy = try Parser.init(&arena_allocator.allocator, true).parse(input); const obj_copy = tree_copy.root.Object; for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| { diff --git a/lib/std/json/write_stream.zig b/lib/std/json/write_stream.zig index 51afe7fcb..213b6768d 100644 --- a/lib/std/json/write_stream.zig +++ b/lib/std/json/write_stream.zig @@ -254,11 +254,11 @@ test "json write stream" { var slice_stream = std.io.SliceOutStream.init(&out_buf); const out = &slice_stream.stream; - var mem_buf: [1024 * 10]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&mem_buf).allocator; + var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_allocator.deinit(); var w = std.json.WriteStream(@TypeOf(out).Child, 10).init(out); - try w.emitJson(try getJson(allocator)); + try w.emitJson(try getJson(&arena_allocator.allocator)); const result = slice_stream.getWritten(); const expected = diff --git a/lib/std/math/big/rational.zig b/lib/std/math/big/rational.zig index 27a3b7300..438c6c94f 100644 --- a/lib/std/math/big/rational.zig +++ b/lib/std/math/big/rational.zig @@ -587,14 +587,13 @@ fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void { r.swap(&x); } -var buffer: [64 * 8192]u8 = undefined; -var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]); -var al = &fixed.allocator; - test "big.rational gcd non-one small" { - var a = try Int.initSet(al, 17); - var b = try Int.initSet(al, 97); - var r = try Int.init(al); + var a = try Int.initSet(testing.allocator, 17); + defer a.deinit(); + var b = try Int.initSet(testing.allocator, 97); + defer b.deinit(); + var r = try Int.init(testing.allocator); + defer r.deinit(); try gcd(&r, a, b); @@ -602,9 +601,12 @@ test "big.rational gcd non-one small" { } test "big.rational gcd non-one small" { - var a = try Int.initSet(al, 4864); - var b = try Int.initSet(al, 3458); - var r = try Int.init(al); + var a = try Int.initSet(testing.allocator, 4864); + defer a.deinit(); + var b = try Int.initSet(testing.allocator, 3458); + defer b.deinit(); + var r = try Int.init(testing.allocator); + defer r.deinit(); try gcd(&r, a, b); @@ -612,9 +614,12 @@ test "big.rational gcd non-one small" { } test "big.rational gcd non-one large" { - var a = try Int.initSet(al, 0xffffffffffffffff); - var b = try Int.initSet(al, 0xffffffffffffffff7777); - var r = try Int.init(al); + var a = try Int.initSet(testing.allocator, 0xffffffffffffffff); + defer a.deinit(); + var b = try Int.initSet(testing.allocator, 0xffffffffffffffff7777); + defer b.deinit(); + var r = try Int.init(testing.allocator); + defer r.deinit(); try gcd(&r, a, b); @@ -622,9 +627,12 @@ test "big.rational gcd non-one large" { } test "big.rational gcd large multi-limb result" { - var a = try Int.initSet(al, 0x12345678123456781234567812345678123456781234567812345678); - var b = try Int.initSet(al, 0x12345671234567123456712345671234567123456712345671234567); - var r = try Int.init(al); + var a = try Int.initSet(testing.allocator, 0x12345678123456781234567812345678123456781234567812345678); + defer a.deinit(); + var b = try Int.initSet(testing.allocator, 0x12345671234567123456712345671234567123456712345671234567); + defer b.deinit(); + var r = try Int.init(testing.allocator); + defer r.deinit(); try gcd(&r, a, b); @@ -632,9 +640,12 @@ test "big.rational gcd large multi-limb result" { } test "big.rational gcd one large" { - var a = try Int.initSet(al, 1897056385327307); - var b = try Int.initSet(al, 2251799813685248); - var r = try Int.init(al); + var a = try Int.initSet(testing.allocator, 1897056385327307); + defer a.deinit(); + var b = try Int.initSet(testing.allocator, 2251799813685248); + defer b.deinit(); + var r = try Int.init(testing.allocator); + defer r.deinit(); try gcd(&r, a, b); @@ -661,7 +672,8 @@ fn extractLowBits(a: Int, comptime T: type) T { } test "big.rational extractLowBits" { - var a = try Int.initSet(al, 0x11112222333344441234567887654321); + var a = try Int.initSet(testing.allocator, 0x11112222333344441234567887654321); + defer a.deinit(); const a1 = extractLowBits(a, u8); testing.expect(a1 == 0x21); @@ -680,7 +692,8 @@ test "big.rational extractLowBits" { } test "big.rational set" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); try a.setInt(5); testing.expect((try a.p.to(u32)) == 5); @@ -708,7 +721,8 @@ test "big.rational set" { } test "big.rational setFloat" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); try a.setFloat(f64, 2.5); testing.expect((try a.p.to(i32)) == 5); @@ -732,7 +746,8 @@ test "big.rational setFloat" { } test "big.rational setFloatString" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); try a.setFloatString("72.14159312071241458852455252781510353"); @@ -742,7 +757,8 @@ test "big.rational setFloatString" { } test "big.rational toFloat" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); // = 3.14159297943115234375 try a.setRatio(3294199, 1048576); @@ -754,7 +770,8 @@ test "big.rational toFloat" { } test "big.rational set/to Float round-trip" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); var prng = std.rand.DefaultPrng.init(0x5EED); var i: usize = 0; while (i < 512) : (i += 1) { @@ -765,23 +782,29 @@ test "big.rational set/to Float round-trip" { } test "big.rational copy" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); - const b = try Int.initSet(al, 5); + const b = try Int.initSet(testing.allocator, 5); + defer b.deinit(); try a.copyInt(b); testing.expect((try a.p.to(u32)) == 5); testing.expect((try a.q.to(u32)) == 1); - const c = try Int.initSet(al, 7); - const d = try Int.initSet(al, 3); + const c = try Int.initSet(testing.allocator, 7); + defer c.deinit(); + const d = try Int.initSet(testing.allocator, 3); + defer d.deinit(); try a.copyRatio(c, d); testing.expect((try a.p.to(u32)) == 7); testing.expect((try a.q.to(u32)) == 3); - const e = try Int.initSet(al, 9); - const f = try Int.initSet(al, 3); + const e = try Int.initSet(testing.allocator, 9); + defer e.deinit(); + const f = try Int.initSet(testing.allocator, 3); + defer f.deinit(); try a.copyRatio(e, f); testing.expect((try a.p.to(u32)) == 3); @@ -789,7 +812,8 @@ test "big.rational copy" { } test "big.rational negate" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); try a.setInt(-50); testing.expect((try a.p.to(i32)) == -50); @@ -805,7 +829,8 @@ test "big.rational negate" { } test "big.rational abs" { - var a = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); try a.setInt(-50); testing.expect((try a.p.to(i32)) == -50); @@ -821,8 +846,10 @@ test "big.rational abs" { } test "big.rational swap" { - var a = try Rational.init(al); - var b = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var b = try Rational.init(testing.allocator); + defer b.deinit(); try a.setRatio(50, 23); try b.setRatio(17, 3); @@ -843,8 +870,10 @@ test "big.rational swap" { } test "big.rational cmp" { - var a = try Rational.init(al); - var b = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var b = try Rational.init(testing.allocator); + defer b.deinit(); try a.setRatio(500, 231); try b.setRatio(18903, 8584); @@ -856,8 +885,10 @@ test "big.rational cmp" { } test "big.rational add single-limb" { - var a = try Rational.init(al); - var b = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var b = try Rational.init(testing.allocator); + defer b.deinit(); try a.setRatio(500, 231); try b.setRatio(18903, 8584); @@ -869,9 +900,12 @@ test "big.rational add single-limb" { } test "big.rational add" { - var a = try Rational.init(al); - var b = try Rational.init(al); - var r = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var b = try Rational.init(testing.allocator); + defer b.deinit(); + var r = try Rational.init(testing.allocator); + defer r.deinit(); try a.setRatio(78923, 23341); try b.setRatio(123097, 12441414); @@ -882,9 +916,12 @@ test "big.rational add" { } test "big.rational sub" { - var a = try Rational.init(al); - var b = try Rational.init(al); - var r = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var b = try Rational.init(testing.allocator); + defer b.deinit(); + var r = try Rational.init(testing.allocator); + defer r.deinit(); try a.setRatio(78923, 23341); try b.setRatio(123097, 12441414); @@ -895,9 +932,12 @@ test "big.rational sub" { } test "big.rational mul" { - var a = try Rational.init(al); - var b = try Rational.init(al); - var r = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var b = try Rational.init(testing.allocator); + defer b.deinit(); + var r = try Rational.init(testing.allocator); + defer r.deinit(); try a.setRatio(78923, 23341); try b.setRatio(123097, 12441414); @@ -908,9 +948,12 @@ test "big.rational mul" { } test "big.rational div" { - var a = try Rational.init(al); - var b = try Rational.init(al); - var r = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var b = try Rational.init(testing.allocator); + defer b.deinit(); + var r = try Rational.init(testing.allocator); + defer r.deinit(); try a.setRatio(78923, 23341); try b.setRatio(123097, 12441414); @@ -921,8 +964,10 @@ test "big.rational div" { } test "big.rational div" { - var a = try Rational.init(al); - var r = try Rational.init(al); + var a = try Rational.init(testing.allocator); + defer a.deinit(); + var r = try Rational.init(testing.allocator); + defer r.deinit(); try a.setRatio(78923, 23341); a.invert(); diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 8f4987e0c..a1b4a3c80 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1011,11 +1011,21 @@ pub fn join(allocator: *Allocator, separator: []const u8, slices: []const []cons } test "mem.join" { - var buf: [1024]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "b", "c" }), "a,b,c")); - testing.expect(eql(u8, try join(a, ",", &[_][]const u8{"a"}), "a")); - testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "", "b", "", "c" }), "a,,b,,c")); + { + const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" }); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "a,b,c")); + } + { + const str = try join(testing.allocator, ",", &[_][]const u8{"a"}); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "a")); + } + { + const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "", "b", "", "c" }); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "a,,b,,c")); + } } /// Copies each T from slices into a new slice that exactly holds all the elements. @@ -1044,15 +1054,21 @@ pub fn concat(allocator: *Allocator, comptime T: type, slices: []const []const T } test "concat" { - var buf: [1024]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - testing.expect(eql(u8, try concat(a, u8, &[_][]const u8{ "abc", "def", "ghi" }), "abcdefghi")); - testing.expect(eql(u32, try concat(a, u32, &[_][]const u32{ - &[_]u32{ 0, 1 }, - &[_]u32{ 2, 3, 4 }, - &[_]u32{}, - &[_]u32{5}, - }), &[_]u32{ 0, 1, 2, 3, 4, 5 })); + { + const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" }); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "abcdefghi")); + } + { + const str = try concat(testing.allocator, u32, &[_][]const u32{ + &[_]u32{ 0, 1 }, + &[_]u32{ 2, 3, 4 }, + &[_]u32{}, + &[_]u32{5}, + }); + defer testing.allocator.free(str); + testing.expect(eql(u32, str, &[_]u32{ 0, 1, 2, 3, 4, 5 })); + } } test "testStringEquality" { diff --git a/lib/std/net/test.zig b/lib/std/net/test.zig index 703a804bf..45d8b1cff 100644 --- a/lib/std/net/test.zig +++ b/lib/std/net/test.zig @@ -67,10 +67,8 @@ test "resolve DNS" { // DNS resolution not implemented on Windows yet. return error.SkipZigTest; } - var buf: [1000 * 10]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - const address_list = net.getAddressList(a, "example.com", 80) catch |err| switch (err) { + const address_list = net.getAddressList(testing.allocator, "example.com", 80) catch |err| switch (err) { // The tests are required to work even when there is no Internet connection, // so some of these errors we must accept and skip the test. error.UnknownHostName => return error.SkipZigTest,