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

165 lines
4.3 KiB
Haskell
Raw Normal View History

2024-07-19 20:50:33 -04:00
{-
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
2024-07-20 16:27:41 -04:00
let
actRes = hammingW' state
numActRows = length actRes
numExpRows = length expRes
2024-07-19 20:50:33 -04:00
context "number of rows" $
2024-07-20 16:27:41 -04:00
it ("should be " ++ show numExpRows) $
numActRows `shouldBe` numExpRows
2024-07-19 20:50:33 -04:00
mapM_
2024-07-25 20:06:13 -04:00
( \(row, actRow, expRow) -> let
2024-07-20 16:27:41 -04:00
numActCols = length actRow
numExpCols = length expRow
2024-07-25 20:06:13 -04:00
in context ("row " ++ show row) $ do
2024-07-19 20:50:33 -04:00
context "number of columns" $
2024-07-20 16:27:41 -04:00
it ("should be " ++ show numExpCols) $
numActCols `shouldBe` numExpCols
2024-07-19 20:50:33 -04:00
mapM_
2024-07-25 20:06:13 -04:00
( \(col, actBit, expBit) ->
context ("column " ++ show col) $
2024-07-19 20:50:33 -04:00
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 =
[ [ ( mempty, ' ' )
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
2024-07-20 16:27:41 -04:00
]
, [ ( marginAttr, '0' )
, ( zeroAttr, '0' )
, ( checkAttr, '0' )
, ( checkAttr, '0' )
, ( mempty, '0' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '1' )
, ( checkAttr, '0' )
, ( mempty, '0' )
, ( mempty, '0' )
, ( mempty, '0' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '2' )
, ( checkAttr, '0' )
, ( mempty, '0' )
, ( mempty, '0' )
, ( mempty, '0' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '3' )
, ( mempty, '0' )
, ( mempty, '0' )
, ( mempty, '0' )
, ( mempty, '0' )
2024-07-19 20:50:33 -04:00
]
]
allOne =
[ [ ( mempty, ' ' )
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
2024-07-20 16:27:41 -04:00
]
, [ ( marginAttr, '0' )
, ( zeroAttr, '1' )
, ( checkAttr, '1' )
, ( checkAttr, '1' )
, ( mempty, '1' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '1' )
, ( checkAttr, '1' )
, ( mempty, '1' )
, ( mempty, '1' )
, ( mempty, '1' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '2' )
, ( checkAttr, '1' )
, ( mempty, '1' )
, ( mempty, '1' )
, ( mempty, '1' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '3' )
, ( mempty, '1' )
, ( mempty, '1' )
, ( mempty, '1' )
, ( mempty, '1' )
2024-07-19 20:50:33 -04:00
]
]
arbitrary =
[ [ ( mempty, ' ' )
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
2024-07-20 16:27:41 -04:00
]
, [ ( marginAttr, '0' )
, ( zeroAttr, '0' )
, ( checkAttr, '1' )
, ( checkAttr, '0' )
, ( mempty, '1' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '1' )
, ( checkAttr, '1' )
, ( mempty, '0' )
, ( mempty, '1' )
, ( mempty, '0' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '2' )
, ( checkAttr, '0' )
, ( mempty, '0' )
, ( mempty, '1' )
, ( mempty, '1' )
2024-07-19 20:50:33 -04:00
]
, [ ( marginAttr, '3' )
, ( mempty, '1' )
, ( mempty, '1' )
, ( mempty, '0' )
, ( mempty, '0' )
2024-07-19 20:50:33 -04:00
]
]
--jl