1+ // ===- SVF-Teaching Assignment 1-------------------------------------//
2+ //
3+ // SVF: Static Value-Flow Analysis Framework for Source Code
4+ //
5+ // Copyright (C) <2013->
6+ //
7+
8+ // This program is free software: you can redistribute it and/or modify
9+ // it under the terms of the GNU General Public License as published by
10+ // the Free Software Foundation, either version 3 of the License, or
11+ // (at your option) any later version.
12+
13+ // This program is distributed in the hope that it will be useful,
14+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
15+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+ // GNU General Public License for more details.
17+
18+ // You should have received a copy of the GNU General Public License
19+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
20+ //
21+ // ===-----------------------------------------------------------------------===//
22+
23+ /*
24+ // SVF-Teaching Assignment 1 : Graph Traversal
25+ //
26+ //
27+ */
28+
29+
30+ #include " ../Assignment-1.h"
31+ #include < assert.h>
32+ #include < map>
33+ #include < fstream> // for GraphBuilderFromFile
34+ #include < string> // for GraphBuilderFromFile
35+ #include < sstream> // for GraphBuilderFromFile
36+ using namespace std ;
37+
38+ int totalPassedCases = 0 ;
39+ // test
40+ // build graph in brief way
41+ void build_g (Graph* g, Node* n1, Node* n2)
42+ {
43+ Edge *edge = new Edge (n1, n2);
44+ n1->addOutEdge (edge);
45+ g->addNode (n1);
46+ g->addNode (n2);
47+
48+ }
49+
50+ /*
51+ * You can build a Graph from a file written by yourself
52+ *
53+ * The file should follow the format:
54+ * Node: nodeID
55+ * Edge: nodeID -> NodeID
56+ *
57+ * like:
58+ 1
59+ 2
60+ 3
61+ 1 -> 2
62+ 2 -> 3
63+ */
64+ void Test (std::string file){
65+
66+ std::map<int ,Node*> id2Nodes;
67+ std::set<std::string> expected;
68+ Graph *g = new Graph ();
69+ int source = 0 ;
70+ int target = 0 ;
71+
72+ string line;
73+ ifstream myfile (file.c_str ());
74+ if (myfile.is_open ())
75+ {
76+ while (myfile.good ())
77+ {
78+ getline (myfile, line);
79+
80+ int token_count = 0 ;
81+ string tmps;
82+ istringstream ss (line);
83+ while (ss.good ())
84+ {
85+ ss >> tmps;
86+ token_count++;
87+ }
88+
89+ if (token_count == 0 || token_count == 1 )
90+ continue ;
91+
92+ // / create nodes
93+ else if (token_count == 2 )
94+ {
95+ string dummy;
96+ int nodeId;
97+ string nodetype;
98+ istringstream ss (line);
99+ ss >> dummy;
100+ ss >> nodeId;
101+ Node *node = new Node (nodeId);
102+ id2Nodes[nodeId] = node;
103+ g->addNode (node);
104+ }
105+ // / create edges
106+ else if (token_count == 3 )
107+ {
108+ int nodeSrc;
109+ int nodeDst;
110+ string ed;
111+ istringstream ss (line);
112+ ss >> nodeSrc;
113+ ss >> ed;
114+ ss >> nodeDst;
115+ assert (id2Nodes.find (nodeSrc)!=id2Nodes.end () && " src node has not been created" );
116+ assert (id2Nodes.find (nodeDst)!=id2Nodes.end () && " dst node has not been created" );
117+ Edge *edge = new Edge (id2Nodes[nodeSrc], id2Nodes[nodeDst]);
118+ id2Nodes[nodeSrc]->addOutEdge (edge);
119+ }
120+ // / correct answer "1 => 2 START: 1->2->4->5->END"
121+ else if (token_count == 5 ){
122+ string dummy;
123+ string start;
124+ string end;
125+ istringstream ss (line);
126+ ss >> source;
127+ ss >> dummy;
128+ ss >> target;
129+ ss >> start;
130+ ss >> end;
131+ expected.insert (start + " " + end);
132+ }
133+ else {
134+ assert (false && " format not supported" );
135+ }
136+ }
137+ }
138+
139+ std::set<const Node *> visited;
140+ std::vector<const Node *> path;
141+ GraphTraversal *dfs = new GraphTraversal ();
142+ assert (id2Nodes.find (source)!=id2Nodes.end () && " source node was not found!" );
143+ assert (id2Nodes.find (target)!=id2Nodes.end () && " target node was not found!" );
144+ dfs->DFS (visited, path, id2Nodes[source], id2Nodes[target]);
145+
146+ string filename = file.substr (file.find_last_of (" /" ) + 1 );
147+ if (dfs->getPaths () == expected){
148+ std::cerr << " Testcase " << filename << " passed!\n " ;
149+ totalPassedCases++;
150+ }
151+ else
152+ std::cerr << " Testcase " << filename << " failed!\n " ;
153+
154+ delete dfs;
155+ delete g;
156+
157+ }
158+
159+ int main (int argc, char *argv[])
160+ {
161+
162+ assert (argc == 2 && " only allow one input file to build graph" );
163+
164+ Test (argv[1 ]);
165+
166+ return 0 ;
167+ }
0 commit comments