1 //===-- ErrnoSetterMatcher.h ------------------------------------*- C++ -*-===//
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
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
{
24 template <typename T
> class ErrnoSetterMatcher
: public Matcher
<T
> {
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
33 static constexpr bool ignore_errno() {
34 #ifdef LIBC_TARGET_ARCH_IS_GPU
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';
51 << str(fputil::FPBits
<T
>(ActualReturn
)) << '\n';
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";
68 ActualErrno
= libc_errno
;
70 if constexpr (ignore_errno())
71 return Got
== ExpectedReturn
;
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