From cca07d97b5291ed899c46333317097bbafa9e162 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sun, 13 Mar 2022 17:41:50 -0700 Subject: [PATCH] [test_runner] Fix static ctor ordering bug The test_runner was potentially initializing the array of tests after tests had been added. Now, allocate the vector dynamically on the first test addition. --- src/user/test_runner/test_case.cpp | 11 ++++++++--- src/user/test_runner/test_case.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/user/test_runner/test_case.cpp b/src/user/test_runner/test_case.cpp index 87fffbe..0326668 100644 --- a/src/user/test_runner/test_case.cpp +++ b/src/user/test_runner/test_case.cpp @@ -2,7 +2,7 @@ namespace test { -util::vector registry::m_tests; +util::vector *registry::m_tests = nullptr; void fixture::_log_failure(const char *test_name, const char *message, @@ -15,14 +15,19 @@ fixture::_log_failure(const char *test_name, const char *message, void registry::register_test_case(fixture &test) { - m_tests.append(&test); + if (!m_tests) + m_tests = new util::vector; + m_tests->append(&test); } size_t registry::run_all_tests() { + if (!m_tests) + return 0; + size_t failures = 0; - for (auto *test : m_tests) { + for (auto *test : *m_tests) { test->test_execute(); failures += test->_test_failure_count; } diff --git a/src/user/test_runner/test_case.h b/src/user/test_runner/test_case.h index d57ed34..260de95 100644 --- a/src/user/test_runner/test_case.h +++ b/src/user/test_runner/test_case.h @@ -34,7 +34,7 @@ public: static size_t run_all_tests(); private: - static util::vector m_tests; + static util::vector *m_tests; }; template