| ... | @@ -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 | } |
| 1458 | | 1458 | |
| 1459 | test "json.parser.dynamic" { | 1459 | test "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(); |
| 1462 | | 1465 | |
| 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 | } |
| 1562 | | 1565 | |
| 1563 | fn test_parse(json_str: []const u8) !Value { | 1566 | fn 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 | } |
| 1567 | | 1572 | |
| 1568 | test "parsing empty string gives appropriate error" { | 1573 | test "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 | } |
| 1571 | | 1577 | |
| 1572 | test "integer after float has proper type" { | 1578 | test "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 | } |
| 1581 | | 1588 | |
| 1582 | test "escaped characters" { | 1589 | test "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 | ; |
| 1597 | | 1605 | |
| 1598 | const obj = (try test_parse(input)).Object; | 1606 | const obj = (try test_parse(&memory, input)).Object; |
| 1599 | | 1607 | |
| 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 | ; |
| 1621 | | 1629 | |
| 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; |
| 1624 | | 1635 | |
| 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; |
| 1627 | | 1638 | |
| 1628 | for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| { | 1639 | for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| { |