From 03c05ff06c0411fb4099715e2ba154e9a50fc6f7 Mon Sep 17 00:00:00 2001 From: G-J van Rooyen Date: Wed, 4 Sep 2024 15:03:44 +0200 Subject: [PATCH] Simpler error return value for grains --- .../practice/grains/.meta/grains_example.odin | 18 +++---- exercises/practice/grains/grains_test.odin | 50 +++++++++---------- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/exercises/practice/grains/.meta/grains_example.odin b/exercises/practice/grains/.meta/grains_example.odin index be44b1c..1dde817 100644 --- a/exercises/practice/grains/.meta/grains_example.odin +++ b/exercises/practice/grains/.meta/grains_example.odin @@ -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 @@ -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 } diff --git a/exercises/practice/grains/grains_test.odin b/exercises/practice/grains/grains_test.odin index 710639f..7f637ea 100644 --- a/exercises/practice/grains/grains_test.odin +++ b/exercises/practice/grains/grains_test.odin @@ -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) }