Skip to content

Commit

Permalink
Simpler error return value for grains
Browse files Browse the repository at this point in the history
  • Loading branch information
gvrooyen committed Sep 4, 2024
1 parent eb874d7 commit 03c05ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
18 changes: 6 additions & 12 deletions exercises/practice/grains/.meta/grains_example.odin
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package grains

Error :: enum {
None = 0,
InvalidSquare,
NotImplemented,
}

/* Calculate the number of grains on the specified square and return the resulting count, as well
as the sum of grains on this and all previous squares.
*/
count :: proc(n: int) -> (u64, u64) {
count :: proc(n: int) -> (on_square: u64, total: u64) {
acc: u64 = 1
val: u64 = 1

Expand All @@ -22,14 +16,14 @@ count :: proc(n: int) -> (u64, u64) {
}

// Returns the number of grains on the specified square.
square :: proc(n: int) -> (u64, Error) {
if n < 1 || n > 64 do return 0, .InvalidSquare
square :: proc(n: int) -> (result: u64, ok: bool) {
if n < 1 || n > 64 do return 0, false
c, _ := count(n)
return c, .None
return c, true
}

// Returns the total number of squares on the board.
total :: proc() -> (u64, Error) {
total :: proc() -> (result: u64, ok: bool) {
_, t := count(64)
return t, .None
return t, true
}
50 changes: 25 additions & 25 deletions exercises/practice/grains/grains_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,95 @@ import "core:testing"
test_returns_the_number_of_grains_on_the_square_grains_on_square_1 :: proc(
t: ^testing.T,
) {
c, e := square(1)
c, ok := square(1)
testing.expect_value(t, c, 1)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_grains_on_square_2 :: proc(
t: ^testing.T,
) {
c, e := square(2)
c, ok := square(2)
testing.expect_value(t, c, 2)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_grains_on_square_3 :: proc(
t: ^testing.T,
) {
c, e := square(3)
c, ok := square(3)
testing.expect_value(t, c, 4)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_grains_on_square_4 :: proc(
t: ^testing.T,
) {
c, e := square(4)
c, ok := square(4)
testing.expect_value(t, c, 8)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_grains_on_square_16 :: proc(
t: ^testing.T,
) {
c, e := square(16)
c, ok := square(16)
testing.expect_value(t, c, 32_768)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_grains_on_square_32 :: proc(
t: ^testing.T,
) {
c, e := square(32)
c, ok := square(32)
testing.expect_value(t, c, 2_147_483_648)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_grains_on_square_64 :: proc(
t: ^testing.T,
) {
c, e := square(64)
c, ok := square(64)
testing.expect_value(t, c, 9_223_372_036_854_775_808)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_square_0_raises_an_exception :: proc(
test_returns_the_number_of_grains_on_the_square_square_0_is_an_error :: proc(
t: ^testing.T,
) {
c, e := square(0)
c, ok := square(0)
testing.expect_value(t, c, 0)
testing.expect_value(t, e, Error.InvalidSquare)
testing.expect(t, !ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_negative_square_raises_an_exception :: proc(
test_returns_the_number_of_grains_on_the_square_negative_square_is_an_error :: proc(
t: ^testing.T,
) {
c, e := square(-1)
c, ok := square(-1)
testing.expect_value(t, c, 0)
testing.expect_value(t, e, Error.InvalidSquare)
testing.expect(t, !ok)
}

// @(test)
test_returns_the_number_of_grains_on_the_square_square_greater_than_64_raises_an_exception :: proc(
test_returns_the_number_of_grains_on_the_square_square_greater_than_64_is_an_error :: proc(
t: ^testing.T,
) {
c, e := square(65)
c, ok := square(65)
testing.expect_value(t, c, 0)
testing.expect_value(t, e, Error.InvalidSquare)
testing.expect(t, !ok)
}

// @(test)
test_returns_the_total_number_of_grains_on_the_board :: proc(t: ^testing.T) {
c, e := total()
c, ok := total()
testing.expect_value(t, c, 18_446_744_073_709_551_615)
testing.expect_value(t, e, Error.None)
testing.expect(t, ok)
}

0 comments on commit 03c05ff

Please sign in to comment.