put margins on Hamming widget

This commit is contained in:
Jonathan Lamothe 2024-07-20 16:27:41 -04:00
parent 1565c1fe99
commit 7531cf192f
2 changed files with 119 additions and 19 deletions

View File

@ -24,6 +24,8 @@ License along with this program. If not, see
|-} |-}
{-# LANGUAGE TupleSections #-}
module Hamming.App.Widgets.Internal (hammingW') where module Hamming.App.Widgets.Internal (hammingW') where
import Data.Bits ((.&.)) import Data.Bits ((.&.))
@ -33,6 +35,15 @@ import Hamming.App.Types
hammingW' :: AppState -> [[([String], Char)]] hammingW' :: AppState -> [[([String], Char)]]
hammingW' state = let hammingW' state = let
header = ([], ' ') : map (["margin"],) ['0'..'3']
body = hammingBody state
in header : zipWith
(\n row -> (["margin"], n) : row)
['0'..'3']
body
hammingBody :: AppState -> [[([String], Char)]]
hammingBody state = let
code = state^.hammingCode code = state^.hammingCode
in map in map
( map ( map

View File

@ -36,16 +36,21 @@ spec = describe "Internal"
hammingW'Spec :: Spec hammingW'Spec :: Spec
hammingW'Spec = describe "hammingW'" $ mapM_ hammingW'Spec = describe "hammingW'" $ mapM_
( \(desc, state, expRes) -> context desc $ do ( \(desc, state, expRes) -> context desc $ do
let actRes = hammingW' state let
actRes = hammingW' state
numActRows = length actRes
numExpRows = length expRes
context "number of rows" $ context "number of rows" $
it "should be 4" $ it ("should be " ++ show numExpRows) $
length actRes `shouldBe` 4 numActRows `shouldBe` numExpRows
mapM_ mapM_
( \(rowNum, actRow, expRow) -> ( \(rowNum, actRow, expRow) -> let
context ("row " ++ show rowNum) $ do numActCols = length actRow
numExpCols = length expRow
in context ("row " ++ show rowNum) $ do
context "number of columns" $ context "number of columns" $
it "should be 4" $ it ("should be " ++ show numExpCols) $
length actRow `shouldBe` 4 numActCols `shouldBe` numExpCols
mapM_ mapM_
( \(colNum, actBit, expBit) -> ( \(colNum, actBit, expBit) ->
context ("column " ++ show colNum) $ context ("column " ++ show colNum) $
@ -60,7 +65,26 @@ hammingW'Spec = describe "hammingW'" $ mapM_
] where ] where
mkState c = initialState & hammingCode .~ c mkState c = initialState & hammingCode .~ c
allZero = allZero =
[ [ ( ["zero", "check"] [ [ ( []
, ' '
)
, ( ["margin"]
, '0'
)
, ( ["margin"]
, '1'
)
, ( ["margin"]
, '2'
)
, ( ["margin"]
, '3'
)
]
, [ ( ["margin"]
, '0'
)
, ( ["zero", "check"]
, '0' , '0'
) )
, ( ["check"] , ( ["check"]
@ -73,7 +97,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '0' , '0'
) )
] ]
, [ ( ["check"] , [ ( ["margin"]
, '1'
)
, ( ["check"]
, '0' , '0'
) )
, ( [] , ( []
@ -86,7 +113,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '0' , '0'
) )
] ]
, [ ( ["check"] , [ ( ["margin"]
, '2'
)
, ( ["check"]
, '0' , '0'
) )
, ( [] , ( []
@ -99,7 +129,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '0' , '0'
) )
] ]
, [ ( [] , [ ( ["margin"]
, '3'
)
, ( []
, '0' , '0'
) )
, ( [] , ( []
@ -114,7 +147,26 @@ hammingW'Spec = describe "hammingW'" $ mapM_
] ]
] ]
allOne = allOne =
[ [ ( ["zero", "check"] [ [ ( []
, ' '
)
, ( ["margin"]
, '0'
)
, ( ["margin"]
, '1'
)
, ( ["margin"]
, '2'
)
, ( ["margin"]
, '3'
)
]
, [ ( ["margin"]
, '0'
)
, ( ["zero", "check"]
, '1' , '1'
) )
, ( ["check"] , ( ["check"]
@ -127,7 +179,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '1' , '1'
) )
] ]
, [ ( ["check"] , [ ( ["margin"]
, '1'
)
, ( ["check"]
, '1' , '1'
) )
, ( [] , ( []
@ -140,7 +195,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '1' , '1'
) )
] ]
, [ ( ["check"] , [ ( ["margin"]
, '2'
)
, ( ["check"]
, '1' , '1'
) )
, ( [] , ( []
@ -153,7 +211,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '1' , '1'
) )
] ]
, [ ( [] , [ ( ["margin"]
, '3'
)
, ( []
, '1' , '1'
) )
, ( [] , ( []
@ -168,7 +229,26 @@ hammingW'Spec = describe "hammingW'" $ mapM_
] ]
] ]
arbitrary = arbitrary =
[ [ ( ["zero", "check"] [ [ ( []
, ' '
)
, ( ["margin"]
, '0'
)
, ( ["margin"]
, '1'
)
, ( ["margin"]
, '2'
)
, ( ["margin"]
, '3'
)
]
, [ ( ["margin"]
, '0'
)
, ( ["zero", "check"]
, '0' , '0'
) )
, ( ["check"] , ( ["check"]
@ -181,7 +261,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '1' , '1'
) )
] ]
, [ ( ["check"] , [ ( ["margin"]
, '1'
)
, ( ["check"]
, '1' , '1'
) )
, ( [] , ( []
@ -194,7 +277,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '0' , '0'
) )
] ]
, [ ( ["check"] , [ ( ["margin"]
, '2'
)
, ( ["check"]
, '0' , '0'
) )
, ( [] , ( []
@ -207,7 +293,10 @@ hammingW'Spec = describe "hammingW'" $ mapM_
, '1' , '1'
) )
] ]
, [ ( [] , [ ( ["margin"]
, '3'
)
, ( []
, '1' , '1'
) )
, ( [] , ( []