| ... | ... | @@ -0,0 +1,180 @@ |
| 1 | // This is the implementation of the test harness for running translated |
| 2 | // C code. For the actual test cases, see test/run_translated_c.zig. |
| 3 | const std = @import("std"); |
| 4 | const build = std.build; |
| 5 | const ArrayList = std.ArrayList; |
| 6 | const fmt = std.fmt; |
| 7 | const mem = std.mem; |
| 8 | const fs = std.fs; |
| 9 | const warn = std.debug.warn; |
| 10 | |
| 11 | pub const RunTranslatedCContext = struct { |
| 12 | b: *build.Builder, |
| 13 | step: *build.Step, |
| 14 | test_index: usize, |
| 15 | test_filter: ?[]const u8, |
| 16 | |
| 17 | const TestCase = struct { |
| 18 | name: []const u8, |
| 19 | sources: ArrayList(SourceFile), |
| 20 | expected_stdout: []const u8, |
| 21 | allow_warnings: bool, |
| 22 | |
| 23 | const SourceFile = struct { |
| 24 | filename: []const u8, |
| 25 | source: []const u8, |
| 26 | }; |
| 27 | |
| 28 | pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void { |
| 29 | self.sources.append(SourceFile{ |
| 30 | .filename = filename, |
| 31 | .source = source, |
| 32 | }) catch unreachable; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | const DoEverythingStep = struct { |
| 37 | step: build.Step, |
| 38 | context: *RunTranslatedCContext, |
| 39 | name: []const u8, |
| 40 | case: *const TestCase, |
| 41 | test_index: usize, |
| 42 | |
| 43 | pub fn create( |
| 44 | context: *RunTranslatedCContext, |
| 45 | name: []const u8, |
| 46 | case: *const TestCase, |
| 47 | ) *DoEverythingStep { |
| 48 | const allocator = context.b.allocator; |
| 49 | const ptr = allocator.create(DoEverythingStep) catch unreachable; |
| 50 | ptr.* = DoEverythingStep{ |
| 51 | .context = context, |
| 52 | .name = name, |
| 53 | .case = case, |
| 54 | .test_index = context.test_index, |
| 55 | .step = build.Step.init("RunTranslatedC", allocator, make), |
| 56 | }; |
| 57 | context.test_index += 1; |
| 58 | return ptr; |
| 59 | } |
| 60 | |
| 61 | fn make(step: *build.Step) !void { |
| 62 | const self = @fieldParentPtr(DoEverythingStep, "step", step); |
| 63 | const b = self.context.b; |
| 64 | |
| 65 | warn("Test {}/{} {}...", .{ self.test_index + 1, self.context.test_index, self.name }); |
| 66 | // translate from c to zig |
| 67 | const translated_c_code = blk: { |
| 68 | var zig_args = ArrayList([]const u8).init(b.allocator); |
| 69 | defer zig_args.deinit(); |
| 70 | |
| 71 | const rel_c_filename = try fs.path.join(b.allocator, &[_][]const u8{ |
| 72 | b.cache_root, |
| 73 | self.case.sources.toSliceConst()[0].filename, |
| 74 | }); |
| 75 | |
| 76 | try zig_args.append(b.zig_exe); |
| 77 | try zig_args.append("translate-c"); |
| 78 | try zig_args.append("-lc"); |
| 79 | try zig_args.append(b.pathFromRoot(rel_c_filename)); |
| 80 | |
| 81 | break :blk try b.exec(zig_args.toSliceConst()); |
| 82 | }; |
| 83 | |
| 84 | // write stdout to a file |
| 85 | |
| 86 | const translated_c_path = try fs.path.join(b.allocator, |
| 87 | &[_][]const u8{ b.cache_root, "translated_c.zig" }); |
| 88 | try fs.cwd().writeFile(translated_c_path, translated_c_code); |
| 89 | |
| 90 | // zig run the result |
| 91 | const run_stdout = blk: { |
| 92 | var zig_args = ArrayList([]const u8).init(b.allocator); |
| 93 | defer zig_args.deinit(); |
| 94 | |
| 95 | try zig_args.append(b.zig_exe); |
| 96 | try zig_args.append("-lc"); |
| 97 | try zig_args.append("run"); |
| 98 | try zig_args.append(translated_c_path); |
| 99 | |
| 100 | break :blk try b.exec(zig_args.toSliceConst()); |
| 101 | }; |
| 102 | // compare stdout |
| 103 | if (!mem.eql(u8, self.case.expected_stdout, run_stdout)) { |
| 104 | warn( |
| 105 | \\ |
| 106 | \\========= Expected this output: ========= |
| 107 | \\{} |
| 108 | \\========= But found: ==================== |
| 109 | \\{} |
| 110 | \\ |
| 111 | , .{ self.case.expected_stdout, run_stdout }); |
| 112 | return error.TestFailed; |
| 113 | } |
| 114 | |
| 115 | warn("OK\n", .{}); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | pub fn create( |
| 120 | self: *RunTranslatedCContext, |
| 121 | allow_warnings: bool, |
| 122 | filename: []const u8, |
| 123 | name: []const u8, |
| 124 | source: []const u8, |
| 125 | expected_stdout: []const u8, |
| 126 | ) *TestCase { |
| 127 | const tc = self.b.allocator.create(TestCase) catch unreachable; |
| 128 | tc.* = TestCase{ |
| 129 | .name = name, |
| 130 | .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), |
| 131 | .expected_stdout = expected_stdout, |
| 132 | .allow_warnings = allow_warnings, |
| 133 | }; |
| 134 | |
| 135 | tc.addSourceFile(filename, source); |
| 136 | return tc; |
| 137 | } |
| 138 | |
| 139 | pub fn add( |
| 140 | self: *RunTranslatedCContext, |
| 141 | name: []const u8, |
| 142 | source: []const u8, |
| 143 | expected_stdout: []const u8, |
| 144 | ) void { |
| 145 | const tc = self.create(false, "source.c", name, source, expected_stdout); |
| 146 | self.addCase(tc); |
| 147 | } |
| 148 | |
| 149 | pub fn addAllowWarnings( |
| 150 | self: *RunTranslatedCContext, |
| 151 | name: []const u8, |
| 152 | source: []const u8, |
| 153 | expected_stdout: []const u8, |
| 154 | ) void { |
| 155 | const tc = self.create(true, "source.c", name, source, expected_stdout); |
| 156 | self.addCase(tc); |
| 157 | } |
| 158 | |
| 159 | pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void { |
| 160 | const b = self.b; |
| 161 | |
| 162 | const annotated_case_name = fmt.allocPrint(self.b.allocator, "run-translated-c {}", .{ case.name }) catch unreachable; |
| 163 | if (self.test_filter) |filter| { |
| 164 | if (mem.indexOf(u8, annotated_case_name, filter) == null) return; |
| 165 | } |
| 166 | |
| 167 | const do_everything_step = DoEverythingStep.create(self, annotated_case_name, case); |
| 168 | self.step.dependOn(&do_everything_step.step); |
| 169 | |
| 170 | for (case.sources.toSliceConst()) |src_file| { |
| 171 | const expanded_src_path = fs.path.join( |
| 172 | b.allocator, |
| 173 | &[_][]const u8{ b.cache_root, src_file.filename }, |
| 174 | ) catch unreachable; |
| 175 | const write_src = b.addWriteFile(expanded_src_path, src_file.source); |
| 176 | do_everything_step.step.dependOn(&write_src.step); |
| 177 | } |
| 178 | } |
| 179 | }; |
| 180 | |