authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-01-07 19:03:11+01:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-01-07 19:03:11+01:00
log814b54d7980c2b71b99539ceb23fc09b21d347c0
tree18e6e050a6af5375bf924037ec676cfc93c9447d
parent6bebf741f92481ae3dbc9aeb8659c2476e4d211b

json tests: don't use debug allocator


1 files changed, 19 insertions(+), 8 deletions(-)

lib/std/json.zig+19-8
...@@ -1457,7 +1457,10 @@ fn unescapeString(output: []u8, input: []const u8) !void {...@@ -1457,7 +1457,10 @@ fn unescapeString(output: []u8, input: []const u8) !void {
1457}1457}
14581458
1459test "json.parser.dynamic" {1459test "json.parser.dynamic" {
1460 var p = Parser.init(debug.global_allocator, false);1460 var memory: [1024 * 16]u8 = undefined;
1461 var buf_alloc = std.heap.FixedBufferAllocator.init(&memory);
1462
1463 var p = Parser.init(&buf_alloc.allocator, false);
1461 defer p.deinit();1464 defer p.deinit();
14621465
1463 const s =1466 const s =
...@@ -1560,17 +1563,21 @@ test "write json then parse it" {...@@ -1560,17 +1563,21 @@ test "write json then parse it" {
1560 testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));1563 testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
1561}1564}
15621565
1563fn test_parse(json_str: []const u8) !Value {1566fn test_parse(memory: []u8, json_str: []const u8) !Value {
1564 var p = Parser.init(debug.global_allocator, false);1567 // buf_alloc goes out of scope, but we don't use it after parsing
1568 var buf_alloc = std.heap.FixedBufferAllocator.init(memory);
1569 var p = Parser.init(&buf_alloc.allocator, false);
1565 return (try p.parse(json_str)).root;1570 return (try p.parse(json_str)).root;
1566}1571}
15671572
1568test "parsing empty string gives appropriate error" {1573test "parsing empty string gives appropriate error" {
1569 testing.expectError(error.UnexpectedEndOfJson, test_parse(""));1574 var memory: [1024 * 4]u8 = undefined;
1575 testing.expectError(error.UnexpectedEndOfJson, test_parse(&memory, ""));
1570}1576}
15711577
1572test "integer after float has proper type" {1578test "integer after float has proper type" {
1573 const json = try test_parse(1579 var memory: [1024 * 8]u8 = undefined;
1580 const json = try test_parse(&memory,
1574 \\{1581 \\{
1575 \\ "float": 3.14,1582 \\ "float": 3.14,
1576 \\ "ints": [1, 2, 3]1583 \\ "ints": [1, 2, 3]
...@@ -1580,6 +1587,7 @@ test "integer after float has proper type" {...@@ -1580,6 +1587,7 @@ test "integer after float has proper type" {
1580}1587}
15811588
1582test "escaped characters" {1589test "escaped characters" {
1590 var memory: [1024 * 16]u8 = undefined;
1583 const input =1591 const input =
1584 \\{1592 \\{
1585 \\ "backslash": "\\",1593 \\ "backslash": "\\",
...@@ -1595,7 +1603,7 @@ test "escaped characters" {...@@ -1595,7 +1603,7 @@ test "escaped characters" {
1595 \\}1603 \\}
1596 ;1604 ;
15971605
1598 const obj = (try test_parse(input)).Object;1606 const obj = (try test_parse(&memory, input)).Object;
15991607
1600 testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\");1608 testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\");
1601 testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/");1609 testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/");
...@@ -1619,10 +1627,13 @@ test "string copy option" {...@@ -1619,10 +1627,13 @@ test "string copy option" {
1619 \\}1627 \\}
1620 ;1628 ;
16211629
1622 const tree_nocopy = try Parser.init(debug.global_allocator, false).parse(input);1630 var mem_buffer: [1024 * 16]u8 = undefined;
1631 var buf_alloc = std.heap.FixedBufferAllocator.init(&mem_buffer);
1632
1633 const tree_nocopy = try Parser.init(&buf_alloc.allocator, false).parse(input);
1623 const obj_nocopy = tree_nocopy.root.Object;1634 const obj_nocopy = tree_nocopy.root.Object;
16241635
1625 const tree_copy = try Parser.init(debug.global_allocator, true).parse(input);1636 const tree_copy = try Parser.init(&buf_alloc.allocator, true).parse(input);
1626 const obj_copy = tree_copy.root.Object;1637 const obj_copy = tree_copy.root.Object;
16271638
1628 for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| {1639 for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| {