/Dependencies/tvmet/testsuite/TestNumericTraitsComplex.h

https://bitbucket.org/sonofusion/.simulation · C Header · 463 lines · 300 code · 102 blank · 61 comment · 32 complexity · 1aa0a0d19f993661b83257d2f6efcc06 MD5 · raw file

  1. /*
  2. * Tiny Vector Matrix Library
  3. * Dense Vector Matrix Libary of Tiny size using Expression Templates
  4. *
  5. * Copyright (C) 2001 - 2006 Olaf Petzold <opetzold@users.sourceforge.net>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * $Id: TestNumericTraitsComplex.h,v 1.3 2006-11-20 20:22:18 opetzold Exp $
  22. */
  23. #ifndef TVMET_TEST_NUMERIC_TRAITS_H
  24. #define TVMET_TEST_NUMERIC_TRAITS_H
  25. #include <cppunit/extensions/HelperMacros.h>
  26. #include <complex>
  27. #include <tvmet/Vector.h>
  28. #include <tvmet/Matrix.h>
  29. #include <cassert>
  30. template <class T>
  31. class TestNumericTraitsComplex : public CppUnit::TestFixture
  32. {
  33. CPPUNIT_TEST_SUITE( TestNumericTraitsComplex );
  34. CPPUNIT_TEST( Real );
  35. CPPUNIT_TEST( Imag );
  36. CPPUNIT_TEST( Conj );
  37. CPPUNIT_TEST( Abs );
  38. CPPUNIT_TEST( Sqrt );
  39. CPPUNIT_TEST( Norm_1 );
  40. CPPUNIT_TEST( Norm_2 );
  41. CPPUNIT_TEST( Norm_Inf );
  42. CPPUNIT_TEST( Equals );
  43. CPPUNIT_TEST_SUITE_END();
  44. private:
  45. typedef tvmet::Vector<T, 3> vector_type;
  46. typedef tvmet::Matrix<T, 3, 3> matrix_type;
  47. public:
  48. TestNumericTraitsComplex()
  49. : m_p_real( 3), m_p_imag( 4),
  50. m_n_real(-3), m_n_imag(-4),
  51. m_z1(m_p_real, m_p_imag),
  52. m_z2(m_n_real, m_p_imag),
  53. m_z3(m_n_real, m_n_imag),
  54. m_z4(m_p_real, m_n_imag)
  55. { }
  56. public: // cppunit interface
  57. /** cppunit hook for fixture set up. */
  58. void setUp();
  59. /** cppunit hook for fixture tear down. */
  60. void tearDown();
  61. protected:
  62. void Real();
  63. void Imag();
  64. void Conj();
  65. void Abs();
  66. void Sqrt();
  67. void Norm_1();
  68. void Norm_2();
  69. void Norm_Inf();
  70. void Equals();
  71. private:
  72. // Helper
  73. void AbsHelper(tvmet::dispatch<true>,
  74. typename tvmet::NumericTraits<T>::base_type);
  75. void AbsHelper(tvmet::dispatch<false>,
  76. typename tvmet::NumericTraits<T>::base_type);
  77. void SqrtHelper(tvmet::dispatch<true>);
  78. void SqrtHelper(tvmet::dispatch<false>);
  79. void NormHelper(tvmet::dispatch<true>,
  80. typename tvmet::NumericTraits<T>::base_type);
  81. void NormHelper(tvmet::dispatch<false>,
  82. typename tvmet::NumericTraits<T>::base_type);
  83. private:
  84. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  85. typedef T value_type;
  86. const base_type m_p_real;
  87. const base_type m_p_imag;
  88. const base_type m_n_real;
  89. const base_type m_n_imag;
  90. // complex quadrant I ... IV
  91. const value_type m_z1;
  92. const value_type m_z2;
  93. const value_type m_z3;
  94. const value_type m_z4;
  95. };
  96. /*****************************************************************************
  97. * Implementation Part I (cppunit part)
  98. ****************************************************************************/
  99. template <class T>
  100. void TestNumericTraitsComplex<T>::setUp () { }
  101. template <class T>
  102. void TestNumericTraitsComplex<T>::tearDown() { }
  103. /*****************************************************************************
  104. * Implementation Part II
  105. ****************************************************************************/
  106. template <class T>
  107. void
  108. TestNumericTraitsComplex<T>::Real()
  109. {
  110. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  111. base_type r1 = tvmet::NumericTraits<T>::real(m_z1);
  112. base_type r2 = tvmet::NumericTraits<T>::real(m_z2);
  113. base_type r3 = tvmet::NumericTraits<T>::real(m_z3);
  114. base_type r4 = tvmet::NumericTraits<T>::real(m_z4);
  115. CPPUNIT_ASSERT( r1 == m_p_real );
  116. CPPUNIT_ASSERT( r2 == m_n_real );
  117. CPPUNIT_ASSERT( r3 == m_n_real );
  118. CPPUNIT_ASSERT( r4 == m_p_real );
  119. }
  120. template <class T>
  121. void
  122. TestNumericTraitsComplex<T>::Imag()
  123. {
  124. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  125. base_type i1 = tvmet::NumericTraits<T>::imag(m_z1);
  126. base_type i2 = tvmet::NumericTraits<T>::imag(m_z2);
  127. base_type i3 = tvmet::NumericTraits<T>::imag(m_z3);
  128. base_type i4 = tvmet::NumericTraits<T>::imag(m_z4);
  129. CPPUNIT_ASSERT( i1 == m_p_imag );
  130. CPPUNIT_ASSERT( i2 == m_p_imag );
  131. CPPUNIT_ASSERT( i3 == m_n_imag );
  132. CPPUNIT_ASSERT( i4 == m_n_imag );
  133. }
  134. // conj only for signed types !!
  135. template <> void TestNumericTraitsComplex<std::complex<unsigned char> >::Conj() { }
  136. template <> void TestNumericTraitsComplex<std::complex<unsigned short int> >::Conj() { }
  137. template <> void TestNumericTraitsComplex<std::complex<unsigned int> >::Conj() { }
  138. template <> void TestNumericTraitsComplex<std::complex<unsigned long> >::Conj() { }
  139. template <class T>
  140. void
  141. TestNumericTraitsComplex<T>::Conj()
  142. {
  143. typedef typename tvmet::NumericTraits<T>::value_type value_type;
  144. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  145. enum {
  146. is_signed = std::numeric_limits<base_type>::is_signed
  147. };
  148. // conjugate
  149. value_type conj_z1 = tvmet::NumericTraits<T>::conj(m_z1);
  150. value_type conj_z2 = tvmet::NumericTraits<T>::conj(m_z2);
  151. value_type conj_z3 = tvmet::NumericTraits<T>::conj(m_z3);
  152. value_type conj_z4 = tvmet::NumericTraits<T>::conj(m_z4);
  153. // real part
  154. base_type r1 = tvmet::NumericTraits<T>::real(conj_z1);
  155. base_type r2 = tvmet::NumericTraits<T>::real(conj_z2);
  156. base_type r3 = tvmet::NumericTraits<T>::real(conj_z3);
  157. base_type r4 = tvmet::NumericTraits<T>::real(conj_z4);
  158. // imag part
  159. base_type i1 = tvmet::NumericTraits<T>::imag(conj_z1);
  160. base_type i2 = tvmet::NumericTraits<T>::imag(conj_z2);
  161. base_type i3 = tvmet::NumericTraits<T>::imag(conj_z3);
  162. base_type i4 = tvmet::NumericTraits<T>::imag(conj_z4);
  163. // check on real part; real is tested before
  164. CPPUNIT_ASSERT( r1 == tvmet::NumericTraits<T>::real(m_z1) );
  165. CPPUNIT_ASSERT( r2 == tvmet::NumericTraits<T>::real(m_z2) );
  166. CPPUNIT_ASSERT( r3 == tvmet::NumericTraits<T>::real(m_z3) );
  167. CPPUNIT_ASSERT( r4 == tvmet::NumericTraits<T>::real(m_z4) );
  168. // check on imag part
  169. CPPUNIT_ASSERT( i1 == -tvmet::NumericTraits<T>::imag(m_z1) );
  170. CPPUNIT_ASSERT( i2 == -tvmet::NumericTraits<T>::imag(m_z2) );
  171. CPPUNIT_ASSERT( i3 == -tvmet::NumericTraits<T>::imag(m_z3) );
  172. CPPUNIT_ASSERT( i4 == -tvmet::NumericTraits<T>::imag(m_z4) );
  173. }
  174. template <class T>
  175. void
  176. TestNumericTraitsComplex<T>::Abs()
  177. {
  178. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  179. enum {
  180. is_signed = std::numeric_limits<base_type>::is_signed
  181. };
  182. base_type a1 = tvmet::NumericTraits<T>::abs(m_z1);
  183. base_type a2 = tvmet::NumericTraits<T>::abs(m_z2);
  184. base_type a3 = tvmet::NumericTraits<T>::abs(m_z3);
  185. base_type a4 = tvmet::NumericTraits<T>::abs(m_z4);
  186. // result depends on signed type
  187. AbsHelper(tvmet::dispatch<is_signed>(), a1);
  188. AbsHelper(tvmet::dispatch<is_signed>(), a2);
  189. AbsHelper(tvmet::dispatch<is_signed>(), a3);
  190. AbsHelper(tvmet::dispatch<is_signed>(), a4);
  191. }
  192. template <class T>
  193. void
  194. TestNumericTraitsComplex<T>::AbsHelper(tvmet::dispatch<true>,
  195. typename tvmet::NumericTraits<T>::base_type r)
  196. {
  197. // signed type
  198. CPPUNIT_ASSERT( r == 5 );
  199. }
  200. template <class T>
  201. void
  202. TestNumericTraitsComplex<T>::AbsHelper(tvmet::dispatch<false>,
  203. typename tvmet::NumericTraits<T>::base_type r)
  204. {
  205. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  206. base_type x = m_z1.real(); // sign doesn't matter on abs()
  207. base_type y = m_z1.imag(); // sign doesn't matter on abs()
  208. // unsigned type
  209. CPPUNIT_ASSERT( r == static_cast<base_type>(
  210. tvmet::NumericTraits<base_type>::sqrt(x * x + y * y))
  211. );
  212. }
  213. template <class T>
  214. void
  215. TestNumericTraitsComplex<T>::Sqrt()
  216. {
  217. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  218. enum {
  219. is_signed = std::numeric_limits<base_type>::is_signed
  220. };
  221. // delegate tests
  222. SqrtHelper(tvmet::dispatch<is_signed>());
  223. }
  224. template <class T>
  225. void
  226. TestNumericTraitsComplex<T>::SqrtHelper(tvmet::dispatch<true>)
  227. {
  228. // signed type
  229. typedef typename tvmet::NumericTraits<T>::value_type value_type;
  230. // sqrt
  231. value_type z1 = tvmet::NumericTraits<T>::sqrt(m_z1);
  232. value_type z2 = tvmet::NumericTraits<T>::sqrt(m_z2);
  233. value_type z3 = tvmet::NumericTraits<T>::sqrt(m_z3);
  234. value_type z4 = tvmet::NumericTraits<T>::sqrt(m_z4);
  235. CPPUNIT_ASSERT( z1 == value_type(2,1) );
  236. CPPUNIT_ASSERT( z2 == value_type(1,2) );
  237. CPPUNIT_ASSERT( z3 == value_type(1,-2) );
  238. CPPUNIT_ASSERT( z4 == value_type(2,-1) );
  239. }
  240. template <class T>
  241. void
  242. TestNumericTraitsComplex<T>::SqrtHelper(tvmet::dispatch<false>)
  243. {
  244. // unsigned type
  245. /* XXX
  246. * very dirty - we assume we calculate right
  247. * on "negative" complex types */
  248. typedef typename tvmet::NumericTraits<T>::value_type value_type;
  249. // sqrt
  250. value_type z1 = tvmet::NumericTraits<T>::sqrt(m_z1);
  251. value_type z2 = tvmet::NumericTraits<T>::sqrt(m_z2);
  252. CPPUNIT_ASSERT( z1 == value_type(2,1) );
  253. CPPUNIT_ASSERT( z2 == value_type(1,2) );
  254. }
  255. template <class T>
  256. void
  257. TestNumericTraitsComplex<T>::Norm_1()
  258. {
  259. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  260. enum {
  261. is_signed = std::numeric_limits<base_type>::is_signed
  262. };
  263. // norm_1
  264. base_type n1 = tvmet::NumericTraits<T>::norm_1(m_z1);
  265. base_type n2 = tvmet::NumericTraits<T>::norm_1(m_z2);
  266. base_type n3 = tvmet::NumericTraits<T>::norm_1(m_z3);
  267. base_type n4 = tvmet::NumericTraits<T>::norm_1(m_z4);
  268. // result depends on signed type
  269. NormHelper(tvmet::dispatch<is_signed>(), n1);
  270. NormHelper(tvmet::dispatch<is_signed>(), n2);
  271. NormHelper(tvmet::dispatch<is_signed>(), n3);
  272. NormHelper(tvmet::dispatch<is_signed>(), n4);
  273. }
  274. template <class T>
  275. void
  276. TestNumericTraitsComplex<T>::Norm_2()
  277. {
  278. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  279. enum {
  280. is_signed = std::numeric_limits<base_type>::is_signed
  281. };
  282. // norm_2
  283. base_type n1 = tvmet::NumericTraits<T>::norm_2(m_z1);
  284. base_type n2 = tvmet::NumericTraits<T>::norm_2(m_z2);
  285. base_type n3 = tvmet::NumericTraits<T>::norm_2(m_z3);
  286. base_type n4 = tvmet::NumericTraits<T>::norm_2(m_z4);
  287. // result depends on signed type
  288. NormHelper(tvmet::dispatch<is_signed>(), n1);
  289. NormHelper(tvmet::dispatch<is_signed>(), n2);
  290. NormHelper(tvmet::dispatch<is_signed>(), n3);
  291. NormHelper(tvmet::dispatch<is_signed>(), n4);
  292. }
  293. template <class T>
  294. void
  295. TestNumericTraitsComplex<T>::Norm_Inf()
  296. {
  297. typedef typename tvmet::NumericTraits<T>::base_type base_type;
  298. enum {
  299. is_signed = std::numeric_limits<base_type>::is_signed
  300. };
  301. // norm_inf
  302. base_type n1 = tvmet::NumericTraits<T>::norm_inf(m_z1);
  303. base_type n2 = tvmet::NumericTraits<T>::norm_inf(m_z2);
  304. base_type n3 = tvmet::NumericTraits<T>::norm_inf(m_z3);
  305. base_type n4 = tvmet::NumericTraits<T>::norm_inf(m_z4);
  306. // result depends on signed type
  307. NormHelper(tvmet::dispatch<is_signed>(), n1);
  308. NormHelper(tvmet::dispatch<is_signed>(), n2);
  309. NormHelper(tvmet::dispatch<is_signed>(), n3);
  310. NormHelper(tvmet::dispatch<is_signed>(), n4);
  311. }
  312. template <class T>
  313. void
  314. TestNumericTraitsComplex<T>::NormHelper(tvmet::dispatch<true>,
  315. typename tvmet::NumericTraits<T>::base_type)
  316. {
  317. // XXX To be implement
  318. }
  319. template <class T>
  320. void
  321. TestNumericTraitsComplex<T>::NormHelper(tvmet::dispatch<false>,
  322. typename tvmet::NumericTraits<T>::base_type)
  323. {
  324. // XXX To be implement
  325. }
  326. template <class T>
  327. void
  328. TestNumericTraitsComplex<T>::Equals()
  329. {
  330. // XXX this test is to simple
  331. typedef typename tvmet::NumericTraits<T>::value_type value_type;
  332. value_type lhs, rhs;
  333. {
  334. lhs = rhs = m_z1;
  335. CPPUNIT_ASSERT( true == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  336. rhs += m_z1;
  337. CPPUNIT_ASSERT( false == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  338. }
  339. {
  340. lhs = rhs = m_z2;
  341. CPPUNIT_ASSERT( true == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  342. rhs += m_z2;
  343. CPPUNIT_ASSERT( false == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  344. }
  345. {
  346. lhs = rhs = m_z3;
  347. CPPUNIT_ASSERT( true == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  348. rhs += m_z3;
  349. CPPUNIT_ASSERT( false == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  350. }
  351. {
  352. lhs = rhs = m_z4;
  353. CPPUNIT_ASSERT( true == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  354. rhs += m_z4;
  355. CPPUNIT_ASSERT( false == tvmet::NumericTraits<T>::equals(lhs,rhs) );
  356. }
  357. }
  358. #endif // TVMET_TEST_NUMERIC_TRAITS_H
  359. // Local Variables:
  360. // mode:C++
  361. // End: