authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-11-18 20:51:42-05:00
committergravatar for ascottcameron@gmail.comAlex Cameron <ascottcameron@gmail.com> 2020-12-23 01:14:35+11:00
log8538053940bafde64941f8ebf6d559e5ade490f1
tree1ac1d7e51246be35d164e5011ba809167cde4f88
parentac33b10b5e75293357ee50b0b5e7a7b3591a0c31

Add header test harness


2 files changed, 48 insertions(+), 4 deletions(-)

src/test.zig+42-4
......@@ -96,6 +96,10 @@ pub const TestContext = struct {
9696 /// stdout against the expected results
9797 /// This is a slice containing the expected message.
9898 Execution: []const u8,
99 /// A header update compiles the input with the equivalent of
100 /// `-Demit_h=true` and tests the produced header against the
101 /// expected result
102 Header: []const u8,
99103 },
100104 };
101105
......@@ -138,6 +142,15 @@ pub const TestContext = struct {
138142 }) catch unreachable;
139143 }
140144
145 /// Adds a subcase in which the module is updated with `src`, and a C
146 /// header is generated.
147 pub fn addHeader(self: *Case, src: [:0]const u8, result: [:0]const u8) void {
148 self.updates.append(.{
149 .src = src,
150 .case = .{ .Header = result },
151 }) catch unreachable;
152 }
153
141154 /// Adds a subcase in which the module is updated with `src`, compiled,
142155 /// run, and the output is tested against `result`.
143156 pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) void {
......@@ -269,6 +282,10 @@ pub const TestContext = struct {
269282 ctx.addC(name, target, .Zig).addTransform(src, cheader ++ out);
270283 }
271284
285 pub fn h(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget, src: [:0]const u8, comptime out: [:0]const u8) void {
286 ctx.addC(name, target, .Zig).addHeader(src, cheader ++ out);
287 }
288
272289 pub fn addCompareOutput(
273290 ctx: *TestContext,
274291 name: []const u8,
......@@ -547,6 +564,10 @@ pub const TestContext = struct {
547564 .directory = emit_directory,
548565 .basename = bin_name,
549566 };
567 const emit_h: Compilation.EmitLoc = .{
568 .directory = emit_directory,
569 .basename = "test_case.h",
570 };
550571 const comp = try Compilation.create(allocator, .{
551572 .local_cache_directory = zig_cache_directory,
552573 .global_cache_directory = zig_cache_directory,
......@@ -561,6 +582,7 @@ pub const TestContext = struct {
561582 // TODO: support testing optimizations
562583 .optimize_mode = .Debug,
563584 .emit_bin = emit_bin,
585 .emit_h = emit_h,
564586 .root_pkg = &root_pkg,
565587 .keep_source_files_loaded = true,
566588 .object_format = ofmt,
......@@ -616,6 +638,22 @@ pub const TestContext = struct {
616638 }
617639
618640 switch (update.case) {
641 .Header => |expected_output| {
642 var file = try tmp.dir.openFile("test_case.h", .{ .read = true });
643 defer file.close();
644 var out = file.reader().readAllAlloc(arena, 1024 * 1024) catch @panic("Unable to read headeroutput!");
645
646 if (expected_output.len != out.len) {
647 std.debug.print("\nTransformed header length differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ expected_output, out });
648 std.process.exit(1);
649 }
650 for (expected_output) |e, i| {
651 if (out[i] != e) {
652 std.debug.print("\nTransformed header differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ expected_output, out });
653 std.process.exit(1);
654 }
655 }
656 },
619657 .Transformation => |expected_output| {
620658 if (case.cbe) {
621659 // The C file is always closed after an update, because we don't support
......@@ -670,8 +708,8 @@ pub const TestContext = struct {
670708 test_node.activate();
671709 defer test_node.end();
672710 var handled_errors = try arena.alloc(bool, e.len);
673 for (handled_errors) |*h| {
674 h.* = false;
711 for (handled_errors) |*handled| {
712 handled.* = false;
675713 }
676714 var all_errors = try comp.getAllErrorsAlloc();
677715 defer all_errors.deinit(allocator);
......@@ -709,8 +747,8 @@ pub const TestContext = struct {
709747 }
710748 }
711749
712 for (handled_errors) |h, i| {
713 if (!h) {
750 for (handled_errors) |handled, i| {
751 if (!handled) {
714752 const er = e[i];
715753 std.debug.print(
716754 "{s}\nDid not receive error:\n================\n{}\n================\nTest failed.\n",
test/stage2/cbe.zig+6
......@@ -19,6 +19,12 @@ pub fn addCases(ctx: *TestContext) !void {
1919 \\}
2020 \\
2121 );
22 ctx.h("Simple header", linux_x64,
23 \\export fn start() void{}
24 ,
25 \\void start(void);
26 \\
27 );
2228 ctx.c("less empty start function", linux_x64,
2329 \\fn main() noreturn {
2430 \\ unreachable;