[ObjC][ARC] Share bundle handling code between steps of the ObjCARCOpts pass and...
[llvm-project.git] / libc / test / ErrnoSetterMatcher.h
blob3d8c914b0491ce42b226d49b6cfdb5037e9f175f
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 "utils/UnitTest/Test.h"
14 #include <errno.h>
16 namespace __llvm_libc {
17 namespace testing {
19 namespace internal {
21 extern "C" char *strerror(int);
23 template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
24 T ExpectedReturn;
25 T ActualReturn;
26 int ExpectedErrno;
27 int ActualErrno;
29 public:
30 ErrnoSetterMatcher(T ExpectedReturn, int ExpectedErrno)
31 : ExpectedReturn(ExpectedReturn), ExpectedErrno(ExpectedErrno) {}
33 void explainError(testutils::StreamWrapper &OS) override {
34 if (ActualReturn != ExpectedReturn)
35 OS << "Expected return to be " << ExpectedReturn << " but got "
36 << ActualReturn << ".\nExpecte errno to be " << strerror(ExpectedErrno)
37 << " but got " << strerror(ActualErrno) << ".\n";
38 else
39 OS << "Correct value " << ExpectedReturn
40 << " was returned\nBut errno was unexpectely set to "
41 << strerror(ActualErrno) << ".\n";
44 bool match(T Got) {
45 ActualReturn = Got;
46 ActualErrno = errno;
47 errno = 0;
48 return Got == ExpectedReturn && ActualErrno == ExpectedErrno;
52 } // namespace internal
54 namespace ErrnoSetterMatcher {
56 template <typename RetT = int>
57 static internal::ErrnoSetterMatcher<RetT> Succeeds(RetT ExpectedReturn = 0,
58 int ExpectedErrno = 0) {
59 return {ExpectedReturn, ExpectedErrno};
62 template <typename RetT = int>
63 static internal::ErrnoSetterMatcher<RetT> Fails(int ExpectedErrno,
64 RetT ExpectedReturn = -1) {
65 return {ExpectedReturn, ExpectedErrno};
68 } // namespace ErrnoSetterMatcher
70 } // namespace testing
71 } // namespace __llvm_libc
73 #endif // LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H