| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
/// |
| 9 |
/// \file |
| 10 |
/// This file contains the definition of the TestResult class, which aggregates |
| 11 |
/// and stores the results of a math test run. |
| 12 |
/// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
#ifndef MATHTEST_TESTRESULT_HPP |
| 16 |
#define MATHTEST_TESTRESULT_HPP |
| 17 |
|
| 18 |
#include <cstdint> |
| 19 |
#include <optional> |
| 20 |
#include <tuple> |
| 21 |
#include <utility> |
| 22 |
|
| 23 |
namespace mathtest { |
| 24 |
|
| 25 |
template <typename OutType, typename... InTypes> |
| 26 |
class [[nodiscard]] TestResult { |
| 27 |
public: |
| 28 |
struct [[nodiscard]] TestCase { |
| 29 |
std::tuple<InTypes...> Inputs; |
| 30 |
OutType Actual; |
| 31 |
OutType Expected; |
| 32 |
|
| 33 |
explicit constexpr TestCase(std::tuple<InTypes...> &&Inputs, OutType Actual, |
| 34 |
OutType Expected) noexcept |
| 35 |
: Inputs(std::move(Inputs)), Actual(std::move(Actual)), |
| 36 |
Expected(std::move(Expected)) {} |
| 37 |
}; |
| 38 |
|
| 39 |
TestResult() = default; |
| 40 |
|
| 41 |
explicit TestResult(uint64_t UlpDistance, bool IsFailure, |
| 42 |
TestCase &&Case) noexcept |
| 43 |
: MaxUlpDistance(UlpDistance), FailureCount(IsFailure ? 1 : 0), |
| 44 |
TestCaseCount(1) { |
| 45 |
if (IsFailure) |
| 46 |
WorstFailingCase.emplace(std::move(Case)); |
| 47 |
} |
| 48 |
|
| 49 |
void accumulate(const TestResult &Other) noexcept { |
| 50 |
if (Other.MaxUlpDistance > MaxUlpDistance) { |
| 51 |
MaxUlpDistance = Other.MaxUlpDistance; |
| 52 |
WorstFailingCase = Other.WorstFailingCase; |
| 53 |
} |
| 54 |
|
| 55 |
FailureCount += Other.FailureCount; |
| 56 |
TestCaseCount += Other.TestCaseCount; |
| 57 |
} |
| 58 |
|
| 59 |
[[nodiscard]] bool hasPassed() const noexcept { return FailureCount == 0; } |
| 60 |
|
| 61 |
[[nodiscard]] uint64_t getMaxUlpDistance() const noexcept { |
| 62 |
return MaxUlpDistance; |
| 63 |
} |
| 64 |
|
| 65 |
[[nodiscard]] uint64_t getFailureCount() const noexcept { |
| 66 |
return FailureCount; |
| 67 |
} |
| 68 |
|
| 69 |
[[nodiscard]] uint64_t getTestCaseCount() const noexcept { |
| 70 |
return TestCaseCount; |
| 71 |
} |
| 72 |
|
| 73 |
[[nodiscard]] const std::optional<TestCase> & |
| 74 |
getWorstFailingCase() const noexcept { |
| 75 |
return WorstFailingCase; |
| 76 |
} |
| 77 |
|
| 78 |
private: |
| 79 |
uint64_t MaxUlpDistance = 0; |
| 80 |
uint64_t FailureCount = 0; |
| 81 |
uint64_t TestCaseCount = 0; |
| 82 |
std::optional<TestCase> WorstFailingCase; |
| 83 |
}; |
| 84 |
} // namespace mathtest |
| 85 |
|
| 86 |
#endif // MATHTEST_TESTRESULT_HPP |