[RDF] Add RegisterRef::idx and make toUnitId constexpr
[llvm-project.git] / libc / test / UnitTest / ErrnoSetterMatcher.h
blob1d786c7e462b159433caa53860f0533aa42c0f57
1 //===-- ErrnoSetterMatcher.h ------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H
10 #define LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "src/__support/FPUtil/fpbits_str.h"
14 #include "src/__support/StringUtil/error_to_string.h"
15 #include "src/__support/macros/properties/architectures.h"
16 #include "src/errno/libc_errno.h"
17 #include "test/UnitTest/Test.h"
19 namespace __llvm_libc {
20 namespace testing {
22 namespace internal {
24 template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
25 T ExpectedReturn;
26 T ActualReturn;
27 int ExpectedErrno;
28 int ActualErrno;
30 // Even though this is a errno matcher primarily, it has to cater to platforms
31 // which do not have an errno. This predicate checks if errno matching is to
32 // be skipped.
33 static constexpr bool ignore_errno() {
34 #ifdef LIBC_TARGET_ARCH_IS_GPU
35 return true;
36 #else
37 return false;
38 #endif
41 public:
42 ErrnoSetterMatcher(T ExpectedReturn, int ExpectedErrno)
43 : ExpectedReturn(ExpectedReturn), ExpectedErrno(ExpectedErrno) {}
45 void explainError() override {
46 if (ActualReturn != ExpectedReturn) {
47 if constexpr (cpp::is_floating_point_v<T>) {
48 tlog << "Expected return value to be: "
49 << str(fputil::FPBits<T>(ExpectedReturn)) << '\n';
50 tlog << " But got: "
51 << str(fputil::FPBits<T>(ActualReturn)) << '\n';
52 } else {
53 tlog << "Expected return value to be " << ExpectedReturn << " but got "
54 << ActualReturn << ".\n";
58 if constexpr (!ignore_errno()) {
59 if (ActualErrno != ExpectedErrno) {
60 tlog << "Expected errno to be \"" << get_error_string(ExpectedErrno)
61 << "\" but got \"" << get_error_string(ActualErrno) << "\".\n";
66 bool match(T Got) {
67 ActualReturn = Got;
68 ActualErrno = libc_errno;
69 libc_errno = 0;
70 if constexpr (ignore_errno())
71 return Got == ExpectedReturn;
72 else
73 return Got == ExpectedReturn && ActualErrno == ExpectedErrno;
77 } // namespace internal
79 namespace ErrnoSetterMatcher {
81 template <typename RetT = int>
82 static internal::ErrnoSetterMatcher<RetT> Succeeds(RetT ExpectedReturn = 0,
83 int ExpectedErrno = 0) {
84 return {ExpectedReturn, ExpectedErrno};
87 template <typename RetT = int>
88 static internal::ErrnoSetterMatcher<RetT> Fails(int ExpectedErrno,
89 RetT ExpectedReturn = -1) {
90 return {ExpectedReturn, ExpectedErrno};
93 } // namespace ErrnoSetterMatcher
95 } // namespace testing
96 } // namespace __llvm_libc
98 #endif // LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H