xref: /aosp_15_r20/external/llvm-libc/test/src/time/TmMatcher.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 "src/__support/macros/config.h"
15 #include "test/UnitTest/Test.h"
16 
17 namespace LIBC_NAMESPACE_DECL {
18 namespace testing {
19 
20 class StructTmMatcher : public 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)40   void describeValue(const char *label, ::tm value) {
41     tlog << label;
42     tlog << " sec: " << value.tm_sec;
43     tlog << " min: " << value.tm_min;
44     tlog << " hour: " << value.tm_hour;
45     tlog << " mday: " << value.tm_mday;
46     tlog << " mon: " << value.tm_mon;
47     tlog << " year: " << value.tm_year;
48     tlog << " wday: " << value.tm_wday;
49     tlog << " yday: " << value.tm_yday;
50     tlog << " isdst: " << value.tm_isdst;
51     tlog << '\n';
52   }
53 
explainError()54   void explainError() override {
55     describeValue("Expected tm_struct value: ", expected);
56     describeValue("  Actual tm_struct value: ", actual);
57   }
58 };
59 
60 } // namespace testing
61 } // namespace LIBC_NAMESPACE_DECL
62 
63 #define EXPECT_TM_EQ(expected, actual)                                         \
64   EXPECT_THAT((actual), LIBC_NAMESPACE::testing::StructTmMatcher((expected)))
65 
66 #endif // LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H
67