/* 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() { T x = Traits::random(); typedef typename Traits::real_type real_type; real_type r = Traits::real(x); TEST_APPROX(r, x); } 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() { T x = Traits::random(); T a = Traits::abs(x); T b = Traits::sqrt(a); // T could be an integer type, so b*b=a is not necessarily true TEST_LESSTHAN(b*b, a); TEST_LESSTHAN(a, (b+1)*(b+1)); } void isApprox() { T x = Traits::random(); TEST(Traits::isApprox(x,x)); TEST(Traits::isApprox(x,x+Traits::epsilon()/10)); TEST(!Traits::isApprox(x,x+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() { T one(1), x = Traits::random(); TEST(Traits::isLessThan(x, x+one)); TEST(!Traits::isLessThan(x+one, x)); TEST(Traits::isLessThan(x, x+Traits::epsilon()/10)); } TestTraits() { real(); imag(); conj(); abs(); sqrt(); isApprox(); isNegligible(); isZero(); isLessThan(); } }; void TvmetTestSuite::testTraits() { TestTraits(); TestTraits(); TestTraits(); }