aboutsummaryrefslogtreecommitdiffhomepage
path: root/tvmet-1.7.1/testsuite/TestNumericTraitsComplex.h
blob: dee00b783ac6c4b0b6d138158ef117d1a7148590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * Tiny Vector Matrix Library
 * Dense Vector Matrix Libary of Tiny size using Expression Templates
 *
 * Copyright (C) 2001 - 2003 Olaf Petzold <opetzold@users.sourceforge.net>
 *
 * 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: TestTraitsComplex.h,v 1.2 2004/11/04 18:12:40 opetzold Exp $
 */

#ifndef TVMET_TEST_NUMERIC_TRAITS_H
#define TVMET_TEST_NUMERIC_TRAITS_H

#include <cppunit/extensions/HelperMacros.h>

#include <complex>

#include <tvmet/Vector.h>
#include <tvmet/Matrix.h>

#include <cassert>

template <class T>
class TestTraitsComplex : public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE( TestTraitsComplex );
  CPPUNIT_TEST( Real );
  CPPUNIT_TEST( Imag );
  CPPUNIT_TEST( Conj );
  CPPUNIT_TEST( Abs );
  CPPUNIT_TEST( Sqrt );
  CPPUNIT_TEST( Norm_1 );
  CPPUNIT_TEST( Norm_2 );
  CPPUNIT_TEST( Norm_Inf );
  CPPUNIT_TEST( Equals );
  CPPUNIT_TEST_SUITE_END();

private:
  typedef tvmet::Vector<T, 3>				vector_type;
  typedef tvmet::Matrix<T, 3, 3>			matrix_type;

public:
  TestTraitsComplex()
    : m_p_real( 3), m_p_imag( 4),
      m_n_real(-3), m_n_imag(-4),
      m_z1(m_p_real, m_p_imag),
      m_z2(m_n_real, m_p_imag),
      m_z3(m_n_real, m_n_imag),
      m_z4(m_p_real, m_n_imag)
  { }

public: // cppunit interface
  /** cppunit hook for fixture set up. */
  void setUp();

  /** cppunit hook for fixture tear down. */
  void tearDown();

protected:
  void Real();
  void Imag();
  void Conj();
  void Abs();
  void Sqrt();
  void Norm_1();
  void Norm_2();
  void Norm_Inf();
  void Equals();

private:
  // Helper
  void AbsHelper(tvmet::dispatch<true>,
		 typename tvmet::Traits<T>::base_type);
  void AbsHelper(tvmet::dispatch<false>,
		 typename tvmet::Traits<T>::base_type);
  void SqrtHelper(tvmet::dispatch<true>);
  void SqrtHelper(tvmet::dispatch<false>);
  void NormHelper(tvmet::dispatch<true>,
		  typename tvmet::Traits<T>::base_type);
  void NormHelper(tvmet::dispatch<false>,
		  typename tvmet::Traits<T>::base_type);


private:
  typedef typename tvmet::Traits<T>::base_type 	base_type;
  typedef T						value_type;

  const base_type					m_p_real;
  const base_type					m_p_imag;
  const base_type					m_n_real;
  const base_type					m_n_imag;

  // complex quadrant I ... IV
  const value_type					m_z1;
  const value_type					m_z2;
  const value_type					m_z3;
  const value_type					m_z4;
};

/*****************************************************************************
 * Implementation Part I (cppunit part)
 ****************************************************************************/

template <class T>
void TestTraitsComplex<T>::setUp () { }

template <class T>
void TestTraitsComplex<T>::tearDown() { }

/*****************************************************************************
 * Implementation Part II
 ****************************************************************************/

template <class T>
void
TestTraitsComplex<T>::Real()
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  base_type r1 = tvmet::Traits<T>::real(m_z1);
  base_type r2 = tvmet::Traits<T>::real(m_z2);
  base_type r3 = tvmet::Traits<T>::real(m_z3);
  base_type r4 = tvmet::Traits<T>::real(m_z4);

  CPPUNIT_ASSERT( r1 == m_p_real );
  CPPUNIT_ASSERT( r2 == m_n_real );
  CPPUNIT_ASSERT( r3 == m_n_real );
  CPPUNIT_ASSERT( r4 == m_p_real );
}


template <class T>
void
TestTraitsComplex<T>::Imag()
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  base_type i1 = tvmet::Traits<T>::imag(m_z1);
  base_type i2 = tvmet::Traits<T>::imag(m_z2);
  base_type i3 = tvmet::Traits<T>::imag(m_z3);
  base_type i4 = tvmet::Traits<T>::imag(m_z4);

  CPPUNIT_ASSERT( i1 == m_p_imag );
  CPPUNIT_ASSERT( i2 == m_p_imag );
  CPPUNIT_ASSERT( i3 == m_n_imag );
  CPPUNIT_ASSERT( i4 == m_n_imag );
}


