[clang] Avoid linking libdl unless needed
[llvm-project.git] / libc / utils / UnitTest / ErrnoSetterMatcher.h
blobd676ee9cbc9179c50ade2760deeaa8f40ac68f5c
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_UTILS_UNITTEST_ERRNOSETTERMATCHER_H
10 #define LLVM_LIBC_UTILS_UNITTEST_ERRNOSETTERMATCHER_H
12 #include "Test.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 {
20 namespace testing {
22 namespace internal {
24 extern "C" const char *strerror(int);
26 template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
27 T ExpectedReturn;
28 T ActualReturn;
29 int ExpectedErrno;
30 int ActualErrno;
32 public:
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";
41 else
42 OS << "Correct value " << ExpectedReturn
43 << " was returned\nBut errno was unexpectely set to "
44 << strerror(ActualErrno) << ".\n";
47 bool match(T Got) {
48 ActualReturn = Got;
49 ActualErrno = llvmlibc_errno;
50 llvmlibc_errno = 0;
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