From d62af65d7ffd962a55d17037ac3d64954de2d85e Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Sat, 21 Jul 2007 10:36:47 +0000 Subject: [PATCH] port TestNumericTraits to QTestLib. This revealed some shortcomings in tvmet's infrastructure, in particular it lacks adequate fuzzy compares. will port that from eigen1. --- tvmet-1.7.1/testsuite/CMakeLists.txt | 1 + tvmet-1.7.1/testsuite/main.h | 1 + tvmet-1.7.1/testsuite/selftest.cpp | 10 +- tvmet-1.7.1/testsuite/testnumerictraits.cpp | 125 ++++++++++++++++++++ 4 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 tvmet-1.7.1/testsuite/testnumerictraits.cpp diff --git a/tvmet-1.7.1/testsuite/CMakeLists.txt b/tvmet-1.7.1/testsuite/CMakeLists.txt index f5ddf3a1a..2f515104b 100644 --- a/tvmet-1.7.1/testsuite/CMakeLists.txt +++ b/tvmet-1.7.1/testsuite/CMakeLists.txt @@ -11,6 +11,7 @@ INCLUDE_DIRECTORIES( ${QT_INCLUDE_DIR} ) SET(testsuite_SOURCES main.cpp selftest.cpp + testnumerictraits.cpp # TestNumericTraits.h TestNumericTraits.cc # TestNumericTraitsComplex.h TestNumericTraitsComplex.cc # TestConstruction.h TestConstruction.cc diff --git a/tvmet-1.7.1/testsuite/main.h b/tvmet-1.7.1/testsuite/main.h index da1fed02d..279edf077 100644 --- a/tvmet-1.7.1/testsuite/main.h +++ b/tvmet-1.7.1/testsuite/main.h @@ -43,6 +43,7 @@ public: private slots: void selfTest(); + void testNumericTraits(); }; #endif // EIGEN_TESTSUITE_MAIN_H diff --git a/tvmet-1.7.1/testsuite/selftest.cpp b/tvmet-1.7.1/testsuite/selftest.cpp index 1b0ea324e..5d1f7f9bf 100644 --- a/tvmet-1.7.1/testsuite/selftest.cpp +++ b/tvmet-1.7.1/testsuite/selftest.cpp @@ -53,11 +53,11 @@ template static void basics2() 2,5,8, 3,6,9; - assert(v1(0) == T(1) && v1(1) == T(2) && v1(2) == T(3)); + QVERIFY(v1(0) == T(1) && v1(1) == T(2) && v1(2) == T(3)); - assert(m1(0,0) == T(1) && m1(0,1) == T(4) && m1(0,2) == T(7) && - m1(1,0) == T(2) && m1(1,1) == T(5) && m1(1,2) == T(8) && - m1(2,0) == T(3) && m1(2,1) == T(6) && m1(2,2) == T(9)); + QVERIFY(m1(0,0) == T(1) && m1(0,1) == T(4) && m1(0,2) == T(7) && + m1(1,0) == T(2) && m1(1,1) == T(5) && m1(1,2) == T(8) && + m1(2,0) == T(3) && m1(2,1) == T(6) && m1(2,2) == T(9)); } void TvmetTestSuite::selfTest() @@ -69,10 +69,8 @@ void TvmetTestSuite::selfTest() basics2 (); basics2 (); -#ifdef EIGEN_USE_COMPLEX basics1, 4> (); basics1, 4> (); basics2 > (); basics2 > (); -#endif } diff --git a/tvmet-1.7.1/testsuite/testnumerictraits.cpp b/tvmet-1.7.1/testsuite/testnumerictraits.cpp new file mode 100644 index 000000000..476823962 --- /dev/null +++ b/tvmet-1.7.1/testsuite/testnumerictraits.cpp @@ -0,0 +1,125 @@ +/* This file is part of Eigen, a C++ template library for linear algebra + * Copyright (C) 2007 Benoit Jacob + * + * Based on Tvmet source code, http://tvmet.sourceforge.net, + * Copyright (C) 2001 - 2003 Olaf Petzold + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: SelfTest.cc,v 1.1 2004/04/24 11:55:15 opetzold Exp $ + */ + +#include "main.h" + +template struct TestNumericTraits +{ + const T m_real; + const T m_imag; + const T m_conj; + const T m_abs_Q1; + + void real() + { + typedef typename tvmet::NumericTraits::base_type real_type; + real_type r = tvmet::NumericTraits::real(m_real); + QVERIFY( r == m_real ); + } + + void imag() + { + typedef typename tvmet::NumericTraits::base_type real_type; + real_type r = tvmet::NumericTraits::real(m_real); + QVERIFY( r == m_real ); + } + + void conj() + { + typedef typename tvmet::NumericTraits::base_type conj_type; + conj_type r = tvmet::NumericTraits::conj(m_conj); + QVERIFY( r == m_conj ); + } + + void abs() + { + typedef typename tvmet::NumericTraits::base_type value_type; + value_type r1 = tvmet::NumericTraits::abs(m_abs_Q1); + value_type r2 = tvmet::NumericTraits::abs(-m_abs_Q1); + QVERIFY( r1 == m_abs_Q1 ); + QVERIFY( r2 == m_abs_Q1 ); + } + + void sqrt() + { + typedef typename tvmet::NumericTraits::base_type value_type; + value_type r1 = tvmet::NumericTraits::sqrt(m_real); + value_type r2 = tvmet::NumericTraits::sqrt(m_imag); + QVERIFY( r1 == 2 ); + QVERIFY( r2 == 3 ); + } + + void norm1() + { + typedef typename tvmet::NumericTraits::base_type value_type; + value_type r = tvmet::NumericTraits::norm_1(m_real); + QVERIFY( r == tvmet::NumericTraits::abs(m_real) ); + } + + void norm2() + { + typedef typename tvmet::NumericTraits::base_type value_type; + value_type r = tvmet::NumericTraits::norm_2(m_real); + QVERIFY( r == tvmet::NumericTraits::abs(m_real) ); + } + + void normInf() + { + typedef typename tvmet::NumericTraits::base_type value_type; + value_type r = tvmet::NumericTraits::norm_inf(m_real); + QVERIFY( r == tvmet::NumericTraits::abs(m_real) ); + } + + void equals() + { + typedef typename tvmet::NumericTraits::base_type value_type; + value_type lhs, rhs; + lhs = rhs = 47; + QVERIFY( true == tvmet::NumericTraits::equals(lhs,rhs) ); + // a not very intelligent test + rhs += 1; + QVERIFY( false == tvmet::NumericTraits::equals(lhs,rhs) ); + } + + + TestNumericTraits() : m_real(4), m_imag(9), m_conj(16), m_abs_Q1(7) + { + real(); + imag(); + conj(); + abs(); + sqrt(); + norm1(); + norm2(); + normInf(); + equals(); + } +}; + +void TvmetTestSuite::testNumericTraits() +{ + TestNumericTraits(); + TestNumericTraits(); + TestNumericTraits(); + TestNumericTraits(); +}