Ticket #7411: matrix_vector_range.cpp

File matrix_vector_range.cpp, 3.2 KB (added by Gunter, 10 years ago)

example code

Line 
1#define BOOST_TEST_MODULE UBLAS_MATRIX_VECTOR_RANGE_TEST
2#include <boost/test/included/unit_test.hpp>
3
4#include "matrix_vector_range.hpp"
5#include <boost/numeric/ublas/matrix.hpp>
6#include <iterator>
7
8using namespace boost::numeric;
9
10BOOST_AUTO_TEST_CASE( matrix_range_test ){
11 std::size_t const rows=5;
12 std::size_t columns=3;
13 ublas::matrix<int> int_matrix(rows,columns);
14 for(std::size_t i = 0; i != rows; ++i){
15 for(std::size_t j = 0; j != columns; ++j){
16 int_matrix(i,j)=i*rows+j;
17 }
18 }
19 ublas::matrix<int> const& const_int_matrix=int_matrix;
20
21 typedef ublas::matrix_row_range<ublas::matrix<int> > RR;
22 typedef ublas::matrix_row_range<ublas::matrix<int> const> CRR;
23 typedef ublas::matrix_column_range<ublas::matrix<int> > CR;
24 typedef ublas::matrix_column_range<ublas::matrix<int> const > CCR;
25
26 RR row_range = make_row_range(int_matrix);
27 CRR const_row_range = make_row_range(const_int_matrix);
28 CR column_range = make_column_range(int_matrix);
29 CCR const_column_range = make_column_range(const_int_matrix);
30
31 //check whether ranges have the correct sizes
32 BOOST_REQUIRE_EQUAL(row_range.size(),rows);
33 BOOST_REQUIRE_EQUAL(const_row_range.size(),rows);
34 BOOST_REQUIRE_EQUAL(column_range.size(),columns);
35 BOOST_REQUIRE_EQUAL(const_column_range.size(),columns);
36
37 //now test element access, this should entail everything else
38 std::size_t row = 0;
39 for(RR::iterator iter=row_range.begin(); iter != row_range.end();++iter,++row){
40 BOOST_REQUIRE_EQUAL(iter->size(),columns);
41 for(std::size_t j =0; j != columns; ++j){
42 BOOST_CHECK_EQUAL((*iter)(j),int_matrix(row,j));
43 }
44 }
45 row = 0;
46 for(CRR::iterator iter=const_row_range.begin(); iter != const_row_range.end();++iter,++row){
47 BOOST_REQUIRE_EQUAL(iter->size(),columns);
48 for(std::size_t j =0; j != columns; ++j){
49 BOOST_CHECK_EQUAL((*iter)(j),int_matrix(row,j));
50 }
51 }
52
53 std::size_t column = 0;
54 for(CR::iterator iter=column_range.begin(); iter != column_range.end();++iter,++column){
55 BOOST_REQUIRE_EQUAL(iter->size(),rows);
56 for(std::size_t i =0; i != rows; ++i){
57 BOOST_CHECK_EQUAL((*iter)(i),int_matrix(i,column));
58 }
59 }
60 column = 0;
61 for(CCR::iterator iter=const_column_range.begin(); iter != const_column_range.end();++iter,++column){
62 BOOST_REQUIRE_EQUAL(iter->size(),rows);
63 for(std::size_t i =0; i != rows; ++i){
64 BOOST_CHECK_EQUAL((*iter)(i),int_matrix(i,column));
65 }
66 }
67}
68
69//sanity test to ensure that std::iter_swap does not fail on the tested platform(burned child...)
70BOOST_AUTO_TEST_CASE( matrix_range_sanity_iter_swap ){
71 ublas::matrix<int> int_matrix(2,2);
72 int_matrix(0,0) = 0; int_matrix(0,1) = 1;
73 int_matrix(1,0) = 2; int_matrix(1,1) = 3;
74
75 ublas::matrix_row_range<ublas::matrix<int> > row_range(int_matrix);
76 ublas::matrix_column_range<ublas::matrix<int> > column_range(int_matrix);
77
78 std::iter_swap(row_range.begin(),row_range.begin()+1);
79 BOOST_CHECK_EQUAL(int_matrix(0,0),2u);
80 BOOST_CHECK_EQUAL(int_matrix(0,1),3u);
81 BOOST_CHECK_EQUAL(int_matrix(1,0),0u);
82 BOOST_CHECK_EQUAL(int_matrix(1,1),1u);
83
84 std::iter_swap(column_range.begin(),column_range.begin()+1);
85 BOOST_CHECK_EQUAL(int_matrix(0,0),3u);
86 BOOST_CHECK_EQUAL(int_matrix(0,1),2u);
87 BOOST_CHECK_EQUAL(int_matrix(1,0),1u);
88 BOOST_CHECK_EQUAL(int_matrix(1,1),0u);
89}