-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing a simple graph api #14
Changes from 23 commits
e0ada79
aa2a7c2
fff5d44
ede942c
2520073
4a2f7bc
aad0b01
645eb02
4f24710
c004654
8518bd0
e00d071
73ed92f
83ee458
288e7a1
95c8348
6799b76
8c1f677
3df235d
4b18f17
8a6b9e6
c23f4f8
666a081
58ad815
b6011fb
30143ab
20ead9b
ab76a56
ae4b278
2f0be80
9afc3d2
d55bb02
dd42b08
46333ec
dab67f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,15 @@ | |
|
||
module Main where | ||
|
||
import Data.Maybe | ||
|
||
import Data.ByteString.Char8 (pack) | ||
import qualified Data.ByteString as BS | ||
import qualified Pangraph as P | ||
import qualified Pangraph.GraphML.Parser as GraphML_P | ||
import qualified Pangraph.VHDL.EnvironmentWriter as VHDL_E | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seem to be no need to give different qualified names to |
||
import qualified Pangraph.VHDL.GraphWriter as VHDL_G | ||
|
||
|
||
type Parser = GraphML_P.Template -> BS.ByteString -> Either BS.ByteString P.Pangraph | ||
|
||
main :: IO () | ||
main = do | ||
putStrLn "\n------Testing begins------" | ||
|
@@ -22,72 +21,71 @@ main = do | |
putStrLn "VHDL:" | ||
testVHDL | ||
putStrLn "\n------All Tests Passed------" | ||
where | ||
|
||
testShowInstance :: IO () | ||
testShowInstance = case a == (show b) of | ||
True -> putStrLn "-Pangraph show instance passed." | ||
False -> error $ "!Show instance failed test: " ++ show b ++ "!=" ++ a | ||
where | ||
a = fst showInstanceTestCase | ||
b = snd showInstanceTestCase | ||
showInstanceTestCase :: (String, P.Pangraph) | ||
showInstanceTestCase = ("Pangraph [Node [Attribute (\"id\",\"0\")],Node [Attribute (\"id\",\"1\")]] [Edge [Attribute (\"source\",\"0\"),Attribute (\"target\",\"1\")]]", | ||
P.makePangraph [P.makeNode [P.makeAttribute ("id","0")],P.makeNode [P.makeAttribute ("id","1")]] [P.makeEdge [P.makeAttribute ("source","0"),P.makeAttribute ("target","1")]]) | ||
testShowInstance = do | ||
literal <-fmap (head . lines) $ readFile "test/show-string.txt" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should avoid reading files during tests. There is no point to test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a test that the show instance has not been changed between commits unexpectedly, I just moved string literal into the text file to remove clutter from the where clause. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the avoidance of IO to make it clear exactly what is being tested? I think maybe the test directory should mirror the src directory in terms of testing this will allow the files to be clearer and do less IO.
|
||
let graph = fromMaybe | ||
(error "Sample graph failed to build") $ | ||
P.makePangraph | ||
sampleVertices [ | ||
P.makeEdge | ||
[("source","0"), ("target","1")] | ||
(head sampleVertices, sampleVertices !! 1) | ||
] | ||
sampleVertices = [P.makeVertex "0" [("id","0")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
,P.makeVertex "1" [ ("id","1")]] | ||
if literal == show (graph :: P.Pangraph) | ||
then putStrLn "-Instance passed" | ||
else error | ||
$ "!Show instance failed test: \"" | ||
++ literal | ||
++ "\"\nSample(above) != Graph(below)\n\"" | ||
++ show graph | ||
++ "\"" | ||
|
||
-- Without escapes: | ||
-- Pangraph [Node [Attribute ("id","0")],Node [Attribute ("id","1")]] [Edge [Attribute ("source","0"),Attribute ("target","1")]] | ||
-- makePangraph [makeVertex "0" [("id","0")],makeVertex "1" [("id","1")]] [makeEdge [("source","0"),("target","1")] (makeVertex "0" [("id","0")],makeVertex "1" [("id","1")])] | ||
|
||
testGraphML :: IO () | ||
testGraphML = do | ||
file <- fmap pack $ readFile path | ||
case (parser (head GraphML_P.template) file) of | ||
Left x -> error (show x) | ||
Right y -> if y == graphs | ||
then putStrLn "-Parse Passed" | ||
else error $ "!Test failed\n" ++ show y | ||
where | ||
path :: FilePath | ||
path = "examples/graphs/small.graphml" | ||
parser :: Parser | ||
parser = GraphML_P.parseTemplateToPangraph | ||
graphs :: P.Pangraph | ||
graphs = | ||
P.makePangraph | ||
[P.makeNode [P.makeAttribute ("id","n0")], | ||
P.makeNode [P.makeAttribute ("id","n1")], | ||
P.makeNode [P.makeAttribute ("id","n2")]] | ||
[P.makeEdge | ||
[P.makeAttribute ("source","n0"), | ||
P.makeAttribute ("target","n2")]] | ||
file <- BS.readFile "examples/graphs/small.graphml" | ||
let graph = fromMaybe (error "Sample graph failed to build") graph' | ||
graph' = P.makePangraph sampleVertices | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now it's much easier to read, but |
||
[P.makeEdge | ||
[("source","n0"),("target","n2")] | ||
(head sampleVertices, sampleVertices !! 2)] | ||
sampleVertices = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for |
||
P.makeVertex "n0" [ ("id","n0")], | ||
P.makeVertex "n1" [ ("id","n1")], | ||
P.makeVertex "n2" [ ("id","n2")]] | ||
|
||
if graph == GraphML_P.unsafeParse file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is unsafe version used in the test? This relies on the fact that the current implementation actually reports an error, but it doesn't have to. I suggest use a safe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsafe throws errors on Hexml returning an error or the graph being malformed which is checked still, the only difference is the code here does not need to handle a |
||
then putStrLn "-Parse Passed" | ||
else error $ "!Test failed\n" | ||
++ show graph | ||
++ "\n!=\n" | ||
++ show (GraphML_P.unsafeParse file) | ||
|
||
testVHDL :: IO () | ||
testVHDL = do | ||
files <- mapM BS.readFile graphPaths | ||
vhdlEnvironment <- mapM readFile enviromentPaths | ||
vhdlGraph <- mapM readFile networkPaths | ||
files <- mapM BS.readFile ["examples/graphs/n1/n1.graphml", | ||
"examples/graphs/n2/n2.graphml"] | ||
vhdlEnvironment <- mapM readFile ["examples/graphs/n1/n1-sim-environment.vhdl", | ||
"examples/graphs/n2/n2-sim-environment.vhdl"] | ||
vhdlGraph <- mapM readFile ["examples/graphs/n1/n1-graph.vhdl", | ||
"examples/graphs/n2/n2-graph.vhdl"] | ||
let test1 = zip files vhdlEnvironment | ||
let test2 = zip files vhdlGraph | ||
putStrLn $ concatMap (\a -> applyTest a VHDL_E.writeEnvironmentVhdl) test1 | ||
putStrLn $ concatMap (\a -> applyTest a VHDL_G.writeGraphVhdl) test2 | ||
|
||
putStrLn $ concatMap (`applyTest` VHDL_E.writeEnvironmentVhdl) test1 | ||
putStrLn $ concatMap (`applyTest` VHDL_G.writeGraphVhdl) test2 | ||
where | ||
-- args: A graphML file, the string which should result, the function which maps the file to result | ||
applyTest :: (BS.ByteString, String) -> (P.Pangraph -> String) -> String | ||
applyTest (g, example) f = case (parser (head GraphML_P.template) g ) of | ||
Left x -> error (show x) | ||
Right p -> case example == f p of | ||
True -> "-VHDL passed test\n" | ||
False -> error $ "!VHDL failed test : " ++ show example ++ "\n!=\n" ++ (show $ f p) | ||
|
||
|
||
graphPaths :: [FilePath] | ||
graphPaths = ["examples/graphs/n1/n1.graphml", | ||
"examples/graphs/n2/n2.graphml"] | ||
enviromentPaths :: [FilePath] | ||
enviromentPaths = ["examples/graphs/n1/n1-sim-environment.vhdl", | ||
"examples/graphs/n2/n2-sim-environment.vhdl"] | ||
networkPaths :: [FilePath] | ||
networkPaths = ["examples/graphs/n1/n1-graph.vhdl", | ||
"examples/graphs/n2/n2-graph.vhdl"] | ||
parser :: Parser | ||
parser = GraphML_P.parseTemplateToPangraph | ||
applyTest (raw, sample) f = | ||
if show (GraphML_P.unsafeParse raw) == sample | ||
then error $ "!VHDL failed test : " | ||
++ show sample | ||
++ "\n!=\n" | ||
++ show (GraphML_P.unsafeParse raw) | ||
else "-VHDL passed test\n" |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GraphML
is sufficiently clear as a qualification name.