implemented getNumRungs

This commit is contained in:
Jonathan Lamothe 2024-08-20 20:52:14 -04:00
parent 9bd9b4a05e
commit 24a1f2f10c
3 changed files with 22 additions and 3 deletions

View File

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

View File

@ -36,7 +36,7 @@ module Abacus.Internal (Abacus (..)) where
data Abacus = Abacus
{ abacusNumBeads :: Int
-- ^ The number of beads per rung of the abacus
, abacusRows :: [Int]
, abacusRungs :: [Int]
-- ^ The number of beads slid across each rung
} deriving (Eq, Show)

View File

@ -31,6 +31,7 @@ spec :: Spec
spec = describe "Abacus" $ do
newAbacusSpec
getNumBeadsSpec
getNumRungsSpec
newAbacusSpec :: Spec
newAbacusSpec = describe "newAbacusSpec" $ mapM_
@ -58,4 +59,18 @@ getNumBeadsSpec = describe "getNumBeads" $ mapM_
tenBeads = build 10
build n = fromJust $ newAbacus n 10
getNumRungsSpec :: Spec
getNumRungsSpec = describe "getNumRungs" $ mapM_
( \(desc, input, expected) -> context desc $ let
actual = getNumRungs input
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "one rung", oneRung, 1 )
, ( "ten rungs", tenRungs, 10 )
] where
oneRung = build 1
tenRungs = build 10
build n = fromJust $ newAbacus 10 n
--jl