OpenShot Audio Library | OpenShotAudio: juce_UnitTest.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0 |

Loading...
Searching...
No Matches
juce_UnitTest.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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
23namespace juce
24{
25
26class UnitTestRunner;
27
28
29//==============================================================================
69 class JUCE_API UnitTest
70{
71public:
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 = exactlyEqual (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 = ! exactlyEqual (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
283private:
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{
326public:
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 {
379 TestResult() = default;
380
381 explicit TestResult (const String& name, const String& subCategory)
382 : unitTestName (name),
383 subcategoryName (subCategory)
384 {
385 }
386
388 String unitTestName;
390 String subcategoryName;
391
393 int passes = 0;
395 int failures = 0;
396
398 StringArray messages;
399
401 Time startTime = Time::getCurrentTime();
403 Time endTime;
404 };
405
409 int getNumResults() const noexcept;
410
414 const TestResult* getResult (int index) const noexcept;
415
416protected:
420 virtual void resultsUpdated();
421
426 virtual void logMessage (const String& message);
427
431 virtual bool shouldAbortTests();
432
433private:
434 //==============================================================================
435 friend class UnitTest;
436
437 UnitTest* currentTest = nullptr;
438 String currentSubCategory;
439 OwnedArray<TestResult, CriticalSection> results;
440 bool assertOnFailure = true, logPasses = false;
441 Random randomForTest;
442
443 void beginNewTest (UnitTest* test, const String& subCategory);
444 void endTest();
445
446 void addPass();
447 void addFail (const String& failureMessage);
448
449 JUCE_DECLARE_NON_COPYABLE (UnitTestRunner)
450};
451
452} // namespace juce
juce::Array
Definition juce_Array.h:56
juce::CriticalSection
Definition juce_CriticalSection.h:43
juce::OwnedArray
Definition juce_OwnedArray.h:50
juce::Random
Definition juce_Random.h:35
juce::StringArray
Definition juce_StringArray.h:35
juce::String
Definition juce_String.h:53
juce::String::isEmpty
bool isEmpty() const noexcept
Definition juce_String.h:310
juce::String::isNotEmpty
bool isNotEmpty() const noexcept
Definition juce_String.h:316
juce::Time
Definition juce_Time.h:37
juce::Time::getCurrentTime
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition juce_Time.cpp:233
juce::UnitTestRunner
Definition juce_UnitTest.h:325
juce::UnitTestRunner::runAllTests
void runAllTests(int64 randomSeed=0)
Definition juce_UnitTest.cpp:177
juce::UnitTestRunner::runTestsInCategory
void runTestsInCategory(const String &category, int64 randomSeed=0)
Definition juce_UnitTest.cpp:182
juce::UnitTestRunner::shouldAbortTests
virtual bool shouldAbortTests()
Definition juce_UnitTest.cpp:192
juce::UnitTestRunner::getResult
const TestResult * getResult(int index) const noexcept
Definition juce_UnitTest.cpp:135
juce::UnitTestRunner::getNumResults
int getNumResults() const noexcept
Definition juce_UnitTest.cpp:130
juce::UnitTestRunner::setAssertOnFailure
void setAssertOnFailure(bool shouldAssert) noexcept
Definition juce_UnitTest.cpp:120
juce::UnitTestRunner::runTests
void runTests(const Array< UnitTest * > &tests, int64 randomSeed=0)
Definition juce_UnitTest.cpp:144
juce::UnitTestRunner::setPassesAreLogged
void setPassesAreLogged(bool shouldDisplayPasses) noexcept
Definition juce_UnitTest.cpp:125
juce::UnitTestRunner::logMessage
virtual void logMessage(const String &message)
Definition juce_UnitTest.cpp:187
juce::UnitTestRunner::resultsUpdated
virtual void resultsUpdated()
Definition juce_UnitTest.cpp:140
juce::UnitTest::getName
const String & getName() const noexcept
Definition juce_UnitTest.h:80
juce::UnitTest::getCategory
const String & getCategory() const noexcept
Definition juce_UnitTest.h:83
juce::UnitTest::expectEquals
void expectEquals(ValueType actual, ValueType expected, String failureMessage=String())
Definition juce_UnitTest.h:151
juce::UnitTest::expectGreaterThan
void expectGreaterThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:171
juce::UnitTest::UnitTest
UnitTest(const String &name, const String &category=String())
Definition juce_UnitTest.cpp:26
juce::UnitTest::beginTest
void beginTest(const String &testName)
Definition juce_UnitTest.cpp:89
juce::UnitTest::expect
void expect(bool testResult, const String &failureMessage=String())
Definition juce_UnitTest.cpp:97
juce::UnitTest::expectWithinAbsoluteError
void expectWithinAbsoluteError(ValueType actual, ValueType expected, ValueType maxAbsoluteError, String failureMessage=String())
Definition juce_UnitTest.h:212
juce::UnitTest::runTest
virtual void runTest()=0
juce::UnitTest::expectLessThan
void expectLessThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:181
juce::UnitTest::expectLessOrEqual
void expectLessOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:201
juce::UnitTest::expectNotEquals
void expectNotEquals(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:161
juce::UnitTest::expectGreaterOrEqual
void expectGreaterOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:191
juce::UnitTestRunner::TestResult
Definition juce_UnitTest.h:378
juce::UnitTestRunner::TestResult::failures
int failures
Definition juce_UnitTest.h:395
juce::UnitTestRunner::TestResult::endTime
Time endTime
Definition juce_UnitTest.h:403
juce::UnitTestRunner::TestResult::messages
StringArray messages
Definition juce_UnitTest.h:398
juce::UnitTestRunner::TestResult::startTime
Time startTime
Definition juce_UnitTest.h:401
juce::UnitTestRunner::TestResult::unitTestName
String unitTestName
Definition juce_UnitTest.h:388
juce::UnitTestRunner::TestResult::subcategoryName
String subcategoryName
Definition juce_UnitTest.h:390
juce::UnitTestRunner::TestResult::passes
int passes
Definition juce_UnitTest.h:393