aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-14 09:45:45 +0100
committerAkshay <[email protected]>2020-10-14 09:45:45 +0100
commit82a64ef64602227daefc3ff96908d6c1b2303b61 (patch)
tree0c4604f2216c148ad0bd010153e56c9173e08361
parent8b08ea946d1fc73d8363efb633b8063f750520bf (diff)
add PBTs for arithmetic operators
- commutativity of addition - commutativity of multiplication
-rw-r--r--lisk.cabal6
-rw-r--r--src/Error/Base.hs3
-rw-r--r--tests/Main.hs4
-rw-r--r--tests/Properties.hs38
4 files changed, 27 insertions, 24 deletions
diff --git a/lisk.cabal b/lisk.cabal
index 4687e9c..8cdb413 100644
--- a/lisk.cabal
+++ b/lisk.cabal
@@ -18,11 +18,11 @@ extra-source-files: CHANGELOG.md
18 18
19library 19library
20 default-language: Haskell2010 20 default-language: Haskell2010
21 hs-source-dirs: src
21 build-depends: 22 build-depends:
22 base == 4.*, 23 base == 4.*,
23 parsec == 3.*, 24 parsec == 3.*,
24 mtl >= 2.1 25 mtl >= 2.1
25 hs-source-dirs: src
26 exposed-modules: 26 exposed-modules:
27 Parser, 27 Parser,
28 Evaluator, 28 Evaluator,
@@ -33,22 +33,22 @@ library
33executable lisk 33executable lisk
34 default-language: Haskell2010 34 default-language: Haskell2010
35 main-is: Main.hs 35 main-is: Main.hs
36 hs-source-dirs: bin
36 build-depends: 37 build-depends:
37 base == 4.*, 38 base == 4.*,
38 parsec == 3.*, 39 parsec == 3.*,
39 readline >= 1.0, 40 readline >= 1.0,
40 mtl >= 2.1, 41 mtl >= 2.1,
41 lisk 42 lisk
42 hs-source-dirs: bin
43 43
44test-suite properties 44test-suite properties
45 default-language: Haskell2010 45 default-language: Haskell2010
46 type: exitcode-stdio-1.0 46 type: exitcode-stdio-1.0
47 main-is: Main.hs 47 main-is: Main.hs
48 hs-source-dirs: tests 48 hs-source-dirs: tests
49 other-modules: Properties
49 build-depends: 50 build-depends:
50 base == 4.*, 51 base == 4.*,
51 parsec == 3.*, 52 parsec == 3.*,
52 QuickCheck >= 2.1 && < 3, 53 QuickCheck >= 2.1 && < 3,
53 lisk 54 lisk
54 other-modules: Properties
diff --git a/src/Error/Base.hs b/src/Error/Base.hs
index b6ae9a3..509377b 100644
--- a/src/Error/Base.hs
+++ b/src/Error/Base.hs
@@ -5,12 +5,9 @@ module Error.Base (
5 ) where 5 ) where
6 6
7import Control.Monad.Except 7import Control.Monad.Except
8import Data.List (intercalate, nub)
9import Parser 8import Parser
10import Text.Parsec 9import Text.Parsec
11import Text.Parsec.Error 10import Text.Parsec.Error
12import Text.Parsec.Pos
13import Text.Parsec.String (Parser)
14import Text.ParserCombinators.Parsec 11import Text.ParserCombinators.Parsec
15 12
16data LispError = Parse ParseError 13data LispError = Parse ParseError
diff --git a/tests/Main.hs b/tests/Main.hs
index c4e0d9b..87dedbc 100644
--- a/tests/Main.hs
+++ b/tests/Main.hs
@@ -1,6 +1,6 @@
1module Main where 1module Main where
2 2
3import Properties 3import Properties (runTests)
4import Test.QuickCheck 4import Test.QuickCheck
5 5
6main = tests 6main = runTests
diff --git a/tests/Properties.hs b/tests/Properties.hs
index e50c7e8..b9550c0 100644
--- a/tests/Properties.hs
+++ b/tests/Properties.hs
@@ -1,25 +1,31 @@
1{-# LANGUAGE TemplateHaskell #-} 1{-# LANGUAGE TemplateHaskell #-}
2module Properties where 2module Properties (
3 runTests
4 ) where
3 5
6import Data.Maybe (fromJust)
7import Error.Base (unwrap)
8import Evaluator (eval)
9import Operators (primitives)
4import Parser (Expr (..), parseLispValue, parseQuote) 10import Parser (Expr (..), parseLispValue, parseQuote)
5
6import Test.QuickCheck 11import Test.QuickCheck
7 12
8-- some tests would go here hopefully 13addition = fromJust $ lookup "+" primitives
14multiplication = fromJust $ lookup "*" primitives
9 15
10-- a filler test to test the test suite :^) 16prop_commutativeAdd :: [Integer] -> Property
11qsort :: (Ord a) => [a] -> [a] 17prop_commutativeAdd xs =
12qsort [] = [] 18 not (null xs) ==> rhs == lhs
13qsort [x] = [x] 19 where rhs = (unwrap . addition) exprs
14qsort (x:xs) = qsort left ++ [x] ++ qsort right 20 lhs = (unwrap . addition . reverse) exprs
15 where left = filter (<= x) xs 21 exprs = map IntLiteral xs
16 right = filter (> x) xs
17 22
18checkList :: (Ord a) => [a] -> Bool 23prop_commutativeMul :: [Integer] -> Property
19checkList = ordered . qsort 24prop_commutativeMul xs =
20 where ordered [] = True 25 not (null xs) ==> rhs == lhs
21 ordered [x] = True 26 where rhs = (unwrap . multiplication) exprs
22 ordered (x:y:xs) = x <= y && ordered (y:xs) 27 lhs = (unwrap . multiplication . reverse) exprs
28 exprs = map IntLiteral xs
23 29
24return [] 30return []
25tests = $quickCheckAll 31runTests = $quickCheckAll