-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwrite_spec_tests.zig
More file actions
215 lines (191 loc) · 6.8 KB
/
Copy pathwrite_spec_tests.zig
File metadata and controls
215 lines (191 loc) · 6.8 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const std = @import("std");
const ForkSeq = @import("config").ForkSeq;
const spec_test_options = @import("spec_test_options");
const RunnerKind = @import("./runner_kind.zig").RunnerKind;
const supported_forks = [_]ForkSeq{
.phase0,
.altair,
.bellatrix,
.capella,
.deneb,
.electra,
.fulu,
};
const supported_test_runners = [_]RunnerKind{
.merkle_proof,
.operations,
.rewards,
.sanity,
.epoch_processing,
.fork,
.transition,
.random,
.finality,
};
const presets = [_][]const u8{
"minimal/tests/minimal",
"mainnet/tests/mainnet",
};
const Key = struct {
suite: []const u8,
case: []const u8,
fn eql(x: Key, y: Key) bool {
return std.mem.eql(u8, x.suite, y.suite) and std.mem.eql(u8, x.case, y.case);
}
fn lessThan(_: void, x: Key, y: Key) bool {
const o = std.mem.order(u8, x.suite, y.suite);
return if (o == .eq) std.mem.lessThan(u8, x.case, y.case) else o == .lt;
}
};
fn TestWriter(comptime kind: RunnerKind) type {
return switch (kind) {
.merkle_proof => @import("./writer/merkle_proof.zig"),
.operations => @import("./writer/operations.zig"),
.rewards => @import("./writer/rewards.zig"),
.sanity => @import("./writer/sanity.zig"),
.epoch_processing => @import("./writer/epoch_processing.zig"),
.fork => @import("./writer/fork.zig"),
.transition => @import("./writer/transition.zig"),
.random => @import("./writer/random.zig"),
.finality => @import("./writer/finality.zig"),
else => @compileError("Unsupported test runner"),
};
}
pub fn main(init: std.process.Init) !void {
const io = init.io;
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
const test_case_dir = "test/spec/test_case/";
try std.Io.Dir.createDirPath(.cwd(), io, test_case_dir);
inline for (supported_test_runners) |kind| {
const test_case_file = test_case_dir ++ @tagName(kind) ++ "_tests.zig";
const out = try std.Io.Dir.createFile(.cwd(), io, test_case_file, .{});
defer out.close(io);
var write_buf: [8 * 1024]u8 = undefined;
var writer = out.writer(io, &write_buf);
try writeTests(io, allocator, &supported_forks, kind, &writer.interface);
try writer.flush();
}
{
const test_root_file = "test/spec/root.zig";
try std.Io.Dir.createDirPath(.cwd(), io, "test/spec");
const out = try std.Io.Dir.createFile(.cwd(), io, test_root_file, .{});
defer out.close(io);
var write_buf: [8 * 1024]u8 = undefined;
var writer = out.writer(io, &write_buf);
try writeTestRoot(&supported_test_runners, &writer.interface);
try writer.flush();
}
}
pub fn writeTestRoot(comptime kinds: []const RunnerKind, writer: *std.Io.Writer) !void {
try writer.print(
\\// This file is generated by write_spec_tests.zig.
\\// Do not commit changes by hand.
\\
\\const testing = @import("std").testing;
\\
\\comptime {{
\\
, .{});
inline for (kinds) |kind| {
try writer.print(
\\ testing.refAllDecls(@import("./test_case/{s}_tests.zig"));
\\
, .{@tagName(kind)});
}
try writer.print(
\\}}
\\
, .{});
}
pub fn writeTests(
io: std.Io,
allocator: std.mem.Allocator,
comptime forks: []const ForkSeq,
comptime kind: RunnerKind,
writer: *std.Io.Writer,
) !void {
@setEvalBranchQuota(8000);
try TestWriter(kind).writeHeader(writer);
var root_dir = try std.Io.Dir.openDir(.cwd(), io, spec_test_options.spec_test_out_dir ++ "/" ++ spec_test_options.spec_test_version, .{});
defer root_dir.close(io);
inline for (forks) |fork| {
const fork_path = @tagName(fork) ++ "/" ++ @tagName(kind);
inline for (TestWriter(kind).handlers) |handler| {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const a = arena.allocator();
var keys: std.ArrayListUnmanaged(Key) = .empty;
for (presets) |preset| {
try collectCases(io, a, root_dir, preset, fork_path, comptime handler.suiteName(), comptime kind.hasSuiteCase(), &keys);
}
std.mem.sortUnstable(Key, keys.items, {}, Key.lessThan);
var write_idx: usize = 0;
for (keys.items) |k| {
if (write_idx > 0 and keys.items[write_idx - 1].eql(k)) continue;
keys.items[write_idx] = k;
write_idx += 1;
}
keys.items.len = write_idx;
for (keys.items) |k| {
if (comptime kind.hasSuiteCase()) {
try TestWriter(kind).writeTest(writer, fork, handler, k.suite, k.case);
} else {
try TestWriter(kind).writeTest(writer, fork, handler, k.case);
}
}
}
}
}
fn collectCases(
io: std.Io,
allocator: std.mem.Allocator,
root: std.Io.Dir,
preset: []const u8,
fork_path: []const u8,
suite_name: []const u8,
has_suite_case: bool,
out: *std.ArrayListUnmanaged(Key),
) !void {
var preset_dir = root.openDir(io, preset, .{}) catch |err| switch (err) {
error.FileNotFound => return,
else => |e| return e,
};
defer preset_dir.close(io);
var fork_dir = preset_dir.openDir(io, fork_path, .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => return,
else => |e| return e,
};
defer fork_dir.close(io);
var handler_dir = fork_dir.openDir(io, suite_name, .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => return,
else => |e| return e,
};
defer handler_dir.close(io);
var iter = handler_dir.iterate();
while (try iter.next(io)) |entry| {
if (entry.kind != .directory) continue;
if (has_suite_case) {
var suite_dir = handler_dir.openDir(io, entry.name, .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => continue,
else => |e| return e,
};
defer suite_dir.close(io);
const suite = try allocator.dupe(u8, entry.name);
var case_iter = suite_dir.iterate();
while (try case_iter.next(io)) |case_entry| {
if (case_entry.kind != .directory) continue;
try out.append(allocator, .{
.suite = suite,
.case = try allocator.dupe(u8, case_entry.name),
});
}
} else {
try out.append(allocator, .{
.suite = "",
.case = try allocator.dupe(u8, entry.name),
});
}
}
}