Skip to content

Commit 63ae22e

Browse files
Add regression test for #3000
1 parent da6da56 commit 63ae22e

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

clash-prelude/clash-prelude.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ test-suite unittests
452452
Clash.Tests.XException
453453

454454
Clash.Tests.Laws.Enum
455+
Clash.Tests.Laws.Num
455456
Clash.Tests.Laws.SaturatingNum
456457

457458
Hedgehog.Extra
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Clash.Tests.Laws.Num (tests) where
2+
3+
import Clash.Tests.Laws.SaturatingNum
4+
( genBoundedIntegral
5+
, genUnsigned
6+
)
7+
8+
import Test.Tasty
9+
import Test.Tasty.Hedgehog.Extra
10+
11+
import Clash.Sized.BitVector (Bit, BitVector)
12+
13+
import Control.DeepSeq (NFData)
14+
import GHC.TypeLits (KnownNat)
15+
16+
import Hedgehog
17+
18+
genBit :: Gen Bit
19+
genBit = genBoundedIntegral
20+
21+
genBitVector :: forall n. KnownNat n => Gen (BitVector n)
22+
genBitVector = genBoundedIntegral
23+
24+
additiveInverse :: (Num a, Show a, Eq a) => Gen a -> TestTree
25+
additiveInverse genA = testPropertyXXX "x + negate x == 0" $ property $ do
26+
a <- forAll genA
27+
a + negate a === 0
28+
29+
testAdditiveInverse :: (NFData a, Ord a, Show a, Eq a, Num a) => String -> Gen a -> TestTree
30+
testAdditiveInverse typeName genA =
31+
testGroup typeName [testGroup "additiveInverse" [additiveInverse genA]]
32+
33+
tests :: TestTree
34+
tests = testGroup "Num"
35+
[ testAdditiveInverse "Bit" genBit
36+
37+
, testAdditiveInverse "Unsigned 0" (genUnsigned @0)
38+
, testAdditiveInverse "Unsigned 1" (genUnsigned @1)
39+
, testAdditiveInverse "Unsigned 32" (genUnsigned @32)
40+
, testAdditiveInverse "Unsigned 127" (genUnsigned @127)
41+
, testAdditiveInverse "Unsigned 128" (genUnsigned @128)
42+
43+
, testAdditiveInverse "BitVector 0" (genBitVector @0)
44+
, testAdditiveInverse "BitVector 1" (genBitVector @1)
45+
, testAdditiveInverse "BitVector 32" (genBitVector @32)
46+
, testAdditiveInverse "BitVector 127" (genBitVector @127)
47+
, testAdditiveInverse "BitVector 128" (genBitVector @128)
48+
49+
-- TODO: Index, Signed, UFixed, SFixed. See discussion in
50+
-- https://github.com/clash-lang/clash-compiler/issues/3015
51+
]

clash-prelude/tests/Clash/Tests/Laws/SaturatingNum.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
{-# OPTIONS_GHC -fplugin=GHC.TypeLits.Normalise #-}
55
{-# OPTIONS_GHC -fplugin=GHC.TypeLits.KnownNat.Solver #-}
66

7-
module Clash.Tests.Laws.SaturatingNum (tests) where
7+
module Clash.Tests.Laws.SaturatingNum
8+
( tests
9+
10+
, genBoundedIntegral
11+
, genIndex
12+
, genSFixed
13+
, genSigned
14+
, genUFixed
15+
, genUnsigned
16+
) where
817

918
import Test.Tasty
1019
import Test.Tasty.Hedgehog.Extra

clash-prelude/tests/unittests.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import qualified Clash.Tests.Vector
2929
import qualified Clash.Tests.XException
3030

3131
import qualified Clash.Tests.Laws.Enum
32+
import qualified Clash.Tests.Laws.Num
3233
import qualified Clash.Tests.Laws.SaturatingNum
3334

3435
tests :: TestTree
@@ -60,6 +61,7 @@ tests = testGroup "Unittests"
6061
, Clash.Tests.XException.tests
6162
, testGroup "Laws"
6263
[ Clash.Tests.Laws.Enum.tests
64+
, Clash.Tests.Laws.Num.tests
6365
, Clash.Tests.Laws.SaturatingNum.tests
6466
]
6567
]

0 commit comments

Comments
 (0)