From 3fed7e6cc8c7fc6aa793ee0317512ae8d8bcfcb8 Mon Sep 17 00:00:00 2001 From: Gunter Winkler Date: Fri, 13 Mar 2009 20:57:52 +0000 Subject: [PATCH] test_lu.cpp: added copyright header triangular_access.cpp: tests constant and mutable iterators over triangular matrices triangular_layout.cpp: tests storage layout of symmetric and triangular matrices common/testhelper.hpp: added more common helper functions for tests Jamfile.v2: added new unit tests svn path=/trunk/libs/numeric/ublas/; revision=51763 --- test/Jamfile.v2 | 6 ++ test/common/testhelper.hpp | 17 ++++ test/test_lu.cpp | 5 + test/triangular_access.cpp | 201 +++++++++++++++++++++++++++++++++++++ test/triangular_layout.cpp | 139 +++++++++++++++++++++++++ 5 files changed, 368 insertions(+) create mode 100644 test/triangular_access.cpp create mode 100644 test/triangular_layout.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f6efac97..0a933cae 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -108,4 +108,10 @@ test-suite numeric/uBLAS ] [ run test_lu.cpp ] + [ run triangular_access.cpp + : : : + NOMESSAGES + ] + [ run triangular_layout.cpp + ] ; diff --git a/test/common/testhelper.hpp b/test/common/testhelper.hpp index ce31fff6..4fbf9c4d 100644 --- a/test/common/testhelper.hpp +++ b/test/common/testhelper.hpp @@ -26,6 +26,23 @@ void assertTrue(const char* message, bool condition) { } } +template < class T > +void assertEquals(const char* message, T expected, T actual) { +#ifndef NOMESSAGES + std::cout << message; +#endif + if ( expected == actual ) { + ++ _success_counter; + std::cout << "1\n"; // success + } else { + #ifndef NOMESSAGES + std::cout << " expected " << expected << " actual " << actual << " "; + #endif + ++ _fail_counter; + std::cout << "0\n"; // failed + } +} + static std::pair getResults() { return std::make_pair(_success_counter, _fail_counter); diff --git a/test/test_lu.cpp b/test/test_lu.cpp index 9d27a493..e2b203f8 100644 --- a/test/test_lu.cpp +++ b/test/test_lu.cpp @@ -1,3 +1,8 @@ +// Copyright 2008 Gunter Winkler +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + // switch automatic singular check off #define BOOST_UBLAS_TYPE_CHECK 0 diff --git a/test/triangular_access.cpp b/test/triangular_access.cpp new file mode 100644 index 00000000..cccf80d0 --- /dev/null +++ b/test/triangular_access.cpp @@ -0,0 +1,201 @@ +/* Test program to test find functions of triagular matrices + * + * author: Gunter Winkler ( guwi17 at gmx dot de ) + */ +// Copyright 2008 Gunter Winkler +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + + +#include +#include +#include + +#include "common/testhelper.hpp" + + +template < class MAT > +void test_iterator( MAT & A ) { + +#ifndef NOMESSAGES + std::cout << "=>"; +#endif + // check mutable iterators + typename MAT::iterator1 it1 = A.begin1(); + typename MAT::iterator1 it1_end = A.end1(); + + for ( ; it1 != it1_end; ++it1 ) { + typename MAT::iterator2 it2 = it1.begin(); + typename MAT::iterator2 it2_end = it1.end(); + for ( ; it2 != it2_end ; ++ it2 ) { +#ifndef NOMESSAGES + std::cout << "( " << it2.index1() << ", " << it2.index2() << ") " << std::flush; +#endif + * it2 = ( 10 * it2.index1() + it2.index2() ); + } +#ifndef NOMESSAGES + std::cout << std::endl; +#endif + } + +} + +template < class MAT > +void test_iterator2( MAT & A ) { + +#ifndef NOMESSAGES + std::cout << "=>"; +#endif + // check mutable iterators + typename MAT::iterator2 it2 = A.begin2(); + typename MAT::iterator2 it2_end = A.end2(); + + for ( ; it2 != it2_end; ++it2 ) { + typename MAT::iterator1 it1 = it2.begin(); + typename MAT::iterator1 it1_end = it2.end(); + for ( ; it1 != it1_end ; ++ it1 ) { +#ifndef NOMESSAGES + std::cout << "( " << it1.index1() << ", " << it1.index2() << ") " << std::flush; +#endif + * it1 = ( 10 * it1.index1() + it1.index2() ); + } +#ifndef NOMESSAGES + std::cout << std::endl; +#endif + } + +} + +template < class MAT > +typename MAT::value_type +test_iterator3( const MAT & A ) { + +#ifndef NOMESSAGES + std::cout << "=>"; +#endif + typename MAT::value_type result = 0; + + // check mutable iterators + typename MAT::const_iterator1 it1 = A.begin1(); + typename MAT::const_iterator1 it1_end = A.end1(); + + for ( ; it1 != it1_end; ++it1 ) { + typename MAT::const_iterator2 it2 = it1.begin(); + typename MAT::const_iterator2 it2_end = it1.end(); + for ( ; it2 != it2_end ; ++ it2 ) { +#ifndef NOMESSAGES + std::cout << "( " << it2.index1() << ", " << it2.index2() << ") " << std::flush; +#endif + result += * it2; + } +#ifndef NOMESSAGES + std::cout << std::endl; +#endif + } + return result; + +} + + +int main (int argc, char * argv[]) { + using namespace boost::numeric::ublas; + + typedef double VALUE_TYPE; + typedef triangular_matrix LT; + typedef triangular_matrix ULT; + typedef triangular_matrix SLT; + typedef triangular_matrix UT; + typedef triangular_matrix UUT; + typedef triangular_matrix SUT; + + LT A(5,5); + + test_iterator(A); + test_iterator2(A); + + ULT B(5,5); + + test_iterator(B); + test_iterator2(B); + + SLT C(5,5); + + test_iterator(C); + test_iterator2(C); + + UT D(5,5); + + test_iterator(D); + test_iterator2(D); + + UUT E(5,5); + + test_iterator(E); + test_iterator2(E); + + SUT F(5,5); + + test_iterator(F); + test_iterator2(F); + + assertTrue("Write access using iterators: ", true); + + assertEquals(" LT: ",420.0,test_iterator3(A)); + assertEquals("ULT: ",315.0,test_iterator3(B)); + assertEquals("SLT: ",310.0,test_iterator3(C)); + assertEquals(" UT: ",240.0,test_iterator3(D)); + assertEquals("UUT: ",135.0,test_iterator3(E)); + assertEquals("SUT: ",130.0,test_iterator3(F)); + + assertTrue("Read access using iterators: ", true); + +#ifndef NOMESSAGES + std::cout << A << B << C << D << E << F << std::endl; +#endif + + typedef matrix MATRIX; + MATRIX mat(5,5); + triangular_adaptor lta((mat)); + triangular_adaptor ulta((mat)); + triangular_adaptor slta((mat)); + triangular_adaptor uta((mat)); + triangular_adaptor uuta((mat)); + triangular_adaptor suta((mat)); + + test_iterator ( lta ); + test_iterator2( lta ); + + test_iterator ( ulta ); + test_iterator2( ulta ); + + test_iterator ( slta ); + test_iterator2( slta ); + + test_iterator ( uta ); + test_iterator2( uta ); + + test_iterator ( uuta ); + test_iterator2( uuta ); + + test_iterator ( suta ); + test_iterator2( suta ); + + assertTrue("Write access using adaptors: ", true); + + assertEquals(" LTA: ",420.0,test_iterator3( lta )); + assertEquals("ULTA: ",315.0,test_iterator3( ulta )); + assertEquals("SLTA: ",310.0,test_iterator3( slta )); + + assertEquals(" UTA: ",240.0,test_iterator3( uta )); + assertEquals("UUTA: ",135.0,test_iterator3( uuta )); + assertEquals("SUTA: ",130.0,test_iterator3( suta )); + + assertTrue("Read access using adaptors: ", true); + +#ifndef NOMESSAGES + std::cout << mat << std::endl; +#endif + + return (getResults().second > 0) ? boost::exit_failure : boost::exit_success; +} diff --git a/test/triangular_layout.cpp b/test/triangular_layout.cpp new file mode 100644 index 00000000..8906bc86 --- /dev/null +++ b/test/triangular_layout.cpp @@ -0,0 +1,139 @@ +// Copyright 2008 Gunter Winkler +// Thanks to Tiago Requeijo for providing this test +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include + +using namespace std; +namespace ublas = boost::numeric::ublas; + +int main(int argc, char* argv[]) +{ + int sz = 4; + ublas::symmetric_matrix UpCol (sz, sz); + ublas::symmetric_matrix UpRow (sz, sz); + ublas::symmetric_matrix LoCol (sz, sz); + ublas::symmetric_matrix LoRow (sz, sz); + + ublas::triangular_matrix TrUpCol (sz, sz); + ublas::triangular_matrix TrUpRow (sz, sz); + ublas::triangular_matrix TrLoCol (sz, sz); + ublas::triangular_matrix TrLoRow (sz, sz); + + for(int i=0; i