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_UTILS_UNITTEST_ERRNOSETTERMATCHER_H
10 #define LLVM_LIBC_UTILS_UNITTEST_ERRNOSETTERMATCHER_H
14 // Using LLVM libc headers in UnitTest is not ideal however we also want the
15 // test/ directory to have the same layout as libc/ so there is no clean place
16 // to put this file except for in utils/UnitTest/.
17 #include "src/errno/llvmlibc_errno.h"
19 namespace __llvm_libc
{
24 extern "C" const char *strerror(int);
26 template <typename T
> class ErrnoSetterMatcher
: public Matcher
<T
> {
33 ErrnoSetterMatcher(T ExpectedReturn
, int ExpectedErrno
)
34 : ExpectedReturn(ExpectedReturn
), ExpectedErrno(ExpectedErrno
) {}
36 void explainError(testutils::StreamWrapper
&OS
) override
{
37 if (ActualReturn
!= ExpectedReturn
)
38 OS
<< "Expected return to be " << ExpectedReturn
<< " but got "
39 << ActualReturn
<< ".\nExpecte errno to be " << strerror(ExpectedErrno
)
40 << " but got " << strerror(ActualErrno
) << ".\n";
42 OS
<< "Correct value " << ExpectedReturn
43 << " was returned\nBut errno was unexpectely set to "
44 << strerror(ActualErrno
) << ".\n";
49 ActualErrno
= llvmlibc_errno
;
51 return Got
== ExpectedReturn
&& ActualErrno
== ExpectedErrno
;
55 } // namespace internal
57 namespace ErrnoSetterMatcher
{
59 template <typename RetT
= int>
60 static internal::ErrnoSetterMatcher
<RetT
> Succeeds(RetT ExpectedReturn
= 0,
61 int ExpectedErrno
= 0) {
62 return {ExpectedReturn
, ExpectedErrno
};
65 template <typename RetT
= int>
66 static internal::ErrnoSetterMatcher
<RetT
> Fails(int ExpectedErrno
,
67 RetT ExpectedReturn
= -1) {
68 return {ExpectedReturn
, ExpectedErrno
};
71 } // namespace ErrnoSetterMatcher
73 } // namespace testing
74 } // namespace __llvm_libc
76 #endif // LLVM_LIBC_UTILS_UNITTEST_ERRNOSETTERMATCHER_H