#define BOOST_TEST_MODULE UBLAS_MATRIX_VECTOR_RANGE_TEST #include #include "matrix_vector_range.hpp" #include #include using namespace boost::numeric; BOOST_AUTO_TEST_CASE( matrix_range_test ){ std::size_t const rows=5; std::size_t columns=3; ublas::matrix int_matrix(rows,columns); for(std::size_t i = 0; i != rows; ++i){ for(std::size_t j = 0; j != columns; ++j){ int_matrix(i,j)=i*rows+j; } } ublas::matrix const& const_int_matrix=int_matrix; typedef ublas::matrix_row_range > RR; typedef ublas::matrix_row_range const> CRR; typedef ublas::matrix_column_range > CR; typedef ublas::matrix_column_range const > CCR; RR row_range = make_row_range(int_matrix); CRR const_row_range = make_row_range(const_int_matrix); CR column_range = make_column_range(int_matrix); CCR const_column_range = make_column_range(const_int_matrix); //check whether ranges have the correct sizes BOOST_REQUIRE_EQUAL(row_range.size(),rows); BOOST_REQUIRE_EQUAL(const_row_range.size(),rows); BOOST_REQUIRE_EQUAL(column_range.size(),columns); BOOST_REQUIRE_EQUAL(const_column_range.size(),columns); //now test element access, this should entail everything else std::size_t row = 0; for(RR::iterator iter=row_range.begin(); iter != row_range.end();++iter,++row){ BOOST_REQUIRE_EQUAL(iter->size(),columns); for(std::size_t j =0; j != columns; ++j){ BOOST_CHECK_EQUAL((*iter)(j),int_matrix(row,j)); } } row = 0; for(CRR::iterator iter=const_row_range.begin(); iter != const_row_range.end();++iter,++row){ BOOST_REQUIRE_EQUAL(iter->size(),columns); for(std::size_t j =0; j != columns; ++j){ BOOST_CHECK_EQUAL((*iter)(j),int_matrix(row,j)); } } std::size_t column = 0; for(CR::iterator iter=column_range.begin(); iter != column_range.end();++iter,++column){ BOOST_REQUIRE_EQUAL(iter->size(),rows); for(std::size_t i =0; i != rows; ++i){ BOOST_CHECK_EQUAL((*iter)(i),int_matrix(i,column)); } } column = 0; for(CCR::iterator iter=const_column_range.begin(); iter != const_column_range.end();++iter,++column){ BOOST_REQUIRE_EQUAL(iter->size(),rows); for(std::size_t i =0; i != rows; ++i){ BOOST_CHECK_EQUAL((*iter)(i),int_matrix(i,column)); } } } //sanity test to ensure that std::iter_swap does not fail on the tested platform(burned child...) BOOST_AUTO_TEST_CASE( matrix_range_sanity_iter_swap ){ ublas::matrix int_matrix(2,2); int_matrix(0,0) = 0; int_matrix(0,1) = 1; int_matrix(1,0) = 2; int_matrix(1,1) = 3; ublas::matrix_row_range > row_range(int_matrix); ublas::matrix_column_range > column_range(int_matrix); std::iter_swap(row_range.begin(),row_range.begin()+1); BOOST_CHECK_EQUAL(int_matrix(0,0),2u); BOOST_CHECK_EQUAL(int_matrix(0,1),3u); BOOST_CHECK_EQUAL(int_matrix(1,0),0u); BOOST_CHECK_EQUAL(int_matrix(1,1),1u); std::iter_swap(column_range.begin(),column_range.begin()+1); BOOST_CHECK_EQUAL(int_matrix(0,0),3u); BOOST_CHECK_EQUAL(int_matrix(0,1),2u); BOOST_CHECK_EQUAL(int_matrix(1,0),1u); BOOST_CHECK_EQUAL(int_matrix(1,1),0u); }