]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright 2008 Gunter Winkler <guwi17@gmx.de> |
2 | // Distributed under the Boost Software License, Version 1.0. (See | |
3 | // accompanying file LICENSE_1_0.txt or copy at | |
4 | // http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | ||
7 | #ifndef _HPP_TESTHELPER_ | |
8 | #define _HPP_TESTHELPER_ | |
9 | ||
10 | #include <utility> | |
11 | #include <iostream> | |
12 | #include <boost/numeric/ublas/vector_expression.hpp> | |
13 | #include <boost/numeric/ublas/matrix_expression.hpp> | |
14 | ||
15 | static unsigned _success_counter = 0; | |
16 | static unsigned _fail_counter = 0; | |
17 | ||
18 | static inline | |
19 | void assertTrue(const char* message, bool condition) { | |
20 | #ifndef NOMESSAGES | |
21 | std::cout << message; | |
22 | #endif | |
23 | if ( condition ) { | |
24 | ++ _success_counter; | |
25 | std::cout << "1\n"; // success | |
26 | } else { | |
27 | ++ _fail_counter; | |
28 | std::cout << "0\n"; // failed | |
29 | } | |
30 | } | |
31 | ||
32 | template < class T > | |
33 | void assertEquals(const char* message, T expected, T actual) { | |
34 | #ifndef NOMESSAGES | |
35 | std::cout << message; | |
36 | #endif | |
37 | if ( expected == actual ) { | |
38 | ++ _success_counter; | |
39 | std::cout << "1\n"; // success | |
40 | } else { | |
41 | #ifndef NOMESSAGES | |
42 | std::cout << " expected " << expected << " actual " << actual << " "; | |
43 | #endif | |
44 | ++ _fail_counter; | |
45 | std::cout << "0\n"; // failed | |
46 | } | |
47 | } | |
48 | ||
49 | inline static | |
50 | std::pair<unsigned, unsigned> getResults() { | |
51 | return std::make_pair(_success_counter, _fail_counter); | |
52 | } | |
53 | ||
54 | template < class M1, class M2 > | |
55 | bool compare( const boost::numeric::ublas::matrix_expression<M1> & m1, | |
56 | const boost::numeric::ublas::matrix_expression<M2> & m2 ) { | |
57 | if ((m1().size1() != m2().size1()) || | |
58 | (m1().size2() != m2().size2())) { | |
59 | return false; | |
60 | } | |
61 | ||
62 | size_t size1 = m1().size1(); | |
63 | size_t size2 = m1().size2(); | |
64 | for (size_t i=0; i < size1; ++i) { | |
65 | for (size_t j=0; j < size2; ++j) { | |
66 | if ( m1()(i,j) != m2()(i,j) ) return false; | |
67 | } | |
68 | } | |
69 | return true; | |
70 | } | |
71 | ||
72 | template < class M1, class M2 > | |
73 | bool compare( const boost::numeric::ublas::vector_expression<M1> & m1, | |
74 | const boost::numeric::ublas::vector_expression<M2> & m2 ) { | |
75 | if (m1().size() != m2().size()) { | |
76 | return false; | |
77 | } | |
78 | ||
79 | size_t size = m1().size(); | |
80 | for (size_t i=0; i < size; ++i) { | |
81 | if ( m1()(i) != m2()(i) ) return false; | |
82 | } | |
83 | return true; | |
84 | } | |
85 | ||
86 | // Compare if two matrices or vectors are equals based on distance. | |
87 | ||
88 | template <class AE> | |
89 | typename AE::value_type mean_square(const boost::numeric::ublas::matrix_expression<AE> &me) { | |
90 | typename AE::value_type s(0); | |
91 | typename AE::size_type i, j; | |
92 | for (i=0; i!= me().size1(); i++) { | |
93 | for (j=0; j!= me().size2(); j++) { | |
94 | s += boost::numeric::ublas::scalar_traits<typename AE::value_type>::type_abs(me()(i,j)); | |
95 | } | |
96 | } | |
97 | return s / (me().size1() * me().size2()); | |
98 | } | |
99 | ||
100 | template <class AE> | |
101 | typename AE::value_type mean_square(const boost::numeric::ublas::vector_expression<AE> &ve) { | |
102 | // We could have use norm2 here, but ublas' ABS does not support unsigned types. | |
103 | typename AE::value_type s(0); | |
104 | typename AE::size_type i; | |
105 | for (i=0; i!= ve().size(); i++) { | |
106 | s += boost::numeric::ublas::scalar_traits<typename AE::value_type>::type_abs(ve()(i)); | |
107 | } | |
108 | return s / ve().size(); | |
109 | } | |
110 | ||
111 | template < class M1, class M2 > | |
112 | bool compare_to( const boost::numeric::ublas::matrix_expression<M1> & m1, | |
113 | const boost::numeric::ublas::matrix_expression<M2> & m2, | |
114 | double tolerance = 0.0 ) { | |
115 | if ((m1().size1() != m2().size1()) || | |
116 | (m1().size2() != m2().size2())) { | |
117 | return false; | |
118 | } | |
119 | ||
120 | return mean_square(m2() - m1()) <= tolerance; | |
121 | } | |
122 | ||
123 | template < class M1, class M2 > | |
124 | bool compare_to( const boost::numeric::ublas::vector_expression<M1> & m1, | |
125 | const boost::numeric::ublas::vector_expression<M2> & m2, | |
126 | double tolerance = 0.0 ) { | |
127 | if (m1().size() != m2().size()) { | |
128 | return false; | |
129 | } | |
130 | ||
131 | return mean_square(m2() - m1()) <= tolerance; | |
132 | } | |
133 | ||
134 | ||
135 | #endif |