Skip to content

Commit 40b9db7

Browse files
authored
Merge pull request #4451 from daurnimator/use-testing.allocator
Migrate (more) tests from FixedBufferAllocator to testing.allocator
2 parents 9206f8a + 6ea6d5a commit 40b9db7

File tree

7 files changed

+651
-350
lines changed

7 files changed

+651
-350
lines changed

lib/std/json.zig

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,21 +1598,21 @@ test "write json then parse it" {
15981598
testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
15991599
}
16001600

1601-
fn test_parse(memory: []u8, json_str: []const u8) !Value {
1602-
// buf_alloc goes out of scope, but we don't use it after parsing
1603-
var buf_alloc = std.heap.FixedBufferAllocator.init(memory);
1604-
var p = Parser.init(&buf_alloc.allocator, false);
1601+
fn test_parse(arena_allocator: *std.mem.Allocator, json_str: []const u8) !Value {
1602+
var p = Parser.init(arena_allocator, false);
16051603
return (try p.parse(json_str)).root;
16061604
}
16071605

16081606
test "parsing empty string gives appropriate error" {
1609-
var memory: [1024 * 4]u8 = undefined;
1610-
testing.expectError(error.UnexpectedEndOfJson, test_parse(&memory, ""));
1607+
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1608+
defer arena_allocator.deinit();
1609+
testing.expectError(error.UnexpectedEndOfJson, test_parse(&arena_allocator.allocator, ""));
16111610
}
16121611

16131612
test "integer after float has proper type" {
1614-
var memory: [1024 * 8]u8 = undefined;
1615-
const json = try test_parse(&memory,
1613+
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1614+
defer arena_allocator.deinit();
1615+
const json = try test_parse(&arena_allocator.allocator,
16161616
\\{
16171617
\\ "float": 3.14,
16181618
\\ "ints": [1, 2, 3]
@@ -1622,7 +1622,8 @@ test "integer after float has proper type" {
16221622
}
16231623

16241624
test "escaped characters" {
1625-
var memory: [1024 * 16]u8 = undefined;
1625+
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1626+
defer arena_allocator.deinit();
16261627
const input =
16271628
\\{
16281629
\\ "backslash": "\\",
@@ -1638,7 +1639,7 @@ test "escaped characters" {
16381639
\\}
16391640
;
16401641

1641-
const obj = (try test_parse(&memory, input)).Object;
1642+
const obj = (try test_parse(&arena_allocator.allocator, input)).Object;
16421643

16431644
testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\");
16441645
testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/");
@@ -1662,13 +1663,13 @@ test "string copy option" {
16621663
\\}
16631664
;
16641665

1665-
var mem_buffer: [1024 * 16]u8 = undefined;
1666-
var buf_alloc = std.heap.FixedBufferAllocator.init(&mem_buffer);
1666+
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1667+
defer arena_allocator.deinit();
16671668

1668-
const tree_nocopy = try Parser.init(&buf_alloc.allocator, false).parse(input);
1669+
const tree_nocopy = try Parser.init(&arena_allocator.allocator, false).parse(input);
16691670
const obj_nocopy = tree_nocopy.root.Object;
16701671

1671-
const tree_copy = try Parser.init(&buf_alloc.allocator, true).parse(input);
1672+
const tree_copy = try Parser.init(&arena_allocator.allocator, true).parse(input);
16721673
const obj_copy = tree_copy.root.Object;
16731674

16741675
for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| {

lib/std/json/write_stream.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ test "json write stream" {
254254
var slice_stream = std.io.SliceOutStream.init(&out_buf);
255255
const out = &slice_stream.stream;
256256

257-
var mem_buf: [1024 * 10]u8 = undefined;
258-
const allocator = &std.heap.FixedBufferAllocator.init(&mem_buf).allocator;
257+
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
258+
defer arena_allocator.deinit();
259259

260260
var w = std.json.WriteStream(@TypeOf(out).Child, 10).init(out);
261-
try w.emitJson(try getJson(allocator));
261+
try w.emitJson(try getJson(&arena_allocator.allocator));
262262

263263
const result = slice_stream.getWritten();
264264
const expected =

0 commit comments

Comments
 (0)