blob: d676ee9cbc9179c50ade2760deeaa8f40ac68f5c [file] [log] [blame]
Paula Toth66d00fe2020-04-08 10:16:30 -07001//===-- ErrnoSetterMatcher.h ------------------------------------*- C++ -*-===//
Alex Brachet1962bc12020-03-05 18:36:11 -05002//
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
19namespace __llvm_libc {
20namespace testing {
21
22namespace internal {
23
24extern "C" const char *strerror(int);
25
26template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
27 T ExpectedReturn;
28 T ActualReturn;
29 int ExpectedErrno;
30 int ActualErrno;
31
32public:
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
57namespace ErrnoSetterMatcher {
58
59template <typename RetT = int>
60static internal::ErrnoSetterMatcher<RetT> Succeeds(RetT ExpectedReturn = 0,
61 int ExpectedErrno = 0) {
62 return {ExpectedReturn, ExpectedErrno};
63}
64
65template <typename RetT = int>
66static 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