1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/graph/src/graph.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,172 @@
1.4 +//=======================================================================
1.5 +// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
1.6 +// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
1.7 +//
1.8 +// Distributed under the Boost Software License, Version 1.0. (See
1.9 +// accompanying file LICENSE_1_0.txt or copy at
1.10 +// http://www.boost.org/LICENSE_1_0.txt)
1.11 +//=======================================================================
1.12 +
1.13 +/*
1.14 + * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
1.15 +*/
1.16 +
1.17 +#include <boost/config.hpp>
1.18 +#include <iostream>
1.19 +#include <vector>
1.20 +#include <utility>
1.21 +#include <algorithm>
1.22 +#include <boost/graph/adjacency_list.hpp>
1.23 +
1.24 +#ifdef __SYMBIAN32__
1.25 +#include "std_log_result.h"
1.26 +#define LOG_FILENAME_LINE __FILE__, __LINE__
1.27 +#endif
1.28 +
1.29 +
1.30 +
1.31 +using namespace boost;
1.32 +using namespace std;
1.33 +
1.34 +typedef property<vertex_color_t, default_color_type,
1.35 + property<vertex_distance_t,int,
1.36 + property<vertex_degree_t,int,
1.37 + property<vertex_in_degree_t, int,
1.38 + property<vertex_out_degree_t,int> > > > > VertexProperty;
1.39 +typedef property<edge_weight_t,int> EdgeProperty;
1.40 +typedef adjacency_list<vecS, vecS, bidirectionalS,
1.41 + VertexProperty, EdgeProperty> Graph;
1.42 +
1.43 +template <class Graph>
1.44 +void print(Graph& g) {
1.45 + typename Graph::vertex_iterator i, end;
1.46 + typename Graph::out_edge_iterator ei, edge_end;
1.47 + for(boost::tie(i,end) = vertices(g); i != end; ++i) {
1.48 + cout << *i << " --> ";
1.49 + for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
1.50 + cout << target(*ei, g) << " ";
1.51 + cout << endl;
1.52 + }
1.53 +}
1.54 +
1.55 +std::size_t myrand(std::size_t N) {
1.56 + std::size_t ret = rand() % N;
1.57 + // cout << "N = " << N << " rand = " << ret << endl;
1.58 + return ret;
1.59 +}
1.60 +
1.61 +template <class Graph>
1.62 +bool check_edge(Graph& g, std::size_t a, std::size_t b) {
1.63 + typedef typename Graph::vertex_descriptor Vertex;
1.64 + typename Graph::adjacency_iterator vi, viend, found;
1.65 + boost::tie(vi, viend) = adjacent_vertices(vertex(a,g), g);
1.66 +
1.67 + found = find(vi, viend, vertex(b, g));
1.68 + if ( found == viend )
1.69 + return false;
1.70 +
1.71 + return true;
1.72 +}
1.73 +
1.74 +int main(int, char*[])
1.75 +{
1.76 + std::size_t N = 5;
1.77 +
1.78 + Graph g(N);
1.79 + int i;
1.80 +
1.81 + bool is_failed = false;
1.82 +
1.83 + for (i=0; i<6; ++i) {
1.84 + std::size_t a = myrand(N), b = myrand(N);
1.85 + while ( a == b ) b = myrand(N);
1.86 + cout << "edge edge (" << a << "," << b <<")" << endl;
1.87 + //add edges
1.88 + add_edge(a, b, g);
1.89 + is_failed = is_failed || (! check_edge(g, a, b) );
1.90 + }
1.91 +
1.92 + if ( is_failed )
1.93 + cerr << " Failed."<< endl;
1.94 + else
1.95 + cerr << " Passed."<< endl;
1.96 +
1.97 + print(g);
1.98 +
1.99 + //remove_edge
1.100 + for (i = 0; i<2; ++i) {
1.101 + std::size_t a = myrand(N), b = myrand(N);
1.102 + while ( a == b ) b = myrand(N);
1.103 + cout << "remove edge (" << a << "," << b <<")" << endl;
1.104 + remove_edge(a, b, g);
1.105 + is_failed = is_failed || check_edge(g, a, b);
1.106 + }
1.107 + if ( is_failed )
1.108 + cerr << " Failed."<< endl;
1.109 + else
1.110 + cerr << " Passed."<< endl;
1.111 +
1.112 + print(g);
1.113 +
1.114 + //add_vertex
1.115 + is_failed = false;
1.116 + std::size_t old_N = N;
1.117 + std::size_t vid = add_vertex(g);
1.118 + std::size_t vidp1 = add_vertex(g);
1.119 +
1.120 + N = num_vertices(g);
1.121 + if ( (N - 2) != old_N )
1.122 + cerr << " Failed."<< endl;
1.123 + else
1.124 + cerr << " Passed."<< endl;
1.125 +
1.126 + is_failed = false;
1.127 + for (i=0; i<2; ++i) {
1.128 + std::size_t a = myrand(N), b = myrand(N);
1.129 + while ( a == vid ) a = myrand(N);
1.130 + while ( b == vidp1 ) b = myrand(N);
1.131 + cout << "add edge (" << vid << "," << a <<")" << endl;
1.132 + cout << "add edge (" << vid << "," << vidp1 <<")" << endl;
1.133 + add_edge(vid, a, g);
1.134 + add_edge(b, vidp1, g);
1.135 + is_failed = is_failed || ! check_edge(g, vid, a);
1.136 + is_failed = is_failed || ! check_edge(g, b, vidp1);
1.137 + }
1.138 + if ( is_failed )
1.139 + cerr << " Failed."<< endl;
1.140 + else
1.141 + cerr << " Passed."<< endl;
1.142 + print(g);
1.143 +
1.144 + // clear_vertex
1.145 + std::size_t c = myrand(N);
1.146 + is_failed = false;
1.147 + clear_vertex(c, g);
1.148 +
1.149 + if ( out_degree(c, g) != 0 )
1.150 + is_failed = true;
1.151 +
1.152 + cout << "Removing vertex " << c << endl;
1.153 + remove_vertex(c, g);
1.154 +
1.155 + old_N = N;
1.156 + N = num_vertices(g);
1.157 +
1.158 + if ( (N + 1) != old_N )
1.159 + is_failed = true;
1.160 +
1.161 + if ( is_failed )
1.162 + cerr << " Failed."<< endl;
1.163 + else
1.164 + cerr << " Passed."<< endl;
1.165 +
1.166 + print(g);
1.167 +
1.168 + #ifdef __SYMBIAN32__
1.169 +
1.170 + std_log(LOG_FILENAME_LINE,"[End Test Case ]");
1.171 + testResultXml("graph");
1.172 + close_log_file();
1.173 + #endif
1.174 + return 0;
1.175 +}