| ... | ... | @@ -21,9 +21,10 @@ const ErrorMsg = struct { |
| 21 | 21 | }; |
| 22 | 22 | |
| 23 | 23 | pub const TestContext = struct { |
| 24 | | zir_cases: std.ArrayList(Case), |
| 24 | /// TODO: find a way to treat cases as individual tests (shouldn't show "1 test passed" if there are 200 cases) |
| 25 | cases: std.ArrayList(Case), |
| 25 | 26 | |
| 26 | | pub const ZIRUpdate = struct { |
| 27 | pub const Update = struct { |
| 27 | 28 | /// The input to the current update. We simulate an incremental update |
| 28 | 29 | /// with the file's contents changed to this value each update. |
| 29 | 30 | /// |
| ... | ... | @@ -40,67 +41,70 @@ pub const TestContext = struct { |
| 40 | 41 | /// fails to compile, and for the expected reasons. |
| 41 | 42 | /// A slice containing the expected errors *in sequential order*. |
| 42 | 43 | Error: []const ErrorMsg, |
| 43 | | /// An execution update compiles and runs the input ZIR, feeding in |
| 44 | | /// provided input and ensuring that the stdout match what is expected. |
| 44 | /// An execution update compiles and runs the input, testing the |
| 45 | /// stdout against the expected results |
| 45 | 46 | Execution: []const u8, |
| 46 | 47 | }, |
| 47 | 48 | }; |
| 48 | 49 | |
| 49 | | /// A Case consists of a set of *updates*. A update can transform ZIR, |
| 50 | | /// compile it, ensure that compilation fails, and more. The same Module is |
| 51 | | /// used for each update, so each update's source is treated as a single file |
| 52 | | /// being updated by the test harness and incrementally compiled. |
| 50 | pub const TestType = enum { |
| 51 | Zig, |
| 52 | ZIR, |
| 53 | }; |
| 54 | |
| 55 | /// A Case consists of a set of *updates*. The same Module is used for each |
| 56 | /// update, so each update's source is treated as a single file being |
| 57 | /// updated by the test harness and incrementally compiled. |
| 53 | 58 | pub const Case = struct { |
| 54 | 59 | name: []const u8, |
| 55 | | /// The platform the ZIR targets. For non-native platforms, an emulator |
| 60 | /// The platform the test targets. For non-native platforms, an emulator |
| 56 | 61 | /// such as QEMU is required for tests to complete. |
| 57 | 62 | target: std.zig.CrossTarget, |
| 58 | | updates: std.ArrayList(ZIRUpdate), |
| 59 | 63 | output_mode: std.builtin.OutputMode, |
| 60 | | /// Either ".zir" or ".zig" |
| 61 | | extension: [4]u8, |
| 64 | updates: std.ArrayList(Update), |
| 65 | @"type": TestType, |
| 62 | 66 | |
| 63 | 67 | /// Adds a subcase in which the module is updated with new ZIR, and the |
| 64 | 68 | /// resulting ZIR is validated. |
| 65 | | pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) void { |
| 66 | | self.updates.append(.{ |
| 69 | pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) !void { |
| 70 | try self.updates.append(.{ |
| 67 | 71 | .src = src, |
| 68 | 72 | .case = .{ .Transformation = result }, |
| 69 | | }) catch unreachable; |
| 73 | }); |
| 70 | 74 | } |
| 71 | 75 | |
| 72 | | pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) void { |
| 73 | | self.updates.append(.{ |
| 76 | pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) !void { |
| 77 | try self.updates.append(.{ |
| 74 | 78 | .src = src, |
| 75 | 79 | .case = .{ .Execution = result }, |
| 76 | | }) catch unreachable; |
| 80 | }); |
| 77 | 81 | } |
| 78 | 82 | |
| 79 | 83 | /// Adds a subcase in which the module is updated with invalid ZIR, and |
| 80 | 84 | /// ensures that compilation fails for the expected reasons. |
| 81 | 85 | /// |
| 82 | 86 | /// Errors must be specified in sequential order. |
| 83 | | pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) void { |
| 84 | | var array = self.updates.allocator.alloc(ErrorMsg, errors.len) catch unreachable; |
| 87 | pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) !void { |
| 88 | var array = try self.updates.allocator.alloc(ErrorMsg, errors.len); |
| 85 | 89 | for (errors) |e, i| { |
| 86 | 90 | if (e[0] != ':') { |
| 87 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 91 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 88 | 92 | } |
| 89 | 93 | var cur = e[1..]; |
| 90 | 94 | var line_index = std.mem.indexOf(u8, cur, ":"); |
| 91 | 95 | if (line_index == null) { |
| 92 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 96 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 93 | 97 | } |
| 94 | 98 | const line = std.fmt.parseInt(u32, cur[0..line_index.?], 10) catch @panic("Unable to parse line number"); |
| 95 | 99 | cur = cur[line_index.? + 1 ..]; |
| 96 | 100 | const column_index = std.mem.indexOf(u8, cur, ":"); |
| 97 | 101 | if (column_index == null) { |
| 98 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 102 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 99 | 103 | } |
| 100 | 104 | const column = std.fmt.parseInt(u32, cur[0..column_index.?], 10) catch @panic("Unable to parse column number"); |
| 101 | 105 | cur = cur[column_index.? + 2 ..]; |
| 102 | 106 | if (!std.mem.eql(u8, cur[0..7], "error: ")) { |
| 103 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 107 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 104 | 108 | } |
| 105 | 109 | const msg = cur[7..]; |
| 106 | 110 | |
| ... | ... | @@ -114,125 +118,87 @@ pub const TestContext = struct { |
| 114 | 118 | .column = column - 1, |
| 115 | 119 | }; |
| 116 | 120 | } |
| 117 | | self.updates.append(.{ .src = src, .case = .{ .Error = array } }) catch unreachable; |
| 121 | try self.updates.append(.{ .src = src, .case = .{ .Error = array } }); |
| 118 | 122 | } |
| 119 | 123 | }; |
| 120 | 124 | |
| 121 | | pub fn addExeZIR( |
| 122 | | ctx: *TestContext, |
| 123 | | name: []const u8, |
| 124 | | target: std.zig.CrossTarget, |
| 125 | | ) *Case { |
| 126 | | const case = Case{ |
| 127 | | .name = name, |
| 128 | | .target = target, |
| 129 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 130 | | .output_mode = .Exe, |
| 131 | | .extension = ".zir".*, |
| 132 | | }; |
| 133 | | ctx.zir_cases.append(case) catch unreachable; |
| 134 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 135 | | } |
| 136 | | |
| 137 | | pub fn addObjZIR( |
| 138 | | ctx: *TestContext, |
| 139 | | name: []const u8, |
| 140 | | target: std.zig.CrossTarget, |
| 141 | | ) *Case { |
| 142 | | const case = Case{ |
| 143 | | .name = name, |
| 144 | | .target = target, |
| 145 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 146 | | .output_mode = .Obj, |
| 147 | | .extension = ".zir".*, |
| 148 | | }; |
| 149 | | ctx.zir_cases.append(case) catch unreachable; |
| 150 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 151 | | } |
| 152 | | |
| 153 | 125 | pub fn addExe( |
| 154 | 126 | ctx: *TestContext, |
| 155 | 127 | name: []const u8, |
| 156 | 128 | target: std.zig.CrossTarget, |
| 157 | | ) *Case { |
| 129 | T: TestType, |
| 130 | ) !*Case { |
| 158 | 131 | const case = Case{ |
| 159 | 132 | .name = name, |
| 160 | 133 | .target = target, |
| 161 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 134 | .updates = std.ArrayList(Update).init(ctx.cases.allocator), |
| 162 | 135 | .output_mode = .Exe, |
| 163 | | .extension = ".zig".*, |
| 136 | .@"type" = T, |
| 164 | 137 | }; |
| 165 | | ctx.zir_cases.append(case) catch unreachable; |
| 166 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 138 | try ctx.cases.append(case); |
| 139 | return &ctx.cases.items[ctx.cases.items.len - 1]; |
| 167 | 140 | } |
| 168 | 141 | |
| 169 | 142 | pub fn addObj( |
| 170 | 143 | ctx: *TestContext, |
| 171 | 144 | name: []const u8, |
| 172 | 145 | target: std.zig.CrossTarget, |
| 173 | | ) *Case { |
| 174 | | const case = Case{ |
| 146 | T: TestType, |
| 147 | ) !*Case { |
| 148 | try ctx.cases.append(Case{ |
| 175 | 149 | .name = name, |
| 176 | 150 | .target = target, |
| 177 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 151 | .updates = std.ArrayList(Update).init(ctx.cases.allocator), |
| 178 | 152 | .output_mode = .Obj, |
| 179 | | .extension = ".zig".*, |
| 180 | | }; |
| 181 | | ctx.zir_cases.append(case) catch unreachable; |
| 182 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 183 | | } |
| 184 | | |
| 185 | | pub fn addZIRCompareOutput( |
| 186 | | ctx: *TestContext, |
| 187 | | name: []const u8, |
| 188 | | src: [:0]const u8, |
| 189 | | expected_stdout: []const u8, |
| 190 | | ) void { |
| 191 | | var c = ctx.addExeZIR(name, .{}); |
| 192 | | c.addCompareOutput(src, expected_stdout); |
| 153 | .@"type" = T, |
| 154 | }); |
| 155 | return &ctx.cases.items[ctx.cases.items.len - 1]; |
| 193 | 156 | } |
| 194 | 157 | |
| 195 | 158 | pub fn addCompareOutput( |
| 196 | 159 | ctx: *TestContext, |
| 197 | 160 | name: []const u8, |
| 161 | T: TestType, |
| 198 | 162 | src: [:0]const u8, |
| 199 | 163 | expected_stdout: []const u8, |
| 200 | | ) void { |
| 201 | | var c = ctx.addExe(name, .{}); |
| 202 | | c.addCompareOutput(src, expected_stdout); |
| 164 | ) !void { |
| 165 | var c = try ctx.addExe(name, .{}, T); |
| 166 | try c.addCompareOutput(src, expected_stdout); |
| 203 | 167 | } |
| 204 | 168 | |
| 205 | | pub fn addZIRTransform( |
| 169 | pub fn addTransform( |
| 206 | 170 | ctx: *TestContext, |
| 207 | 171 | name: []const u8, |
| 208 | 172 | target: std.zig.CrossTarget, |
| 173 | T: TestType, |
| 209 | 174 | src: [:0]const u8, |
| 210 | 175 | result: [:0]const u8, |
| 211 | | ) void { |
| 212 | | var c = ctx.addObjZIR(name, target); |
| 213 | | c.addTransform(src, result); |
| 176 | ) !void { |
| 177 | var c = try ctx.addObj(name, target, T); |
| 178 | try c.addTransform(src, result); |
| 214 | 179 | } |
| 215 | 180 | |
| 216 | | pub fn addZIRError( |
| 181 | pub fn addError( |
| 217 | 182 | ctx: *TestContext, |
| 218 | 183 | name: []const u8, |
| 219 | 184 | target: std.zig.CrossTarget, |
| 185 | T: TestType, |
| 220 | 186 | src: [:0]const u8, |
| 221 | 187 | expected_errors: []const []const u8, |
| 222 | | ) void { |
| 223 | | var c = ctx.addObjZIR(name, target); |
| 224 | | c.addError(src, expected_errors); |
| 188 | ) !void { |
| 189 | var c = try ctx.addObj(name, target, T); |
| 190 | try c.addError(src, expected_errors); |
| 225 | 191 | } |
| 226 | 192 | |
| 227 | 193 | fn init() TestContext { |
| 228 | 194 | const allocator = std.heap.page_allocator; |
| 229 | 195 | return .{ |
| 230 | | .zir_cases = std.ArrayList(Case).init(allocator), |
| 196 | .cases = std.ArrayList(Case).init(allocator), |
| 231 | 197 | }; |
| 232 | 198 | } |
| 233 | 199 | |
| 234 | 200 | fn deinit(self: *TestContext) void { |
| 235 | | for (self.zir_cases.items) |c| { |
| 201 | for (self.cases.items) |c| { |
| 236 | 202 | for (c.updates.items) |u| { |
| 237 | 203 | if (u.case == .Error) { |
| 238 | 204 | c.updates.allocator.free(u.case.Error); |
| ... | ... | @@ -240,18 +206,18 @@ pub const TestContext = struct { |
| 240 | 206 | } |
| 241 | 207 | c.updates.deinit(); |
| 242 | 208 | } |
| 243 | | self.zir_cases.deinit(); |
| 209 | self.cases.deinit(); |
| 244 | 210 | self.* = undefined; |
| 245 | 211 | } |
| 246 | 212 | |
| 247 | 213 | fn run(self: *TestContext) !void { |
| 248 | 214 | var progress = std.Progress{}; |
| 249 | | const root_node = try progress.start("zir", self.zir_cases.items.len); |
| 215 | const root_node = try progress.start("tests", self.cases.items.len); |
| 250 | 216 | defer root_node.end(); |
| 251 | 217 | |
| 252 | 218 | const native_info = try std.zig.system.NativeTargetInfo.detect(std.heap.page_allocator, .{}); |
| 253 | 219 | |
| 254 | | for (self.zir_cases.items) |case| { |
| 220 | for (self.cases.items) |case| { |
| 255 | 221 | std.testing.base_allocator_instance.reset(); |
| 256 | 222 | |
| 257 | 223 | var prg_node = root_node.start(case.name, case.updates.items.len); |
| ... | ... | @@ -267,17 +233,19 @@ pub const TestContext = struct { |
| 267 | 233 | } |
| 268 | 234 | } |
| 269 | 235 | |
| 270 | | fn runOneCase(self: *TestContext, allocator: *Allocator, prg_node: *std.Progress.Node, case: Case, target: std.Target) !void { |
| 236 | fn runOneCase(self: *TestContext, allocator: *Allocator, root_node: *std.Progress.Node, case: Case, target: std.Target) !void { |
| 271 | 237 | var tmp = std.testing.tmpDir(.{}); |
| 272 | 238 | defer tmp.cleanup(); |
| 273 | 239 | |
| 274 | | const root_name = "test_case"; |
| 275 | | const tmp_src_path = try std.fmt.allocPrint(allocator, "{}{}", .{ root_name, case.extension }); |
| 276 | | defer allocator.free(tmp_src_path); |
| 240 | const tmp_src_path = if (case.type == .Zig) "test_case.zig" else if (case.type == .ZIR) "test_case.zir" else unreachable; |
| 277 | 241 | const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path); |
| 278 | 242 | defer root_pkg.destroy(); |
| 279 | 243 | |
| 280 | | const bin_name = try std.zig.binNameAlloc(allocator, root_name, target, case.output_mode, null); |
| 244 | var prg_node = root_node.start(case.name, case.updates.items.len); |
| 245 | prg_node.activate(); |
| 246 | defer prg_node.end(); |
| 247 | |
| 248 | const bin_name = try std.zig.binNameAlloc(allocator, "test_case", target, case.output_mode, null); |
| 281 | 249 | defer allocator.free(bin_name); |
| 282 | 250 | |
| 283 | 251 | var module = try Module.init(allocator, .{ |