[Openmp-commits] [openmp] e146855 - [OpenMP][omptest] Enforce test case order for 'standalone' build (#154977)
via Openmp-commits
openmp-commits at lists.llvm.org
Fri Aug 22 11:23:12 PDT 2025
Author: Michael Halkenhäuser
Date: 2025-08-22T20:23:09+02:00
New Revision: e1468558af1e0ce66ed89d98de5c0dbc3ec94028
URL: https://github.com/llvm/llvm-project/commit/e1468558af1e0ce66ed89d98de5c0dbc3ec94028
DIFF: https://github.com/llvm/llvm-project/commit/e1468558af1e0ce66ed89d98de5c0dbc3ec94028.diff
LOG: [OpenMP][omptest] Enforce test case order for 'standalone' build (#154977)
Note: this only applies to 'standalone' builds, i.e. when:
LIBOMPTEST_BUILD_STANDALONE evaluates to 'ON'.
Use std::vector<std::pair<std::string, TestSuite>> instead of a
std::map.
Background:
In some cases it could happen that the test execution order would change
vs. the order of appearance.
This can lead to suite failures when e.g. testing for device
initialization because it is performed by the first executed test case.
By storing the test suites and cases in order of appearance this issue
is avoided. (So far GoogleTest has behaved in the same way.)
Added:
Modified:
openmp/tools/omptest/include/OmptTesterStandalone.h
openmp/tools/omptest/src/OmptTester.cpp
openmp/tools/omptest/src/OmptTesterStandalone.cpp
Removed:
################################################################################
diff --git a/openmp/tools/omptest/include/OmptTesterStandalone.h b/openmp/tools/omptest/include/OmptTesterStandalone.h
index 6a0995538e2ca..aca72f7db375f 100644
--- a/openmp/tools/omptest/include/OmptTesterStandalone.h
+++ b/openmp/tools/omptest/include/OmptTesterStandalone.h
@@ -19,7 +19,7 @@
#include "OmptAsserter.h"
#include "OmptTesterGlobals.h"
-#include <map>
+#include <utility>
#include <vector>
// Forward declarations.
@@ -60,14 +60,15 @@ struct TestCase {
/// A pretty crude test suite abstraction
struct TestSuite {
using TestCaseVec = std::vector<std::unique_ptr<TestCase>>;
- std::string Name;
TestSuite() = default;
+ TestSuite(const std::string &TSName) : Name(TSName) {}
TestSuite(const TestSuite &O) = delete;
TestSuite(TestSuite &&O);
void setup();
void teardown();
TestCaseVec::iterator begin();
TestCaseVec::iterator end();
+ std::string Name;
TestCaseVec TestCases;
};
/// Static class used to register all test cases and provide them to the driver
@@ -75,14 +76,16 @@ class TestRegistrar {
public:
static TestRegistrar &get();
static std::vector<TestSuite> getTestSuites();
- static void addCaseToSuite(TestCase *TC, std::string TSName);
+ static void addCaseToSuite(TestCase *TC, const std::string &TSName);
private:
TestRegistrar() = default;
TestRegistrar(const TestRegistrar &o) = delete;
TestRegistrar operator=(const TestRegistrar &o) = delete;
- // Keep tests in order 'of appearance' (top -> bottom), avoid unordered_map
- static std::map<std::string, TestSuite> Tests;
+ // Keep tests in order 'of appearance', i.e. top -> bottom.
+ // This effectively mimicks the (observed) behavior of GoogleTest.
+ // Avoid maps as they do not have corresponding order guarantees.
+ static std::vector<std::pair<std::string, TestSuite>> Tests;
};
/// Hack to register test cases
struct Registerer {
diff --git a/openmp/tools/omptest/src/OmptTester.cpp b/openmp/tools/omptest/src/OmptTester.cpp
index 32eec51f44fbf..afa96ac1d4b23 100644
--- a/openmp/tools/omptest/src/OmptTester.cpp
+++ b/openmp/tools/omptest/src/OmptTester.cpp
@@ -42,7 +42,7 @@ static OmptEventReporter *EventReporter;
#define OMPT_BUFFER_REQUEST_SIZE 256
#ifdef OPENMP_LIBOMPTEST_BUILD_STANDALONE
-std::map<std::string, TestSuite> TestRegistrar::Tests;
+std::vector<std::pair<std::string, TestSuite>> TestRegistrar::Tests;
#endif
static std::atomic<ompt_id_t> NextOpId{0x8000000000000001};
diff --git a/openmp/tools/omptest/src/OmptTesterStandalone.cpp b/openmp/tools/omptest/src/OmptTesterStandalone.cpp
index 660e385d28499..6f13939c92941 100644
--- a/openmp/tools/omptest/src/OmptTesterStandalone.cpp
+++ b/openmp/tools/omptest/src/OmptTesterStandalone.cpp
@@ -15,6 +15,7 @@
#include "OmptTesterStandalone.h"
#include "OmptCallbackHandler.h"
+#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
@@ -86,11 +87,21 @@ std::vector<TestSuite> TestRegistrar::getTestSuites() {
return TSs;
}
-void TestRegistrar::addCaseToSuite(TestCase *TC, std::string TSName) {
- auto &TS = Tests[TSName];
- if (TS.Name.empty())
- TS.Name = TSName;
- TS.TestCases.emplace_back(TC);
+void TestRegistrar::addCaseToSuite(TestCase *TC, const std::string &TSName) {
+ // Search the test suites for a matching name
+ auto It = std::find_if(Tests.begin(), Tests.end(),
+ [&](const auto &P) { return P.first == TSName; });
+
+ if (It != Tests.end()) {
+ // Test suite exists: add the test case
+ It->second.TestCases.emplace_back(TC);
+ } else {
+ // Test suite does not exist: construct it and add the test case
+ TestSuite TS(TSName);
+ TS.TestCases.emplace_back(TC);
+ // Move and emplace the suite
+ Tests.emplace_back(TSName, std::move(TS));
+ }
}
Registerer::Registerer(TestCase *TC, const std::string SuiteName) {
More information about the Openmp-commits
mailing list