hamming/test/Hamming/App/Widgets/InternalSpec.hs

226 lines
3.9 KiB
Haskell

{-
hamming
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
<https://www.gnu.org/licenses/>.
-}
module Hamming.App.Widgets.InternalSpec (spec) where
import Lens.Micro ((&), (.~))
import Test.Hspec (Spec, context, describe, it, shouldBe)
import Hamming.App
import Hamming.App.Types
import Hamming.App.Widgets.Internal
spec :: Spec
spec = describe "Internal"
hammingW'Spec
hammingW'Spec :: Spec
hammingW'Spec = describe "hammingW'" $ mapM_
( \(desc, state, expRes) -> context desc $ do
let actRes = hammingW' state
context "number of rows" $
it "should be 4" $
length actRes `shouldBe` 4
mapM_
( \(rowNum, actRow, expRow) ->
context ("row " ++ show rowNum) $ do
context "number of columns" $
it "should be 4" $
length actRow `shouldBe` 4
mapM_
( \(colNum, actBit, expBit) ->
context ("column " ++ show colNum) $
it ("should be " ++ show expBit) $
actBit `shouldBe` expBit
) $ zip3 [(0 :: Int)..] actRow expRow
) $ zip3 [(0 :: Int)..] actRes expRes
)
[ ( "all zero", mkState 0, allZero )
, ( "all one", mkState 0xffff, allOne )
, ( "arbitrary", mkState 0x3c5a, arbitrary )
] where
mkState c = initialState & hammingCode .~ c
allZero =
[ [ ( ["zero", "check"]
, '0'
)
, ( ["check"]
, '0'
)
, ( ["check"]
, '0'
)
, ( []
, '0'
)
]
, [ ( ["check"]
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
]
, [ ( ["check"]
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
]
, [ ( []
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
]
]
allOne =
[ [ ( ["zero", "check"]
, '1'
)
, ( ["check"]
, '1'
)
, ( ["check"]
, '1'
)
, ( []
, '1'
)
]
, [ ( ["check"]
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
]
, [ ( ["check"]
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
]
, [ ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
]
]
arbitrary =
[ [ ( ["zero", "check"]
, '0'
)
, ( ["check"]
, '1'
)
, ( ["check"]
, '0'
)
, ( []
, '1'
)
]
, [ ( ["check"]
, '1'
)
, ( []
, '0'
)
, ( []
, '1'
)
, ( []
, '0'
)
]
, [ ( ["check"]
, '0'
)
, ( []
, '0'
)
, ( []
, '1'
)
, ( []
, '1'
)
]
, [ ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '0'
)
, ( []
, '0'
)
]
]
--jl