| //===- CommonTestUtils.h --------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #ifndef ORC_RT_UNITTEST_COMMONTESTUTILS_H | |
| #define ORC_RT_UNITTEST_COMMONTESTUTILS_H | |
| // Helpers here must not depend on Bedrock: this header is included by | |
| // SupportTests translation units, which link Support alone. Bedrock-dependent | |
| // helpers belong in BedrockTestUtils.h. (Session is forward-declared below so | |
| // that the error-reporter helpers can also serve as Session error reporters; | |
| // they never use the Session, so this adds no link dependency.) | |
| #include "orc-rt/support/Error.h" | |
| #include "orc-rt/support/WrapperFunction.h" | |
| #include "orc-rt/support/move_only_function.h" | |
| #include "orc-rt-c/support/CoreTypes.h" | |
| #include "orc-rt-c/support/WrapperFunction.h" | |
| #include <cassert> | |
| #include <cstddef> | |
| #include <cstdint> | |
| #include <future> | |
| #include <string> | |
| #include <vector> | |
| #include "ErrorMatchers.h" | |
| #include "gtest/gtest.h" | |
| namespace orc_rt { | |
| class Session; | |
| } // namespace orc_rt | |
| namespace orc_rt::test { | |
| /// Error reporter for tests that expect no errors: any reported error fails | |
| /// the current test. Usable both as a plain error reporter and as a Session | |
| /// error reporter. | |
| /// | |
| /// This is a function object rather than a pair of overloaded functions so | |
| /// that it can be passed to templated callable parameters (e.g. | |
| /// move_only_function's constructor): the name of an overload set can't be | |
| /// deduced. | |
| inline constexpr struct NoErrors { | |
| void operator()(Error Err) const noexcept { | |
| EXPECT_THAT_ERROR(std::move(Err), Succeeded()); | |
| } | |
| void operator()(Session &, Error Err) const noexcept { | |
| EXPECT_THAT_ERROR(std::move(Err), Succeeded()); | |
| } | |
| } noErrors; | |
| /// ReportError callback for tests that records the message of every reported | |
| /// error, in the order reported. Like noErrors, usable both as a plain error | |
| /// reporter and as a Session error reporter. | |
| class AccumulateErrors { | |
| public: | |
| AccumulateErrors(std::vector<std::string> &ErrMsgs) : ErrMsgs(ErrMsgs) {} | |
| void operator()(Error Err) noexcept { | |
| ErrMsgs.push_back(toString(std::move(Err))); | |
| } | |
| void operator()(Session &, Error Err) noexcept { (*this)(std::move(Err)); } | |
| private: | |
| std::vector<std::string> &ErrMsgs; | |
| }; | |
| template <size_t Idx = 0> class OpCounter { | |
| public: | |
| OpCounter() { ++DefaultConstructions; } | |
| OpCounter(const OpCounter &Other) { ++CopyConstructions; } | |
| OpCounter &operator=(const OpCounter &Other) { | |
| ++CopyAssignments; | |
| return *this; | |
| } | |
| OpCounter(OpCounter &&Other) { ++MoveConstructions; } | |
| OpCounter &operator=(OpCounter &&Other) { | |
| ++MoveAssignments; | |
| return *this; | |
| } | |
| ~OpCounter() { ++Destructions; } | |
| static size_t defaultConstructions() { return DefaultConstructions; } | |
| static size_t copyConstructions() { return CopyConstructions; } | |
| static size_t copyAssignments() { return CopyAssignments; } | |
| static size_t copies() { return copyConstructions() + copyAssignments(); } | |
| static size_t moveConstructions() { return MoveConstructions; } | |
| static size_t moveAssignments() { return MoveAssignments; } | |
| static size_t moves() { return moveConstructions() + moveAssignments(); } | |
| static size_t destructions() { return Destructions; } | |
| static bool destructionsMatch() { | |
| return destructions() == defaultConstructions() + copies() + moves(); | |
| } | |
| static void reset() { | |
| DefaultConstructions = 0; | |
| CopyConstructions = 0; | |
| CopyAssignments = 0; | |
| MoveConstructions = 0; | |
| MoveAssignments = 0; | |
| Destructions = 0; | |
| } | |
| private: | |
| static size_t DefaultConstructions; | |
| static size_t CopyConstructions; | |
| static size_t CopyAssignments; | |
| static size_t MoveConstructions; | |
| static size_t MoveAssignments; | |
| static size_t Destructions; | |
| }; | |
| template <size_t Idx> size_t OpCounter<Idx>::DefaultConstructions = 0; | |
| template <size_t Idx> size_t OpCounter<Idx>::CopyConstructions = 0; | |
| template <size_t Idx> size_t OpCounter<Idx>::CopyAssignments = 0; | |
| template <size_t Idx> size_t OpCounter<Idx>::MoveConstructions = 0; | |
| template <size_t Idx> size_t OpCounter<Idx>::MoveAssignments = 0; | |
| template <size_t Idx> size_t OpCounter<Idx>::Destructions = 0; | |
| template <typename T> | |
| move_only_function<void(T) noexcept> waitFor(std::future<T> &F) { | |
| std::promise<T> P; | |
| F = P.get_future(); | |
| return [P = std::move(P)](T Val) mutable noexcept { | |
| P.set_value(std::move(Val)); | |
| }; | |
| } | |
| inline move_only_function<void() noexcept> waitFor(std::future<void> &F) { | |
| std::promise<void> P; | |
| F = P.get_future(); | |
| return [P = std::move(P)]() mutable noexcept { P.set_value(); }; | |
| } | |
| } // namespace orc_rt::test | |
| #endif // ORC_RT_UNITTEST_COMMONTESTUTILS_H |