From 116546a996a12a2e68a68ddf7926d2b4e708c52c Mon Sep 17 00:00:00 2001 From: Arya-Elfren <109028294+Arya-Elfren@users.noreply.github.com> Date: Wed, 26 Apr 2023 22:07:20 +0100 Subject: [PATCH 01/12] Clarify `f16` maths - closes #204 --- exercises/060_floats.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/exercises/060_floats.zig b/exercises/060_floats.zig index 8ba51db..0ebd7a2 100644 --- a/exercises/060_floats.zig +++ b/exercises/060_floats.zig @@ -1,7 +1,8 @@ // // Zig has support for IEEE-754 floating-point numbers in these // 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 a2: f32 = 1.2e+3; // 1,200 @@ -26,7 +27,10 @@ // operations with numeric literals, ensure the types match. Zig // does not perform unsafe type coercions behind your back: // -// var foo: f16 = 13.5 * 5; // ERROR! +// fn foo(bar: u16) f16 { return 13.5 * bar; } // ERROR! +// var foo: f16 = 13.5 * @as(u8, 5); // ERROR! +// var foo: f16 = 13.5 * 5; // This is a safe compile-time +// // conversion, so no problem! // var foo: f16 = 13.5 * 5.0; // No problem, both are floats // // Please fix the two float problems with this program and From 18f69f5634c7469042dc601e4c5609af9e0f382c Mon Sep 17 00:00:00 2001 From: Arya-Elfren <109028294+Arya-Elfren@users.noreply.github.com> Date: Wed, 26 Apr 2023 22:47:03 +0100 Subject: [PATCH 02/12] Clarify the methods syntax sugar & a bit more I think it's a bit clearer to show exactly what the syntax sugar of methods is, because that's all it is. Every function in Zig is in a struct (files are structs after all) and methods just simplify their use. I also thought we might use the explicit saturating subtraction as that is why the feature is in Zig. --- exercises/047_methods.zig | 43 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index 96d4c8e..7211caa 100644 --- a/exercises/047_methods.zig +++ b/exercises/047_methods.zig @@ -2,9 +2,9 @@ // Help! Evil alien creatures have hidden eggs all over the Earth // 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{ // pub fn hello() void { @@ -12,31 +12,34 @@ // } // }; // -// 2. A function that is a member of a struct is a "method" and is -// called with the "dot syntax" like so: +// 2. A function that is a member of a struct is "namespaced" within +// that struct and is called by specifying the "namespace" and then +// using the "dot syntax": // // Foo.hello(); // -// 3. The NEAT feature of methods is the special parameter named -// "self" that takes an instance of that type of struct: +// 3. The NEAT feature of these functions is that if they take either +// an instance of the struct or a pointer to an instance of the struct +// then they have some syntax sugar: // // const Bar = struct{ -// number: u32, -// -// pub fn printMe(self: Bar) void { -// std.debug.print("{}\n", .{self.number}); -// } +// pub fn a(self: Bar) void { _ = self; } +// pub fn b(this: *Bar, other: u8) void { _ = this; _ = other; } +// pub fn c(bar: *const Bar) void { _ = bar; } // }; // -// (Actually, you can name the first parameter anything, but -// please follow convention and use "self".) +// var bar = Bar{}; +// 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 -// with the "dot syntax", the instance will be automatically -// passed as the "self" parameter: +// Notice that the name of the parameter doesn't matter. Some use +// self, others use a lowercase version of the type name, but feel +// free to use whatever is most appropriate. // -// var my_bar = Bar{ .number = 2000 }; -// my_bar.printMe(); // prints "2000" +// Effectively, the method syntax sugar just does this transformation: +// thing.function(args); +// @TypeOf(thing).function(thing, args); // // Okay, you're armed. // @@ -63,7 +66,9 @@ const HeatRay = struct { // We love this method: pub fn zap(self: HeatRay, alien: *Alien) void { - alien.health -= if (self.damage >= alien.health) alien.health else self.damage; + alien.health -|= self.damage; // Saturating inplace substraction + // It subtracts but doesn't go below the + // lowest value for our type (in this case 0) } }; From 3612c67f04e0d902a12c3f71ed52b1de8422804e Mon Sep 17 00:00:00 2001 From: Arya-Elfren <109028294+Arya-Elfren@users.noreply.github.com> Date: Fri, 28 Apr 2023 11:12:42 +0100 Subject: [PATCH 03/12] Simplify methods explanation in 047 --- exercises/047_methods.zig | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index 7211caa..6b2dbef 100644 --- a/exercises/047_methods.zig +++ b/exercises/047_methods.zig @@ -18,14 +18,14 @@ // // Foo.hello(); // -// 3. The NEAT feature of these functions is that if they take either -// an instance of the struct or a pointer to an instance of the struct -// then they have some syntax sugar: +// 3. The NEAT feature of these functions is that if their first argument +// 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{ -// pub fn a(self: Bar) void { _ = self; } -// pub fn b(this: *Bar, other: u8) void { _ = this; _ = other; } -// pub fn c(bar: *const Bar) void { _ = bar; } +// pub fn a(self: Bar) void {} +// pub fn b(this: *Bar, other: u8) void {} +// pub fn c(bar: *const Bar) void {} // }; // // var bar = Bar{}; @@ -37,10 +37,6 @@ // self, others use a lowercase version of the type name, but feel // free to use whatever is most appropriate. // -// Effectively, the method syntax sugar just does this transformation: -// thing.function(args); -// @TypeOf(thing).function(thing, args); -// // Okay, you're armed. // // Now, please zap the alien structs until they're all gone or @@ -66,9 +62,7 @@ const HeatRay = struct { // We love this method: pub fn zap(self: HeatRay, alien: *Alien) void { - alien.health -|= self.damage; // Saturating inplace substraction - // It subtracts but doesn't go below the - // lowest value for our type (in this case 0) + alien.health -= if (self.damage >= alien.health) alien.health else self.damage; } }; From 599bea57051efcfaa7a81cb2a846fe095b9759c0 Mon Sep 17 00:00:00 2001 From: Arya-Elfren <109028294+Arya-Elfren@users.noreply.github.com> Date: Fri, 28 Apr 2023 11:32:45 +0100 Subject: [PATCH 04/12] Simplify `f16` coersion example --- exercises/060_floats.zig | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/exercises/060_floats.zig b/exercises/060_floats.zig index 0ebd7a2..a04a54d 100644 --- a/exercises/060_floats.zig +++ b/exercises/060_floats.zig @@ -23,15 +23,12 @@ // const pi: f16 = 3.1415926535; // rounds to 3.140625 // const av: f16 = 6.02214076e+23; // Avogadro's inf(inity)! // -// A float literal has a decimal point. When performing math +// A float literal doesn't need a decimal point. When performing math // operations with numeric literals, ensure the types match. Zig // does not perform unsafe type coercions behind your back: // -// fn foo(bar: u16) f16 { return 13.5 * bar; } // ERROR! -// var foo: f16 = 13.5 * @as(u8, 5); // ERROR! -// var foo: f16 = 13.5 * 5; // This is a safe compile-time -// // conversion, so no problem! -// var foo: f16 = 13.5 * 5.0; // No problem, both are floats +// var foo: f16 = 5; // NO ERROR +// var foo: f16 = @as(u16, 5); // ERROR // // Please fix the two float problems with this program and // display the result as a whole number. From c2fe843a8a780e77e9875c9f6da8b3c3999915f2 Mon Sep 17 00:00:00 2001 From: Arya-Elfren <109028294+Arya-Elfren@users.noreply.github.com> Date: Fri, 28 Apr 2023 15:11:43 +0100 Subject: [PATCH 05/12] 060 - remove `@as()` --- exercises/060_floats.zig | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/exercises/060_floats.zig b/exercises/060_floats.zig index a04a54d..1320171 100644 --- a/exercises/060_floats.zig +++ b/exercises/060_floats.zig @@ -4,8 +4,8 @@ // literals may be written in the same ways as integers but also // in scientific notation: // -// const a1: f32 = 1200.0; // 1,200 -// const a2: f32 = 1.2e+3; // 1,200 +// const a1: f32 = 1200; // 1,200 +// const a2: f32 = 1.2e+3; // 1,200 // const b1: f32 = -500_000.0; // -500,000 // const b2: f32 = -5.0e+5; // -500,000 // @@ -23,12 +23,14 @@ // const pi: f16 = 3.1415926535; // rounds to 3.140625 // const av: f16 = 6.02214076e+23; // Avogadro's inf(inity)! // -// A float literal doesn't need a decimal point. When performing math -// operations with numeric literals, ensure the types match. Zig -// does not perform unsafe type coercions behind your back: +// When performing math operations with numeric literals, ensure +// the types match. Zig does not perform unsafe type coercions +// behind your back: // // var foo: f16 = 5; // NO ERROR -// var foo: f16 = @as(u16, 5); // ERROR +// +// 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 // display the result as a whole number. From 5c488a1402be83f198cc29f23f448313001a9a1c Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Thu, 4 May 2023 16:34:17 +0200 Subject: [PATCH 06/12] test: improve test case 1 and 2 In test case 1 and 2, remove the `i` variable and use `ex.number()` instead. In test case 2, when checking the exercise output from stderr, also check that stdout is empty and vice versa. --- test/tests.zig | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/tests.zig b/test/tests.zig index 752ca50..060b553 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -32,21 +32,23 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { // Test that `zig build -Dhealed -Dn=n test` selects the nth exercise. const case_step = createCase(b, "case-1"); - var i: usize = 0; for (exercises[0 .. exercises.len - 1]) |ex| { - i += 1; + const n = ex.number(); if (ex.skip) continue; const cmd = b.addSystemCommand( - &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{i}), "test" }, + &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{n}), "test" }, ); - cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{i})); + cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); - if (ex.check_stdout) - expectStdOutMatch(cmd, ex.output) - else + if (ex.check_stdout) { + expectStdOutMatch(cmd, ex.output); + cmd.expectStdErrEqual(""); + } else { expectStdErrMatch(cmd, ex.output); + cmd.expectStdOutEqual(""); + } cmd.step.dependOn(&heal_step.step); @@ -60,15 +62,14 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { // Test that `zig build -Dhealed -Dn=n test` skips disabled esercises. const case_step = createCase(b, "case-2"); - var i: usize = 0; for (exercises[0 .. exercises.len - 1]) |ex| { - i += 1; + const n = ex.number(); if (!ex.skip) continue; const cmd = b.addSystemCommand( - &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{i}), "test" }, + &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{n}), "test" }, ); - cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{i})); + cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); cmd.expectStdOutEqual(""); expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file})); From 277c95db7af09732f5179677fcbd191e36fefc81 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Thu, 4 May 2023 18:48:35 +0200 Subject: [PATCH 07/12] test: fix doc-comments CheckStep, FailStep, fail, HealStep and heal incorrectly used a normal comment, instead of a doc-comment. Additionally, improve the documentation for FailStep and HealStep. --- test/tests.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/tests.zig b/test/tests.zig index 060b553..dbf237b 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -158,7 +158,7 @@ fn createCase(b: *Build, name: []const u8) *Step { return case_step; } -// Check the output of `zig build` or `zig build -Dn=1 start`. +/// Checks the output of `zig build` or `zig build -Dn=1 start`. const CheckStep = struct { step: Step, exercises: []const Exercise, @@ -282,7 +282,7 @@ const CheckStep = struct { } }; -// A step that will fail. +/// Fails with a custom error message. const FailStep = struct { step: Step, error_msg: []const u8, @@ -311,9 +311,9 @@ const FailStep = struct { } }; -// A variant of `std.Build.Step.fail` that does not return an error so that it -// can be used in the configuration phase. It returns a FailStep, so that the -// error will be cleanly handled by the build runner. +/// A variant of `std.Build.Step.fail` that does not return an error so that it +/// can be used in the configuration phase. It returns a FailStep, so that the +/// error will be cleanly handled by the build runner. fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step { const b = step.owner; @@ -323,7 +323,7 @@ fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step { return step; } -// A step that heals exercises. +/// Heals the exercises. const HealStep = struct { step: Step, exercises: []const Exercise, @@ -353,7 +353,7 @@ const HealStep = struct { } }; -// Heals all the exercises. +/// Heals all the exercises. fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8) !void { const join = fs.path.join; From eda73b2fa8dcb5ca4fc96903f8d1f46ac464acab Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Thu, 4 May 2023 19:01:48 +0200 Subject: [PATCH 08/12] test: remove obsolete comment in the heal function Remove the comment about using POSIX patch variant, since we now use the -s option. --- test/tests.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/tests.zig b/test/tests.zig index dbf237b..1fda49d 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -7,6 +7,7 @@ const fs = std.fs; const mem = std.mem; const Allocator = std.mem.Allocator; +const Child = std.process.Child; const Build = std.build; const FileSource = std.Build.FileSource; const Reader = fs.File.Reader; @@ -363,7 +364,6 @@ fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8 for (exercises) |ex| { const name = ex.name(); - // Use the POSIX patch variant. const file = try join(allocator, &.{ exercises_path, ex.main_file }); const patch = b: { const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name}); @@ -373,7 +373,7 @@ fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8 const argv = &.{ "patch", "-i", patch, "-o", output, "-s", file }; - var child = std.process.Child.init(argv, allocator); + var child = Child.init(argv, allocator); _ = try child.spawnAndWait(); } } From 8a3d722a33ed09fd8e534fffa0bb67d84f40528a Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Thu, 4 May 2023 19:04:58 -0400 Subject: [PATCH 09/12] Ex 001 remove ancient script reference + wording --- exercises/001_hello.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/001_hello.zig b/exercises/001_hello.zig index d2093c7..9534b60 100644 --- a/exercises/001_hello.zig +++ b/exercises/001_hello.zig @@ -1,18 +1,18 @@ // -// Oh no! This program is supposed to print "Hello world!" but it needs -// your help! +// Oh no, this is supposed to print "Hello world!" but it needs +// your help. // +// Zig functions are private by default but the main() function +// should be public. // -// Zig functions are private by default but the main() function should -// be public. -// -// A function is declared public with the "pub" statement like so: +// A function is made public with the "pub" statement like so: // // pub fn foo() void { // ... // } // -// Try to fix the program and run `ziglings` to see if it works! +// Perhaps knowing this well help solve the errors we're getting +// with this little program? // const std = @import("std"); From 25611b831255cd1a84b837c708a7e6398d133869 Mon Sep 17 00:00:00 2001 From: Arnon <98772127+arnon4@users.noreply.github.com> Date: Fri, 5 May 2023 13:11:20 +0300 Subject: [PATCH 10/12] Fixed example syntax for inline else --- exercises/092_interfaces.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/092_interfaces.zig b/exercises/092_interfaces.zig index 5ac5768..8f0a937 100644 --- a/exercises/092_interfaces.zig +++ b/exercises/092_interfaces.zig @@ -40,7 +40,7 @@ // // switch (thing) { // .a => |a| special(a), -// inline else |t| => normal(t), +// inline else => |t| normal(t), // } // // We can have special handling of some cases and then Zig From 0cd86d2f9b8a372c7c8ab2a253e3643cc2ab6212 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Fri, 5 May 2023 16:13:57 +0200 Subject: [PATCH 11/12] build: add the healed-path option This is necessary in the unit tests, to ensure each test case use a different exercises directory. Update test/tests.zig to use the new healed-path option, ensuring that each temp directory is removed. In test case 3, 4 and 5, move case_step as the first step in the dependency chain. This will improve the build summary tree. In test case 5, remove the dependency to heal_step, since it is not necessary. --- build.zig | 9 ++-- test/tests.zig | 119 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 89 insertions(+), 39 deletions(-) diff --git a/build.zig b/build.zig index 64968b9..a880450 100644 --- a/build.zig +++ b/build.zig @@ -118,11 +118,12 @@ pub fn build(b: *Build) !void { \\ ; - const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false; + const healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false; + const override_healed_path = b.option([]const u8, "healed-path", "Override healed path"); const exno: ?usize = b.option(usize, "n", "Select exercise"); - const healed_path = "patches/healed"; - const work_path = if (use_healed) healed_path else "exercises"; + const healed_path = if (override_healed_path) |path| path else "patches/healed"; + const work_path = if (healed) healed_path else "exercises"; const header_step = PrintStep.create(b, logo); @@ -168,7 +169,7 @@ pub fn build(b: *Build) !void { start_step.dependOn(&prev_step.step); return; - } else if (use_healed and false) { + } else if (healed and false) { // Special case when healed by the eowyn script, where we can make the // code more efficient. // diff --git a/test/tests.zig b/test/tests.zig index 1fda49d..0fd3286 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -19,27 +19,28 @@ const Exercise = root.Exercise; pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { const step = b.step("test-cli", "Test the command line interface"); - // We should use a temporary path, but it will make the implementation of - // `build.zig` more complex. - const work_path = "patches/healed"; - - fs.cwd().makePath(work_path) catch |err| { - return fail(step, "unable to make '{s}': {s}\n", .{ work_path, @errorName(err) }); - }; - - const heal_step = HealStep.create(b, exercises, work_path); - { // Test that `zig build -Dhealed -Dn=n test` selects the nth exercise. const case_step = createCase(b, "case-1"); + const tmp_path = makeTempPath(b) catch |err| { + return fail(step, "unable to make tmp path: {s}\n", .{@errorName(err)}); + }; + + const heal_step = HealStep.create(b, exercises, tmp_path); + for (exercises[0 .. exercises.len - 1]) |ex| { const n = ex.number(); if (ex.skip) continue; - const cmd = b.addSystemCommand( - &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{n}), "test" }, - ); + const cmd = b.addSystemCommand(&.{ + b.zig_exe, + "build", + "-Dhealed", + b.fmt("-Dhealed-path={s}", .{tmp_path}), + b.fmt("-Dn={}", .{n}), + "test", + }); cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); @@ -56,20 +57,34 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { case_step.dependOn(&cmd.step); } - step.dependOn(case_step); + const cleanup = b.addRemoveDirTree(tmp_path); + cleanup.step.dependOn(case_step); + + step.dependOn(&cleanup.step); } { // Test that `zig build -Dhealed -Dn=n test` skips disabled esercises. const case_step = createCase(b, "case-2"); + const tmp_path = makeTempPath(b) catch |err| { + return fail(step, "unable to make tmp path: {s}\n", .{@errorName(err)}); + }; + + const heal_step = HealStep.create(b, exercises, tmp_path); + for (exercises[0 .. exercises.len - 1]) |ex| { const n = ex.number(); if (!ex.skip) continue; - const cmd = b.addSystemCommand( - &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{n}), "test" }, - ); + const cmd = b.addSystemCommand(&.{ + b.zig_exe, + "build", + "-Dhealed", + b.fmt("-Dhealed-path={s}", .{tmp_path}), + b.fmt("-Dn={}", .{n}), + "test", + }); cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); cmd.expectStdOutEqual(""); @@ -80,15 +95,30 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { case_step.dependOn(&cmd.step); } - step.dependOn(case_step); + const cleanup = b.addRemoveDirTree(tmp_path); + cleanup.step.dependOn(case_step); + + step.dependOn(&cleanup.step); } { // Test that `zig build -Dhealed` process all the exercises in order. const case_step = createCase(b, "case-3"); + const tmp_path = makeTempPath(b) catch |err| { + return fail(step, "unable to make tmp path: {s}\n", .{@errorName(err)}); + }; + + const heal_step = HealStep.create(b, exercises, tmp_path); + heal_step.step.dependOn(case_step); + // TODO: when an exercise is modified, the cache is not invalidated. - const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dhealed" }); + const cmd = b.addSystemCommand(&.{ + b.zig_exe, + "build", + "-Dhealed", + b.fmt("-Dhealed-path={s}", .{tmp_path}), + }); cmd.setName("zig build -Dhealed"); cmd.expectExitCode(0); cmd.step.dependOn(&heal_step.step); @@ -97,9 +127,10 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { const verify = CheckStep.create(b, exercises, stderr, true); verify.step.dependOn(&cmd.step); - case_step.dependOn(&verify.step); + const cleanup = b.addRemoveDirTree(tmp_path); + cleanup.step.dependOn(&verify.step); - step.dependOn(case_step); + step.dependOn(&cleanup.step); } { @@ -107,10 +138,22 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { // in order. const case_step = createCase(b, "case-4"); + const tmp_path = makeTempPath(b) catch |err| { + return fail(step, "unable to make tmp path: {s}\n", .{@errorName(err)}); + }; + + const heal_step = HealStep.create(b, exercises, tmp_path); + heal_step.step.dependOn(case_step); + // TODO: when an exercise is modified, the cache is not invalidated. - const cmd = b.addSystemCommand( - &.{ b.zig_exe, "build", "-Dhealed", "-Dn=1", "start" }, - ); + const cmd = b.addSystemCommand(&.{ + b.zig_exe, + "build", + "-Dhealed", + b.fmt("-Dhealed-path={s}", .{tmp_path}), + "-Dn=1", + "start", + }); cmd.setName("zig build -Dhealed -Dn=1 start"); cmd.expectExitCode(0); cmd.step.dependOn(&heal_step.step); @@ -119,9 +162,10 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { const verify = CheckStep.create(b, exercises, stderr, false); verify.step.dependOn(&cmd.step); - case_step.dependOn(&verify.step); + const cleanup = b.addRemoveDirTree(tmp_path); + cleanup.step.dependOn(&verify.step); - step.dependOn(case_step); + step.dependOn(&cleanup.step); } { @@ -133,18 +177,11 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { cmd.expectExitCode(1); expectStdErrMatch(cmd, exercises[0].hint); - cmd.step.dependOn(&heal_step.step); - - case_step.dependOn(&cmd.step); + cmd.step.dependOn(case_step); - step.dependOn(case_step); + step.dependOn(&cmd.step); } - // Don't add the cleanup step, since it may delete work_path while a test - // case is running. - //const cleanup = b.addRemoveDirTree(work_path); - //step.dependOn(&cleanup.step); - return step; } @@ -378,6 +415,18 @@ fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8 } } +/// This function is the same as the one in std.Build.makeTempPath, with the +/// difference that returns an error when the temp path cannot be created. +pub fn makeTempPath(b: *Build) ![]const u8 { + const rand_int = std.crypto.random.int(u64); + const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ Build.hex64(rand_int); + const path = b.cache_root.join(b.allocator, &.{tmp_dir_sub_path}) catch + @panic("OOM"); + try b.cache_root.handle.makePath(tmp_dir_sub_path); + + return path; +} + // // Missing functions from std.Build.RunStep // From e5341b91c107894e585e515731d5ec34fd56c1af Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Fri, 5 May 2023 18:34:36 -0400 Subject: [PATCH 12/12] Ex 101: Magic bags better than buckets for metaphors --- exercises/101_for5.zig | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/exercises/101_for5.zig b/exercises/101_for5.zig index 3861417..037989f 100644 --- a/exercises/101_for5.zig +++ b/exercises/101_for5.zig @@ -79,19 +79,19 @@ pub fn main() void { // all about: // // Let's say you've been tasked with grabbing three glass -// marbles, three spoons, and three feathers from a bucket. But -// you can't use your hands to grab them. Instead, you have a -// special marble scoop, spoon magnet, and feather tongs to grab +// marbles, three spoons, and three feathers from a magic bag. +// But you can't use your hands to grab them. Instead, you must +// use a marble scoop, spoon magnet, and feather tongs to grab // each type of object. // -// Now, would you rather have: +// Now, would you rather the magic bag: // -// A. The items layered so you have to pick up one marble, then -// one spoon, then one feather? +// A. Grouped the items in clusters so you have to pick up one +// marble, then one spoon, then one feather? // // OR // -// B. The items separated by type so you can pick up all of the +// B. Grouped the items by type so you can pick up all of the // marbles at once, then all the spoons, then all of the // feathers? // @@ -103,14 +103,16 @@ pub fn main() void { // efficient for modern CPUs. // // Decades of OOP practices have steered people towards grouping -// different data types together into "objects" with the hope -// that it would be friendlier to the human mind. But -// data-oriented design groups data in a way that is more -// efficient for the computer. -// -// In Zig terminology, the difference in groupings is sometimes -// known as "Array of Structs" (AoS) versus "Struct of Arrays" -// (SoA). +// different data types together into mixed-type "objects" with +// the intent that these are easier on the human mind. +// Data-oriented design groups data by type in a way that is +// easier on the computer. +// +// With clever language design, maybe we can have both. +// +// In the Zig community, you may see the difference in groupings +// presented with the terms "Array of Structs" (AoS) versus +// "Struct of Arrays" (SoA). // // To envision these two designs in action, imagine an array of // RPG character structs, each containing three different data