implemented getNumBeads

This commit is contained in:
Jonathan Lamothe 2024-08-20 20:41:28 -04:00
parent f43eb607ff
commit 9bd9b4a05e
2 changed files with 22 additions and 2 deletions

View File

@ -24,7 +24,7 @@ License along with this program. If not, see
-} -}
module Abacus (Abacus, newAbacus) where module Abacus (Abacus, newAbacus, getNumBeads) where
import Abacus.Internal import Abacus.Internal
@ -40,4 +40,8 @@ newAbacus beads rungs = if beads < 1 || rungs < 1
then Nothing then Nothing
else Just $ Abacus beads $ replicate rungs 0 else Just $ Abacus beads $ replicate rungs 0
-- | Returns the number of beads per rung in an abacus
getNumBeads :: Abacus -> Int
getNumBeads = abacusNumBeads
--jl --jl

View File

@ -21,14 +21,16 @@ License along with this program. If not, see
module AbacusSpec (spec) where module AbacusSpec (spec) where
import Data.Maybe (fromJust)
import Test.Hspec (Spec, context, describe, it, shouldBe) import Test.Hspec (Spec, context, describe, it, shouldBe)
import Abacus import Abacus
import Abacus.Internal import Abacus.Internal
spec :: Spec spec :: Spec
spec = describe "Abacus" spec = describe "Abacus" $ do
newAbacusSpec newAbacusSpec
getNumBeadsSpec
newAbacusSpec :: Spec newAbacusSpec :: Spec
newAbacusSpec = describe "newAbacusSpec" $ mapM_ newAbacusSpec = describe "newAbacusSpec" $ mapM_
@ -42,4 +44,18 @@ newAbacusSpec = describe "newAbacusSpec" $ mapM_
, ( "no rungs", 10, 0, Nothing ) , ( "no rungs", 10, 0, Nothing )
] ]
getNumBeadsSpec :: Spec
getNumBeadsSpec = describe "getNumBeads" $ mapM_
( \(desc, input, expected) -> context desc $ let
actual = getNumBeads input
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "one bead", oneBead, 1 )
, ( "ten beads", tenBeads, 10 )
] where
oneBead = build 1
tenBeads = build 10
build n = fromJust $ newAbacus n 10
--jl --jl