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 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H 10 #define LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H 11 12 #include "src/errno/llvmlibc_errno.h" 13 #include "utils/UnitTest/Test.h" 14 15 namespace __llvm_libc { 16 namespace testing { 17 18 namespace internal { 19 20 extern "C" const char *strerror(int); 21 22 template <typename T> class ErrnoSetterMatcher : public Matcher<T> { 23 T ExpectedReturn; 24 T ActualReturn; 25 int ExpectedErrno; 26 int ActualErrno; 27 28 public: ErrnoSetterMatcher(T ExpectedReturn,int ExpectedErrno)29 ErrnoSetterMatcher(T ExpectedReturn, int ExpectedErrno) 30 : ExpectedReturn(ExpectedReturn), ExpectedErrno(ExpectedErrno) {} 31 explainError(testutils::StreamWrapper & OS)32 void explainError(testutils::StreamWrapper &OS) override { 33 if (ActualReturn != ExpectedReturn) 34 OS << "Expected return to be " << ExpectedReturn << " but got " 35 << ActualReturn << ".\nExpecte errno to be " << strerror(ExpectedErrno) 36 << " but got " << strerror(ActualErrno) << ".\n"; 37 else 38 OS << "Correct value " << ExpectedReturn 39 << " was returned\nBut errno was unexpectely set to " 40 << strerror(ActualErrno) << ".\n"; 41 } 42 match(T Got)43 bool match(T Got) { 44 ActualReturn = Got; 45 ActualErrno = llvmlibc_errno; 46 llvmlibc_errno = 0; 47 return Got == ExpectedReturn && ActualErrno == ExpectedErrno; 48 } 49 }; 50 51 } // namespace internal 52 53 namespace ErrnoSetterMatcher { 54 55 template <typename RetT = int> 56 static internal::ErrnoSetterMatcher<RetT> Succeeds(RetT ExpectedReturn = 0, 57 int ExpectedErrno = 0) { 58 return {ExpectedReturn, ExpectedErrno}; 59 } 60 61 template <typename RetT = int> 62 static internal::ErrnoSetterMatcher<RetT> Fails(int ExpectedErrno, 63 RetT ExpectedReturn = -1) { 64 return {ExpectedReturn, ExpectedErrno}; 65 } 66 67 } // namespace ErrnoSetterMatcher 68 69 } // namespace testing 70 } // namespace __llvm_libc 71 72 #endif // LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H 73