Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

tests for finite maps

  • Loading branch information...
commit e5df06eb982cbcc93052738a7aa61683713f54ee 1 parent 2cf4d9b
@lbolla authored
View
30 haskell/Okasaki/src/FiniteMap.hs
@@ -1,6 +1,12 @@
module FiniteMap where
import Prelude hiding (lookup)
+import Test.Framework (Test)
+import Test.Framework.Providers.HUnit (testCase)
+import Test.Framework.Providers.QuickCheck2 (testProperty)
+import Test.HUnit (assert, Assertion)
+
+import Data.Maybe (isNothing, isJust)
-- Exercise 2.6
class FiniteMap m where
@@ -25,3 +31,27 @@ instance FiniteMap KVTree where
fromList :: Ord k => [(k, v)] -> KVTree k v
fromList = foldr (uncurry bind) empty
+
+-------------------------------------------------------------------------------
+-- TESTS
+
+testEmpty :: Assertion
+testEmpty = assert . isNothing $ lookup (1 :: Integer) E
+
+testDup :: Assertion
+testDup = assert $ lookup (1 :: Integer) m == Just (2 :: Integer)
+ where m = fromList ([(1, 0), (1, 2)] :: [(Integer, Integer)])
+
+propLookup :: [(Integer, Integer)] -> Bool
+propLookup xs = let m = fromList xs in all (\x -> isJust (lookup (fst x) m)) xs
+
+propBind :: (Integer, String) -> Bool
+propBind (k, v) = let m = bind k v (empty :: KVTree Integer String) in lookup k m == Just v
+
+tests :: [Test]
+tests = [
+ testCase "Empty" testEmpty
+ , testCase "Dup" testDup
+ , testProperty "Lookup" propLookup
+ , testProperty "Bind" propBind
+ ]
View
2  haskell/Okasaki/src/OkasakiTester.hs
@@ -5,6 +5,7 @@ import Test.Framework (defaultMain, testGroup)
import qualified Heap
import qualified Stack
import qualified Set
+import qualified FiniteMap
main :: IO ()
main = defaultMain tests
@@ -13,4 +14,5 @@ tests = [
testGroup "Stack" Stack.tests
, testGroup "Set" Set.tests
, testGroup "Heap" Heap.tests
+ , testGroup "FiniteMap" FiniteMap.tests
]
Please sign in to comment.
Something went wrong with that request. Please try again.