OpenShot Audio Library | OpenShotAudio  0.3.1
juce_UnitTest.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 class UnitTestRunner;
27 
28 
29 //==============================================================================
69 class JUCE_API UnitTest
70 {
71 public:
72  //==============================================================================
74  explicit UnitTest (const String& name, const String& category = String());
75 
77  virtual ~UnitTest();
78 
80  const String& getName() const noexcept { return name; }
81 
83  const String& getCategory() const noexcept { return category; }
84 
89  void performTest (UnitTestRunner* runner);
90 
92  static Array<UnitTest*>& getAllTests();
93 
95  static Array<UnitTest*> getTestsInCategory (const String& category);
96 
98  static StringArray getAllCategories();
99 
100  //==============================================================================
104  virtual void initialise();
105 
109  virtual void shutdown();
110 
116  virtual void runTest() = 0;
117 
118  //==============================================================================
123  void beginTest (const String& testName);
124 
125  //==============================================================================
144  void expect (bool testResult, const String& failureMessage = String());
145 
146  //==============================================================================
150  template <class ValueType>
151  void expectEquals (ValueType actual, ValueType expected, String failureMessage = String())
152  {
153  bool result = actual == expected;
154  expectResultAndPrint (actual, expected, result, "", failureMessage);
155  }
156 
160  template <class ValueType>
161  void expectNotEquals (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
162  {
163  bool result = value != valueToCompareTo;
164  expectResultAndPrint (value, valueToCompareTo, result, "unequal to", failureMessage);
165  }
166 
170  template <class ValueType>
171  void expectGreaterThan (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
172  {
173  bool result = value > valueToCompareTo;
174  expectResultAndPrint (value, valueToCompareTo, result, "greater than", failureMessage);
175  }
176 
180  template <class ValueType>
181  void expectLessThan (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
182  {
183  bool result = value < valueToCompareTo;
184  expectResultAndPrint (value, valueToCompareTo, result, "less than", failureMessage);
185  }
186 
190  template <class ValueType>
191  void expectGreaterOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
192  {
193  bool result = value >= valueToCompareTo;
194  expectResultAndPrint (value, valueToCompareTo, result, "greater or equal to", failureMessage);
195  }
196 
200  template <class ValueType>
201  void expectLessOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
202  {
203  bool result = value <= valueToCompareTo;
204  expectResultAndPrint (value, valueToCompareTo, result, "less or equal to", failureMessage);
205  }
206 
211  template <class ValueType>
212  void expectWithinAbsoluteError (ValueType actual, ValueType expected, ValueType maxAbsoluteError, String failureMessage = String())
213  {
214  const ValueType diff = std::abs (actual - expected);
215  const bool result = diff <= maxAbsoluteError;
216 
217  expectResultAndPrint (actual, expected, result, " within " + String (maxAbsoluteError) + " of" , failureMessage);
218  }
219 
220  //==============================================================================
222  #define expectDoesNotThrow(expr) \
223  try \
224  { \
225  (expr); \
226  expect (true); \
227  } \
228  catch (...) \
229  { \
230  expect (false, "Expected: does not throw an exception, Actual: throws."); \
231  }
232 
234  #define expectThrows(expr) \
235  try \
236  { \
237  (expr); \
238  expect (false, "Expected: throws an exception, Actual: does not throw."); \
239  } \
240  catch (...) \
241  { \
242  expect (true); \
243  }
244 
246  #define expectThrowsType(expr, type) \
247  try \
248  { \
249  (expr); \
250  expect (false, "Expected: throws an exception of type " #type ", Actual: does not throw."); \
251  } \
252  catch (type&) \
253  { \
254  expect (true); \
255  } \
256  catch (...) \
257  { \
258  expect (false, "Expected: throws an exception of type " #type ", Actual: throws another type."); \
259  }
260 
261  //==============================================================================
265  void logMessage (const String& message);
266 
281  Random getRandom() const;
282 
283 private:
284  //==============================================================================
285  template <class ValueType>
286  void expectResultAndPrint (ValueType value, ValueType valueToCompareTo, bool result,
287  String compDescription, String failureMessage)
288  {
289  if (! result)
290  {
291  if (failureMessage.isNotEmpty())
292  failureMessage << " -- ";
293 
294  failureMessage << "Expected value" << (compDescription.isEmpty() ? "" : " ")
295  << compDescription << ": " << valueToCompareTo
296  << ", Actual value: " << value;
297  }
298 
299  expect (result, failureMessage);
300  }
301 
302  //==============================================================================
303  const String name, category;
304  UnitTestRunner* runner = nullptr;
305 
306  JUCE_DECLARE_NON_COPYABLE (UnitTest)
307 };
308 
309 
310 //==============================================================================
324 class JUCE_API UnitTestRunner
325 {
326 public:
327  //==============================================================================
329  UnitTestRunner();
330 
332  virtual ~UnitTestRunner();
333 
342  void runTests (const Array<UnitTest*>& tests, int64 randomSeed = 0);
343 
350  void runAllTests (int64 randomSeed = 0);
351 
358  void runTestsInCategory (const String& category, int64 randomSeed = 0);
359 
363  void setAssertOnFailure (bool shouldAssert) noexcept;
364 
368  void setPassesAreLogged (bool shouldDisplayPasses) noexcept;
369 
370  //==============================================================================
377  struct TestResult
378  {
383 
385  int passes;
387  int failures;
388 
391  };
392 
396  int getNumResults() const noexcept;
397 
401  const TestResult* getResult (int index) const noexcept;
402 
403 protected:
407  virtual void resultsUpdated();
408 
413  virtual void logMessage (const String& message);
414 
418  virtual bool shouldAbortTests();
419 
420 private:
421  //==============================================================================
422  friend class UnitTest;
423 
424  UnitTest* currentTest = nullptr;
425  String currentSubCategory;
427  bool assertOnFailure = true, logPasses = false;
428  Random randomForTest;
429 
430  void beginNewTest (UnitTest* test, const String& subCategory);
431  void endTest();
432 
433  void addPass();
434  void addFail (const String& failureMessage);
435 
436  JUCE_DECLARE_NON_COPYABLE (UnitTestRunner)
437 };
438 
439 } // namespace juce
bool isEmpty() const noexcept
Definition: juce_String.h:296
bool isNotEmpty() const noexcept
Definition: juce_String.h:302
void expectEquals(ValueType actual, ValueType expected, String failureMessage=String())
void expectGreaterThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
const String & getName() const noexcept
Definition: juce_UnitTest.h:80
void expectWithinAbsoluteError(ValueType actual, ValueType expected, ValueType maxAbsoluteError, String failureMessage=String())
const String & getCategory() const noexcept
Definition: juce_UnitTest.h:83
virtual void runTest()=0
void expectLessThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
void expectLessOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
void expectNotEquals(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
void expectGreaterOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())