-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checked versions of 'resize', 'truncateB', and 'fromIntegral'
Depending on the type 'resize', 'truncateB', and 'fromIntegral' either yield an XException or silently perform wrap-around if its argument does not fit in the resulting type's bounds. The added functions check the bound condition and fail with an error call if the condition is violated. They do not affect HDL generation. Useful in cases where runtime behavior should ensure that and out of bound or wrap around should not occur and users want their code to fail hard if this invariant is ever violated.
- Loading branch information
1 parent
e2f9b2c
commit ffcd5fb
Showing
5 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
changelog/2020-08-27T16_45_36+02_00_added_checked_functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
ADDED: Checked versions of 'resize', 'truncateB', and 'fromIntegral' | ||
|
||
Depending on the type 'resize', 'truncateB', and 'fromIntegral' either | ||
yield an XException or silently perform wrap-around if its argument does | ||
not fit in the resulting type's bounds. The added functions check the | ||
bound condition and fail with an error call if the condition is | ||
violated. They do not affect HDL generation. | ||
|
||
Useful in cases where runtime behavior should ensure that and out of | ||
bound or wrap around should not occur and users want their code to fail | ||
hard if this invariant is ever violated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
module Clash.Tests.Resize (tests) where | ||
|
||
import Control.DeepSeq (NFData) | ||
import Control.Exception (SomeException, try, evaluate) | ||
import Clash.XException (XException) | ||
import Data.Either (isLeft) | ||
import Data.Proxy (Proxy(Proxy)) | ||
import GHC.TypeNats (KnownNat, type (<=)) | ||
import Test.Tasty (TestTree, testGroup) | ||
import Test.Tasty.QuickCheck | ||
|
||
import qualified Clash.Class.Resize as Resize | ||
import Clash.Sized.Index | ||
|
||
-- | Anything that's in bounds should not cause an error | ||
indexProp :: | ||
forall a b. | ||
((a <= b), KnownNat a, KnownNat b) => | ||
Proxy b -> Index a -> Bool | ||
indexProp Proxy a = | ||
Resize.resize a == Resize.checkedResize @a @b a | ||
|
||
-- | Anything that's out of bounds should cause an error | ||
indexFailProp :: | ||
forall a b. | ||
((b <= a), KnownNat a, KnownNat b) => | ||
Proxy b -> Index a -> Property | ||
indexFailProp Proxy a = | ||
let checked = Resize.checkedResize @a @b a in | ||
if toInteger a > toInteger (maxBound @(Index b)) then | ||
expectExceptionNoX checked | ||
else | ||
discard | ||
|
||
-- | Succeed if evaluating leads to a non-XException Exception | ||
expectExceptionNoX :: (Show a, NFData a) => a -> Property | ||
expectExceptionNoX a0 = ioProperty $ do | ||
a1 <- try @SomeException (try @XException (evaluate a0)) | ||
pure $ | ||
counterexample | ||
("Expected non-XException Exception, got: " <> show a1) | ||
(isLeft a1) | ||
|
||
tests :: TestTree | ||
tests = testGroup "Resize" | ||
[ testGroup "checkedResize" | ||
[ testProperty "indexProp @17 @19" (indexProp @17 @19 Proxy) | ||
, testProperty "indexProp @19 @19" (indexProp @19 @19 Proxy) | ||
, testProperty "indexFailProp @37 @7" (indexFailProp @37 @7 Proxy) | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters