— How to use: runghc testBinomialHeap.hs
import System.Environment (getArgs)
import System.Exit (exitFailure)
import Test.HUnit
import Text.Read (readMaybe)
import BinomialHeapDef
import qualified BinomialHeap as C
— Re-assert general types.
link :: Ord a => BinomialTree a -> BinomialTree a -> BinomialTree a
link = C.link
insertTree :: Ord a => BinomialTree a -> [BinomialTree a] -> [BinomialTree a]
insertTree = C.insertTree
findMin :: Ord a => [BinomialTree a] -> Maybe a
findMin = C.findMin
merge :: Ord a => [BinomialTree a] -> [BinomialTree a] -> [BinomialTree a]
merge = C.merge
tests = testlink ++ testfindmin ++ testinserttree ++ testmerge
r1a = Node 1 6 [Node 0 8 []]
r1b = Node 1 12 [Node 0 17 []]
r2a = Node 2 6 [r1b, Node 0 8 []]
— 2 marks
testlink =
[ “link handout” ~:
[ link r1a r1b ~?= r2a
, link r1b r1a ~?= r2a
]
]
— More when marking.
r1c = Node 1 2 [Node 0 8 []]
r2c = Node 2 3 [r1c, Node 0 5 []]
hf1 = [ Node 0 14 []
, r1c
, r2c
]
— 2 marks
testfindmin =
[ “findmin empty” ~:
(findMin [] :: Maybe Integer) ~?= Nothing
, “findmin handout” ~:
findMin hf1 ~?= Just 2
]
— More when marking.
r2b = Node 2 7 [Node 1 7 [Node 0 10 []],Node 0 11 []]
r3a = Node 3 6 [r2b, r1b, Node 0 8 []]
— 3 marks
testinserttree =
[ “insertTree handout” ~:
insertTree (Node 0 17 [])
[ Node 0 12 []
, r1a
, r3a
]
~?= [ r2a, r3a ]
]
— More when marking.
r1d = Node 1 9 [Node 0 16 []]
r1e = Node 1 1 [Node 0 15 []]
r2d = Node 2 2 [Node 1 4 [Node 0 14 []], Node 0 5 []]
r3b = Node 3 6 [Node 2 7 [Node 1 8 [Node 0 10 []],Node 0 11 []],Node 1 12 [Node 0 17 []],Node 0 13 []]
hm0 = [Node 1 9 [Node 0 16 []],Node 2 2 [Node 1 4 [Node 0 14 []],Node 0 5 []],Node 3 6 [Node 2 7 [Node 1 8 [Node 0 10 []],Node 0 11 []],Node 1 12 [Node 0 17 []],Node 0 13 []]]
hm1 = [Node 2 1 [Node 1 9 [Node 0 16 []],Node 0 15 []]]
hm2 = [Node 4 1 [Node 3 6 [Node 2 7 [Node 1 8 [Node 0 10 []],Node 0 11 []],Node 1 12 [Node 0 17 []],Node 0 13 []],Node 2 2 [Node 1 4 [Node 0 14 []],Node 0 5 []],Node 1 9 [Node 0 16 []],Node 0 15 []]]
— 3 marks
testmerge =
[ “merge [1,3] [2]” ~:
merge [r1d, r3b] [r2d] ~?= hm0
, “merge [2] [1,3]” ~:
merge [r2d] [r1d, r3b] ~?= hm0
, “merge [1] [1]” ~:
merge [r1d] [r1e] ~?= hm1
, “merge [1,3] [1,2]” ~:
merge [r1d, r3b] [r1e, r2d] ~?= hm2
]
main = do
args <- getArgs
case args of
a:_ | Just n <- readMaybe a, 0 <= n, n < length tests ->
do c@Counts{errors=e, failures=f} <- runTestTT (tests !! n)
if e == 0 && f == 0
then return c
else exitFailure
| otherwise -> error “No such test number.”
_ -> runTestTT (TestList tests)