authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-12 13:31:50-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-12 13:31:50-05:00
logb4e44c4e80fd4f477a46251fdaf485a742ffe6f7
treeb1d21e106f3165140961f4998a0e4147c331e078
parentec0846a00fd3d0ae0b7b94961f855b8ab6c938db

self hosted parser tests every combination of memory allocation failure


1 files changed, 9 insertions(+), 10 deletions(-)

std/zig/parser.zig+9-10
...@@ -1050,13 +1050,13 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 {...@@ -1050,13 +1050,13 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 {
1050 defer tree.deinit();1050 defer tree.deinit();
10511051
1052 var buffer = try std.Buffer.initSize(allocator, 0);1052 var buffer = try std.Buffer.initSize(allocator, 0);
1053 errdefer buffer.deinit();
1054
1053 var buffer_out_stream = io.BufferOutStream.init(&buffer);1055 var buffer_out_stream = io.BufferOutStream.init(&buffer);
1054 try parser.renderSource(&buffer_out_stream.stream, tree.root_node);1056 try parser.renderSource(&buffer_out_stream.stream, tree.root_node);
1055 return buffer.toOwnedSlice();1057 return buffer.toOwnedSlice();
1056}1058}
10571059
1058// TODO test for memory leaks
1059// TODO test for valid frees
1060fn testCanonical(source: []const u8) !void {1060fn testCanonical(source: []const u8) !void {
1061 const needed_alloc_count = x: {1061 const needed_alloc_count = x: {
1062 // Try it once with unlimited memory, make sure it works1062 // Try it once with unlimited memory, make sure it works
...@@ -1083,14 +1083,13 @@ fn testCanonical(source: []const u8) !void {...@@ -1083,14 +1083,13 @@ fn testCanonical(source: []const u8) !void {
1083 return error.NondeterministicMemoryUsage;1083 return error.NondeterministicMemoryUsage;
1084 } else |err| switch (err) {1084 } else |err| switch (err) {
1085 error.OutOfMemory => {1085 error.OutOfMemory => {
1086 // TODO make this pass1086 if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) {
1087 //if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) {1087 warn("\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n",
1088 // warn("\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n",1088 fail_index, needed_alloc_count,
1089 // fail_index, needed_alloc_count,1089 failing_allocator.allocated_bytes, failing_allocator.freed_bytes,
1090 // failing_allocator.allocated_bytes, failing_allocator.freed_bytes,1090 failing_allocator.index, failing_allocator.deallocations);
1091 // failing_allocator.index, failing_allocator.deallocations);1091 return error.MemoryLeakDetected;
1092 // return error.MemoryLeakDetected;1092 }
1093 //}
1094 },1093 },
1095 error.ParseError => @panic("test failed"),1094 error.ParseError => @panic("test failed"),
1096 }1095 }