diff options
author | Akshay <[email protected]> | 2020-10-14 09:45:45 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-10-14 09:45:45 +0100 |
commit | 82a64ef64602227daefc3ff96908d6c1b2303b61 (patch) | |
tree | 0c4604f2216c148ad0bd010153e56c9173e08361 /tests | |
parent | 8b08ea946d1fc73d8363efb633b8063f750520bf (diff) |
add PBTs for arithmetic operators
- commutativity of addition
- commutativity of multiplication
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Main.hs | 4 | ||||
-rw-r--r-- | tests/Properties.hs | 38 |
2 files changed, 24 insertions, 18 deletions
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 @@ | |||
1 | module Main where | 1 | module Main where |
2 | 2 | ||
3 | import Properties | 3 | import Properties (runTests) |
4 | import Test.QuickCheck | 4 | import Test.QuickCheck |
5 | 5 | ||
6 | main = tests | 6 | main = 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 #-} |
2 | module Properties where | 2 | module Properties ( |
3 | runTests | ||
4 | ) where | ||
3 | 5 | ||
6 | import Data.Maybe (fromJust) | ||
7 | import Error.Base (unwrap) | ||
8 | import Evaluator (eval) | ||
9 | import Operators (primitives) | ||
4 | import Parser (Expr (..), parseLispValue, parseQuote) | 10 | import Parser (Expr (..), parseLispValue, parseQuote) |
5 | |||
6 | import Test.QuickCheck | 11 | import Test.QuickCheck |
7 | 12 | ||
8 | -- some tests would go here hopefully | 13 | addition = fromJust $ lookup "+" primitives |
14 | multiplication = fromJust $ lookup "*" primitives | ||
9 | 15 | ||
10 | -- a filler test to test the test suite :^) | 16 | prop_commutativeAdd :: [Integer] -> Property |
11 | qsort :: (Ord a) => [a] -> [a] | 17 | prop_commutativeAdd xs = |
12 | qsort [] = [] | 18 | not (null xs) ==> rhs == lhs |
13 | qsort [x] = [x] | 19 | where rhs = (unwrap . addition) exprs |
14 | qsort (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 | ||
18 | checkList :: (Ord a) => [a] -> Bool | 23 | prop_commutativeMul :: [Integer] -> Property |
19 | checkList = ordered . qsort | 24 | prop_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 | ||
24 | return [] | 30 | return [] |
25 | tests = $quickCheckAll | 31 | runTests = $quickCheckAll |