LMMS
Loading...
Searching...
No Matches
juce_UnitTest.h
Go to the documentation of this file.
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//==============================================================================
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
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
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 //==============================================================================
305
307};
308
309
310//==============================================================================
325{
326public:
327 //==============================================================================
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 //==============================================================================
378 {
379 TestResult() = default;
380
381 explicit TestResult (const String& name, const String& subCategory)
382 : unitTestName (name),
383 subcategoryName (subCategory)
384 {
385 }
386
391
393 int passes = 0;
395 int failures = 0;
396
399
404 };
405
410
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
442
443 void beginNewTest (UnitTest* test, const String& subCategory);
444 void endTest();
445
446 void addPass();
447 void addFail (const String& failureMessage);
448
450};
451
452} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
static void shutdown(void)
Definition adplugdb.cpp:297
static void message(int level, const char *fmt,...)
Definition adplugdb.cpp:120
Definition String.h:48
Definition juce_Array.h:56
Definition juce_CriticalSection.h:43
Definition juce_OwnedArray.h:51
Definition juce_Random.h:35
Definition juce_StringArray.h:35
Definition juce_String.h:53
bool isEmpty() const noexcept
Definition juce_String.h:310
bool isNotEmpty() const noexcept
Definition juce_String.h:316
Definition juce_Time.h:37
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition juce_Time.cpp:233
const String & getName() const noexcept
Definition juce_UnitTest.h:80
void expectResultAndPrint(ValueType value, ValueType valueToCompareTo, bool result, String compDescription, String failureMessage)
Definition juce_UnitTest.h:286
const String category
Definition juce_UnitTest.h:303
const String & getCategory() const noexcept
Definition juce_UnitTest.h:83
void expectEquals(ValueType actual, ValueType expected, String failureMessage=String())
Definition juce_UnitTest.h:151
void expectGreaterThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:171
UnitTest(const String &name, const String &category=String())
Definition juce_UnitTest.cpp:26
void beginTest(const String &testName)
Definition juce_UnitTest.cpp:89
void expect(bool testResult, const String &failureMessage=String())
Definition juce_UnitTest.cpp:97
UnitTestRunner * runner
Definition juce_UnitTest.h:304
void expectWithinAbsoluteError(ValueType actual, ValueType expected, ValueType maxAbsoluteError, String failureMessage=String())
Definition juce_UnitTest.h:212
virtual void runTest()=0
void expectLessThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:181
const String name
Definition juce_UnitTest.h:303
void expectLessOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:201
void expectNotEquals(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:161
void expectGreaterOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:191
Definition juce_UnitTest.h:325
UnitTest * currentTest
Definition juce_UnitTest.h:437
void runAllTests(int64 randomSeed=0)
Definition juce_UnitTest.cpp:177
void runTestsInCategory(const String &category, int64 randomSeed=0)
Definition juce_UnitTest.cpp:182
virtual bool shouldAbortTests()
Definition juce_UnitTest.cpp:192
const TestResult * getResult(int index) const noexcept
Definition juce_UnitTest.cpp:135
void beginNewTest(UnitTest *test, const String &subCategory)
Definition juce_UnitTest.cpp:197
int getNumResults() const noexcept
Definition juce_UnitTest.cpp:130
void setAssertOnFailure(bool shouldAssert) noexcept
Definition juce_UnitTest.cpp:120
friend class UnitTest
Definition juce_UnitTest.h:435
void runTests(const Array< UnitTest * > &tests, int64 randomSeed=0)
Definition juce_UnitTest.cpp:144
bool assertOnFailure
Definition juce_UnitTest.h:440
void endTest()
Definition juce_UnitTest.cpp:211
bool logPasses
Definition juce_UnitTest.h:440
void setPassesAreLogged(bool shouldDisplayPasses) noexcept
Definition juce_UnitTest.cpp:125
virtual void logMessage(const String &message)
Definition juce_UnitTest.cpp:187
void addPass()
Definition juce_UnitTest.cpp:234
String currentSubCategory
Definition juce_UnitTest.h:438
void addFail(const String &failureMessage)
Definition juce_UnitTest.cpp:255
OwnedArray< TestResult, CriticalSection > results
Definition juce_UnitTest.h:439
Random randomForTest
Definition juce_UnitTest.h:441
UnitTestRunner()
Definition juce_UnitTest.cpp:117
virtual void resultsUpdated()
Definition juce_UnitTest.cpp:140
static PuglViewHint int value
Definition pugl.h:1708
static const char * name
Definition pugl.h:1582
#define JUCE_DECLARE_NON_COPYABLE(className)
#define JUCE_API
Definition juce_StandardHeader.h:152
static struct TestCase tests[]
Definition lilv_test.c:2218
Definition carla_juce.cpp:31
long long int64
Definition juce_MathsFunctions.h:54
#define true
Definition ordinals.h:82
#define false
Definition ordinals.h:83
static bool diff(const std::string fn1, const std::string fn2)
Definition playertest.cpp:161
static int test(SerdEnv *env, bool top_level, bool pretty_numbers)
Definition sratom_test.c:79
Definition juce_UnitTest.h:378
int failures
Definition juce_UnitTest.h:395
Time endTime
Definition juce_UnitTest.h:403
StringArray messages
Definition juce_UnitTest.h:398
Time startTime
Definition juce_UnitTest.h:401
String unitTestName
Definition juce_UnitTest.h:388
String subcategoryName
Definition juce_UnitTest.h:390
TestResult(const String &name, const String &subCategory)
Definition juce_UnitTest.h:381
int passes
Definition juce_UnitTest.h:393
int result
Definition process.c:1455
#define const
Definition zconf.h:137