HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unittest.h
Go to the documentation of this file.
1 /*
2  Copyright 2010 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 #pragma once
32 
33 #include <algorithm>
34 #include <cmath>
35 #include <cstdlib>
36 #include <iostream>
37 
38 #include <OpenImageIO/strutil.h>
39 #include <OpenImageIO/sysutil.h>
40 
41 
43 
44 namespace simd {
45 // Force a float-based abs and max to appear in namespace simd
46 inline float
47 abs(float x)
48 {
49  return std::abs(x);
50 }
51 inline float
52 max(float x, float y)
53 {
54  return std::max(x, y);
55 }
56 } // namespace simd
57 
58 namespace pvt {
59 
61 public:
63  : m_failures(0)
64  {
65  }
67  {
68  if (m_failures) {
69  std::cout << Sysutil::Term(std::cout).ansi("red", "ERRORS!\n");
70  std::exit(m_failures != 0);
71  } else {
72  std::cout << Sysutil::Term(std::cout).ansi("green", "OK\n");
73  }
74  }
76  {
77  ++m_failures;
78  return *this;
79  } // prefix
80  int operator++(int) { return m_failures++; } // postfix
82  {
83  m_failures += i;
84  return *this;
85  }
86  operator int() const { return m_failures; }
87 
88 private:
89  int m_failures = 0;
90 };
91 
92 
93 template<typename X, typename Y>
94 inline bool
95 equal_approx(const X& x, const Y& y)
96 {
97  using namespace simd;
98  return all(abs((x) - (y)) <= 0.001f * max(abs(x), abs(y)));
99 }
100 
101 
102 } // end namespace pvt
103 
105 
106 // Helper: print entire vectors. This makes the OIIO_CHECK_EQUAL macro
107 // work for std::vector!
108 template<typename T>
109 inline std::ostream&
110 operator<<(std::ostream& out, const std::vector<T>& v)
111 {
112  out << '{' << OIIO::Strutil::join(v, ",") << '}';
113  return out;
114 }
115 
116 static OIIO::pvt::UnitTestFailureCounter unit_test_failures;
117 
118 
119 
120 /// OIIO_CHECK_* macros checks if the conditions is met, and if not,
121 /// prints an error message indicating the module and line where the
122 /// error occurred, but does NOT abort. This is helpful for unit tests
123 /// where we do not want one failure.
124 #define OIIO_CHECK_ASSERT(x) \
125  ((x) ? ((void)0) \
126  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
127  << __FILE__ << ":" << __LINE__ << ":\n" \
128  << "FAILED: " \
129  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
130  << "\n"), \
131  (void)++unit_test_failures))
132 
133 #define OIIO_CHECK_EQUAL(x, y) \
134  (((x) == (y)) \
135  ? ((void)0) \
136  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
137  << __FILE__ << ":" << __LINE__ << ":\n" \
138  << "FAILED: " \
139  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
140  << " == " << #y << "\n" \
141  << "\tvalues were '" << (x) << "' and '" << (y) \
142  << "'\n"), \
143  (void)++unit_test_failures))
144 
145 #define OIIO_CHECK_EQUAL_THRESH(x, y, eps) \
146  ((std::abs((x) - (y)) <= eps) \
147  ? ((void)0) \
148  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
149  << __FILE__ << ":" << __LINE__ << ":\n" \
150  << "FAILED: " \
151  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
152  << " == " << #y << "\n" \
153  << "\tvalues were '" << (x) << "' and '" << (y) << "'" \
154  << ", diff was " << std::abs((x) - (y)) << "\n"), \
155  (void)++unit_test_failures))
156 
157 #define OIIO_CHECK_EQUAL_APPROX(x, y) \
158  (OIIO::pvt::equal_approx(x, y) \
159  ? ((void)0) \
160  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
161  << __FILE__ << ":" << __LINE__ << ":\n" \
162  << "FAILED: " \
163  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
164  << " == " << #y << "\n" \
165  << "\tvalues were '" << (x) << "' and '" << (y) << "'" \
166  << ", diff was " << ((x) - (y)) << "\n"), \
167  (void)++unit_test_failures))
168 
169 #define OIIO_CHECK_NE(x, y) \
170  (((x) != (y)) \
171  ? ((void)0) \
172  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
173  << __FILE__ << ":" << __LINE__ << ":\n" \
174  << "FAILED: " \
175  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
176  << " != " << #y << "\n" \
177  << "\tvalues were '" << (x) << "' and '" << (y) \
178  << "'\n"), \
179  (void)++unit_test_failures))
180 
181 #define OIIO_CHECK_LT(x, y) \
182  (((x) < (y)) \
183  ? ((void)0) \
184  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
185  << __FILE__ << ":" << __LINE__ << ":\n" \
186  << "FAILED: " \
187  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
188  << " < " << #y << "\n" \
189  << "\tvalues were '" << (x) << "' and '" << (y) \
190  << "'\n"), \
191  (void)++unit_test_failures))
192 
193 #define OIIO_CHECK_GT(x, y) \
194  (((x) > (y)) \
195  ? ((void)0) \
196  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
197  << __FILE__ << ":" << __LINE__ << ":\n" \
198  << "FAILED: " \
199  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
200  << " > " << #y << "\n" \
201  << "\tvalues were '" << (x) << "' and '" << (y) \
202  << "'\n"), \
203  (void)++unit_test_failures))
204 
205 #define OIIO_CHECK_LE(x, y) \
206  (((x) <= (y)) \
207  ? ((void)0) \
208  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
209  << __FILE__ << ":" << __LINE__ << ":\n" \
210  << "FAILED: " \
211  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
212  << " <= " << #y << "\n" \
213  << "\tvalues were '" << (x) << "' and '" << (y) \
214  << "'\n"), \
215  (void)++unit_test_failures))
216 
217 #define OIIO_CHECK_GE(x, y) \
218  (((x) >= (y)) \
219  ? ((void)0) \
220  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
221  << __FILE__ << ":" << __LINE__ << ":\n" \
222  << "FAILED: " \
223  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
224  << " >= " << #y << "\n" \
225  << "\tvalues were '" << (x) << "' and '" << (y) \
226  << "'\n"), \
227  (void)++unit_test_failures))
228 
229 
230 // Special SIMD related equality checks that use all()
231 #define OIIO_CHECK_SIMD_EQUAL(x, y) \
232  (all((x) == (y)) \
233  ? ((void)0) \
234  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
235  << __FILE__ << ":" << __LINE__ << ":\n" \
236  << "FAILED: " \
237  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
238  << " == " << #y << "\n" \
239  << "\tvalues were '" << (x) << "' and '" << (y) \
240  << "'\n"), \
241  (void)++unit_test_failures))
242 
243 #define OIIO_CHECK_SIMD_EQUAL_THRESH(x, y, eps) \
244  (all(abs((x) - (y)) < (eps)) \
245  ? ((void)0) \
246  : ((std::cout << OIIO::Sysutil::Term(std::cout).ansi("red,bold") \
247  << __FILE__ << ":" << __LINE__ << ":\n" \
248  << "FAILED: " \
249  << OIIO::Sysutil::Term(std::cout).ansi("normal") << #x \
250  << " == " << #y << "\n" \
251  << "\tvalues were '" << (x) << "' and '" << (y) \
252  << "'\n"), \
253  (void)++unit_test_failures))
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
const GLdouble * v
Definition: glew.h:1391
String-related utilities, all in namespace Strutil.
vint4 abs(const vint4 &a)
Definition: simd.h:4684
std::string ansi(string_view command) const
UnitTestFailureCounter operator+=(int i)
Definition: unittest.h:81
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER T abs(T a)
Definition: ImathFun.h:55
GLclampf f
Definition: glew.h:3499
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
Platform-independent utilities for various OS, hardware, and system resource functionality, all in namespace Sysutil.
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
float max(float x, float y)
Definition: unittest.h:52
arg_join< It, char > join(It begin, It end, string_view sep)
Definition: format.h:3326
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
bool all(const vbool4 &v)
Definition: simd.h:3371
const UnitTestFailureCounter & operator++()
Definition: unittest.h:75
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65
bool equal_approx(const X &x, const Y &y)
Definition: unittest.h:95