2323#include < boost/range/iterator_range.hpp>
2424#include < boost/concept_check.hpp> // for ignore_unused_variable_warning
2525#include < iostream>
26+ #include < set>
2627#include < vector>
2728#include < algorithm>
2829#include < boost/lexical_cast.hpp>
@@ -450,6 +451,53 @@ void test_bidirectional_sorted_edges()
450451 }
451452}
452453
454+ // Checks that the CSR has the same out edges per vertex as its source.
455+ template < typename Source, typename Csr >
456+ void check_same_out_edges (const Source& src, const Csr& csr)
457+ {
458+ check_consistency (csr);
459+ BOOST_TEST_EQ (num_vertices (csr), num_vertices (src));
460+ std::size_t expected_edges = 0 ;
461+ for (const auto & v : boost::make_iterator_range (vertices (src)))
462+ {
463+ expected_edges += out_degree (v, src);
464+ std::multiset< std::size_t > expected;
465+ for (const auto & e : boost::make_iterator_range (out_edges (v, src)))
466+ {
467+ expected.insert (target (e, src));
468+ }
469+ std::multiset< std::size_t > actual;
470+ for (const auto & e : boost::make_iterator_range (out_edges (v, csr)))
471+ {
472+ actual.insert (target (e, csr));
473+ }
474+ BOOST_TEST (expected == actual);
475+ }
476+ BOOST_TEST_EQ (num_edges (csr), expected_edges);
477+ }
478+
479+ // Copies an adjacency_list into a CSR through every constructor and assign.
480+ template < typename Directed, typename Csr > void test_copy_from ()
481+ {
482+ using source_type = boost::adjacency_list< boost::vecS, boost::vecS, Directed >;
483+ source_type src (4 );
484+ add_edge (0 , 1 , src);
485+ add_edge (1 , 2 , src);
486+ add_edge (2 , 3 , src);
487+ add_edge (3 , 0 , src);
488+ add_edge (2 , 2 , src);
489+ const Csr from_ctor (src);
490+ check_same_out_edges (src, from_ctor);
491+ const Csr from_ctor_index (src, get (boost::vertex_index, src));
492+ check_same_out_edges (src, from_ctor_index);
493+ Csr from_assign;
494+ from_assign.assign (src);
495+ check_same_out_edges (src, from_assign);
496+ Csr from_assign_index;
497+ from_assign_index.assign (src, get (boost::vertex_index, src));
498+ check_same_out_edges (src, from_assign_index);
499+ }
500+
453501int main ()
454502{
455503 int seed = 42 ;
@@ -469,6 +517,9 @@ int main()
469517 test_graph_properties ();
470518 test_vertex_and_edge_properties ();
471519 test_bidirectional_sorted_edges ();
520+ test_copy_from< boost::directedS, CSRGraphT >();
521+ test_copy_from< boost::bidirectionalS, CSRGraphT >();
522+ test_copy_from< boost::directedS, BidirCSRGraphT >();
472523
473524 {
474525 std::cout << " Testing CSR graph built from unsorted edges" << std::endl;
0 commit comments