1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
module Main where
import Test.HUnit
import Control.Monad
import AufgabeFFP4
-------------------------------------------------------------------------------
import Data.List
type KnapsackWantedSolution = (SolKnp,Value)
type KnapsackWantedSolutions = [KnapsackWantedSolution]
type KnapsackGotSolution = (SolKnp,Value)
knapsackOk :: KnapsackWantedSolutions -> KnapsackGotSolution -> Bool
knapsackOk wanted got = any (equalKnapsack got) wanted
where
equalKnapsack (sg,vg) (sw,vw) = vg == vw && equalKnapsackContent sg sw
equalKnapsackContent sg sw = sort sg == sort sw
assertKnapsackOneOf :: String -> KnapsackWantedSolutions -> KnapsackGotSolution -> Assertion
assertKnapsackOneOf preface expected actual = unless (knapsackOk expected actual) (assertFailure msg)
where
msg = (if null preface then "" else preface ++ "\n") ++
"expected one of: " ++ show expected ++ "\n but got: " ++ show actual
-------------------------------------------------------------------------------
cases1 = TestLabel "knapsack" $ TestList [
TestCase $ assertKnapsackOneOf "exercise example"
[([(2,3), (2,3), (3,4), (3,4)], 14)]
(knapsack [(2,3), (2,3), (3,4), (3,4), (5,6)] 10),
TestCase $ assertKnapsackOneOf "no objects"
[([], 0)]
(knapsack [(2,3), (2,3), (3,4), (3,4), (5,6)] 1),
TestCase $ assertKnapsackOneOf "all objects"
[([(2,3), (2,3), (3,4), (3,4), (5,6)], 20)]
(knapsack [(2,3), (2,3), (3,4), (3,4), (5,6)] 100),
TestCase $ assertKnapsackOneOf "a"
[([(2,2), (3,3)], 5), ([(5,5)], 5)]
(knapsack [(2,2), (3,3), (4,4), (5,5)] 5)
]
cases2 = TestLabel "binomDyn" $ TestList [
]
tests :: [Test]
tests = [cases1, cases2]
main = do
forM tests $ \test ->
runTestTT test
|