mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
Merge branch 'main' of github.com:ratfactor/ziglings
This commit is contained in:
commit
f2b3e93402
5 changed files with 214 additions and 141 deletions
36
.github/workflows/ci.yml
vendored
36
.github/workflows/ci.yml
vendored
|
@ -27,21 +27,21 @@ jobs:
|
||||||
- name: Check compatibility with old Zig compilers
|
- name: Check compatibility with old Zig compilers
|
||||||
run: ci/compat.sh
|
run: ci/compat.sh
|
||||||
|
|
||||||
# test:
|
test:
|
||||||
# name: Unit Tests
|
name: Unit Tests
|
||||||
# strategy:
|
strategy:
|
||||||
# matrix:
|
matrix:
|
||||||
# os: [ubuntu-latest, windows-latest, macos-latest]
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||||
# runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
# timeout-minutes: 30
|
timeout-minutes: 30
|
||||||
# steps:
|
steps:
|
||||||
# - name: Checkout
|
- name: Checkout
|
||||||
# uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
#
|
|
||||||
# - name: Setup Zig
|
- name: Setup Zig
|
||||||
# uses: goto-bus-stop/setup-zig@v2
|
uses: goto-bus-stop/setup-zig@v2
|
||||||
# with:
|
with:
|
||||||
# version: master
|
version: master
|
||||||
#
|
|
||||||
# - name: Run unit tests
|
- name: Run unit tests
|
||||||
# run: zig build test
|
run: zig build test
|
||||||
|
|
204
build.zig
204
build.zig
|
@ -210,9 +210,8 @@ pub fn build(b: *Build) !void {
|
||||||
}
|
}
|
||||||
ziglings_step.dependOn(prev_step);
|
ziglings_step.dependOn(prev_step);
|
||||||
|
|
||||||
// Disabled, see issue 272
|
const test_step = b.step("test", "Run all the tests");
|
||||||
// const test_step = b.step("test", "Run all the tests");
|
test_step.dependOn(tests.addCliTests(b, &exercises));
|
||||||
// // test_step.dependOn(tests.addCliTests(b, &exercises));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var use_color_escapes = false;
|
var use_color_escapes = false;
|
||||||
|
@ -224,181 +223,199 @@ var reset_text: []const u8 = "";
|
||||||
const ZiglingStep = struct {
|
const ZiglingStep = struct {
|
||||||
step: Step,
|
step: Step,
|
||||||
exercise: Exercise,
|
exercise: Exercise,
|
||||||
builder: *Build,
|
|
||||||
work_path: []const u8,
|
work_path: []const u8,
|
||||||
|
|
||||||
result_messages: []const u8 = "",
|
result_messages: []const u8 = "",
|
||||||
result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty,
|
result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty,
|
||||||
|
|
||||||
pub fn create(builder: *Build, exercise: Exercise, work_path: []const u8) *@This() {
|
pub fn create(b: *Build, exercise: Exercise, work_path: []const u8) *ZiglingStep {
|
||||||
const self = builder.allocator.create(@This()) catch unreachable;
|
const self = b.allocator.create(ZiglingStep) catch @panic("OOM");
|
||||||
self.* = .{
|
self.* = .{
|
||||||
.step = Step.init(Step.Options{ .id = .custom, .name = exercise.main_file, .owner = builder, .makeFn = make }),
|
.step = Step.init(.{
|
||||||
|
.id = .custom,
|
||||||
|
.name = exercise.main_file,
|
||||||
|
.owner = b,
|
||||||
|
.makeFn = make,
|
||||||
|
}),
|
||||||
.exercise = exercise,
|
.exercise = exercise,
|
||||||
.builder = builder,
|
|
||||||
.work_path = work_path,
|
.work_path = work_path,
|
||||||
};
|
};
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make(step: *Step, prog_node: *std.Progress.Node) anyerror!void {
|
fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||||
const self = @fieldParentPtr(@This(), "step", step);
|
const self = @fieldParentPtr(ZiglingStep, "step", step);
|
||||||
|
|
||||||
if (self.exercise.skip) {
|
if (self.exercise.skip) {
|
||||||
print("Skipping {s}\n\n", .{self.exercise.main_file});
|
print("Skipping {s}\n\n", .{self.exercise.main_file});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.makeInternal(prog_node) catch {
|
|
||||||
|
const exe_path = self.compile(prog_node) catch {
|
||||||
if (self.exercise.hint.len > 0) {
|
if (self.exercise.hint.len > 0) {
|
||||||
print("\n{s}HINT: {s}{s}", .{ bold_text, self.exercise.hint, reset_text });
|
print("\n{s}HINT: {s}{s}", .{
|
||||||
|
bold_text, self.exercise.hint, reset_text,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
print("\n{s}Edit exercises/{s} and run this again.{s}", .{ red_text, self.exercise.main_file, reset_text });
|
self.help();
|
||||||
print("\n{s}To continue from this zigling, use this command:{s}\n {s}zig build -Dn={s}{s}\n", .{ red_text, reset_text, bold_text, self.exercise.key(), reset_text });
|
std.os.exit(1);
|
||||||
|
};
|
||||||
|
|
||||||
|
self.run(exe_path, prog_node) catch {
|
||||||
|
if (self.exercise.hint.len > 0) {
|
||||||
|
print("\n{s}HINT: {s}{s}", .{
|
||||||
|
bold_text, self.exercise.hint, reset_text,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.help();
|
||||||
std.os.exit(1);
|
std.os.exit(1);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn makeInternal(self: *@This(), prog_node: *std.Progress.Node) !void {
|
fn run(self: *ZiglingStep, exe_path: []const u8, _: *std.Progress.Node) !void {
|
||||||
print("Compiling {s}...\n", .{self.exercise.main_file});
|
|
||||||
|
|
||||||
const exe_file = try self.doCompile(prog_node);
|
|
||||||
|
|
||||||
resetLine();
|
resetLine();
|
||||||
print("Checking {s}...\n", .{self.exercise.main_file});
|
print("Checking {s}...\n", .{self.exercise.main_file});
|
||||||
|
|
||||||
const cwd = self.builder.build_root.path.?;
|
const b = self.step.owner;
|
||||||
|
|
||||||
const argv = [_][]const u8{exe_file};
|
|
||||||
|
|
||||||
var child = std.ChildProcess.init(&argv, self.builder.allocator);
|
|
||||||
|
|
||||||
child.cwd = cwd;
|
|
||||||
child.env_map = self.builder.env_map;
|
|
||||||
|
|
||||||
child.stdin_behavior = .Inherit;
|
|
||||||
if (self.exercise.check_stdout) {
|
|
||||||
child.stdout_behavior = .Pipe;
|
|
||||||
child.stderr_behavior = .Inherit;
|
|
||||||
} else {
|
|
||||||
child.stdout_behavior = .Inherit;
|
|
||||||
child.stderr_behavior = .Pipe;
|
|
||||||
}
|
|
||||||
|
|
||||||
child.spawn() catch |err| {
|
|
||||||
print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text });
|
|
||||||
return err;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Allow up to 1 MB of stdout capture.
|
// Allow up to 1 MB of stdout capture.
|
||||||
const max_output_len = 1 * 1024 * 1024;
|
const max_output_bytes = 1 * 1024 * 1024;
|
||||||
const output = if (self.exercise.check_stdout)
|
|
||||||
try child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_output_len)
|
|
||||||
else
|
|
||||||
try child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_output_len);
|
|
||||||
|
|
||||||
// At this point stdout is closed, wait for the process to terminate.
|
var result = Child.exec(.{
|
||||||
const term = child.wait() catch |err| {
|
.allocator = b.allocator,
|
||||||
print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text });
|
.argv = &.{exe_path},
|
||||||
|
.cwd = b.build_root.path.?,
|
||||||
|
.cwd_dir = b.build_root.handle,
|
||||||
|
.max_output_bytes = max_output_bytes,
|
||||||
|
}) catch |err| {
|
||||||
|
print("{s}Unable to spawn {s}: {s}{s}\n", .{
|
||||||
|
red_text, exe_path, @errorName(err), reset_text,
|
||||||
|
});
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const raw_output = if (self.exercise.check_stdout)
|
||||||
|
result.stdout
|
||||||
|
else
|
||||||
|
result.stderr;
|
||||||
|
|
||||||
// Make sure it exited cleanly.
|
// Make sure it exited cleanly.
|
||||||
switch (term) {
|
switch (result.term) {
|
||||||
.Exited => |code| {
|
.Exited => |code| {
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ red_text, self.exercise.main_file, code, 0, reset_text });
|
print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, code, 0, reset_text,
|
||||||
|
});
|
||||||
return error.BadExitCode;
|
return error.BadExitCode;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
print("{s}{s} terminated unexpectedly{s}\n", .{ red_text, self.exercise.main_file, reset_text });
|
print("{s}{s} terminated unexpectedly{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, reset_text,
|
||||||
|
});
|
||||||
return error.UnexpectedTermination;
|
return error.UnexpectedTermination;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the output.
|
// Validate the output.
|
||||||
const trimOutput = std.mem.trimRight(u8, output, " \r\n");
|
const output = std.mem.trimRight(u8, raw_output, " \r\n");
|
||||||
const trimExerciseOutput = std.mem.trimRight(u8, self.exercise.output, " \r\n");
|
const exercise_output = std.mem.trimRight(u8, self.exercise.output, " \r\n");
|
||||||
if (!std.mem.eql(u8, trimOutput, trimExerciseOutput)) {
|
if (!std.mem.eql(u8, output, exercise_output)) {
|
||||||
|
const red = red_text;
|
||||||
|
const reset = reset_text;
|
||||||
|
|
||||||
print(
|
print(
|
||||||
\\
|
\\
|
||||||
\\{s}----------- Expected this output -----------{s}
|
\\{s}========= expected this output: =========={s}
|
||||||
\\"{s}"
|
\\{s}
|
||||||
\\{s}----------- but found -----------{s}
|
\\{s}========= but found: ====================={s}
|
||||||
\\"{s}"
|
\\{s}
|
||||||
\\{s}-----------{s}
|
\\{s}=========================================={s}
|
||||||
\\
|
\\
|
||||||
, .{ red_text, reset_text, trimExerciseOutput, red_text, reset_text, trimOutput, red_text, reset_text });
|
, .{ red, reset, exercise_output, red, reset, output, red, reset });
|
||||||
return error.InvalidOutput;
|
return error.InvalidOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, trimOutput, reset_text });
|
print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text });
|
||||||
}
|
}
|
||||||
|
|
||||||
// The normal compile step calls os.exit, so we can't use it as a library :(
|
fn compile(self: *ZiglingStep, prog_node: *std.Progress.Node) ![]const u8 {
|
||||||
// This is a stripped down copy of std.build.LibExeObjStep.make.
|
print("Compiling {s}...\n", .{self.exercise.main_file});
|
||||||
fn doCompile(self: *@This(), prog_node: *std.Progress.Node) ![]const u8 {
|
|
||||||
const builder = self.builder;
|
|
||||||
|
|
||||||
var zig_args = std.ArrayList([]const u8).init(builder.allocator);
|
const b = self.step.owner;
|
||||||
|
const exercise_path = self.exercise.main_file;
|
||||||
|
const path = join(b.allocator, &.{ self.work_path, exercise_path }) catch
|
||||||
|
@panic("OOM");
|
||||||
|
|
||||||
|
var zig_args = std.ArrayList([]const u8).init(b.allocator);
|
||||||
defer zig_args.deinit();
|
defer zig_args.deinit();
|
||||||
|
|
||||||
zig_args.append(builder.zig_exe) catch unreachable;
|
zig_args.append(b.zig_exe) catch @panic("OOM");
|
||||||
zig_args.append("build-exe") catch unreachable;
|
zig_args.append("build-exe") catch @panic("OOM");
|
||||||
|
|
||||||
// Enable C support for exercises that use C functions
|
// Enable C support for exercises that use C functions.
|
||||||
if (self.exercise.link_libc) {
|
if (self.exercise.link_libc) {
|
||||||
zig_args.append("-lc") catch unreachable;
|
zig_args.append("-lc") catch @panic("OOM");
|
||||||
}
|
}
|
||||||
|
|
||||||
const zig_file = join(builder.allocator, &.{ self.work_path, self.exercise.main_file }) catch unreachable;
|
zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
|
||||||
zig_args.append(builder.pathFromRoot(zig_file)) catch unreachable;
|
|
||||||
|
|
||||||
zig_args.append("--cache-dir") catch unreachable;
|
zig_args.append("--cache-dir") catch @panic("OOM");
|
||||||
zig_args.append(builder.pathFromRoot(builder.cache_root.path.?)) catch unreachable;
|
zig_args.append(b.pathFromRoot(b.cache_root.path.?)) catch @panic("OOM");
|
||||||
|
|
||||||
zig_args.append("--listen=-") catch unreachable;
|
zig_args.append("--listen=-") catch @panic("OOM");
|
||||||
|
|
||||||
const argv = zig_args.items;
|
const argv = zig_args.items;
|
||||||
var code: u8 = undefined;
|
var code: u8 = undefined;
|
||||||
const file_name = self.eval(argv, &code, prog_node) catch |err| {
|
const exe_path = self.eval(argv, &code, prog_node) catch |err| {
|
||||||
self.printErrors();
|
self.printErrors();
|
||||||
|
|
||||||
switch (err) {
|
switch (err) {
|
||||||
error.FileNotFound => {
|
error.FileNotFound => {
|
||||||
print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{ red_text, self.exercise.main_file, reset_text });
|
print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, reset_text,
|
||||||
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
error.ExitCodeFailure => {
|
error.ExitCodeFailure => {
|
||||||
print("{s}{s}: The following command exited with error code {}:{s}\n", .{ red_text, self.exercise.main_file, code, reset_text });
|
print("{s}{s}: The following command exited with error code {}:{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, code, reset_text,
|
||||||
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
error.ProcessTerminated => {
|
error.ProcessTerminated => {
|
||||||
print("{s}{s}: The following command terminated unexpectedly:{s}\n", .{ red_text, self.exercise.main_file, reset_text });
|
print("{s}{s}: The following command terminated unexpectedly:{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, reset_text,
|
||||||
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
error.ZigIPCError => {
|
error.ZigIPCError => {
|
||||||
print("{s}{s}: The following command failed to communicate the compilation result:{s}\n", .{
|
print("{s}{s}: The following command failed to communicate the compilation result:{s}\n", .{
|
||||||
red_text,
|
red_text, self.exercise.main_file, reset_text,
|
||||||
self.exercise.main_file,
|
});
|
||||||
reset_text,
|
for (argv) |v| print("{s} ", .{v});
|
||||||
|
print("\n", .{});
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
print("{s}{s}: Unexpected error: {s}{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, @errorName(err), reset_text,
|
||||||
});
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
else => {},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
self.printErrors();
|
self.printErrors();
|
||||||
|
|
||||||
return file_name;
|
return exe_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code adapted from `std.Build.execAllowFail and `std.Build.Step.evalZigProcess`.
|
// Code adapted from `std.Build.execAllowFail and `std.Build.Step.evalZigProcess`.
|
||||||
|
@ -501,6 +518,23 @@ const ZiglingStep = struct {
|
||||||
return result orelse return error.ZigIPCError;
|
return result orelse return error.ZigIPCError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn help(self: *ZiglingStep) void {
|
||||||
|
const path = self.exercise.main_file;
|
||||||
|
const key = self.exercise.key();
|
||||||
|
|
||||||
|
print("\n{s}Edit exercises/{s} and run this again.{s}", .{
|
||||||
|
red_text, path, reset_text,
|
||||||
|
});
|
||||||
|
|
||||||
|
const format =
|
||||||
|
\\
|
||||||
|
\\{s}To continue from this zigling, use this command:{s}
|
||||||
|
\\ {s}zig build -Dn={s}{s}
|
||||||
|
\\
|
||||||
|
;
|
||||||
|
print(format, .{ red_text, reset_text, bold_text, key, reset_text });
|
||||||
|
}
|
||||||
|
|
||||||
fn printErrors(self: *ZiglingStep) void {
|
fn printErrors(self: *ZiglingStep) void {
|
||||||
resetLine();
|
resetLine();
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
// Help! Evil alien creatures have hidden eggs all over the Earth
|
// Help! Evil alien creatures have hidden eggs all over the Earth
|
||||||
// and they're starting to hatch!
|
// and they're starting to hatch!
|
||||||
//
|
//
|
||||||
// Before you jump into battle, you'll need to know four things:
|
// Before you jump into battle, you'll need to know three things:
|
||||||
//
|
//
|
||||||
// 1. You can attach functions to structs:
|
// 1. You can attach functions to structs (and other "type definitions"):
|
||||||
//
|
//
|
||||||
// const Foo = struct{
|
// const Foo = struct{
|
||||||
// pub fn hello() void {
|
// pub fn hello() void {
|
||||||
|
@ -12,31 +12,30 @@
|
||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
//
|
//
|
||||||
// 2. A function that is a member of a struct is a "method" and is
|
// 2. A function that is a member of a struct is "namespaced" within
|
||||||
// called with the "dot syntax" like so:
|
// that struct and is called by specifying the "namespace" and then
|
||||||
|
// using the "dot syntax":
|
||||||
//
|
//
|
||||||
// Foo.hello();
|
// Foo.hello();
|
||||||
//
|
//
|
||||||
// 3. The NEAT feature of methods is the special parameter named
|
// 3. The NEAT feature of these functions is that if their first argument
|
||||||
// "self" that takes an instance of that type of struct:
|
// is an instance of the struct (or a pointer to one) then we can use
|
||||||
|
// the instance as the namespace instead of the type:
|
||||||
//
|
//
|
||||||
// const Bar = struct{
|
// const Bar = struct{
|
||||||
// number: u32,
|
// pub fn a(self: Bar) void {}
|
||||||
//
|
// pub fn b(this: *Bar, other: u8) void {}
|
||||||
// pub fn printMe(self: Bar) void {
|
// pub fn c(bar: *const Bar) void {}
|
||||||
// std.debug.print("{}\n", .{self.number});
|
|
||||||
// }
|
|
||||||
// };
|
// };
|
||||||
//
|
//
|
||||||
// (Actually, you can name the first parameter anything, but
|
// var bar = Bar{};
|
||||||
// please follow convention and use "self".)
|
// bar.a() // is equivalent to Bar.a(bar)
|
||||||
|
// bar.b(3) // is equivalent to Bar.b(&bar, 3)
|
||||||
|
// bar.c() // is equivalent to Bar.c(&bar)
|
||||||
//
|
//
|
||||||
// 4. Now when you call the method on an INSTANCE of that struct
|
// Notice that the name of the parameter doesn't matter. Some use
|
||||||
// with the "dot syntax", the instance will be automatically
|
// self, others use a lowercase version of the type name, but feel
|
||||||
// passed as the "self" parameter:
|
// free to use whatever is most appropriate.
|
||||||
//
|
|
||||||
// var my_bar = Bar{ .number = 2000 };
|
|
||||||
// my_bar.printMe(); // prints "2000"
|
|
||||||
//
|
//
|
||||||
// Okay, you're armed.
|
// Okay, you're armed.
|
||||||
//
|
//
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
//
|
//
|
||||||
// Zig has support for IEEE-754 floating-point numbers in these
|
// Zig has support for IEEE-754 floating-point numbers in these
|
||||||
// specific sizes: f16, f32, f64, f80, and f128. Floating point
|
// specific sizes: f16, f32, f64, f80, and f128. Floating point
|
||||||
// literals may be written in scientific notation:
|
// literals may be written in the same ways as integers but also
|
||||||
|
// in scientific notation:
|
||||||
//
|
//
|
||||||
// const a1: f32 = 1200.0; // 1,200
|
// const a1: f32 = 1200; // 1,200
|
||||||
// const a2: f32 = 1.2e+3; // 1,200
|
// const a2: f32 = 1.2e+3; // 1,200
|
||||||
// const b1: f32 = -500_000.0; // -500,000
|
// const b1: f32 = -500_000.0; // -500,000
|
||||||
// const b2: f32 = -5.0e+5; // -500,000
|
// const b2: f32 = -5.0e+5; // -500,000
|
||||||
//
|
//
|
||||||
|
@ -22,12 +23,14 @@
|
||||||
// const pi: f16 = 3.1415926535; // rounds to 3.140625
|
// const pi: f16 = 3.1415926535; // rounds to 3.140625
|
||||||
// const av: f16 = 6.02214076e+23; // Avogadro's inf(inity)!
|
// const av: f16 = 6.02214076e+23; // Avogadro's inf(inity)!
|
||||||
//
|
//
|
||||||
// A float literal has a decimal point. When performing math
|
// When performing math operations with numeric literals, ensure
|
||||||
// operations with numeric literals, ensure the types match. Zig
|
// the types match. Zig does not perform unsafe type coercions
|
||||||
// does not perform unsafe type coercions behind your back:
|
// behind your back:
|
||||||
//
|
//
|
||||||
// var foo: f16 = 13.5 * 5; // ERROR!
|
// var foo: f16 = 5; // NO ERROR
|
||||||
// var foo: f16 = 13.5 * 5.0; // No problem, both are floats
|
//
|
||||||
|
// var foo: u16 = 5; // A literal of a different type
|
||||||
|
// var bar: f16 = foo; // ERROR
|
||||||
//
|
//
|
||||||
// Please fix the two float problems with this program and
|
// Please fix the two float problems with this program and
|
||||||
// display the result as a whole number.
|
// display the result as a whole number.
|
||||||
|
|
|
@ -20,15 +20,14 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
|
||||||
|
|
||||||
// We should use a temporary path, but it will make the implementation of
|
// We should use a temporary path, but it will make the implementation of
|
||||||
// `build.zig` more complex.
|
// `build.zig` more complex.
|
||||||
const outdir = "patches/healed";
|
const work_path = "patches/healed";
|
||||||
|
|
||||||
fs.cwd().makePath(outdir) catch |err| {
|
fs.cwd().makePath(work_path) catch |err| {
|
||||||
return fail(step, "unable to make '{s}': {s}\n", .{ outdir, @errorName(err) });
|
return fail(step, "unable to make '{s}': {s}\n", .{ work_path, @errorName(err) });
|
||||||
};
|
|
||||||
heal(b.allocator, exercises, outdir) catch |err| {
|
|
||||||
return fail(step, "unable to heal exercises: {s}\n", .{@errorName(err)});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const heal_step = HealStep.create(b, exercises, work_path);
|
||||||
|
|
||||||
{
|
{
|
||||||
// Test that `zig build -Dhealed -Dn=n test` selects the nth exercise.
|
// Test that `zig build -Dhealed -Dn=n test` selects the nth exercise.
|
||||||
const case_step = createCase(b, "case-1");
|
const case_step = createCase(b, "case-1");
|
||||||
|
@ -49,6 +48,8 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
|
||||||
else
|
else
|
||||||
expectStdErrMatch(cmd, ex.output);
|
expectStdErrMatch(cmd, ex.output);
|
||||||
|
|
||||||
|
cmd.step.dependOn(&heal_step.step);
|
||||||
|
|
||||||
case_step.dependOn(&cmd.step);
|
case_step.dependOn(&cmd.step);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,6 +73,8 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
|
||||||
cmd.expectStdOutEqual("");
|
cmd.expectStdOutEqual("");
|
||||||
expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file}));
|
expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file}));
|
||||||
|
|
||||||
|
cmd.step.dependOn(&heal_step.step);
|
||||||
|
|
||||||
case_step.dependOn(&cmd.step);
|
case_step.dependOn(&cmd.step);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,6 +89,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
|
||||||
const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dhealed" });
|
const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dhealed" });
|
||||||
cmd.setName("zig build -Dhealed");
|
cmd.setName("zig build -Dhealed");
|
||||||
cmd.expectExitCode(0);
|
cmd.expectExitCode(0);
|
||||||
|
cmd.step.dependOn(&heal_step.step);
|
||||||
|
|
||||||
const stderr = cmd.captureStdErr();
|
const stderr = cmd.captureStdErr();
|
||||||
const verify = CheckStep.create(b, exercises, stderr, true);
|
const verify = CheckStep.create(b, exercises, stderr, true);
|
||||||
|
@ -107,6 +111,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
|
||||||
);
|
);
|
||||||
cmd.setName("zig build -Dhealed -Dn=1 start");
|
cmd.setName("zig build -Dhealed -Dn=1 start");
|
||||||
cmd.expectExitCode(0);
|
cmd.expectExitCode(0);
|
||||||
|
cmd.step.dependOn(&heal_step.step);
|
||||||
|
|
||||||
const stderr = cmd.captureStdErr();
|
const stderr = cmd.captureStdErr();
|
||||||
const verify = CheckStep.create(b, exercises, stderr, false);
|
const verify = CheckStep.create(b, exercises, stderr, false);
|
||||||
|
@ -126,14 +131,16 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
|
||||||
cmd.expectExitCode(1);
|
cmd.expectExitCode(1);
|
||||||
expectStdErrMatch(cmd, exercises[0].hint);
|
expectStdErrMatch(cmd, exercises[0].hint);
|
||||||
|
|
||||||
|
cmd.step.dependOn(&heal_step.step);
|
||||||
|
|
||||||
case_step.dependOn(&cmd.step);
|
case_step.dependOn(&cmd.step);
|
||||||
|
|
||||||
step.dependOn(case_step);
|
step.dependOn(case_step);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't add the cleanup step, since it may delete outdir while a test case
|
// Don't add the cleanup step, since it may delete work_path while a test
|
||||||
// is running.
|
// case is running.
|
||||||
//const cleanup = b.addRemoveDirTree(outdir);
|
//const cleanup = b.addRemoveDirTree(work_path);
|
||||||
//step.dependOn(&cleanup.step);
|
//step.dependOn(&cleanup.step);
|
||||||
|
|
||||||
return step;
|
return step;
|
||||||
|
@ -315,8 +322,38 @@ fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step {
|
||||||
return step;
|
return step;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A step that heals exercises.
|
||||||
|
const HealStep = struct {
|
||||||
|
step: Step,
|
||||||
|
exercises: []const Exercise,
|
||||||
|
work_path: []const u8,
|
||||||
|
|
||||||
|
pub fn create(owner: *Build, exercises: []const Exercise, work_path: []const u8) *HealStep {
|
||||||
|
const self = owner.allocator.create(HealStep) catch @panic("OOM");
|
||||||
|
self.* = .{
|
||||||
|
.step = Step.init(.{
|
||||||
|
.id = .custom,
|
||||||
|
.name = "heal",
|
||||||
|
.owner = owner,
|
||||||
|
.makeFn = make,
|
||||||
|
}),
|
||||||
|
.exercises = exercises,
|
||||||
|
.work_path = work_path,
|
||||||
|
};
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make(step: *Step, _: *std.Progress.Node) !void {
|
||||||
|
const b = step.owner;
|
||||||
|
const self = @fieldParentPtr(HealStep, "step", step);
|
||||||
|
|
||||||
|
return heal(b.allocator, self.exercises, self.work_path);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Heals all the exercises.
|
// Heals all the exercises.
|
||||||
fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !void {
|
fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8) !void {
|
||||||
const join = fs.path.join;
|
const join = fs.path.join;
|
||||||
|
|
||||||
const exercises_path = "exercises";
|
const exercises_path = "exercises";
|
||||||
|
@ -331,7 +368,7 @@ fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !
|
||||||
const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name});
|
const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name});
|
||||||
break :b try join(allocator, &.{ patches_path, patch_name });
|
break :b try join(allocator, &.{ patches_path, patch_name });
|
||||||
};
|
};
|
||||||
const output = try join(allocator, &.{ outdir, ex.main_file });
|
const output = try join(allocator, &.{ work_path, ex.main_file });
|
||||||
|
|
||||||
const argv = &.{ "patch", "-i", patch, "-o", output, "-s", file };
|
const argv = &.{ "patch", "-i", patch, "-o", output, "-s", file };
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue