summaryrefslogtreecommitdiffstats
path: root/src/SelfTests.cpp
blob: 7e35e675e43317ca562e4eac3870d7eea8fa9200 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

// SelfTests.h

// Implements the cSelfTests class representing the singleton used for registering self-tests
// This class is only declared if SELF_TEST macro is defined.

#include "Globals.h"
#include "SelfTests.h"





#ifdef SELF_TEST
	cSelfTests::cSelfTests(void):
		m_AllowRegistering(true)
	{
	}





	cSelfTests & cSelfTests::Get(void)
	{
		static cSelfTests singleton;
		return singleton;
	}





	void cSelfTests::Register(cSelfTests::SelfTestFunction a_FnToExecute, const AString & a_TestName)
	{
		ASSERT(Get().m_AllowRegistering);
		Get().m_SelfTests.push_back(std::make_pair(a_FnToExecute, a_TestName));
	}





	void cSelfTests::ExecuteAll(void)
	{
		Get().m_AllowRegistering = false;
		LOG("--- Performing self-tests ---");
		for (auto & test: Get().m_SelfTests)
		{
			LOG("Performing self-test: %s", test.second.c_str());
			try
			{
				test.first();
			}
			catch (const std::exception & exc)
			{
				LOGWARNING("Exception in test %s: %s", test.second.c_str(), exc.what());
			}
			catch (...)
			{
				LOGWARNING("Unknown exception in test %s", test.second.c_str());
			}
		}  // for test - m_SelfTests[]
		LOG("--- Self-tests finished ---");
	}

#endif  // SELF_TEST