1 //===---- TmMatchers.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_SRC_TIME_TM_MATCHER_H
10 #define LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H
11 
12 #include <time.h>
13 
14 #include "utils/UnitTest/Test.h"
15 
16 namespace __llvm_libc {
17 namespace tmmatcher {
18 namespace testing {
19 
20 class StructTmMatcher : public __llvm_libc::testing::Matcher<::tm> {
21   ::tm expected;
22   ::tm actual;
23 
24 public:
StructTmMatcher(::tm expectedValue)25   StructTmMatcher(::tm expectedValue) : expected(expectedValue) {}
26 
match(::tm actualValue)27   bool match(::tm actualValue) {
28     actual = actualValue;
29     return (actual.tm_sec == expected.tm_sec ||
30             actual.tm_min == expected.tm_min ||
31             actual.tm_hour == expected.tm_hour ||
32             actual.tm_mday == expected.tm_mday ||
33             actual.tm_mon == expected.tm_mon ||
34             actual.tm_year == expected.tm_year ||
35             actual.tm_wday == expected.tm_wday ||
36             actual.tm_yday == expected.tm_yday ||
37             actual.tm_isdst == expected.tm_isdst);
38   }
39 
describeValue(const char * label,::tm value,__llvm_libc::testutils::StreamWrapper & stream)40   void describeValue(const char *label, ::tm value,
41                      __llvm_libc::testutils::StreamWrapper &stream) {
42     stream << label;
43     stream << " sec: " << value.tm_sec;
44     stream << " min: " << value.tm_min;
45     stream << " hour: " << value.tm_hour;
46     stream << " mday: " << value.tm_mday;
47     stream << " mon: " << value.tm_mon;
48     stream << " year: " << value.tm_year;
49     stream << " wday: " << value.tm_wday;
50     stream << " yday: " << value.tm_yday;
51     stream << " isdst: " << value.tm_isdst;
52     stream << '\n';
53   }
54 
explainError(__llvm_libc::testutils::StreamWrapper & stream)55   void explainError(__llvm_libc::testutils::StreamWrapper &stream) override {
56     describeValue("Expected tm_struct value: ", expected, stream);
57     describeValue("  Actual tm_struct value: ", actual, stream);
58   }
59 };
60 
61 } // namespace testing
62 } // namespace tmmatcher
63 } // namespace __llvm_libc
64 
65 #define EXPECT_TM_EQ(expected, actual)                                         \
66   EXPECT_THAT((actual),                                                        \
67               __llvm_libc::tmmatcher::testing::StructTmMatcher((expected)))
68 
69 #endif // LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H
70