From 6fe01d0c4c65cab751f7f030d30e9ff82f8e97a2 Mon Sep 17 00:00:00 2001 From: Jeremiah Willcock Date: Sun, 4 Dec 2005 05:28:26 +0000 Subject: [PATCH] Added more comprehensive CSR tests, and fixed a few m_last_source-related bugs [SVN r31897] --- .../graph/compressed_sparse_row_graph.hpp | 10 +++--- test/csr_graph_test.cpp | 35 ++++++++++++++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/include/boost/graph/compressed_sparse_row_graph.hpp b/include/boost/graph/compressed_sparse_row_graph.hpp index 124c9e7e..a7289f9b 100644 --- a/include/boost/graph/compressed_sparse_row_graph.hpp +++ b/include/boost/graph/compressed_sparse_row_graph.hpp @@ -440,7 +440,7 @@ class compressed_sparse_row_graph std::vector m_rowstart; std::vector m_column; GraphProperty m_property; - Vertex m_last_source; + Vertex m_last_source; // Last source of added edge, plus one }; template @@ -483,10 +483,11 @@ add_vertices(Vertex count, BOOST_CSR_GRAPH_TYPE& g) { template inline typename BOOST_CSR_GRAPH_TYPE::edge_descriptor add_edge(Vertex src, Vertex tgt, BOOST_CSR_GRAPH_TYPE& g) { - assert (src >= g.m_last_source); + assert (src >= g.m_last_source - 1 && src < num_vertices(g)); + assert (tgt < num_vertices(g)); EdgeIndex num_edges_orig = g.m_column.size(); - for (; g.m_last_source < src; ++g.m_last_source) - g.m_rowstart[g.m_last_source + 1] = num_edges_orig; + for (; g.m_last_source <= src; ++g.m_last_source) + g.m_rowstart[g.m_last_source] = num_edges_orig; g.m_rowstart[src + 1] = num_edges_orig + 1; g.m_column.push_back(tgt); return typename BOOST_CSR_GRAPH_TYPE::edge_descriptor(src, num_edges_orig); @@ -638,6 +639,7 @@ inline typename BOOST_CSR_GRAPH_TYPE::edge_descriptor edge_from_index(EdgeIndex idx, const BOOST_CSR_GRAPH_TYPE& g) { typedef typename std::vector::const_iterator row_start_iter; + assert (idx < num_edges(g)); row_start_iter src_plus_1 = std::upper_bound(g.m_rowstart.begin(), g.m_rowstart.begin() + g.m_last_source + 1, diff --git a/test/csr_graph_test.cpp b/test/csr_graph_test.cpp index 557ff2c5..8d72ed0d 100644 --- a/test/csr_graph_test.cpp +++ b/test/csr_graph_test.cpp @@ -134,11 +134,33 @@ make_edge_to_index_pair(const Graph& g) g)); } +void check_consistency(const CSRGraph& g) { + // Do a bunch of tests on the graph internal data + // Check that m_last_source is valid + BOOST_CHECK(g.m_last_source <= num_vertices(g)); + // Check that m_rowstart entries are valid, and that entries after + // m_last_source + 1 are all zero + BOOST_CHECK(g.m_rowstart[0] == 0); + for (CSRGraph::vertices_size_type i = 0; i < g.m_last_source; ++i) { + BOOST_CHECK(g.m_rowstart[i + 1] >= g.m_rowstart[i]); + BOOST_CHECK(g.m_rowstart[i + 1] <= num_edges(g)); + } + for (CSRGraph::vertices_size_type i = g.m_last_source + 1; + i < g.m_rowstart.size(); ++i) { + BOOST_CHECK(g.m_rowstart[i] == 0); + } + // Check that m_column entries are within range + for (CSRGraph::edges_size_type i = 0; i < num_edges(g); ++i) { + BOOST_CHECK(g.m_column[i] < num_vertices(g)); + } +} + template void test(const OrigGraph& g) { // Check copying of a graph CSRGraph g2(g); + check_consistency(g2); BOOST_CHECK((std::size_t)std::distance(edges(g2).first, edges(g2).second) == num_edges(g2)); assert_graphs_equal(g, boost::identity_property_map(), @@ -151,6 +173,7 @@ void test(const OrigGraph& g) boost::make_transform_iterator(edges(g2).second, make_edge_to_index_pair(g2)), num_vertices(g)); + check_consistency(g3); BOOST_CHECK((std::size_t)std::distance(edges(g3).first, edges(g3).second) == num_edges(g3)); assert_graphs_equal(g2, boost::identity_property_map(), @@ -164,10 +187,12 @@ void test(const OrigGraph& g) BOOST_CHECK(first_vert == 0); BOOST_CHECK(num_vertices(g4) == num_vertices(g3)); CSRGraph::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g3); ei != ei_end; ++ei) { + int i; + for (boost::tie(ei, ei_end) = edges(g3), i = 0; ei != ei_end; ++ei, ++i) { CSRGraph::edge_descriptor e = add_edge(source(*ei, g3), target(*ei, g3), g4); BOOST_CHECK(source(e, g4) == source(*ei, g3)); BOOST_CHECK(target(e, g4) == target(*ei, g3)); + if (i % 13 == 0) check_consistency(g4); } assert_graphs_equal(g3, boost::identity_property_map(), g4, boost::identity_property_map(), @@ -330,6 +355,11 @@ int test_main(int argc, char* argv[]) if (argc > 1) seed = boost::lexical_cast(argv[1]); std::cout << "Seed = " << seed << std::endl; + { + std::cout << "Testing empty graph" << std::endl; + CSRGraph g; + test(g); + } test(1000, 0.05, seed); test(1000, 0.0, seed); test(1000, 0.1, seed); @@ -340,8 +370,11 @@ int test_main(int argc, char* argv[]) CSRGraph g; add_vertices(std::size_t(5), g); add_edge(std::size_t(1), std::size_t(2), g); + check_consistency(g); add_edge(std::size_t(2), std::size_t(3), g); + check_consistency(g); add_edge(std::size_t(2), std::size_t(4), g); + check_consistency(g); CSRGraph::edge_iterator ei, ei_end; for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { BOOST_CHECK(edge_from_index(get(boost::edge_index, g, *ei), g) == *ei);