]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright Andrew Sutton 2009 |
2 | // | |
3 | // Use, modification and distribution are subject to the | |
4 | // Boost Software License, Version 1.0 (See accompanying file | |
5 | // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
92f5a8d4 TL |
7 | /*********************************************************** |
8 | ||
9 | IMPORTANT: this file should not be tested - it creates invalid graphs/sequences | |
10 | which *do* crash at runtime - this seems to be the intent, but it's not | |
11 | clear why or whether the file should be retained. | |
12 | ||
13 | ***********************************************************/ | |
14 | ||
7c673cae FG |
15 | #include <iostream> |
16 | #include <string> | |
17 | #include <boost/graph/adjacency_list.hpp> | |
18 | ||
92f5a8d4 | 19 | #include "typestr.hpp" |
7c673cae FG |
20 | |
21 | using namespace std; | |
22 | using namespace boost; | |
23 | ||
24 | // The purpose of this test is simply to provide a testing ground for the | |
25 | // invalidation of iterators and descriptors. | |
26 | ||
27 | template <typename Graph> | |
28 | void make_graph(Graph& g) | |
29 | { | |
30 | // Build a simple (barbell) graph. | |
31 | typedef typename graph_traits<Graph>::vertex_descriptor Vertex; | |
32 | Vertex u = add_vertex(10, g); | |
33 | Vertex v = add_vertex(20, g); | |
34 | add_edge(u, v, 100, g); | |
35 | } | |
36 | ||
37 | // Invalid iterators and descriptors will cause a segfault. | |
38 | template <typename Graph, typename Iterator, typename Descriptor> | |
39 | void test(Graph& g, Iterator i, Descriptor d, string const& str) | |
40 | { | |
41 | int x; | |
42 | cout << "... " << str << " iter" << endl; | |
43 | x = g[*i]; | |
44 | // cout << "... " << x << endl; | |
45 | cout << "... " << str << " desc" << endl; | |
46 | x = g[d]; | |
47 | // cout << "... " << x << endl; | |
48 | } | |
49 | ||
50 | template <typename Graph> | |
51 | void invalidate_edges() | |
52 | { | |
53 | typedef typename graph_traits<Graph>::edge_descriptor Edge; | |
54 | typedef typename graph_traits<Graph>::edge_iterator EdgeIterator; | |
55 | ||
56 | Graph g; | |
57 | make_graph(g); | |
58 | ||
59 | // The actual test. These are valid here. | |
60 | EdgeIterator i = edges(g).first; | |
61 | Edge e = *i; | |
62 | ||
63 | // Add a vertex, see what breaks. | |
64 | add_vertex(g); | |
65 | test(g, i, e, "edges"); | |
66 | }; | |
67 | ||
68 | template <typename Graph> | |
69 | void invalidate_vertices() | |
70 | { | |
71 | typedef typename graph_traits<Graph>::vertex_descriptor Vertex; | |
72 | typedef typename graph_traits<Graph>::vertex_iterator VertexIterator; | |
73 | ||
74 | Graph g; | |
75 | make_graph(g); | |
76 | ||
77 | // The actual test. These are valid here. | |
78 | VertexIterator i = vertices(g).first; | |
79 | Vertex v = *i; | |
80 | ||
81 | // Add a vertex, see what breaks. | |
82 | add_vertex(g); | |
83 | test(g, i, v, "vertices"); | |
84 | } | |
85 | ||
86 | template <typename Graph> | |
87 | void invalidate_out_edges() | |
88 | { | |
89 | typedef typename graph_traits<Graph>::edge_descriptor Edge; | |
90 | typedef typename graph_traits<Graph>::out_edge_iterator OutIterator; | |
91 | ||
92 | Graph g; | |
93 | make_graph(g); | |
94 | ||
95 | // The actual test. These are valid here. | |
96 | OutIterator i = out_edges(*vertices(g).first, g).first; | |
97 | Edge e = *i; | |
98 | ||
99 | // Add a vertex, see what breaks. | |
100 | add_vertex(g); | |
101 | test(g, i, e, "out edges"); | |
102 | } | |
103 | ||
104 | template <typename Graph> | |
105 | void invalidate_adj_verts() | |
106 | { | |
107 | typedef typename graph_traits<Graph>::vertex_descriptor Vertex; | |
108 | typedef typename graph_traits<Graph>::adjacency_iterator AdjIterator; | |
109 | ||
110 | Graph g; | |
111 | make_graph(g); | |
112 | ||
113 | // The actual test. These are valid here. | |
114 | AdjIterator i = adjacent_vertices(*vertices(g).first, g).first; | |
115 | Vertex v = *i; | |
116 | ||
117 | // Add a vertex, see what breaks. | |
118 | add_vertex(g); | |
119 | test(g, i, v, "adjacent vertices"); | |
120 | } | |
121 | ||
122 | ||
123 | int main() | |
124 | { | |
125 | typedef adjacency_list<vecS, vecS, undirectedS, int, int> VVU; | |
126 | cout << "vecS vecS undirectedS" << endl; | |
127 | invalidate_vertices<VVU>(); | |
128 | invalidate_edges<VVU>(); | |
129 | invalidate_out_edges<VVU>(); | |
130 | invalidate_adj_verts<VVU>(); | |
131 | ||
132 | typedef adjacency_list<vecS, vecS, bidirectionalS, int, int> VVB; | |
133 | cout << "vecS vecS bidirectionals" << endl; | |
134 | invalidate_vertices<VVB>(); | |
135 | invalidate_edges<VVB>(); | |
136 | invalidate_out_edges<VVB>(); | |
137 | invalidate_adj_verts<VVB>(); | |
138 | ||
139 | // If you comment out the tests before this, then adj_verts test will | |
140 | // run without segfaulting - at least under gcc-4.3. Not really sure why, | |
141 | // but I'm guessing it's still not generating valid results, and shouldn't | |
142 | // be taken as an indicator of stability. | |
143 | typedef adjacency_list<vecS, vecS, directedS, int, int> VVD; | |
144 | cout << "vecS vecS directedS" << endl; | |
145 | invalidate_vertices<VVD>(); | |
146 | // invalidate_edges<VVD>(); | |
147 | // invalidate_out_edges<VVD>(); | |
148 | // invalidate_adj_verts<VVD>(); | |
149 | ||
150 | typedef adjacency_list<listS, vecS, directedS, int, int> LVD; | |
151 | cout << "listS vecS directedS" << endl; | |
152 | invalidate_vertices<LVD>(); | |
153 | // invalidate_edges<LVD>(); | |
154 | // invalidate_out_edges<LVD>(); | |
155 | // invalidate_adj_verts<LVD>(); | |
156 | } | |
157 |