1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
{-|
Module : Parser
Description : Test the parser.
Copyright : (c) 2019, Yann Herklotz Grave
License : GPL-3
Maintainer : ymherklotz [at] gmail [dot] com
Stability : experimental
Portability : POSIX
Test the parser.
-}
module Parser
( parserTests
, parseUnitTests
)
where
import Data.Either (either, isRight)
import Hedgehog (Gen, Property, (===))
import qualified Hedgehog as Hog
import qualified Hedgehog.Gen as Hog
import Test.Tasty
import Test.Tasty.Hedgehog
import Test.Tasty.HUnit
import Text.Parsec
import VeriFuzz
import VeriFuzz.Verilog.Lex
import VeriFuzz.Verilog.Parser
randomMod' :: Gen ModDecl
randomMod' = Hog.resize 20 (randomMod 3 10)
parserInput :: Property
parserInput = Hog.property $ do
v <- Hog.forAll randomMod'
Hog.assert . isRight $ parse parseModDecl
"input_test.v"
(alexScanTokens $ str v)
where str = show . GenVerilog
parserIdempotent :: Property
parserIdempotent = Hog.property $ do
v <- Hog.forAll randomMod'
let sv = vshow v
p sv === (p . p) sv
where
vshow = show . GenVerilog
p sv =
either (\x -> show x <> "\n" <> sv) vshow
. parse parseModDecl "idempotent_test.v"
$ alexScanTokens sv
parserTests :: TestTree
parserTests = testGroup "Parser properties"
[ testProperty "Input" parserInput
, testProperty "Idempotence" parserIdempotent
]
testParse :: (Eq a, Show a) => Parser a -> String -> String -> a -> TestTree
testParse p name input golden = testCase name $
case parse p "testcase" (alexScanTokens $ input) of
Left e -> assertFailure $ show e
Right result -> golden @=? result
testParseFail :: (Eq a, Show a) => Parser a -> String -> String -> TestTree
testParseFail p name input = testCase name $
case parse p "testcase" (alexScanTokens $ input) of
Left _ -> return ()
Right _ -> assertFailure "Parse incorrectly succeeded"
parseEventUnit :: TestTree
parseEventUnit =
testGroup "Event"
[ testFailure "No empty event" "@()"
, test "@*" EAll
, test "@(*)" EAll
, test "@(posedge clk)" $ EPosEdge "clk"
, test "@(negedge clk)" $ ENegEdge "clk"
, test "@(wire1)" $ EId "wire1"
, test "@(a or b or c or d)" $ EOr (EId "a") (EOr (EId "b") (EOr (EId "c") (EId "d")))
, test "@(a, b, c, d)" $ EComb (EId "a") (EComb (EId "b") (EComb (EId "c") (EId "d")))
]
where
test a = testParse parseEvent ("Test " <> a) a
testFailure = testParseFail parseEvent
parseUnitTests :: TestTree
parseUnitTests =
testGroup "Parser unit"
[ parseEventUnit
]
|