/* 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 TestTraits { void real_imag_conj_abs() { T x = Traits::random(); typedef typename Traits::real_type real; real r = Traits::real(x); real i = Traits::imag(x); T c = Traits::conj(x); real a = Traits::abs(x); // a must be real TEST_APPROX(a, Traits::real(a)); TEST_APPROX(a, Traits::real(a)); TEST_ZERO(Traits::imag(a)); TEST_ZERO(Traits::imag(a)); // check Pythagora's formula if(Traits::isFloat() || !Traits::isComplex()) TEST_APPROX(r*r + i*i, a*a); // check complex conjugation TEST_APPROX(-i, Traits::imag(c)); } void imag() { T x = Traits::random(); typedef typename Traits::real_type real_type; real_type r = Traits::imag(x); TEST_ZERO(r); } void conj() { T x = Traits::random(); typedef typename Traits::real_type conj_type; conj_type r = Traits::conj(x); TEST_APPROX(r, x); } void abs() { T x = Traits::random(); typedef typename Traits::real_type value_type; value_type r1 = Traits::abs(x); value_type r2 = Traits::abs(-x); TEST_APPROX(r1, r2); } void sqrt() { // only test compilation here T x = Traits::random(); Traits::sqrt(x); } void isApprox() { T x = Traits::random(); T e = T(Traits::epsilon()) / T(10); TEST(Traits::isApprox(x,x)); TEST(Traits::isApprox(x,x+e)); TEST(!Traits::isApprox(x,x+T(1))); } void isNegligible() { T zero(0), one(1), x = Traits::random(); TEST(Traits::isNegligible(zero, zero)); TEST(Traits::isNegligible(zero, one)); TEST(!Traits::isNegligible(one, one)); if(x != zero) { TEST(Traits::isNegligible(zero, x)); } TEST(!Traits::isNegligible(one, x)); } void isZero() { T zero(0), one(1), x = Traits::random(), y = Traits::random(); TEST(Traits::isZero(zero)); TEST(!Traits::isZero(one)); TEST(Traits::isZero((x+y)*(x+y)-x*x-y*y-x*y-y*x)); } void isLessThan() { if(Traits::isComplex()) { T x = Traits::random(), y = Traits::random(); TEST(!Traits::isLessThan(x,y)); } else { T one(1), x = Traits::random(); T e = T(Traits::epsilon()) / T(10); TEST(Traits::isLessThan(x, x+one)); TEST(!Traits::isLessThan(x+one, x)); TEST(Traits::isLessThan(x, x+e)); } } TestTraits() { real_imag_conj_abs(); sqrt(); isApprox(); isNegligible(); isZero(); isLessThan(); } }; void TvmetTestSuite::testTraits() { TestTraits(); TestTraits(); TestTraits(); TestTraits >(); TestTraits >(); TestTraits >(); }