From b0c66adfb1c72d060ec98ebf1004a73b6e4cd559 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Mon, 1 Oct 2018 23:21:37 +0200 Subject: bug #231: initial implementation of STL iterators for dense expressions --- test/stl_iterators.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 test/stl_iterators.cpp (limited to 'test/stl_iterators.cpp') diff --git a/test/stl_iterators.cpp b/test/stl_iterators.cpp new file mode 100644 index 000000000..1ed52b354 --- /dev/null +++ b/test/stl_iterators.cpp @@ -0,0 +1,128 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2018 Gael Guennebaud +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "main.h" + +template< class Iterator > +std::reverse_iterator +make_reverse_iterator( Iterator i ) +{ + return std::reverse_iterator(i); +} + +template +void test_range_for_loop(int rows=Rows, int cols=Cols) +{ + using std::begin; + using std::end; + + typedef Matrix VectorType; + typedef Matrix ColMatrixType; + typedef Matrix RowMatrixType; + VectorType v = VectorType::Random(rows); + ColMatrixType A = ColMatrixType::Random(rows,cols); + RowMatrixType B = RowMatrixType::Random(rows,cols); + + Index i, j; + +#if EIGEN_HAS_CXX11 + i = 0; + for(auto x : v) { VERIFY_IS_EQUAL(x,v[i++]); } + + j = internal::random(0,A.cols()-1); + i = 0; + for(auto x : A.col(j)) { VERIFY_IS_EQUAL(x,A(i++,j)); } + + i = 0; + for(auto x : (v+A.col(j))) { VERIFY_IS_APPROX(x,v(i)+A(i,j)); ++i; } + + j = 0; + i = internal::random(0,A.rows()-1); + for(auto x : A.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); } + + i = 0; + for(auto x : A.reshaped()) { VERIFY_IS_EQUAL(x,A(i++)); } + + Matrix Bc = B; + i = 0; + for(auto x : B.reshaped()) { VERIFY_IS_EQUAL(x,Bc(i++)); } + + VectorType w(v.size()); + i = 0; + for(auto& x : w) { x = v(i++); } + VERIFY_IS_EQUAL(v,w); +#endif + + if(rows>=2) + { + v(1) = v(0)-Scalar(1); + VERIFY(!std::is_sorted(begin(v),end(v))); + } + std::sort(begin(v),end(v)); + VERIFY(std::is_sorted(begin(v),end(v))); + VERIFY(!std::is_sorted(make_reverse_iterator(end(v)),make_reverse_iterator(begin(v)))); + + { + j = internal::random(0,A.cols()-1); + // std::sort(begin(A.col(j)),end(A.col(j))); // does not compile because this returns const iterators + typename ColMatrixType::ColXpr Acol = A.col(j); + std::sort(begin(Acol),end(Acol)); + VERIFY(std::is_sorted(Acol.cbegin(),Acol.cend())); + + // This raises an assert because this creates a pair of iterator referencing two different proxy objects: + // std::sort(A.col(j).begin(),A.col(j).end()); + // VERIFY(std::is_sorted(A.col(j).cbegin(),A.col(j).cend())); // same issue + } + + { + j = internal::random(0,A.cols()-1); + typename ColMatrixType::ColXpr Acol = A.col(j); + std::partial_sum(begin(Acol), end(Acol), begin(v)); + VERIFY_IS_APPROX(v(seq(1,last)), v(seq(0,last-1))+Acol(seq(1,last))); + + // inplace + std::partial_sum(begin(Acol), end(Acol), begin(Acol)); + VERIFY_IS_APPROX(v, Acol); + } + +#if EIGEN_HAS_CXX11 + j = 0; + for(auto c : A.allCols()) { VERIFY_IS_APPROX(c.sum(), A.col(j).sum()); ++j; } + j = 0; + for(auto c : B.allCols()) { VERIFY_IS_APPROX(c.sum(), B.col(j).sum()); ++j; } + + j = 0; + for(auto c : B.allCols()) { + i = 0; + for(auto& x : c) { + VERIFY_IS_EQUAL(x, B(i,j)); + x = A(i,j); + ++i; + } + ++j; + } + VERIFY_IS_APPROX(A,B); + B = Bc; // restore B + + i = 0; + for(auto r : A.allRows()) { VERIFY_IS_APPROX(r.sum(), A.row(i).sum()); ++i; } + i = 0; + for(auto r : B.allRows()) { VERIFY_IS_APPROX(r.sum(), B.row(i).sum()); ++i; } + +#endif +} + +EIGEN_DECLARE_TEST(stl_iterators) +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST_1(( test_range_for_loop() )); + CALL_SUBTEST_1(( test_range_for_loop() )); + CALL_SUBTEST_1(( test_range_for_loop(internal::random(10,200), internal::random(10,200)) )); + } +} -- cgit v1.2.3