summaryrefslogtreecommitdiffstats
path: root/src/SelfTests.cpp
diff options
context:
space:
mode:
authorAlexander Harkness <me@bearbin.net>2015-06-12 12:00:47 +0200
committerAlexander Harkness <me@bearbin.net>2015-06-12 12:00:47 +0200
commit001305247fa28e78d0f3d9d565ff7aff25864edc (patch)
tree9f1e41fa0e6afee7ce9f90f3778f02e7a770ebf7 /src/SelfTests.cpp
parentUpdate gratipay (diff)
parentSelfTests are registered and executed after logging framework init. (diff)
downloadcuberite-001305247fa28e78d0f3d9d565ff7aff25864edc.tar
cuberite-001305247fa28e78d0f3d9d565ff7aff25864edc.tar.gz
cuberite-001305247fa28e78d0f3d9d565ff7aff25864edc.tar.bz2
cuberite-001305247fa28e78d0f3d9d565ff7aff25864edc.tar.lz
cuberite-001305247fa28e78d0f3d9d565ff7aff25864edc.tar.xz
cuberite-001305247fa28e78d0f3d9d565ff7aff25864edc.tar.zst
cuberite-001305247fa28e78d0f3d9d565ff7aff25864edc.zip
Diffstat (limited to '')
-rw-r--r--src/SelfTests.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/SelfTests.cpp b/src/SelfTests.cpp
new file mode 100644
index 000000000..f03035a76
--- /dev/null
+++ b/src/SelfTests.cpp
@@ -0,0 +1,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"
+
+
+
+
+
+#if 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
+
+
+
+