From 806228f6c05ba3f0489899e68a1bad61da498042 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 27 Mar 2023 20:00:01 +0200 Subject: [PATCH 1/9] exercises(high-scores): implement --- config.json | 18 +++ .../high-scores/.docs/instructions.md | 6 + .../practice/high-scores/.meta/config.json | 18 +++ .../practice/high-scores/.meta/example.zig | 27 +++++ .../practice/high-scores/.meta/tests.toml | 46 +++++++ .../practice/high-scores/high_scores.zig | 25 ++++ .../practice/high-scores/test_high_scores.zig | 112 ++++++++++++++++++ 7 files changed, 252 insertions(+) create mode 100644 exercises/practice/high-scores/.docs/instructions.md create mode 100644 exercises/practice/high-scores/.meta/config.json create mode 100644 exercises/practice/high-scores/.meta/example.zig create mode 100644 exercises/practice/high-scores/.meta/tests.toml create mode 100644 exercises/practice/high-scores/high_scores.zig create mode 100644 exercises/practice/high-scores/test_high_scores.zig diff --git a/config.json b/config.json index e8a3d156..d619be5a 100644 --- a/config.json +++ b/config.json @@ -35,6 +35,24 @@ "exercises": { "concept": [], "practice": [ + { + "uuid": "fafe82fb-a54a-40d3-b082-4429e080e435", + "slug": "high-scores", + "name": "High scores", + "practices": [ + "integers", + "slices", + "sorting", + "structs" + ], + "prerequisites": [ + "integers", + "slices", + "sorting", + "structs" + ], + "difficulty": 1 + }, { "uuid": "25a0331e-612f-48c1-8a3f-9b0e9601a2fa", "slug": "hello-world", diff --git a/exercises/practice/high-scores/.docs/instructions.md b/exercises/practice/high-scores/.docs/instructions.md new file mode 100644 index 00000000..55802488 --- /dev/null +++ b/exercises/practice/high-scores/.docs/instructions.md @@ -0,0 +1,6 @@ +# Instructions + +Manage a game player's High Score list. + +Your task is to build a high-score component of the classic Frogger game, one of the highest selling and most addictive games of all time, and a classic of the arcade era. +Your task is to write methods that return the highest score from the list, the last added score and the three highest scores. diff --git a/exercises/practice/high-scores/.meta/config.json b/exercises/practice/high-scores/.meta/config.json new file mode 100644 index 00000000..3f7469ba --- /dev/null +++ b/exercises/practice/high-scores/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "ee7" + ], + "files": { + "solution": [ + "high_scores.zig" + ], + "test": [ + "test_high_scores.zig" + ], + "example": [ + ".meta/example.zig" + ] + }, + "blurb": "Manage a player's High Score list.", + "source": "Tribute to the eighties' arcade game Frogger" +} diff --git a/exercises/practice/high-scores/.meta/example.zig b/exercises/practice/high-scores/.meta/example.zig new file mode 100644 index 00000000..29824042 --- /dev/null +++ b/exercises/practice/high-scores/.meta/example.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub const HighScores = struct { + scores: []const u32, + + pub fn init(s: []const u32) HighScores { + return .{ .scores = s }; + } + + pub fn latest(self: HighScores) u32 { + return self.scores[self.scores.len - 1]; + } + + pub fn personalBest(self: HighScores) u32 { + return std.mem.max(u32, self.scores); + } + + pub fn personalTopThree(self: HighScores, buffer: *[3]u32) []u32 { + std.debug.assert(self.scores.len <= 100); + var sorted: [100]u32 = undefined; + std.mem.copy(u32, &sorted, self.scores); + std.sort.sort(u32, sorted[0..self.scores.len], {}, std.sort.desc(u32)); + var i = @min(self.scores.len, 3); + std.mem.copy(u32, buffer, sorted[0..i]); + return buffer[0..i]; + } +}; diff --git a/exercises/practice/high-scores/.meta/tests.toml b/exercises/practice/high-scores/.meta/tests.toml new file mode 100644 index 00000000..7c946338 --- /dev/null +++ b/exercises/practice/high-scores/.meta/tests.toml @@ -0,0 +1,46 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[1035eb93-2208-4c22-bab8-fef06769a73c] +description = "List of scores" + +[6aa5dbf5-78fa-4375-b22c-ffaa989732d2] +description = "Latest score" + +[b661a2e1-aebf-4f50-9139-0fb817dd12c6] +description = "Personal best" + +[3d996a97-c81c-4642-9afc-80b80dc14015] +description = "Top 3 scores -> Personal top three from a list of scores" + +[1084ecb5-3eb4-46fe-a816-e40331a4e83a] +description = "Top 3 scores -> Personal top highest to lowest" + +[e6465b6b-5a11-4936-bfe3-35241c4f4f16] +description = "Top 3 scores -> Personal top when there is a tie" + +[f73b02af-c8fd-41c9-91b9-c86eaa86bce2] +description = "Top 3 scores -> Personal top when there are less than 3" + +[16608eae-f60f-4a88-800e-aabce5df2865] +description = "Top 3 scores -> Personal top when there is only one" + +[2df075f9-fec9-4756-8f40-98c52a11504f] +description = "Top 3 scores -> Latest score after personal top scores" + +[809c4058-7eb1-4206-b01e-79238b9b71bc] +description = "Top 3 scores -> Scores after personal top scores" + +[ddb0efc0-9a86-4f82-bc30-21ae0bdc6418] +description = "Top 3 scores -> Latest score after personal best" + +[6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364] +description = "Top 3 scores -> Scores after personal best" diff --git a/exercises/practice/high-scores/high_scores.zig b/exercises/practice/high-scores/high_scores.zig new file mode 100644 index 00000000..c6101504 --- /dev/null +++ b/exercises/practice/high-scores/high_scores.zig @@ -0,0 +1,25 @@ +pub const HighScores = struct { + // Please implement the field(s) of this struct. + foo: void, + + pub fn init(s: []const u32) HighScores { + _ = s; + @compileError("please implement the init method"); + } + + pub fn latest(self: HighScores) u32 { + _ = self; + @compileError("please implement the latest method"); + } + + pub fn personalBest(self: HighScores) u32 { + _ = self; + @compileError("please implement the personalBest method"); + } + + pub fn personalTopThree(self: HighScores, buffer: *[3]u32) []u32 { + _ = self; + _ = buffer; + @compileError("please implement the personalTopThree method"); + } +}; diff --git a/exercises/practice/high-scores/test_high_scores.zig b/exercises/practice/high-scores/test_high_scores.zig new file mode 100644 index 00000000..e49099a8 --- /dev/null +++ b/exercises/practice/high-scores/test_high_scores.zig @@ -0,0 +1,112 @@ +const std = @import("std"); +const testing = std.testing; + +const high_scores = @import("high_scores.zig"); +const HighScores = high_scores.HighScores; + +test "list of scores" { + const expected = [_]u32{ 30, 50, 20, 70 }; + const scores = [_]u32{ 30, 50, 20, 70 }; + const hs = HighScores.init(&scores); + const actual = hs.scores; + try testing.expectEqualSlices(u32, &expected, actual); +} + +test "latest score" { + const expected: u32 = 30; + const scores = [_]u32{ 100, 0, 90, 30 }; + const hs = HighScores.init(&scores); + const actual = hs.latest(); + try testing.expectEqual(expected, actual); +} + +test "personal best" { + const expected: u32 = 100; + const scores = [_]u32{ 40, 100, 70 }; + const hs = HighScores.init(&scores); + const actual = hs.personalBest(); + try testing.expectEqual(expected, actual); +} + +test "personal top three from a list of scores" { + const expected = [_]u32{ 100, 90, 70 }; + const scores = [_]u32{ 10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70 }; + const hs = HighScores.init(&scores); + var buffer: [3]u32 = undefined; + const actual = hs.personalTopThree(&buffer); + try testing.expectEqualSlices(u32, &expected, actual); +} + +test "personal top highest to lowest" { + const expected = [_]u32{ 30, 20, 10 }; + const scores = [_]u32{ 20, 10, 30 }; + const hs = HighScores.init(&scores); + var buffer: [3]u32 = undefined; + const actual = hs.personalTopThree(&buffer); + try testing.expectEqualSlices(u32, &expected, actual); +} + +test "personal top when there is a tie" { + const expected = [_]u32{ 40, 40, 30 }; + const scores = [_]u32{ 40, 20, 40, 30 }; + const hs = HighScores.init(&scores); + var buffer: [3]u32 = undefined; + const actual = hs.personalTopThree(&buffer); + try testing.expectEqualSlices(u32, &expected, actual); +} + +test "personal top when there are less than 3" { + const expected = [_]u32{ 70, 30 }; + const scores = [_]u32{ 30, 70 }; + const hs = HighScores.init(&scores); + var buffer: [3]u32 = undefined; + const actual = hs.personalTopThree(&buffer); + try testing.expectEqualSlices(u32, &expected, actual); +} + +test "personal top when there is only one" { + const expected = [_]u32{40}; + const scores = [_]u32{40}; + const hs = HighScores.init(&scores); + var buffer: [3]u32 = undefined; + const actual = hs.personalTopThree(&buffer); + try testing.expectEqualSlices(u32, &expected, actual); +} + +test "latest score after personal top scores" { + const expected: u32 = 30; + const scores = [_]u32{ 70, 50, 20, 30 }; + const hs = HighScores.init(&scores); + var buffer: [3]u32 = undefined; + _ = hs.personalTopThree(&buffer); + const actual = hs.latest(); + try testing.expectEqual(expected, actual); +} + +test "scores after personal top scores" { + const expected = [_]u32{ 30, 50, 20, 70 }; + const scores = [_]u32{ 30, 50, 20, 70 }; + const hs = HighScores.init(&scores); + var buffer: [3]u32 = undefined; + _ = hs.personalTopThree(&buffer); + const actual = hs.scores; + try testing.expectEqualSlices(u32, &expected, actual); +} + +test "latest score after personal best" { + const expected: u32 = 30; + const scores = [_]u32{ 20, 70, 15, 25, 30 }; + const hs = HighScores.init(&scores); + _ = hs.personalBest(); + const actual = hs.latest(); + try testing.expectEqual(expected, actual); +} + +test "scores after personal best" { + const expected = [_]u32{ 20, 70, 15, 25, 30 }; + const scores = [_]u32{ 20, 70, 15, 25, 30 }; + const hs = HighScores.init(&scores); + _ = hs.personalBest(); + const actual = hs.scores; + try testing.expectEqualSlices(u32, &expected, actual); +} From fd6fe614d3cab419c2ca9ba1370880afa58e2685 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 27 Mar 2023 20:00:02 +0200 Subject: [PATCH 2/9] exercises(high-scores): allow sorting in buffer --- exercises/practice/high-scores/.meta/example.zig | 13 +++++-------- exercises/practice/high-scores/high_scores.zig | 2 +- .../practice/high-scores/test_high_scores.zig | 14 +++++++------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/exercises/practice/high-scores/.meta/example.zig b/exercises/practice/high-scores/.meta/example.zig index 29824042..1646adb5 100644 --- a/exercises/practice/high-scores/.meta/example.zig +++ b/exercises/practice/high-scores/.meta/example.zig @@ -15,13 +15,10 @@ pub const HighScores = struct { return std.mem.max(u32, self.scores); } - pub fn personalTopThree(self: HighScores, buffer: *[3]u32) []u32 { - std.debug.assert(self.scores.len <= 100); - var sorted: [100]u32 = undefined; - std.mem.copy(u32, &sorted, self.scores); - std.sort.sort(u32, sorted[0..self.scores.len], {}, std.sort.desc(u32)); - var i = @min(self.scores.len, 3); - std.mem.copy(u32, buffer, sorted[0..i]); - return buffer[0..i]; + pub fn personalTopThree(self: HighScores, buffer: []u32) []u32 { + std.debug.assert(buffer.len >= self.scores.len); + std.mem.copy(u32, buffer, self.scores); + std.sort.sort(u32, buffer, {}, std.sort.desc(u32)); + return buffer[0..@min(buffer.len, 3)]; } }; diff --git a/exercises/practice/high-scores/high_scores.zig b/exercises/practice/high-scores/high_scores.zig index c6101504..17b6f91a 100644 --- a/exercises/practice/high-scores/high_scores.zig +++ b/exercises/practice/high-scores/high_scores.zig @@ -17,7 +17,7 @@ pub const HighScores = struct { @compileError("please implement the personalBest method"); } - pub fn personalTopThree(self: HighScores, buffer: *[3]u32) []u32 { + pub fn personalTopThree(self: HighScores, buffer: []u32) []u32 { _ = self; _ = buffer; @compileError("please implement the personalTopThree method"); diff --git a/exercises/practice/high-scores/test_high_scores.zig b/exercises/practice/high-scores/test_high_scores.zig index e49099a8..fb9f2926 100644 --- a/exercises/practice/high-scores/test_high_scores.zig +++ b/exercises/practice/high-scores/test_high_scores.zig @@ -32,7 +32,7 @@ test "personal top three from a list of scores" { const expected = [_]u32{ 100, 90, 70 }; const scores = [_]u32{ 10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70 }; const hs = HighScores.init(&scores); - var buffer: [3]u32 = undefined; + var buffer: [scores.len]u32 = undefined; const actual = hs.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } @@ -41,7 +41,7 @@ test "personal top highest to lowest" { const expected = [_]u32{ 30, 20, 10 }; const scores = [_]u32{ 20, 10, 30 }; const hs = HighScores.init(&scores); - var buffer: [3]u32 = undefined; + var buffer: [scores.len]u32 = undefined; const actual = hs.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } @@ -50,7 +50,7 @@ test "personal top when there is a tie" { const expected = [_]u32{ 40, 40, 30 }; const scores = [_]u32{ 40, 20, 40, 30 }; const hs = HighScores.init(&scores); - var buffer: [3]u32 = undefined; + var buffer: [scores.len]u32 = undefined; const actual = hs.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } @@ -59,7 +59,7 @@ test "personal top when there are less than 3" { const expected = [_]u32{ 70, 30 }; const scores = [_]u32{ 30, 70 }; const hs = HighScores.init(&scores); - var buffer: [3]u32 = undefined; + var buffer: [scores.len]u32 = undefined; const actual = hs.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } @@ -68,7 +68,7 @@ test "personal top when there is only one" { const expected = [_]u32{40}; const scores = [_]u32{40}; const hs = HighScores.init(&scores); - var buffer: [3]u32 = undefined; + var buffer: [scores.len]u32 = undefined; const actual = hs.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } @@ -77,7 +77,7 @@ test "latest score after personal top scores" { const expected: u32 = 30; const scores = [_]u32{ 70, 50, 20, 30 }; const hs = HighScores.init(&scores); - var buffer: [3]u32 = undefined; + var buffer: [scores.len]u32 = undefined; _ = hs.personalTopThree(&buffer); const actual = hs.latest(); try testing.expectEqual(expected, actual); @@ -87,7 +87,7 @@ test "scores after personal top scores" { const expected = [_]u32{ 30, 50, 20, 70 }; const scores = [_]u32{ 30, 50, 20, 70 }; const hs = HighScores.init(&scores); - var buffer: [3]u32 = undefined; + var buffer: [scores.len]u32 = undefined; _ = hs.personalTopThree(&buffer); const actual = hs.scores; try testing.expectEqualSlices(u32, &expected, actual); From 428b6415c2b50accc467deba29631cfe90536b4f Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 18 Sep 2023 18:20:01 +0200 Subject: [PATCH 3/9] exercises(high-scores): example: change std.sort.sort to std.mem.sort Zig 0.11.0 [1] removed std.sort.sort [2]. [1] https://ziglang.org/download/0.11.0/release-notes.html [2] https://github.com/ziglang/zig/commit/3db3cf77904e --- exercises/practice/high-scores/.meta/example.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/high-scores/.meta/example.zig b/exercises/practice/high-scores/.meta/example.zig index 1646adb5..25052095 100644 --- a/exercises/practice/high-scores/.meta/example.zig +++ b/exercises/practice/high-scores/.meta/example.zig @@ -18,7 +18,7 @@ pub const HighScores = struct { pub fn personalTopThree(self: HighScores, buffer: []u32) []u32 { std.debug.assert(buffer.len >= self.scores.len); std.mem.copy(u32, buffer, self.scores); - std.sort.sort(u32, buffer, {}, std.sort.desc(u32)); + std.mem.sort(u32, buffer, {}, std.sort.desc(u32)); return buffer[0..@min(buffer.len, 3)]; } }; From 81c838bb6244a199148c3ceaa10e166b09b03501 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:00:01 +0200 Subject: [PATCH 4/9] exercises(high-scores): example, stub: rename s to scores --- exercises/practice/high-scores/.meta/example.zig | 4 ++-- exercises/practice/high-scores/high_scores.zig | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/practice/high-scores/.meta/example.zig b/exercises/practice/high-scores/.meta/example.zig index 25052095..e332937f 100644 --- a/exercises/practice/high-scores/.meta/example.zig +++ b/exercises/practice/high-scores/.meta/example.zig @@ -3,8 +3,8 @@ const std = @import("std"); pub const HighScores = struct { scores: []const u32, - pub fn init(s: []const u32) HighScores { - return .{ .scores = s }; + pub fn init(scores: []const u32) HighScores { + return .{ .scores = scores }; } pub fn latest(self: HighScores) u32 { diff --git a/exercises/practice/high-scores/high_scores.zig b/exercises/practice/high-scores/high_scores.zig index 17b6f91a..30be2027 100644 --- a/exercises/practice/high-scores/high_scores.zig +++ b/exercises/practice/high-scores/high_scores.zig @@ -2,8 +2,8 @@ pub const HighScores = struct { // Please implement the field(s) of this struct. foo: void, - pub fn init(s: []const u32) HighScores { - _ = s; + pub fn init(scores: []const u32) HighScores { + _ = scores; @compileError("please implement the init method"); } From cbb4e461a02e9bbd1a0588f3208cb05e70393388 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:00:02 +0200 Subject: [PATCH 5/9] exercises(high-scores): example: improve assertion --- exercises/practice/high-scores/.meta/example.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/high-scores/.meta/example.zig b/exercises/practice/high-scores/.meta/example.zig index e332937f..ee0f25aa 100644 --- a/exercises/practice/high-scores/.meta/example.zig +++ b/exercises/practice/high-scores/.meta/example.zig @@ -16,7 +16,7 @@ pub const HighScores = struct { } pub fn personalTopThree(self: HighScores, buffer: []u32) []u32 { - std.debug.assert(buffer.len >= self.scores.len); + std.debug.assert(buffer.len == self.scores.len); std.mem.copy(u32, buffer, self.scores); std.mem.sort(u32, buffer, {}, std.sort.desc(u32)); return buffer[0..@min(buffer.len, 3)]; From fb88844ade67182f8630a1bf2db44238c9e688d1 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:00:03 +0200 Subject: [PATCH 6/9] exercises(high-scores): example: personalTopThree: add doc comment --- exercises/practice/high-scores/.meta/example.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/practice/high-scores/.meta/example.zig b/exercises/practice/high-scores/.meta/example.zig index ee0f25aa..20da8de9 100644 --- a/exercises/practice/high-scores/.meta/example.zig +++ b/exercises/practice/high-scores/.meta/example.zig @@ -15,6 +15,8 @@ pub const HighScores = struct { return std.mem.max(u32, self.scores); } + /// Writes (at most) the three highest scores from `self` into `buffer`. + /// Asserts `buffer.len == self.scores.len`. pub fn personalTopThree(self: HighScores, buffer: []u32) []u32 { std.debug.assert(buffer.len == self.scores.len); std.mem.copy(u32, buffer, self.scores); From 7baebf3bc5767f82f5c1d12a35d75d163e5a5b3f Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:00:04 +0200 Subject: [PATCH 7/9] exercises(high-scores): stub: personalTopThree: add doc comment --- exercises/practice/high-scores/high_scores.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/practice/high-scores/high_scores.zig b/exercises/practice/high-scores/high_scores.zig index 30be2027..49194137 100644 --- a/exercises/practice/high-scores/high_scores.zig +++ b/exercises/practice/high-scores/high_scores.zig @@ -17,6 +17,7 @@ pub const HighScores = struct { @compileError("please implement the personalBest method"); } + /// Writes (at most) the three highest scores from `self` into `buffer`. pub fn personalTopThree(self: HighScores, buffer: []u32) []u32 { _ = self; _ = buffer; From 01d81c05dde6e03b880cdd8156cb129277ea9ae6 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:00:05 +0200 Subject: [PATCH 8/9] exercises(high-scores): test: rename hs to h I find this more readable. --- .../practice/high-scores/test_high_scores.zig | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/exercises/practice/high-scores/test_high_scores.zig b/exercises/practice/high-scores/test_high_scores.zig index fb9f2926..65d75b17 100644 --- a/exercises/practice/high-scores/test_high_scores.zig +++ b/exercises/practice/high-scores/test_high_scores.zig @@ -7,106 +7,106 @@ const HighScores = high_scores.HighScores; test "list of scores" { const expected = [_]u32{ 30, 50, 20, 70 }; const scores = [_]u32{ 30, 50, 20, 70 }; - const hs = HighScores.init(&scores); - const actual = hs.scores; + const h = HighScores.init(&scores); + const actual = h.scores; try testing.expectEqualSlices(u32, &expected, actual); } test "latest score" { const expected: u32 = 30; const scores = [_]u32{ 100, 0, 90, 30 }; - const hs = HighScores.init(&scores); - const actual = hs.latest(); + const h = HighScores.init(&scores); + const actual = h.latest(); try testing.expectEqual(expected, actual); } test "personal best" { const expected: u32 = 100; const scores = [_]u32{ 40, 100, 70 }; - const hs = HighScores.init(&scores); - const actual = hs.personalBest(); + const h = HighScores.init(&scores); + const actual = h.personalBest(); try testing.expectEqual(expected, actual); } test "personal top three from a list of scores" { const expected = [_]u32{ 100, 90, 70 }; const scores = [_]u32{ 10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70 }; - const hs = HighScores.init(&scores); + const h = HighScores.init(&scores); var buffer: [scores.len]u32 = undefined; - const actual = hs.personalTopThree(&buffer); + const actual = h.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } test "personal top highest to lowest" { const expected = [_]u32{ 30, 20, 10 }; const scores = [_]u32{ 20, 10, 30 }; - const hs = HighScores.init(&scores); + const h = HighScores.init(&scores); var buffer: [scores.len]u32 = undefined; - const actual = hs.personalTopThree(&buffer); + const actual = h.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } test "personal top when there is a tie" { const expected = [_]u32{ 40, 40, 30 }; const scores = [_]u32{ 40, 20, 40, 30 }; - const hs = HighScores.init(&scores); + const h = HighScores.init(&scores); var buffer: [scores.len]u32 = undefined; - const actual = hs.personalTopThree(&buffer); + const actual = h.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } test "personal top when there are less than 3" { const expected = [_]u32{ 70, 30 }; const scores = [_]u32{ 30, 70 }; - const hs = HighScores.init(&scores); + const h = HighScores.init(&scores); var buffer: [scores.len]u32 = undefined; - const actual = hs.personalTopThree(&buffer); + const actual = h.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } test "personal top when there is only one" { const expected = [_]u32{40}; const scores = [_]u32{40}; - const hs = HighScores.init(&scores); + const h = HighScores.init(&scores); var buffer: [scores.len]u32 = undefined; - const actual = hs.personalTopThree(&buffer); + const actual = h.personalTopThree(&buffer); try testing.expectEqualSlices(u32, &expected, actual); } test "latest score after personal top scores" { const expected: u32 = 30; const scores = [_]u32{ 70, 50, 20, 30 }; - const hs = HighScores.init(&scores); + const h = HighScores.init(&scores); var buffer: [scores.len]u32 = undefined; - _ = hs.personalTopThree(&buffer); - const actual = hs.latest(); + _ = h.personalTopThree(&buffer); + const actual = h.latest(); try testing.expectEqual(expected, actual); } test "scores after personal top scores" { const expected = [_]u32{ 30, 50, 20, 70 }; const scores = [_]u32{ 30, 50, 20, 70 }; - const hs = HighScores.init(&scores); + const h = HighScores.init(&scores); var buffer: [scores.len]u32 = undefined; - _ = hs.personalTopThree(&buffer); - const actual = hs.scores; + _ = h.personalTopThree(&buffer); + const actual = h.scores; try testing.expectEqualSlices(u32, &expected, actual); } test "latest score after personal best" { const expected: u32 = 30; const scores = [_]u32{ 20, 70, 15, 25, 30 }; - const hs = HighScores.init(&scores); - _ = hs.personalBest(); - const actual = hs.latest(); + const h = HighScores.init(&scores); + _ = h.personalBest(); + const actual = h.latest(); try testing.expectEqual(expected, actual); } test "scores after personal best" { const expected = [_]u32{ 20, 70, 15, 25, 30 }; const scores = [_]u32{ 20, 70, 15, 25, 30 }; - const hs = HighScores.init(&scores); - _ = hs.personalBest(); - const actual = hs.scores; + const h = HighScores.init(&scores); + _ = h.personalBest(); + const actual = h.scores; try testing.expectEqualSlices(u32, &expected, actual); } From e583a02a72885747ee56daddb848ca756161c20a Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:00:06 +0200 Subject: [PATCH 9/9] exercises(high-scores): example, stub: init: add assert --- exercises/practice/high-scores/.meta/example.zig | 2 ++ exercises/practice/high-scores/high_scores.zig | 1 + 2 files changed, 3 insertions(+) diff --git a/exercises/practice/high-scores/.meta/example.zig b/exercises/practice/high-scores/.meta/example.zig index 20da8de9..a9e91313 100644 --- a/exercises/practice/high-scores/.meta/example.zig +++ b/exercises/practice/high-scores/.meta/example.zig @@ -3,7 +3,9 @@ const std = @import("std"); pub const HighScores = struct { scores: []const u32, + /// Asserts `scores.len > 0`. pub fn init(scores: []const u32) HighScores { + std.debug.assert(scores.len > 0); return .{ .scores = scores }; } diff --git a/exercises/practice/high-scores/high_scores.zig b/exercises/practice/high-scores/high_scores.zig index 49194137..ddbdb718 100644 --- a/exercises/practice/high-scores/high_scores.zig +++ b/exercises/practice/high-scores/high_scores.zig @@ -2,6 +2,7 @@ pub const HighScores = struct { // Please implement the field(s) of this struct. foo: void, + /// Asserts `scores.len > 0`. pub fn init(scores: []const u32) HighScores { _ = scores; @compileError("please implement the init method");