Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises(grains): assert, rather than returning error union #336

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 4 additions & 10 deletions exercises/practice/grains/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -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);
}
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