diff --git a/config.json b/config.json index 18a74d5c..b0d18aab 100644 --- a/config.json +++ b/config.json @@ -190,17 +190,17 @@ "slug": "grains", "name": "Grains", "practices": [ + "asserting", "bitwise-operations", "builtin-functions", "conditionals", - "error-sets", "functions" ], "prerequisites": [ + "asserting", "bitwise-operations", "builtin-functions", "conditionals", - "error-sets", "functions" ], "difficulty": 1 diff --git a/exercises/practice/grains/.meta/example.zig b/exercises/practice/grains/.meta/example.zig index bcd0d47b..de7d055d 100644 --- a/exercises/practice/grains/.meta/example.zig +++ b/exercises/practice/grains/.meta/example.zig @@ -1,17 +1,11 @@ const std = @import("std"); -const math = std.math; -pub const ChessboardError = error{IndexOutOfBounds}; - -const number_of_chess_squares = 64; - -pub fn square(index: usize) ChessboardError!u64 { - if (index > number_of_chess_squares or index == 0) { - return ChessboardError.IndexOutOfBounds; - } +/// Asserts that `index` is greater than 0 and less than 65. +pub fn square(index: u7) u64 { + std.debug.assert(index > 0 and index < 65); return @as(u64, 1) << @as(u6, @truncate(index - 1)); } pub fn total() u64 { - return math.maxInt(u64); + return std.math.maxInt(u64); } diff --git a/exercises/practice/grains/.meta/tests.toml b/exercises/practice/grains/.meta/tests.toml index 661b1604..e250bdc1 100644 --- a/exercises/practice/grains/.meta/tests.toml +++ b/exercises/practice/grains/.meta/tests.toml @@ -32,6 +32,7 @@ description = "returns the number of grains on the square -> grains on square 64 [1d47d832-3e85-4974-9466-5bd35af484e3] description = "returns the number of grains on the square -> square 0 raises an exception" +include = false [61974483-eeb2-465e-be54-ca5dde366453] description = "returns the number of grains on the square -> negative square raises an exception" @@ -39,6 +40,7 @@ include = false [a95e4374-f32c-45a7-a10d-ffec475c012f] description = "returns the number of grains on the square -> square greater than 64 raises an exception" +include = false [6eb07385-3659-4b45-a6be-9dc474222750] description = "returns the total number of grains on the board" diff --git a/exercises/practice/grains/grains.zig b/exercises/practice/grains/grains.zig index 04e294f9..c6854dd7 100644 --- a/exercises/practice/grains/grains.zig +++ b/exercises/practice/grains/grains.zig @@ -1,4 +1,5 @@ -pub fn square(index: usize) ChessboardError!u64 { +/// Asserts that `index` is greater than 0 and less than 65. +pub fn square(index: usize) u64 { _ = index; @compileError("please implement the square function"); } diff --git a/exercises/practice/grains/test_grains.zig b/exercises/practice/grains/test_grains.zig index 37f2df64..e4b51731 100644 --- a/exercises/practice/grains/test_grains.zig +++ b/exercises/practice/grains/test_grains.zig @@ -2,62 +2,49 @@ const std = @import("std"); const testing = std.testing; const grains = @import("grains.zig"); -const ChessboardError = grains.ChessboardError; test "grains on square 1" { const expected: u64 = 1; - const actual = try grains.square(1); + const actual = grains.square(1); try testing.expectEqual(expected, actual); } test "grains on square 2" { const expected: u64 = 2; - const actual = try grains.square(2); + const actual = grains.square(2); try testing.expectEqual(expected, actual); } test "grains on square 3" { const expected: u64 = 4; - const actual = try grains.square(3); + const actual = grains.square(3); try testing.expectEqual(expected, actual); } test "grains on square 4" { const expected: u64 = 8; - const actual = try grains.square(4); + const actual = grains.square(4); try testing.expectEqual(expected, actual); } test "grains on square 16" { const expected: u64 = 32_768; - const actual = try grains.square(16); + const actual = grains.square(16); try testing.expectEqual(expected, actual); } test "grains on square 32" { const expected: u64 = 2_147_483_648; - const actual = try grains.square(32); + const actual = grains.square(32); try testing.expectEqual(expected, actual); } test "grains on square 64" { const expected: u64 = 9_223_372_036_854_775_808; - const actual = try grains.square(64); + const actual = grains.square(64); try testing.expectEqual(expected, actual); } -test "square 0 produces an error" { - const expected = ChessboardError.IndexOutOfBounds; - const actual = grains.square(0); - try testing.expectError(expected, actual); -} - -test "square greater than 64 produces an error" { - const expected = ChessboardError.IndexOutOfBounds; - const actual = grains.square(65); - try testing.expectError(expected, actual); -} - test "returns the total number of grains on the board" { const expected: u64 = 18_446_744_073_709_551_615; const actual = grains.total();