Paula Toth | 66d00fe | 2020-04-08 10:16:30 -0700 | [diff] [blame] | 1 | //===-- ErrnoSetterMatcher.h ------------------------------------*- C++ -*-===// |
Alex Brachet | 1962bc1 | 2020-03-05 18:36:11 -0500 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIBC_UTILS_UNITTEST_ERRNOSETTERMATCHER_H |
| 10 | #define LLVM_LIBC_UTILS_UNITTEST_ERRNOSETTERMATCHER_H |
| 11 | |
| 12 | #include "Test.h" |
| 13 | |
| 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" |
| 18 | |
| 19 | namespace __llvm_libc { |
| 20 | namespace testing { |
| 21 | |
| 22 | namespace internal { |
| 23 | |
| 24 | extern "C" const char *strerror(int); |
| 25 | |
| 26 | template <typename T> class ErrnoSetterMatcher : public Matcher<T> { |
| 27 | T ExpectedReturn; |
| 28 | T ActualReturn; |
| 29 | int ExpectedErrno; |
| 30 | int ActualErrno; |
| 31 | |
| 32 | public: |
| 33 | ErrnoSetterMatcher(T ExpectedReturn, int ExpectedErrno) |
| 34 | : ExpectedReturn(ExpectedReturn), ExpectedErrno(ExpectedErrno) {} |
| 35 | |
| 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"; |
| 45 | } |
| 46 | |
| 47 | bool match(T Got) { |
| 48 | ActualReturn = Got; |
| 49 | ActualErrno = llvmlibc_errno; |
| 50 | llvmlibc_errno = 0; |
| 51 | return Got == ExpectedReturn && ActualErrno == ExpectedErrno; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | } // namespace internal |
| 56 | |
| 57 | namespace ErrnoSetterMatcher { |
| 58 | |
| 59 | template <typename RetT = int> |
| 60 | static internal::ErrnoSetterMatcher<RetT> Succeeds(RetT ExpectedReturn = 0, |
| 61 | int ExpectedErrno = 0) { |
| 62 | return {ExpectedReturn, ExpectedErrno}; |
| 63 | } |
| 64 | |
| 65 | template <typename RetT = int> |
| 66 | static internal::ErrnoSetterMatcher<RetT> Fails(int ExpectedErrno, |
| 67 | RetT ExpectedReturn = -1) { |
| 68 | return {ExpectedReturn, ExpectedErrno}; |
| 69 | } |
| 70 | |
| 71 | } // namespace ErrnoSetterMatcher |
| 72 | |
| 73 | } // namespace testing |
| 74 | } // namespace __llvm_libc |
| 75 | |
| 76 | #endif // LLVM_LIBC_UTILS_UNITTEST_ERRNOSETTERMATCHER_H |