Skip to content

Commit

Permalink
exercises(grains): assert rather than error
Browse files Browse the repository at this point in the history
  • Loading branch information
ee7 committed Sep 21, 2023
1 parent 74fadd5 commit 8576b88
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions exercises/practice/grains/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const std = @import("std");

pub const ChessboardError = error{IndexOutOfBounds};

pub fn square(index: u7) ChessboardError!u64 {
const number_of_chess_squares = 64;
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));
}

Expand Down
2 changes: 2 additions & 0 deletions exercises/practice/grains/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ 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"
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"
3 changes: 2 additions & 1 deletion exercises/practice/grains/grains.zig
Original file line number Diff line number Diff line change
@@ -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");
}
Expand Down
27 changes: 7 additions & 20 deletions exercises/practice/grains/test_grains.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8576b88

Please sign in to comment.