diff options
| author | totycro <totycro@unknown-horizons.org> | 2012-05-15 03:52:27 +0200 |
|---|---|---|
| committer | totycro <totycro@unknown-horizons.org> | 2012-05-15 03:52:27 +0200 |
| commit | cc9fb7c09a62e1811aef5c22e3633e3bd42e2662 (patch) | |
| tree | 027554828d068a1a98ccd837dc8131a39011a280 | |
| parent | aa0d10e643990b240b3e50b4beca056c642289b0 (diff) | |
| download | ffp-cc9fb7c09a62e1811aef5c22e3633e3bd42e2662.tar.gz ffp-cc9fb7c09a62e1811aef5c22e3633e3bd42e2662.tar.bz2 ffp-cc9fb7c09a62e1811aef5c22e3633e3bd42e2662.zip | |
Tests für gtf und bt
| -rw-r--r-- | AufgabeFFP6.hs | 4 | ||||
| -rw-r--r-- | TestAufgabeFFP6.hs | 24 |
2 files changed, 27 insertions, 1 deletions
diff --git a/AufgabeFFP6.hs b/AufgabeFFP6.hs index 1fde756..c60053d 100644 --- a/AufgabeFFP6.hs +++ b/AufgabeFFP6.hs | |||
| @@ -91,22 +91,26 @@ yield_bt f w = map (\(_, _, _, g) -> listArray (1, length g) g) nodes | |||
| 91 | yield_gtf :: F -> W -> [G] | 91 | yield_gtf :: F -> W -> [G] |
| 92 | yield_gtf = filt . transform . generate | 92 | yield_gtf = filt . transform . generate |
| 93 | 93 | ||
| 94 | -- produce [G] by using get_combinations | ||
| 94 | generate :: F -> W -> (F, W, [G]) | 95 | generate :: F -> W -> (F, W, [G]) |
| 95 | generate f w = (f, w, [ array (1,n) entries | entries <- get_combinations n ] ) | 96 | generate f w = (f, w, [ array (1,n) entries | entries <- get_combinations n ] ) |
| 96 | where | 97 | where |
| 97 | n = (snd $ bounds f) - 1 | 98 | n = (snd $ bounds f) - 1 |
| 98 | 99 | ||
| 100 | -- provide all selections of length n, work recursively and attach index for level (convenient for creating arrays) | ||
| 99 | get_combinations :: Int -> [[(Int, Op)]] | 101 | get_combinations :: Int -> [[(Int, Op)]] |
| 100 | get_combinations 1 = [[(1,(+))], [(1, (-))], [(1, (*))], [(1,(./.))]] | 102 | get_combinations 1 = [[(1,(+))], [(1, (-))], [(1, (*))], [(1,(./.))]] |
| 101 | get_combinations n = [ (up i) ++ entr | entr <- get_combinations (n-1), i <- get_combinations 1 ] | 103 | get_combinations n = [ (up i) ++ entr | entr <- get_combinations (n-1), i <- get_combinations 1 ] |
| 102 | where | 104 | where |
| 103 | up = map (\(num, x) -> ((num+n-1), x)) | 105 | up = map (\(num, x) -> ((num+n-1), x)) |
| 104 | 106 | ||
| 107 | -- attaches a list of results | ||
| 105 | transform :: (W -> (F,W,[G])) -> W -> ((F, W, [G]), [W]) | 108 | transform :: (W -> (F,W,[G])) -> W -> ((F, W, [G]), [W]) |
| 106 | transform fun w = ((f, w, g), map (eval f) g ) | 109 | transform fun w = ((f, w, g), map (eval f) g ) |
| 107 | where | 110 | where |
| 108 | (f, w, g) = fun w | 111 | (f, w, g) = fun w |
| 109 | 112 | ||
| 113 | -- take those, where the entry in the result list corresponds to | ||
| 110 | filt :: (W -> ((F,W,[G]),[W]) ) -> W -> [G] | 114 | filt :: (W -> ((F,W,[G]),[W]) ) -> W -> [G] |
| 111 | filt fun w = [ g!!i | i <- [0..n], res!!i == w ] | 115 | filt fun w = [ g!!i | i <- [0..n], res!!i == w ] |
| 112 | where | 116 | where |
diff --git a/TestAufgabeFFP6.hs b/TestAufgabeFFP6.hs index 77cae92..c435045 100644 --- a/TestAufgabeFFP6.hs +++ b/TestAufgabeFFP6.hs | |||
| @@ -22,8 +22,30 @@ cases1 = TestLabel "eval" $ TestList [ | |||
| 22 | (eval (array (1,6) [(1,4), (2,2), (3,3), (4,-4), (5,5), (6,2)]) (array (1,5) [(1,(*)), (2,(-)), (3,(+)), (4,(./.)), (5,(+))])) | 22 | (eval (array (1,6) [(1,4), (2,2), (3,3), (4,-4), (5,5), (6,2)]) (array (1,5) [(1,(*)), (2,(-)), (3,(+)), (4,(./.)), (5,(+))])) |
| 23 | ] | 23 | ] |
| 24 | 24 | ||
| 25 | instance Eq (Integer -> Integer -> Integer) where | ||
| 26 | (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0 | ||
| 27 | instance Eq (Int -> Int -> Int) where | ||
| 28 | (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0 | ||
| 29 | |||
| 30 | cases2 = TestLabel "yield_bt" $ TestList [ | ||
| 31 | TestCase $ assertEqual "yield_bt1" (yield_bt (array (1,3) [(1,1),(2,2),(3,3)] ) 6) | ||
| 32 | [array (1,2) [(1,(+)),(2,(+))],array (1,2) [(1,(*)),(2,(*))]], | ||
| 33 | TestCase $ assertEqual "yield_bt2" (yield_bt (array (1,3) [(1,1),(2,2),(3,3)] ) 4) [], | ||
| 34 | TestCase $ assertEqual "yield_bt3" (yield_bt (array (1,3) [(1,1),(2,2),(3,3)]) 0) | ||
| 35 | [(array (1,2) [(1,(+)),(2,(-))]),(array (1,2) [(1,(*)),(2,(./.))]),(array (1,2) [(1,(./.)),(2,(*))]),(array (1,2) [(1,(./.)),(2,(./.))])] | ||
| 36 | ] | ||
| 37 | |||
| 38 | |||
| 39 | cases3 = TestLabel "yield_gtf" $ TestList [ | ||
| 40 | TestCase $ assertEqual "yield_gtf1" (yield_gtf (array (1,3) [(1,1),(2,2),(3,3)] ) 6) | ||
| 41 | [array (1,2) [(1,(+)),(2,(+))],array (1,2) [(1,(*)),(2,(*))]], | ||
| 42 | TestCase $ assertEqual "yield_gtf2" (yield_gtf (array (1,3) [(1,1),(2,2),(3,3)] ) 4) [], | ||
| 43 | TestCase $ assertEqual "yield_gtf3" (yield_gtf (array (1,3) [(1,1),(2,2),(3,3)]) 0) | ||
| 44 | [(array (1,2) [(1,(+)),(2,(-))]),(array (1,2) [(1,(*)),(2,(./.))]),(array (1,2) [(1,(./.)),(2,(*))]),(array (1,2) [(1,(./.)),(2,(./.))])] | ||
| 45 | ] | ||
| 46 | |||
| 25 | tests :: [Test] | 47 | tests :: [Test] |
| 26 | tests = [cases1] | 48 | tests = [cases1, cases2, cases3] |
| 27 | 49 | ||
| 28 | main = do | 50 | main = do |
| 29 | forM tests $ \test -> | 51 | forM tests $ \test -> |