// conj only for signed types !!
template <> void TestTraitsComplex<std::complex<unsigned char> >::Conj() { }
template <> void TestTraitsComplex<std::complex<unsigned short int> >::Conj() { }
template <> void TestTraitsComplex<std::complex<unsigned int> >::Conj() { }
template <> void TestTraitsComplex<std::complex<unsigned long> >::Conj() { }


template <class T>
void
TestTraitsComplex<T>::Conj()
{
  typedef typename tvmet::Traits<T>::value_type value_type;
  typedef typename tvmet::Traits<T>::base_type base_type;

  enum {
    is_signed = std::numeric_limits<base_type>::is_signed
  };

  // conjugate
  value_type conj_z1 = tvmet::Traits<T>::conj(m_z1);
  value_type conj_z2 = tvmet::Traits<T>::conj(m_z2);
  value_type conj_z3 = tvmet::Traits<T>::conj(m_z3);
  value_type conj_z4 = tvmet::Traits<T>::conj(m_z4);

  // real part
  base_type r1 = tvmet::Traits<T>::real(conj_z1);
  base_type r2 = tvmet::Traits<T>::real(conj_z2);
  base_type r3 = tvmet::Traits<T>::real(conj_z3);
  base_type r4 = tvmet::Traits<T>::real(conj_z4);

  // imag part
  base_type i1 = tvmet::Traits<T>::imag(conj_z1);
  base_type i2 = tvmet::Traits<T>::imag(conj_z2);
  base_type i3 = tvmet::Traits<T>::imag(conj_z3);
  base_type i4 = tvmet::Traits<T>::imag(conj_z4);

  // check on real part; real is tested before
  CPPUNIT_ASSERT( r1 == tvmet::Traits<T>::real(m_z1) );
  CPPUNIT_ASSERT( r2 == tvmet::Traits<T>::real(m_z2) );
  CPPUNIT_ASSERT( r3 == tvmet::Traits<T>::real(m_z3) );
  CPPUNIT_ASSERT( r4 == tvmet::Traits<T>::real(m_z4) );

  // check on imag part
  CPPUNIT_ASSERT( i1 == -tvmet::Traits<T>::imag(m_z1) );
  CPPUNIT_ASSERT( i2 == -tvmet::Traits<T>::imag(m_z2) );
  CPPUNIT_ASSERT( i3 == -tvmet::Traits<T>::imag(m_z3) );
  CPPUNIT_ASSERT( i4 == -tvmet::Traits<T>::imag(m_z4) );
}


template <class T>
void
TestTraitsComplex<T>::Abs()
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  enum {
    is_signed = std::numeric_limits<base_type>::is_signed
  };

  base_type a1 = tvmet::Traits<T>::abs(m_z1);
  base_type a2 = tvmet::Traits<T>::abs(m_z2);
  base_type a3 = tvmet::Traits<T>::abs(m_z3);
  base_type a4 = tvmet::Traits<T>::abs(m_z4);

  // result depends on signed type
  AbsHelper(tvmet::dispatch<is_signed>(), a1);
  AbsHelper(tvmet::dispatch<is_signed>(), a2);
  AbsHelper(tvmet::dispatch<is_signed>(), a3);
  AbsHelper(tvmet::dispatch<is_signed>(), a4);
}


template <class T>
void
TestTraitsComplex<T>::AbsHelper(tvmet::dispatch<true>,
				typename tvmet::Traits<T>::base_type r)
{
  // signed type
  CPPUNIT_ASSERT( r == 5 );
}


template <class T>
void
TestTraitsComplex<T>::AbsHelper(tvmet::dispatch<false>,
				typename tvmet::Traits<T>::base_type r)
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  base_type x = m_z1.real();	// sign doesn't matter on abs()
  base_type y = m_z1.imag();	// sign doesn't matter on abs()

  // unsigned type
  CPPUNIT_ASSERT( r == static_cast<base_type>(
			 tvmet::Traits<base_type>::sqrt(x * x + y * y))
		);
}


template <class T>
void
TestTraitsComplex<T>::Sqrt()
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  enum {
    is_signed = std::numeric_limits<base_type>::is_signed
  };

  // delegate tests
  SqrtHelper(tvmet::dispatch<is_signed>());
}


template <class T>
void
TestTraitsComplex<T>::SqrtHelper(tvmet::dispatch<true>)
{
  // signed type
  typedef typename tvmet::Traits<T>::value_type value_type;

  // sqrt
  value_type z1 = tvmet::Traits<T>::sqrt(m_z1);
  value_type z2 = tvmet::Traits<T>::sqrt(m_z2);
  value_type z3 = tvmet::Traits<T>::sqrt(m_z3);
  value_type z4 = tvmet::Traits<T>::sqrt(m_z4);

  CPPUNIT_ASSERT( z1 == value_type(2,1) );
  CPPUNIT_ASSERT( z2 == value_type(1,2) );
  CPPUNIT_ASSERT( z3 == value_type(1,-2) );
  CPPUNIT_ASSERT( z4 == value_type(2,-1) );

}


