Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
testcase.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_RENDER_TESTCASE_H_)
21 #define __MITSUBA_RENDER_TESTCASE_H_
22 
23 #include <mitsuba/render/util.h>
24 
26 
27 #if defined(MTS_TESTCASE)
28 /**
29  * When a testcase is being compiled, define the following preprocessor macros for convenience
30  */
31 #define assertEquals(actual, expected) assertEqualsImpl(actual, expected, 0, __FILE__, __LINE__)
32 #define assertEqualsEpsilon(actual, expected, epsilon) assertEqualsImpl(actual, expected, epsilon, __FILE__, __LINE__)
33 #define assertTrue(expr) assertTrueImpl(expr, #expr, __FILE__, __LINE__)
34 #define assertFalse(expr) assertFalseImpl(expr, #expr, __FILE__, __LINE__)
35 #define failAndContinue(msg) failAndContinueImpl(msg, __FILE__, __LINE__)
36 #endif
37 
38 /** \brief Base class of all testcases.
39  *
40  * Implementations of this interface can be executed using the 'mtsutil' command.
41  * The execution order is as follows: after initializaiton using init(), any tests
42  * declared using the MTS_DECLARE_TEST() macro are executed. Finally,
43  * the shutdown() method is called. See the files in 'mitsuba/src/tests'
44  * for examples.
45  *
46  * \ingroup librender
47  */
49 public:
50  /**
51  * Perform any required initializations. The default
52  * implementation simply returns
53  */
54  virtual void init();
55 
56  /**
57  * Execute any required shutdown code. The default
58  * implementation simply returns
59  */
60  virtual void shutdown();
61 
62  /// Return the number of executed testcases
63  inline int getExecuted() const { return m_executed; }
64 
65  /// Return the number of successfully executed testcases
66  inline int getSucceeded() const { return m_succeeded; }
67 
69 protected:
70  /// Virtual destructor
71  virtual ~TestCase() { }
72 
73  /// Asserts that the two integer values are equal
74  void assertEqualsImpl(int actual, int expected, Float epsilon, const char *file, int line);
75 
76  /// Asserts that the two floating point values are equal
77  void assertEqualsImpl(Float actual, Float expected, Float epsilon, const char *file, int line);
78 
79  /// Asserts that the two spectral power distributions are equal
80  void assertEqualsImpl(const Spectrum &actual, const Spectrum &expected, Float epsilon, const char *file, int line);
81 
82  /// Asserts that the two 2D vectors are equal
83  void assertEqualsImpl(const Vector2 &actual, const Vector2 &expected, Float epsilon, const char *file, int line);
84 
85  /// Asserts that the two 3D vectors are equal
86  void assertEqualsImpl(const Vector &actual, const Vector &expected, Float epsilon, const char *file, int line);
87 
88  /// Asserts that the two 4D vectors are equal
89  void assertEqualsImpl(const Vector4 &actual, const Vector4 &expected, Float epsilon, const char *file, int line);
90 
91  /// Asserts that the two 2D points are equal
92  void assertEqualsImpl(const Point2 &actual, const Point2 &expected, Float epsilon, const char *file, int line);
93 
94  /// Asserts that the two 3D points are equal
95  void assertEqualsImpl(const Point &actual, const Point &expected, Float epsilon, const char *file, int line);
96 
97  /// Asserts that the two 4x4 matrices are equal
98  template<int M, int N> void assertEqualsImpl(const Matrix<M, N, Float> &actual, const Matrix<M, N, Float> &expected, Float epsilon, const char *file, int line) {
99  bool match = true;
100  for (int i=0; i<M; ++i)
101  for (int j=0; j<N; ++j)
102  if (std::abs(expected.m[i][j]-actual.m[i][j]) > epsilon)
103  match = false;
104  if (!match)
105  Thread::getThread()->getLogger()->log(EError, NULL, file, line, "Assertion failure: "
106  "expected matrix %s, got %s.", expected.toString().c_str(), actual.toString().c_str());
107  }
108 
109  /// Asserts that a condition is true
110  void assertTrueImpl(bool condition, const char *expr, const char *file, int line);
111 
112  /// Asserts that a condition is false
113  void assertFalseImpl(bool condition, const char *expr, const char *file, int line);
114 
115  /// Note a failure and continue
116  void failAndContinueImpl(const std::string &msg, const char *file, int line);
117 
118  /// Increase the number of succeeded tests
119  void succeed();
120 protected:
121  int m_executed, m_succeeded;
122 };
123 
125 
126 #define EXECUTE_GUARDED(name) \
127  try { \
128  Log(EInfo, "Executing test \"%s\" ..", #name); \
129  m_executed++;\
130  name();\
131  m_succeeded++;\
132  } catch (std::exception &e) {\
133  Log(EInfo, "Testcase failed with error: %s", e.what());\
134  }
135 
136 #define MTS_BEGIN_TESTCASE() \
137  MTS_DECLARE_CLASS() \
138  int run(int argc, char **argv) {\
139  init(); \
140  Log(EInfo, "Executing testcase \"%s\" ..", getClass()->getName().c_str()); \
141  m_executed = m_succeeded = 0;
142 
143 #define MTS_DECLARE_TEST(name) \
144  EXECUTE_GUARDED(name)
145 
146 #define MTS_END_TESTCASE()\
147  shutdown();\
148  return m_executed - m_succeeded;\
149  }
150 
151 #define MTS_EXPORT_TESTCASE(name, descr) \
152  MTS_IMPLEMENT_CLASS(name, false, TestCase) \
153  extern "C" { \
154  void MTS_EXPORT *CreateUtility() { \
155  return new name(); \
156  } \
157  const char MTS_EXPORT *GetDescription() { \
158  return descr; \
159  } \
160  }
161 
162 #endif /* __MITSUBA_RENDER_TESTCASE_H_ */
std::string toString() const
Return a string representation.
Definition: matrix.h:426
Logger * getLogger()
Return the thread&#39;s logger instance.
void assertEqualsImpl(const Matrix< M, N, Float > &actual, const Matrix< M, N, Float > &expected, Float epsilon, const char *file, int line)
Asserts that the two 4x4 matrices are equal.
Definition: testcase.h:98
Generic fixed-size dense matrix class using a row-major storage format.
Definition: matrix.h:33
int getExecuted() const
Return the number of executed testcases.
Definition: testcase.h:63
int m_succeeded
Definition: testcase.h:121
Abstract utility class – can be used to implement loadable utility plugins that perform various actio...
Definition: util.h:32
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
static Thread * getThread()
Return the current thread.
void log(ELogLevel level, const Class *theClass, const char *fileName, int lineNumber, const char *fmt,...)
Process a log message.
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
T m[M][N]
Definition: matrix.h:35
Definition: fwd.h:99
Base class of all testcases.
Definition: testcase.h:48
Definition: fwd.h:96
Error message, causes an exception to be thrown.
Definition: formatter.h:33
int getSucceeded() const
Return the number of successfully executed testcases.
Definition: testcase.h:66
Definition: fwd.h:100
#define MTS_EXPORT_RENDER
Definition: platform.h:109
Discrete spectral power distribution based on a number of wavelength bins over the 360-830 nm range...
Definition: spectrum.h:663
Definition: fwd.h:97
Definition: fwd.h:95
#define MTS_NAMESPACE_END
Definition: platform.h:138