Skip to content

Commit de5df64

Browse files
committed
Testing infrastructure
1 parent 46ad625 commit de5df64

File tree

7 files changed

+102
-81
lines changed

7 files changed

+102
-81
lines changed

TODO.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# TODO
22

3-
- Write real tests, using exposed Internal module
3+
- More tests (including Internal module)
4+
- Available Expression Analysis

ppa.cabal

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ library
2020
exposed-modules: PPA.Lang.While.Syntax PPA.Lang.While.Internal.Syntax PPA.Lang.While.Util
2121
-- other-modules:
2222
other-extensions: FlexibleContexts
23-
build-depends: base >=4.8 && <4.9, mtl >=2.2 && <2.3, parsec >=3.1 && <3.2, containers >=0.5 && <0.6
23+
build-depends: base == 4.8.*, parsec == 3.1.*, containers == 0.5.*
2424
hs-source-dirs: src
2525
default-language: Haskell2010
2626

2727
test-suite tests
2828
type: exitcode-stdio-1.0
29+
other-modules: PPATest.Common
2930
main-is: Main.hs
3031
hs-source-dirs: test
31-
build-depends: base, containers >=0.5 && <0.6, ppa
32+
build-depends: base == 4.8.*, containers == 0.5.*, hspec == 2.2.*, ppa
33+
default-language: Haskell2010

test/Main.hs

+11-78
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,17 @@
11
module Main (main) where
22

3-
import Prelude hiding (init)
3+
import Test.Hspec
44

5-
import qualified Data.Set as Set
6-
import qualified Data.List as List
5+
import qualified PPATest.Lang.While.SyntaxSpec
6+
import qualified PPATest.Lang.While.UtilSpec
77

8-
import PPA.Lang.While.Internal.Syntax
9-
import PPA.Lang.While.Util
10-
11-
-- Convenience
12-
13-
printStmt :: Stmt -> IO ()
14-
printStmt = putStrLn . showL
15-
16-
printSet :: (Show a) => Set.Set a -> IO ()
17-
printSet s = putStrLn $ "{" ++ es ++ "}"
18-
where es = concat $ List.intersperse ", " $ map show $ Set.toList s
19-
20-
printCase :: String -> String -> IO ()
21-
printCase name prog = do
22-
let s = read prog
23-
putStr $ name ++ ": "
24-
printStmt s
25-
putStr $ "labels(" ++ name ++ ") = "
26-
printSet $ labels s
27-
putStr $ "init(" ++ name ++ ") = "
28-
print $ init s
29-
putStr $ "flow(" ++ name ++ ") = "
30-
printSet $ flow s
31-
putStr $ "final(" ++ name ++ ") = "
32-
printSet $ final s
33-
34-
-- Progs
35-
36-
ifBasic :: String
37-
ifBasic =
38-
" if (a > 0 || a < 0) then \
39-
\ (x := -2) \
40-
\ else \
41-
\ (skip)"
42-
43-
ifTwice :: String
44-
ifTwice =
45-
" if (a > 0 || a < 0) then \
46-
\ (x := -2) \
47-
\ else \
48-
\ (skip); \
49-
\ if (b < 5) then \
50-
\ (y := 1) \
51-
\ else \
52-
\ (skip)"
53-
54-
{- y^x
55-
- result stored in z -}
56-
power :: String
57-
power =
58-
" z := 1; \
59-
\ while (x > 0) do \
60-
\ (z := z * y; \
61-
\ x := x - 1)"
62-
63-
{- x!
64-
- result stored in z -}
65-
factorial :: String
66-
factorial =
67-
" y := x; \
68-
\ z := 1; \
69-
\ while (y > 1) do \
70-
\ (z := z * y; \
71-
\ y := y - 1); \
72-
\ y := 0"
73-
74-
-- Harness
8+
import qualified PPATest.Lang.While.Internal.SyntaxSpec
759

7610
main :: IO ()
77-
main = do
78-
printCase "power" power
79-
putStrLn ""
80-
printCase "factorial" factorial
81-
putStrLn ""
82-
printCase "ifBasic" ifBasic
83-
putStrLn ""
84-
printCase "ifTwice" ifTwice
11+
main = hspec spec
12+
13+
spec :: Spec
14+
spec = do
15+
describe "Lang.While.SyntaxSpec" PPATest.Lang.While.SyntaxSpec.spec
16+
describe "Lang.While.UtilSpec" PPATest.Lang.While.UtilSpec.spec
17+
describe "Lang.While.Internal.SyntaxSpec" PPATest.Lang.While.Internal.SyntaxSpec.spec

test/PPATest/Common.hs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module PPATest.Common where
2+
3+
-- Progs
4+
5+
ifBasic :: String
6+
ifBasic =
7+
" if (a > 0 || a < 0) then \
8+
\ (x := -2) \
9+
\ else \
10+
\ (skip)"
11+
12+
ifTwice :: String
13+
ifTwice =
14+
" if (a > 0 || a < 0) then \
15+
\ (x := -2) \
16+
\ else \
17+
\ (skip); \
18+
\ if (b < 5) then \
19+
\ (y := 1) \
20+
\ else \
21+
\ (skip)"
22+
23+
{- y^x
24+
- result stored in z -}
25+
power :: String
26+
power =
27+
" z := 1; \
28+
\ while (x > 0) do \
29+
\ (z := z * y; \
30+
\ x := x - 1)"
31+
32+
{- x!
33+
- result stored in z -}
34+
factorial :: String
35+
factorial =
36+
" y := x; \
37+
\ z := 1; \
38+
\ while (y > 1) do \
39+
\ (z := z * y; \
40+
\ y := y - 1); \
41+
\ y := 0"
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module PPATest.Lang.While.Internal.SyntaxSpec (spec) where
2+
3+
import Test.Hspec
4+
5+
spec :: Spec
6+
spec = return ()

test/PPATest/Lang/While/SyntaxSpec.hs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module PPATest.Lang.While.SyntaxSpec (spec) where
2+
3+
import Test.Hspec
4+
5+
import PPATest.Common
6+
7+
import PPA.Lang.While.Syntax
8+
9+
spec :: Spec
10+
spec = do
11+
describe "read . show" $ do
12+
it "-- ifBasic" $
13+
(read . show) ifBasic `shouldBe` ifBasic
14+
it "-- ifTwice" $
15+
(read . show) ifTwice `shouldBe` ifTwice
16+
it "-- power" $
17+
(read . show) power `shouldBe` power
18+
it "-- factorial" $
19+
(read . show) factorial `shouldBe` factorial

test/PPATest/Lang/While/UtilSpec.hs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module PPATest.Lang.While.UtilSpec (spec) where
2+
3+
import Test.Hspec
4+
5+
import PPATest.Common
6+
7+
import qualified Data.Set as Set
8+
9+
import PPA.Lang.While.Util
10+
11+
spec :: Spec
12+
spec = do
13+
describe "flow" $ do
14+
it "-- factorial" $
15+
(flow $ read factorial) `shouldBe` Set.fromList [(1,2), (2,3), (3,4), (3,6), (4,5), (5,3)]
16+
it "-- power" $
17+
(flow $ read power) `shouldBe` Set.fromList [(1,2), (2,3), (3,4), (4,2)]

0 commit comments

Comments
 (0)