template <class T>
void
TestTraitsComplex<T>::SqrtHelper(tvmet::dispatch<false>)
{
  // unsigned type

  /* XXX
   * very dirty - we assume we calculate right
   * on "negative" complex types */

  typedef typename tvmet::Traits<T>::value_type value_type;

  // sqrt
  value_type z1 = tvmet::Traits<T>::sqrt(m_z1);
  value_type z2 = tvmet::Traits<T>::sqrt(m_z2);

  CPPUNIT_ASSERT( z1 == value_type(2,1) );
  CPPUNIT_ASSERT( z2 == value_type(1,2) );
}


template <class T>
void
TestTraitsComplex<T>::Norm_1()
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  enum {
    is_signed = std::numeric_limits<base_type>::is_signed
  };

  // norm_1
  base_type n1 = tvmet::Traits<T>::norm_1(m_z1);
  base_type n2 = tvmet::Traits<T>::norm_1(m_z2);
  base_type n3 = tvmet::Traits<T>::norm_1(m_z3);
  base_type n4 = tvmet::Traits<T>::norm_1(m_z4);

  // result depends on signed type
  NormHelper(tvmet::dispatch<is_signed>(), n1);
  NormHelper(tvmet::dispatch<is_signed>(), n2);
  NormHelper(tvmet::dispatch<is_signed>(), n3);
  NormHelper(tvmet::dispatch<is_signed>(), n4);
}


template <class T>
void
TestTraitsComplex<T>::Norm_2()
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  enum {
    is_signed = std::numeric_limits<base_type>::is_signed
  };

  // norm_2
  base_type n1 = tvmet::Traits<T>::norm_2(m_z1);
  base_type n2 = tvmet::Traits<T>::norm_2(m_z2);
  base_type n3 = tvmet::Traits<T>::norm_2(m_z3);
  base_type n4 = tvmet::Traits<T>::norm_2(m_z4);

  // result depends on signed type
  NormHelper(tvmet::dispatch<is_signed>(), n1);
  NormHelper(tvmet::dispatch<is_signed>(), n2);
  NormHelper(tvmet::dispatch<is_signed>(), n3);
  NormHelper(tvmet::dispatch<is_signed>(), n4);
}


template <class T>
void
TestTraitsComplex<T>::Norm_Inf()
{
  typedef typename tvmet::Traits<T>::base_type base_type;

  enum {
    is_signed = std::numeric_limits<base_type>::is_signed
  };

  // norm_inf
  base_type n1 = tvmet::Traits<T>::norm_inf(m_z1);
  base_type n2 = tvmet::Traits<T>::norm_inf(m_z2);
  base_type n3 = tvmet::Traits<T>::norm_inf(m_z3);
  base_type n4 = tvmet::Traits<T>::norm_inf(m_z4);

  // result depends on signed type
  NormHelper(tvmet::dispatch<is_signed>(), n1);
  NormHelper(tvmet::dispatch<is_signed>(), n2);
  NormHelper(tvmet::dispatch<is_signed>(), n3);
  NormHelper(tvmet::dispatch<is_signed>(), n4);
}

template <class T>
void
TestTraitsComplex<T>::NormHelper(tvmet::dispatch<true>,
				typename tvmet::Traits<T>::base_type)
{
  // XXX To be implement
}


template <class T>
void
TestTraitsComplex<T>::NormHelper(tvmet::dispatch<false>,
				typename tvmet::Traits<T>::base_type)
{
  // XXX To be implement
}


template <class T>
void
TestTraitsComplex<T>::Equals()
{
  // XXX this test is to simple

  typedef typename tvmet::Traits<T>::value_type value_type;

  value_type lhs, rhs;

  {
    lhs = rhs = m_z1;

    CPPUNIT_ASSERT( true == tvmet::Traits<T>::equals(lhs,rhs) );

    rhs += m_z1;

    CPPUNIT_ASSERT( false == tvmet::Traits<T>::equals(lhs,rhs) );
  }
  {
    lhs = rhs = m_z2;

    CPPUNIT_ASSERT( true == tvmet::Traits<T>::equals(lhs,rhs) );

    rhs += m_z2;

    CPPUNIT_ASSERT( false == tvmet::Traits<T>::equals(lhs,rhs) );
  }
  {
    lhs = rhs = m_z3;

    CPPUNIT_ASSERT( true == tvmet::Traits<T>::equals(lhs,rhs) );

    rhs += m_z3;

    CPPUNIT_ASSERT( false == tvmet::Traits<T>::equals(lhs,rhs) );
  }
  {
    lhs = rhs = m_z4;

    CPPUNIT_ASSERT( true == tvmet::Traits<T>::equals(lhs,rhs) );

    rhs += m_z4;

    CPPUNIT_ASSERT( false == tvmet::Traits<T>::equals(lhs,rhs) );
  }
}


#endif // TVMET_TEST_NUMERIC_TRAITS_H


// Local Variables:
// mode:C++
// End: