From 82a64ef64602227daefc3ff96908d6c1b2303b61 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 14 Oct 2020 14:15:45 +0530 Subject: add PBTs for arithmetic operators - commutativity of addition - commutativity of multiplication --- lisk.cabal | 6 +++--- src/Error/Base.hs | 3 --- tests/Main.hs | 4 ++-- tests/Properties.hs | 38 ++++++++++++++++++++++---------------- 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 library default-language: Haskell2010 + hs-source-dirs: src build-depends: base == 4.*, parsec == 3.*, mtl >= 2.1 - hs-source-dirs: src exposed-modules: Parser, Evaluator, @@ -33,22 +33,22 @@ library executable lisk default-language: Haskell2010 main-is: Main.hs + hs-source-dirs: bin build-depends: base == 4.*, parsec == 3.*, readline >= 1.0, mtl >= 2.1, lisk - hs-source-dirs: bin test-suite properties default-language: Haskell2010 type: exitcode-stdio-1.0 main-is: Main.hs hs-source-dirs: tests + other-modules: Properties build-depends: base == 4.*, parsec == 3.*, QuickCheck >= 2.1 && < 3, lisk - 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 ( ) where import Control.Monad.Except -import Data.List (intercalate, nub) import Parser import Text.Parsec import Text.Parsec.Error -import Text.Parsec.Pos -import Text.Parsec.String (Parser) import Text.ParserCombinators.Parsec data 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 @@ module Main where -import Properties +import Properties (runTests) import Test.QuickCheck -main = tests +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 @@ {-# LANGUAGE TemplateHaskell #-} -module Properties where +module Properties ( + runTests + ) where +import Data.Maybe (fromJust) +import Error.Base (unwrap) +import Evaluator (eval) +import Operators (primitives) import Parser (Expr (..), parseLispValue, parseQuote) - import Test.QuickCheck --- some tests would go here hopefully +addition = fromJust $ lookup "+" primitives +multiplication = fromJust $ lookup "*" primitives --- a filler test to test the test suite :^) -qsort :: (Ord a) => [a] -> [a] -qsort [] = [] -qsort [x] = [x] -qsort (x:xs) = qsort left ++ [x] ++ qsort right - where left = filter (<= x) xs - right = filter (> x) xs +prop_commutativeAdd :: [Integer] -> Property +prop_commutativeAdd xs = + not (null xs) ==> rhs == lhs + where rhs = (unwrap . addition) exprs + lhs = (unwrap . addition . reverse) exprs + exprs = map IntLiteral xs -checkList :: (Ord a) => [a] -> Bool -checkList = ordered . qsort - where ordered [] = True - ordered [x] = True - ordered (x:y:xs) = x <= y && ordered (y:xs) +prop_commutativeMul :: [Integer] -> Property +prop_commutativeMul xs = + not (null xs) ==> rhs == lhs + where rhs = (unwrap . multiplication) exprs + lhs = (unwrap . multiplication . reverse) exprs + exprs = map IntLiteral xs return [] -tests = $quickCheckAll +runTests = $quickCheckAll -- cgit v1.2.3