1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/graph/src/graphviz_test.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,228 @@
1.4 +// Copyright 2004-5 Trustees of Indiana University
1.5 +
1.6 +// Use, modification and distribution is subject to the Boost Software
1.7 +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
1.8 +// http://www.boost.org/LICENSE_1_0.txt)
1.9 +
1.10 +//
1.11 +// graphviz_test.cpp - Test cases for the Boost.Spirit implementation of a
1.12 +// Graphviz DOT Language reader.
1.13 +//
1.14 +
1.15 +// Author: Ronald Garcia
1.16 +/*
1.17 + * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
1.18 +*/
1.19 +
1.20 +//#define BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS
1.21 +#define BOOST_GRAPHVIZ_USE_ISTREAM
1.22 +#include <boost/graph/graphviz.hpp>
1.23 +#include <boost/assign/std/map.hpp>
1.24 +#include <boost/graph/adjacency_list.hpp>
1.25 +#include <boost/graph/graph_traits.hpp>
1.26 +#include <boost/tuple/tuple.hpp>
1.27 +#include <boost/dynamic_property_map.hpp>
1.28 +#include <boost/test/test_tools.hpp>
1.29 +#include <boost/test/floating_point_comparison.hpp>
1.30 +#include <algorithm>
1.31 +#include <string>
1.32 +#include <iostream>
1.33 +#include <iterator>
1.34 +#include <map>
1.35 +#include <utility>
1.36 +
1.37 +#ifdef __SYMBIAN32__
1.38 +#include "std_log_result.h"
1.39 +#define LOG_FILENAME_LINE __FILE__, __LINE__
1.40 +#endif
1.41 +using namespace std;
1.42 +using namespace boost;
1.43 +
1.44 +#ifndef BOOST_GRAPHVIZ_USE_ISTREAM
1.45 +using namespace boost::spirit;
1.46 +#endif
1.47 +using namespace boost::assign;
1.48 +
1.49 +typedef std::string node_t;
1.50 +typedef std::pair<node_t,node_t> edge_t;
1.51 +
1.52 +typedef std::map<node_t,float> mass_map_t;
1.53 +typedef std::map<edge_t,double> weight_map_t;
1.54 +
1.55 +template <typename Directedness, typename OutEdgeList>
1.56 +bool test_graph(std::istream& dotfile, mass_map_t const& masses,
1.57 + weight_map_t const& weights,
1.58 + std::string const& node_id = "node_id") {
1.59 +
1.60 + typedef adjacency_list < OutEdgeList, vecS, Directedness,
1.61 + property < vertex_name_t, std::string,
1.62 + property < vertex_color_t, float > >,
1.63 + property < edge_weight_t, double > > graph_t;
1.64 + typedef typename graph_traits < graph_t >::edge_descriptor edge_t;
1.65 + typedef typename graph_traits < graph_t >::vertex_descriptor vertex_t;
1.66 +
1.67 + // Construct a graph and set up the dynamic_property_maps.
1.68 + graph_t graph(0);
1.69 + dynamic_properties dp;
1.70 + typename property_map<graph_t, vertex_name_t>::type name =
1.71 + get(vertex_name, graph);
1.72 + dp.property(node_id,name);
1.73 + typename property_map<graph_t, vertex_color_t>::type mass =
1.74 + get(vertex_color, graph);
1.75 + dp.property("mass",mass);
1.76 + typename property_map<graph_t, edge_weight_t>::type weight =
1.77 + get(edge_weight, graph);
1.78 + dp.property("weight",weight);
1.79 +
1.80 + // Read in space characters too!
1.81 + dotfile >> noskipws;
1.82 +
1.83 + bool result = true;
1.84 +#ifdef BOOST_GRAPHVIZ_USE_ISTREAM
1.85 + if(read_graphviz(dotfile,graph,dp,node_id)) {
1.86 +#else
1.87 + std::string data;
1.88 + std::copy(std::istream_iterator<char>(dotfile),
1.89 + std::istream_iterator<char>(),
1.90 + std::back_inserter(data));
1.91 + if(read_graphviz(data.begin(),data.end(),graph,dp,node_id)) {
1.92 +#endif
1.93 + // check masses
1.94 + if(!masses.empty()) {
1.95 + // assume that all the masses have been set
1.96 + // for each vertex:
1.97 + typename graph_traits<graph_t>::vertex_iterator i,j;
1.98 + for(boost::tie(i,j) = vertices(graph); i != j; ++i) {
1.99 + // - get its name
1.100 + std::string node_name = get(name,*i);
1.101 + // - get its mass
1.102 + float node_mass = get(mass,*i);
1.103 + float ref_mass = masses.find(node_name)->second;
1.104 + // - compare the mass to the result in the table
1.105 +
1.106 + BOOST_CHECK_CLOSE(node_mass, ref_mass, 0.01f);
1.107 + }
1.108 + }
1.109 + // check weights
1.110 + if(!weights.empty()) {
1.111 + // assume that all weights have been set
1.112 + /// for each edge:
1.113 + typename graph_traits<graph_t>::edge_iterator i,j;
1.114 + for(boost::tie(i,j) = edges(graph); i != j; ++i) {
1.115 + // - get its name
1.116 + std::pair<std::string,std::string>
1.117 + edge_name = make_pair(get(name, source(*i,graph)),
1.118 + get(name, target(*i,graph)));
1.119 + // - get its weight
1.120 + double edge_weight = get(weight,*i);
1.121 + double ref_weight = weights.find(edge_name)->second;
1.122 + // - compare the weight to teh result in the table
1.123 + BOOST_CHECK_CLOSE(edge_weight, ref_weight, 0.01);
1.124 + }
1.125 + }
1.126 +
1.127 +
1.128 + } else {
1.129 + std::cerr << "Parsing Failed!\n";
1.130 + result = false;
1.131 + }
1.132 +
1.133 + return result;
1.134 + }
1.135 +
1.136 +int test_main(int, char*[]) {
1.137 +
1.138 + typedef istringstream gs_t;
1.139 +
1.140 + // Basic directed graph tests
1.141 + {
1.142 + mass_map_t masses;
1.143 + insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
1.144 + gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }");
1.145 + BOOST_CHECK((test_graph<directedS,vecS>(gs,masses,weight_map_t())));
1.146 + }
1.147 +
1.148 + {
1.149 + weight_map_t weights;
1.150 + insert( weights )(make_pair("a","b"),0.0)
1.151 + (make_pair("c","d"),7.7)(make_pair("e","f"),6.66);
1.152 + gs_t gs("digraph { a -> b edge [weight = 7.7] "
1.153 + "c -> d e-> f [weight = 6.66] }");
1.154 + BOOST_CHECK((test_graph<directedS,vecS>(gs,mass_map_t(),weights)));
1.155 + }
1.156 +
1.157 + // undirected graph with alternate node_id property name
1.158 + {
1.159 + mass_map_t masses;
1.160 + insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
1.161 + gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }");
1.162 + BOOST_CHECK((test_graph<undirectedS,vecS>(gs,masses,weight_map_t(),
1.163 + "nodenames")));
1.164 + }
1.165 +
1.166 + // Basic undirected graph tests
1.167 + {
1.168 + mass_map_t masses;
1.169 + insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
1.170 + gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }");
1.171 + BOOST_CHECK((test_graph<undirectedS,vecS>(gs,masses,weight_map_t())));
1.172 + }
1.173 +
1.174 + {
1.175 + weight_map_t weights;
1.176 + insert( weights )(make_pair("a","b"),0.0)
1.177 + (make_pair("c","d"),7.7)(make_pair("e","f"),6.66);
1.178 + gs_t gs("graph { a -- b eDge [weight = 7.7] "
1.179 + "c -- d e -- f [weight = 6.66] }");
1.180 + BOOST_CHECK((test_graph<undirectedS,vecS>(gs,mass_map_t(),weights)));
1.181 + }
1.182 +
1.183 + // Mismatch directed graph test
1.184 + {
1.185 + mass_map_t masses;
1.186 + insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
1.187 + gs_t gs("graph { a nodE [mass = 7.7] c e [mass = 6.66] }");
1.188 + try {
1.189 + test_graph<directedS,vecS>(gs,masses,weight_map_t());
1.190 + } catch (boost::undirected_graph_error&) {}
1.191 + }
1.192 +
1.193 + // Mismatch undirected graph test
1.194 + {
1.195 + mass_map_t masses;
1.196 + insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
1.197 + gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }");
1.198 + try {
1.199 + test_graph<undirectedS,vecS>(gs,masses,weight_map_t());
1.200 + BOOST_ERROR("Failed to throw boost::directed_graph_error.");
1.201 + } catch (boost::directed_graph_error&) {}
1.202 + }
1.203 +
1.204 + // Complain about parallel edges
1.205 + {
1.206 + weight_map_t weights;
1.207 + insert( weights )(make_pair("a","b"),7.7);
1.208 + gs_t gs("diGraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }");
1.209 + try {
1.210 + test_graph<directedS,setS>(gs,mass_map_t(),weights);
1.211 + BOOST_ERROR("Failed to throw boost::bad_parallel_edge.");
1.212 + } catch (boost::bad_parallel_edge&) {}
1.213 + }
1.214 +
1.215 + // Handle parallel edges gracefully
1.216 + {
1.217 + weight_map_t weights;
1.218 + insert( weights )(make_pair("a","b"),7.7);
1.219 + gs_t gs("digraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }");
1.220 + BOOST_CHECK((test_graph<directedS,vecS>(gs,mass_map_t(),weights)));
1.221 + }
1.222 +
1.223 + #ifdef __SYMBIAN32__
1.224 +
1.225 + std_log(LOG_FILENAME_LINE,"[End Test Case ]");
1.226 +
1.227 + testResultXml("graphiz_test");
1.228 + close_log_file();
1.229 +#endif
1.230 + return 0;
1.231 +}