MarmotTesting.h
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------
2  * _
3  * _ __ ___ __ _ _ __ _ __ ___ ___ | |_
4  * | '_ ` _ \ / _` | '__| '_ ` _ \ / _ \| __|
5  * | | | | | | (_| | | | | | | | | (_) | |_
6  * |_| |_| |_|\__,_|_| |_| |_| |_|\___/ \__|
7  *
8  * Unit of Strength of Materials and Structural Analysis
9  * University of Innsbruck,
10  * 2020 - today
11  *
12  * festigkeitslehre@uibk.ac.at
13  *
14  * Alexander Dummer alexander.dummer@uibk.ac.at
15  *
16  * This file is part of the MAteRialMOdellingToolbox (marmot).
17  *
18  * This library is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  *
23  * The full text of the license can be found in the file LICENSE.md at
24  * the top level directory of marmot.
25  * ---------------------------------------------------------------------
26  */
27 
28 #include "autodiff/forward/dual/dual.hpp"
29 #include "unsupported/Eigen/CXX11/Tensor"
30 #include <Eigen/Core>
31 #include <iostream>
32 
33 namespace Marmot::Testing {
34 
35  bool checkIfEqual( const double a, const double b, const double tol = 1e-15 );
36 
37  bool checkIfEqual( const autodiff::dual a, const autodiff::dual b, const double tol = 1e-15 );
38 
39  bool checkIfEqual( const std::complex< double > a, const std::complex< double > b, const double tol = 1e-15 );
40 
41  std::string getString( const double a );
42  std::string getString( const autodiff::dual a );
43 
44  template < typename T >
45  bool checkIfEqual( const Eigen::Matrix< T, -1, -1 >& a,
46  const Eigen::Matrix< T, -1, -1 >& b,
47  const double tol = 1e-15 )
48  {
49  if ( a.rows() != b.rows() || a.cols() != b.cols() ) {
50  return false;
51  }
52  for ( int i = 0; i < a.rows(); i++ ) {
53  for ( int j = 0; j < a.cols(); j++ ) {
54  auto cond = checkIfEqual( a( i, j ), b( i, j ), tol );
55  if ( !cond ) {
56  std::cout << " -> HINT: a(" << i << "," << j << ") = " << getString( a( i, j ) ) << " != b(" << i << ","
57  << j << ") =" << getString( b( i, j ) ) << std::endl;
58  return false;
59  }
60  }
61  }
62  return true;
63  }
64 
65  template < typename T, long int... Rest >
66  bool checkIfEqual( const Eigen::TensorFixedSize< T, Eigen::Sizes< Rest... > >& a,
67  const Eigen::TensorFixedSize< T, Eigen::Sizes< Rest... > >& b,
68  const double tol = 1e-15 )
69  {
70  const T* a_data = a.data();
71  const T* b_data = b.data();
72 
73  for ( int i = 0; i < a.size(); i++ ) {
74  auto cond = checkIfEqual( a_data[i], b_data[i], tol );
75  if ( !cond ) {
76  std::cout << " -> HINT: a(" << i << ") = " << getString( a_data[i] ) << " != b(" << i
77  << ") =" << getString( b_data[i] ) << std::endl;
78  return false;
79  }
80  }
81  return true;
82  }
83 
84  void throwExceptionOnFailure( const bool condition, const std::string& message = "" );
85 
86  void executeTestsAndCollectExceptions( const std::vector< std::function< void() > >& testFunctions );
87 
88 } // namespace Marmot::Testing
Marmot::Testing
Definition: MarmotTesting.h:33
Marmot::Testing::getString
std::string getString(const double a)
Marmot::Testing::checkIfEqual
bool checkIfEqual(const double a, const double b, const double tol=1e-15)
Marmot::Testing::throwExceptionOnFailure
void throwExceptionOnFailure(const bool condition, const std::string &message="")
Marmot::Testing::executeTestsAndCollectExceptions
void executeTestsAndCollectExceptions(const std::vector< std::function< void() > > &testFunctions)