authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-06-15 17:56:28-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-06-15 20:33:39-04:00
log1e5945d0a926b74517b1bed3cdab00f244aa592c
treecdb36bdb67184af8723afd429606a1c808a623fe
parent71dca252a575a6a09e16dfc8faf8b704e847eb87
signature Commit is signed but in an unrecognized format.

Stage2/Testing: remove ZIRTransformCase


1 files changed, 2 insertions(+), 157 deletions(-)

src-self-hosted/test.zig+2-157
...@@ -25,14 +25,9 @@ pub const TestContext = struct {...@@ -25,14 +25,9 @@ pub const TestContext = struct {
25 // TODO: remove these. They are deprecated.25 // TODO: remove these. They are deprecated.
26 zir_cmp_output_cases: std.ArrayList(ZIRCompareOutputCase),26 zir_cmp_output_cases: std.ArrayList(ZIRCompareOutputCase),
27 // TODO: remove27 // TODO: remove
28 zir_transform_cases: std.ArrayList(ZIRTransformCase),
29 // TODO: remove
30 zir_error_cases: std.ArrayList(ZIRErrorCase),28 zir_error_cases: std.ArrayList(ZIRErrorCase),
3129
32 /// TODO: find a way to treat cases as individual tests as far as30 /// TODO: find a way to treat cases as individual tests (shouldn't show "1 test passed" if there are 200 cases)
33 /// `zig test` is concerned. If we have 100 tests, they should *not* be
34 /// considered as *one*. "ZIR" isn't really a *test*, it's a *category* of
35 /// tests.
36 zir_cases: std.ArrayList(ZIRCase),31 zir_cases: std.ArrayList(ZIRCase),
3732
38 // TODO: remove33 // TODO: remove
...@@ -43,37 +38,7 @@ pub const TestContext = struct {...@@ -43,37 +38,7 @@ pub const TestContext = struct {
43 };38 };
4439
45 // TODO: remove40 // TODO: remove
46 pub const ZIRTransformCase = struct {
47 name: []const u8,
48 cross_target: std.zig.CrossTarget,
49 updates: std.ArrayList(Update),
50
51 pub const Update = struct {
52 expected: Expected,
53 src: [:0]const u8,
54 };
55
56 pub const Expected = union(enum) {
57 zir: []const u8,
58 errors: []const []const u8,
59 };
60
61 pub fn addZIR(case: *ZIRTransformCase, src: [:0]const u8, zir_text: []const u8) void {
62 case.updates.append(.{
63 .src = src,
64 .expected = .{ .zir = zir_text },
65 }) catch unreachable;
66 }
6741
68 pub fn addError(case: *ZIRTransformCase, src: [:0]const u8, errors: []const []const u8) void {
69 case.updates.append(.{
70 .src = src,
71 .expected = .{ .errors = errors },
72 }) catch unreachable;
73 }
74 };
75
76 // TODO: remove
77 pub const ZIRErrorCase = struct {42 pub const ZIRErrorCase = struct {
78 name: []const u8,43 name: []const u8,
79 src: [:0]const u8,44 src: [:0]const u8,
...@@ -171,25 +136,6 @@ pub const TestContext = struct {...@@ -171,25 +136,6 @@ pub const TestContext = struct {
171 }) catch unreachable;136 }) catch unreachable;
172 }137 }
173138
174 pub fn addZIRTransform(
175 ctx: *TestContext,
176 name: []const u8,
177 cross_target: std.zig.CrossTarget,
178 src: [:0]const u8,
179 expected_zir: []const u8,
180 ) void {
181 const case = ctx.zir_transform_cases.addOne() catch unreachable;
182 case.* = .{
183 .name = name,
184 .cross_target = cross_target,
185 .updates = std.ArrayList(ZIRTransformCase.Update).init(std.heap.page_allocator),
186 };
187 case.updates.append(.{
188 .src = src,
189 .expected = .{ .zir = expected_zir },
190 }) catch unreachable;
191 }
192
193 pub fn addZIRError(139 pub fn addZIRError(
194 ctx: *TestContext,140 ctx: *TestContext,
195 name: []const u8,141 name: []const u8,
...@@ -237,7 +183,6 @@ pub const TestContext = struct {...@@ -237,7 +183,6 @@ pub const TestContext = struct {
237 const allocator = std.heap.page_allocator;183 const allocator = std.heap.page_allocator;
238 self.* = .{184 self.* = .{
239 .zir_cmp_output_cases = std.ArrayList(ZIRCompareOutputCase).init(allocator),185 .zir_cmp_output_cases = std.ArrayList(ZIRCompareOutputCase).init(allocator),
240 .zir_transform_cases = std.ArrayList(ZIRTransformCase).init(allocator),
241 .zir_error_cases = std.ArrayList(ZIRErrorCase).init(allocator),186 .zir_error_cases = std.ArrayList(ZIRErrorCase).init(allocator),
242 .zir_cases = std.ArrayList(ZIRCase).init(allocator),187 .zir_cases = std.ArrayList(ZIRCase).init(allocator),
243 };188 };
...@@ -245,7 +190,6 @@ pub const TestContext = struct {...@@ -245,7 +190,6 @@ pub const TestContext = struct {
245190
246 fn deinit(self: *TestContext) void {191 fn deinit(self: *TestContext) void {
247 self.zir_cmp_output_cases.deinit();192 self.zir_cmp_output_cases.deinit();
248 self.zir_transform_cases.deinit();
249 for (self.zir_error_cases.items) |e| {193 for (self.zir_error_cases.items) |e| {
250 self.zir_error_cases.allocator.free(e.expected_errors);194 self.zir_error_cases.allocator.free(e.expected_errors);
251 }195 }
...@@ -256,8 +200,7 @@ pub const TestContext = struct {...@@ -256,8 +200,7 @@ pub const TestContext = struct {
256200
257 fn run(self: *TestContext) !void {201 fn run(self: *TestContext) !void {
258 var progress = std.Progress{};202 var progress = std.Progress{};
259 const root_node = try progress.start("zir", self.zir_cmp_output_cases.items.len +203 const root_node = try progress.start("zir", self.zir_cases.items.len);
260 self.zir_transform_cases.items.len);
261 defer root_node.end();204 defer root_node.end();
262205
263 const native_info = try std.zig.system.NativeTargetInfo.detect(std.heap.page_allocator, .{});206 const native_info = try std.zig.system.NativeTargetInfo.detect(std.heap.page_allocator, .{});
...@@ -275,12 +218,6 @@ pub const TestContext = struct {...@@ -275,12 +218,6 @@ pub const TestContext = struct {
275 try self.runOneZIRCmpOutputCase(std.testing.allocator, root_node, case, native_info.target);218 try self.runOneZIRCmpOutputCase(std.testing.allocator, root_node, case, native_info.target);
276 try std.testing.allocator_instance.validate();219 try std.testing.allocator_instance.validate();
277 }220 }
278 for (self.zir_transform_cases.items) |case| {
279 std.testing.base_allocator_instance.reset();
280 const info = try std.zig.system.NativeTargetInfo.detect(std.testing.allocator, case.cross_target);
281 try self.runOneZIRTransformCase(std.testing.allocator, root_node, case, info.target);
282 try std.testing.allocator_instance.validate();
283 }
284 for (self.zir_error_cases.items) |case| {221 for (self.zir_error_cases.items) |case| {
285 std.testing.base_allocator_instance.reset();222 std.testing.base_allocator_instance.reset();
286 const info = try std.zig.system.NativeTargetInfo.detect(std.testing.allocator, case.cross_target);223 const info = try std.zig.system.NativeTargetInfo.detect(std.testing.allocator, case.cross_target);
...@@ -432,98 +369,6 @@ pub const TestContext = struct {...@@ -432,98 +369,6 @@ pub const TestContext = struct {
432 }369 }
433 }370 }
434371
435 fn runOneZIRTransformCase(
436 self: *TestContext,
437 allocator: *Allocator,
438 root_node: *std.Progress.Node,
439 case: ZIRTransformCase,
440 target: std.Target,
441 ) !void {
442 var tmp = std.testing.tmpDir(.{});
443 defer tmp.cleanup();
444
445 var update_node = root_node.start(case.name, case.updates.items.len);
446 update_node.activate();
447 defer update_node.end();
448
449 const tmp_src_path = "test-case.zir";
450 const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path);
451 defer root_pkg.destroy();
452
453 var module = try Module.init(allocator, .{
454 .target = target,
455 .output_mode = .Obj,
456 .optimize_mode = .Debug,
457 .bin_file_dir = tmp.dir,
458 .bin_file_path = "test-case.o",
459 .root_pkg = root_pkg,
460 });
461 defer module.deinit();
462
463 for (case.updates.items) |update| {
464 var prg_node = update_node.start("", 3);
465 prg_node.activate();
466 defer prg_node.end();
467
468 try tmp.dir.writeFile(tmp_src_path, update.src);
469
470 var module_node = prg_node.start("parse/analysis/codegen", null);
471 module_node.activate();
472 try module.update();
473 module_node.end();
474
475 switch (update.expected) {
476 .zir => |expected_zir| {
477 var emit_node = prg_node.start("emit", null);
478 emit_node.activate();
479 var new_zir_module = try zir.emit(allocator, module);
480 defer new_zir_module.deinit(allocator);
481 emit_node.end();
482
483 var write_node = prg_node.start("write", null);
484 write_node.activate();
485 var out_zir = std.ArrayList(u8).init(allocator);
486 defer out_zir.deinit();
487 try new_zir_module.writeToStream(allocator, out_zir.outStream());
488 write_node.end();
489
490 std.testing.expectEqualSlices(u8, expected_zir, out_zir.items);
491 },
492 .errors => |expected_errors| {
493 var all_errors = try module.getAllErrorsAlloc();
494 defer all_errors.deinit(module.allocator);
495 for (expected_errors) |expected_error| {
496 for (all_errors.list) |full_err_msg| {
497 const text = try std.fmt.allocPrint(allocator, ":{}:{}: error: {}", .{
498 full_err_msg.line + 1,
499 full_err_msg.column + 1,
500 full_err_msg.msg,
501 });
502 defer allocator.free(text);
503 if (std.mem.eql(u8, text, expected_error)) {
504 break;
505 }
506 } else {
507 std.debug.warn(
508 "{}\nExpected this error:\n================\n{}\n================\nBut found these errors:\n================\n",
509 .{ case.name, expected_error },
510 );
511 for (all_errors.list) |full_err_msg| {
512 std.debug.warn(":{}:{}: error: {}\n", .{
513 full_err_msg.line + 1,
514 full_err_msg.column + 1,
515 full_err_msg.msg,
516 });
517 }
518 std.debug.warn("================\nTest failed\n", .{});
519 std.process.exit(1);
520 }
521 }
522 },
523 }
524 }
525 }
526
527 fn runOneZIRErrorCase(372 fn runOneZIRErrorCase(
528 self: *TestContext,373 self: *TestContext,
529 allocator: *Allocator,374 allocator: *Allocator,