Unit-tests updated for the stuff in the previous commit.

This commit is contained in:
Benoit Jacob 2007-07-23 07:39:05 +00:00
parent f1bd09024b
commit ca438b2c12
2 changed files with 35 additions and 70 deletions

View File

@ -27,21 +27,25 @@
#include <complex> #include <complex>
#endif #endif
#define EIGEN_USE_COMPLEX
#include <tvmet/Vector.h> #include <tvmet/Vector.h>
#include <tvmet/Matrix.h> #include <tvmet/Matrix.h>
#include <tvmet/util/Random.h>
#include "compare.h"
using namespace tvmet; using namespace tvmet;
using namespace util;
using namespace std; using namespace std;
class TvmetTestSuite : public QObject class TvmetTestSuite : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
TvmetTestSuite() {}; TvmetTestSuite() {};
private slots: private slots:
void selfTest(); void selfTest();
void testNumericTraits(); void testNumericTraits();
}; };

View File

@ -23,103 +23,64 @@
#include "main.h" #include "main.h"
template<typename T, int n> struct TestNumericTraits template<typename T> struct TestNumericTraits
{ {
const T m_real;
const T m_imag;
const T m_conj;
const T m_abs_Q1;
void real() void real()
{ {
typedef typename tvmet::NumericTraits<T>::base_type real_type; T x = someRandom<T>();
real_type r = tvmet::NumericTraits<T>::real(m_real); typedef typename NumericTraits<T>::real_type real_type;
QVERIFY( r == m_real ); real_type r = NumericTraits<T>::real(x);
TEST_APPROX(r, x);
} }
void imag() void imag()
{ {
typedef typename tvmet::NumericTraits<T>::base_type real_type; T x = someRandom<T>();
real_type r = tvmet::NumericTraits<T>::real(m_real); typedef typename NumericTraits<T>::real_type real_type;
QVERIFY( r == m_real ); real_type r = NumericTraits<T>::imag(x);
TEST_ZERO(r);
} }
void conj() void conj()
{ {
typedef typename tvmet::NumericTraits<T>::base_type conj_type; T x = someRandom<T>();
conj_type r = tvmet::NumericTraits<T>::conj(m_conj); typedef typename NumericTraits<T>::real_type conj_type;
QVERIFY( r == m_conj ); conj_type r = NumericTraits<T>::conj(x);
TEST_APPROX(r, x);
} }
void abs() void abs()
{ {
typedef typename tvmet::NumericTraits<T>::base_type value_type; T x = someRandom<T>();
value_type r1 = tvmet::NumericTraits<T>::abs(m_abs_Q1); typedef typename NumericTraits<T>::real_type value_type;
value_type r2 = tvmet::NumericTraits<T>::abs(-m_abs_Q1); value_type r1 = NumericTraits<T>::abs(x);
QVERIFY( r1 == m_abs_Q1 ); value_type r2 = NumericTraits<T>::abs(-x);
QVERIFY( r2 == m_abs_Q1 ); TEST_APPROX(r1, r2);
} }
void sqrt() void sqrt()
{ {
typedef typename tvmet::NumericTraits<T>::base_type value_type; T x = someRandom<T>();
value_type r1 = tvmet::NumericTraits<T>::sqrt(m_real); T a = NumericTraits<T>::abs(x);
value_type r2 = tvmet::NumericTraits<T>::sqrt(m_imag); T b = NumericTraits<T>::sqrt(a);
QVERIFY( r1 == 2 ); // T could be an integer type, so b*b=a is not necessarily true
QVERIFY( r2 == 3 ); TEST_LESSTHAN(b*b, a);
TEST_LESSTHAN(a, (b+1)*(b+1));
} }
void norm1() TestNumericTraits()
{
typedef typename tvmet::NumericTraits<T>::base_type value_type;
value_type r = tvmet::NumericTraits<T>::norm_1(m_real);
QVERIFY( r == tvmet::NumericTraits<T>::abs(m_real) );
}
void norm2()
{
typedef typename tvmet::NumericTraits<T>::base_type value_type;
value_type r = tvmet::NumericTraits<T>::norm_2(m_real);
QVERIFY( r == tvmet::NumericTraits<T>::abs(m_real) );
}
void normInf()
{
typedef typename tvmet::NumericTraits<T>::base_type value_type;
value_type r = tvmet::NumericTraits<T>::norm_inf(m_real);
QVERIFY( r == tvmet::NumericTraits<T>::abs(m_real) );
}
void equals()
{
typedef typename tvmet::NumericTraits<T>::base_type value_type;
value_type lhs, rhs;
lhs = rhs = 47;
QVERIFY( true == tvmet::NumericTraits<T>::equals(lhs,rhs) );
// a not very intelligent test
rhs += 1;
QVERIFY( false == tvmet::NumericTraits<T>::equals(lhs,rhs) );
}
TestNumericTraits() : m_real(4), m_imag(9), m_conj(16), m_abs_Q1(7)
{ {
real(); real();
imag(); imag();
conj(); conj();
abs(); abs();
sqrt(); sqrt();
norm1();
norm2();
normInf();
equals();
} }
}; };
void TvmetTestSuite::testNumericTraits() void TvmetTestSuite::testNumericTraits()
{ {
TestNumericTraits<double,1>(); TestNumericTraits<int>();
TestNumericTraits<int, 2>(); TestNumericTraits<float>();
TestNumericTraits<float, 3>(); TestNumericTraits<double>();
TestNumericTraits<double,4>();
} }