Skip to content

Commit

Permalink
Add and fix up testcases for hLit/oLit don't cares
Browse files Browse the repository at this point in the history
clash-lang#2505 (comment)
Also adjusts comments/documentation so the precise strings are shown.
  • Loading branch information
NadiaYvette committed Jul 30, 2023
1 parent 0b2660f commit fed1136
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 8 additions & 6 deletions clash-prelude/src/Clash/Sized/Internal/BitVector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,9 @@ read# cs0 = (fromIntegral (length cs1), BV m v)
++ show c ++ " in input: " ++ cs0

-- | Create a hexadecimal literal.
-- >>> $(hLit "dead")
-- 0b1101111010101101
-- ghci> $(hLit "dead")
-- 0b1101_1110_1010_1101
-- it :: BitVector 16
--
-- Don't care digits set 4 bits in the undefined mask.
hLit :: String -> ExpQ
Expand All @@ -580,16 +581,17 @@ read16# cs0 = (fromIntegral $ 4 * length cs1, BV m v)
v = combineHexDigits vs
m = combineHexDigits ms
-- The dot is a don't care, which applies to a whole digit.
readHexDigit '.' = (0, 0xf)
readHexDigit c = case readHex [c] of
[(n, "")] -> (n, 0)
[(_, ".")] -> (0, 0xf)
_ -> error $
"Clash.Sized.Internal.hLit: unknown character: "
++ show c ++ " in input: " ++ cs0

-- | Create an octal literal.
-- >>> $(oLit "5234")
-- 0b101011010100
-- ghci> $(oLit "5234")
-- 0b1010_1001_1100
-- it :: BitVector 12
--
-- Don't care digits set 3 bits in the undefined mask.
oLit :: String -> ExpQ
Expand All @@ -610,9 +612,9 @@ read8# cs0 = (fromIntegral $ 3 * length cs1, BV m v)
v = combineOctDigits vs
m = combineOctDigits ms
-- The dot is a don't care, which applies to a whole digit.
readOctDigit '.' = (0, 0o7)
readOctDigit c = case readOct [c] of
[(n, "")] -> (n, 0)
[(_, ".")] -> (0, 0o7)
_ -> error $
"Clash.Sized.Internal.oLit: unknown character: "
++ show c ++ " in input: " ++ cs0
Expand Down
4 changes: 4 additions & 0 deletions clash-prelude/tests/Clash/Tests/BitVector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ tests = localOption (Q.QuickCheckMaxRatio 2) $ testGroup "All"
, testCase "show13" $ show @(BitVector 9) $(bLit "10001010.") @?= "0b1_0001_010."

, testCase "show14" $ show @(BitVector 16) $(hLit "dead") @?= "0b1101_1110_1010_1101"
, testCase "show14" $ show @(BitVector 16) $(hLit "de.d") @?= "0b1101_1110_...._1101"
, testCase "show15" $ show @(BitVector 16) $(hLit "beef") @?= "0b1011_1110_1110_1111"
, testCase "show15" $ show @(BitVector 16) $(hLit ".eef") @?= "0b...._1110_1110_1111"
, testCase "show16" $ show @(BitVector 12) $(oLit "7734") @?= "0b1111_1101_1100"
, testCase "show16" $ show @(BitVector 12) $(oLit "77.4") @?= "0b1111_11.._.100"
, testCase "show17" $ show @(BitVector 12) $(oLit "5324") @?= "0b1010_1101_0100"
, testCase "show17" $ show @(BitVector 12) $(oLit ".324") @?= "0b...0_1101_0100"
]
]

Expand Down

0 comments on commit fed1136

Please sign in to comment.