-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathCompareOutput.zig
176 lines (151 loc) · 6.01 KB
/
CompareOutput.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! This is the implementation of the test harness.
//! For the actual test cases, see test/compare_output.zig.
b: *std.Build,
step: *std.Build.Step,
test_index: usize,
test_filters: []const []const u8,
optimize_modes: []const OptimizeMode,
const Special = enum {
None,
Asm,
RuntimeSafety,
};
const TestCase = struct {
name: []const u8,
sources: ArrayList(SourceFile),
expected_output: []const u8,
link_libc: bool,
special: Special,
cli_args: []const []const u8,
const SourceFile = struct {
filename: []const u8,
source: []const u8,
};
pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
self.sources.append(SourceFile{
.filename = filename,
.source = source,
}) catch @panic("OOM");
}
pub fn setCommandLineArgs(self: *TestCase, args: []const []const u8) void {
self.cli_args = args;
}
};
pub fn createExtra(self: *CompareOutput, name: []const u8, source: []const u8, expected_output: []const u8, special: Special) TestCase {
var tc = TestCase{
.name = name,
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
.expected_output = expected_output,
.link_libc = false,
.special = special,
.cli_args = &[_][]const u8{},
};
const root_src_name = if (special == Special.Asm) "source.s" else "source.zig";
tc.addSourceFile(root_src_name, source);
return tc;
}
pub fn create(self: *CompareOutput, name: []const u8, source: []const u8, expected_output: []const u8) TestCase {
return createExtra(self, name, source, expected_output, Special.None);
}
pub fn addC(self: *CompareOutput, name: []const u8, source: []const u8, expected_output: []const u8) void {
var tc = self.create(name, source, expected_output);
tc.link_libc = true;
self.addCase(tc);
}
pub fn add(self: *CompareOutput, name: []const u8, source: []const u8, expected_output: []const u8) void {
const tc = self.create(name, source, expected_output);
self.addCase(tc);
}
pub fn addAsm(self: *CompareOutput, name: []const u8, source: []const u8, expected_output: []const u8) void {
const tc = self.createExtra(name, source, expected_output, Special.Asm);
self.addCase(tc);
}
pub fn addRuntimeSafety(self: *CompareOutput, name: []const u8, source: []const u8) void {
const tc = self.createExtra(name, source, undefined, Special.RuntimeSafety);
self.addCase(tc);
}
pub fn addCase(self: *CompareOutput, case: TestCase) void {
const b = self.b;
const write_src = b.addWriteFiles();
const first_src = case.sources.items[0];
const first_file = write_src.add(first_src.filename, first_src.source);
for (case.sources.items[1..]) |src_file| {
_ = write_src.add(src_file.filename, src_file.source);
}
switch (case.special) {
Special.Asm => {
const annotated_case_name = b.fmt("run assemble-and-link {s}", .{
case.name,
});
for (self.test_filters) |test_filter| {
if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
} else if (self.test_filters.len > 0) return;
const exe = b.addExecutable(.{
.name = "test",
.root_module = b.createModule(.{
.root_source_file = null,
.target = b.graph.host,
.optimize = .Debug,
}),
});
exe.root_module.addAssemblyFile(first_file);
const run = b.addRunArtifact(exe);
run.setName(annotated_case_name);
run.addArgs(case.cli_args);
run.expectStdOutEqual(case.expected_output);
self.step.dependOn(&run.step);
},
Special.None => {
for (self.optimize_modes) |optimize| {
const annotated_case_name = b.fmt("run compare-output {s} ({s})", .{
case.name, @tagName(optimize),
});
for (self.test_filters) |test_filter| {
if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
} else if (self.test_filters.len > 0) return;
const exe = b.addExecutable(.{
.name = "test",
.root_module = b.createModule(.{
.root_source_file = first_file,
.optimize = optimize,
.target = b.graph.host,
}),
});
if (case.link_libc) exe.root_module.link_libc = true;
const run = b.addRunArtifact(exe);
run.setName(annotated_case_name);
run.addArgs(case.cli_args);
run.expectStdOutEqual(case.expected_output);
self.step.dependOn(&run.step);
}
},
Special.RuntimeSafety => {
// TODO iterate over self.optimize_modes and test this in both
// debug and release safe mode
const annotated_case_name = b.fmt("run safety {s}", .{case.name});
for (self.test_filters) |test_filter| {
if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
} else if (self.test_filters.len > 0) return;
const exe = b.addExecutable(.{
.name = "test",
.root_module = b.createModule(.{
.root_source_file = first_file,
.target = b.graph.host,
.optimize = .Debug,
}),
});
if (case.link_libc) exe.root_module.link_libc = true;
const run = b.addRunArtifact(exe);
run.setName(annotated_case_name);
run.addArgs(case.cli_args);
run.expectExitCode(126);
self.step.dependOn(&run.step);
},
}
}
const CompareOutput = @This();
const std = @import("std");
const ArrayList = std.ArrayList;
const mem = std.mem;
const fs = std.fs;
const OptimizeMode = std.builtin.OptimizeMode;