sl@0: // Copyright 2004-5 Trustees of Indiana University sl@0: sl@0: // Use, modification and distribution is subject to the Boost Software sl@0: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // sl@0: // graphviz_test.cpp - Test cases for the Boost.Spirit implementation of a sl@0: // Graphviz DOT Language reader. sl@0: // sl@0: sl@0: // Author: Ronald Garcia sl@0: /* sl@0: * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: //#define BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS sl@0: #define BOOST_GRAPHVIZ_USE_ISTREAM sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: #include "std_log_result.h" sl@0: #define LOG_FILENAME_LINE __FILE__, __LINE__ sl@0: #endif sl@0: using namespace std; sl@0: using namespace boost; sl@0: sl@0: #ifndef BOOST_GRAPHVIZ_USE_ISTREAM sl@0: using namespace boost::spirit; sl@0: #endif sl@0: using namespace boost::assign; sl@0: sl@0: typedef std::string node_t; sl@0: typedef std::pair edge_t; sl@0: sl@0: typedef std::map mass_map_t; sl@0: typedef std::map weight_map_t; sl@0: sl@0: template sl@0: bool test_graph(std::istream& dotfile, mass_map_t const& masses, sl@0: weight_map_t const& weights, sl@0: std::string const& node_id = "node_id") { sl@0: sl@0: typedef adjacency_list < OutEdgeList, vecS, Directedness, sl@0: property < vertex_name_t, std::string, sl@0: property < vertex_color_t, float > >, sl@0: property < edge_weight_t, double > > graph_t; sl@0: typedef typename graph_traits < graph_t >::edge_descriptor edge_t; sl@0: typedef typename graph_traits < graph_t >::vertex_descriptor vertex_t; sl@0: sl@0: // Construct a graph and set up the dynamic_property_maps. sl@0: graph_t graph(0); sl@0: dynamic_properties dp; sl@0: typename property_map::type name = sl@0: get(vertex_name, graph); sl@0: dp.property(node_id,name); sl@0: typename property_map::type mass = sl@0: get(vertex_color, graph); sl@0: dp.property("mass",mass); sl@0: typename property_map::type weight = sl@0: get(edge_weight, graph); sl@0: dp.property("weight",weight); sl@0: sl@0: // Read in space characters too! sl@0: dotfile >> noskipws; sl@0: sl@0: bool result = true; sl@0: #ifdef BOOST_GRAPHVIZ_USE_ISTREAM sl@0: if(read_graphviz(dotfile,graph,dp,node_id)) { sl@0: #else sl@0: std::string data; sl@0: std::copy(std::istream_iterator(dotfile), sl@0: std::istream_iterator(), sl@0: std::back_inserter(data)); sl@0: if(read_graphviz(data.begin(),data.end(),graph,dp,node_id)) { sl@0: #endif sl@0: // check masses sl@0: if(!masses.empty()) { sl@0: // assume that all the masses have been set sl@0: // for each vertex: sl@0: typename graph_traits::vertex_iterator i,j; sl@0: for(boost::tie(i,j) = vertices(graph); i != j; ++i) { sl@0: // - get its name sl@0: std::string node_name = get(name,*i); sl@0: // - get its mass sl@0: float node_mass = get(mass,*i); sl@0: float ref_mass = masses.find(node_name)->second; sl@0: // - compare the mass to the result in the table sl@0: sl@0: BOOST_CHECK_CLOSE(node_mass, ref_mass, 0.01f); sl@0: } sl@0: } sl@0: // check weights sl@0: if(!weights.empty()) { sl@0: // assume that all weights have been set sl@0: /// for each edge: sl@0: typename graph_traits::edge_iterator i,j; sl@0: for(boost::tie(i,j) = edges(graph); i != j; ++i) { sl@0: // - get its name sl@0: std::pair sl@0: edge_name = make_pair(get(name, source(*i,graph)), sl@0: get(name, target(*i,graph))); sl@0: // - get its weight sl@0: double edge_weight = get(weight,*i); sl@0: double ref_weight = weights.find(edge_name)->second; sl@0: // - compare the weight to teh result in the table sl@0: BOOST_CHECK_CLOSE(edge_weight, ref_weight, 0.01); sl@0: } sl@0: } sl@0: sl@0: sl@0: } else { sl@0: std::cerr << "Parsing Failed!\n"; sl@0: result = false; sl@0: } sl@0: sl@0: return result; sl@0: } sl@0: sl@0: int test_main(int, char*[]) { sl@0: sl@0: typedef istringstream gs_t; sl@0: sl@0: // Basic directed graph tests sl@0: { sl@0: mass_map_t masses; sl@0: insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); sl@0: gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }"); sl@0: BOOST_CHECK((test_graph(gs,masses,weight_map_t()))); sl@0: } sl@0: sl@0: { sl@0: weight_map_t weights; sl@0: insert( weights )(make_pair("a","b"),0.0) sl@0: (make_pair("c","d"),7.7)(make_pair("e","f"),6.66); sl@0: gs_t gs("digraph { a -> b edge [weight = 7.7] " sl@0: "c -> d e-> f [weight = 6.66] }"); sl@0: BOOST_CHECK((test_graph(gs,mass_map_t(),weights))); sl@0: } sl@0: sl@0: // undirected graph with alternate node_id property name sl@0: { sl@0: mass_map_t masses; sl@0: insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); sl@0: gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }"); sl@0: BOOST_CHECK((test_graph(gs,masses,weight_map_t(), sl@0: "nodenames"))); sl@0: } sl@0: sl@0: // Basic undirected graph tests sl@0: { sl@0: mass_map_t masses; sl@0: insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); sl@0: gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }"); sl@0: BOOST_CHECK((test_graph(gs,masses,weight_map_t()))); sl@0: } sl@0: sl@0: { sl@0: weight_map_t weights; sl@0: insert( weights )(make_pair("a","b"),0.0) sl@0: (make_pair("c","d"),7.7)(make_pair("e","f"),6.66); sl@0: gs_t gs("graph { a -- b eDge [weight = 7.7] " sl@0: "c -- d e -- f [weight = 6.66] }"); sl@0: BOOST_CHECK((test_graph(gs,mass_map_t(),weights))); sl@0: } sl@0: sl@0: // Mismatch directed graph test sl@0: { sl@0: mass_map_t masses; sl@0: insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); sl@0: gs_t gs("graph { a nodE [mass = 7.7] c e [mass = 6.66] }"); sl@0: try { sl@0: test_graph(gs,masses,weight_map_t()); sl@0: } catch (boost::undirected_graph_error&) {} sl@0: } sl@0: sl@0: // Mismatch undirected graph test sl@0: { sl@0: mass_map_t masses; sl@0: insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); sl@0: gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }"); sl@0: try { sl@0: test_graph(gs,masses,weight_map_t()); sl@0: BOOST_ERROR("Failed to throw boost::directed_graph_error."); sl@0: } catch (boost::directed_graph_error&) {} sl@0: } sl@0: sl@0: // Complain about parallel edges sl@0: { sl@0: weight_map_t weights; sl@0: insert( weights )(make_pair("a","b"),7.7); sl@0: gs_t gs("diGraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }"); sl@0: try { sl@0: test_graph(gs,mass_map_t(),weights); sl@0: BOOST_ERROR("Failed to throw boost::bad_parallel_edge."); sl@0: } catch (boost::bad_parallel_edge&) {} sl@0: } sl@0: sl@0: // Handle parallel edges gracefully sl@0: { sl@0: weight_map_t weights; sl@0: insert( weights )(make_pair("a","b"),7.7); sl@0: gs_t gs("digraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }"); sl@0: BOOST_CHECK((test_graph(gs,mass_map_t(),weights))); sl@0: } sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: sl@0: std_log(LOG_FILENAME_LINE,"[End Test Case ]"); sl@0: sl@0: testResultXml("graphiz_test"); sl@0: close_log_file(); sl@0: #endif sl@0: return 0; sl@0: }