• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_TEST_SUPPORT_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_TEST_SUPPORT_H_
7 
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "third_party/abseil-cpp/absl/types/optional.h"
10 
11 // Struct intended to be used with designated initializers and passed
12 // to the `CountersMatch()` matcher.
13 //
14 // TODO(tsepez): Although we only want one kind of these, the class is still
15 // a template to circumvent the chromium-style out-of-line constructor rule.
16 // Adding such a constructor would make this no longer be an aggregate and
17 // that would prohibit designated initiaizers.
18 template <int IGNORE>
19 struct CountingRawPtrExpectationTemplate {
20   absl::optional<int> wrap_raw_ptr_cnt;
21   absl::optional<int> release_wrapped_ptr_cnt;
22   absl::optional<int> get_for_dereference_cnt;
23   absl::optional<int> get_for_extraction_cnt;
24   absl::optional<int> get_for_comparison_cnt;
25   absl::optional<int> wrapped_ptr_swap_cnt;
26   absl::optional<int> wrapped_ptr_less_cnt;
27   absl::optional<int> pointer_to_member_operator_cnt;
28   absl::optional<int> wrap_raw_ptr_for_dup_cnt;
29   absl::optional<int> get_for_duplication_cnt;
30 };
31 using CountingRawPtrExpectations = CountingRawPtrExpectationTemplate<0>;
32 
33 #define REPORT_UNEQUAL_RAW_PTR_COUNTER(member_name)                          \
34   {                                                                          \
35     if (arg.member_name.has_value() &&                                       \
36         arg.member_name.value() !=                                           \
37             base::test::RawPtrCountingImplForTest::member_name) {            \
38       *result_listener << "Expected `" #member_name "` to be "               \
39                        << arg.member_name.value() << " but got "             \
40                        << base::test::RawPtrCountingImplForTest::member_name \
41                        << "; ";                                              \
42       result = false;                                                        \
43     }                                                                        \
44   }
45 
46 #define REPORT_UNEQUAL_RAW_PTR_COUNTERS(result)                    \
47   {                                                                \
48     result = true;                                                 \
49     REPORT_UNEQUAL_RAW_PTR_COUNTER(wrap_raw_ptr_cnt)               \
50     REPORT_UNEQUAL_RAW_PTR_COUNTER(release_wrapped_ptr_cnt)        \
51     REPORT_UNEQUAL_RAW_PTR_COUNTER(get_for_dereference_cnt)        \
52     REPORT_UNEQUAL_RAW_PTR_COUNTER(get_for_extraction_cnt)         \
53     REPORT_UNEQUAL_RAW_PTR_COUNTER(get_for_comparison_cnt)         \
54     REPORT_UNEQUAL_RAW_PTR_COUNTER(wrapped_ptr_swap_cnt)           \
55     REPORT_UNEQUAL_RAW_PTR_COUNTER(wrapped_ptr_less_cnt)           \
56     REPORT_UNEQUAL_RAW_PTR_COUNTER(pointer_to_member_operator_cnt) \
57     REPORT_UNEQUAL_RAW_PTR_COUNTER(wrap_raw_ptr_for_dup_cnt)       \
58     REPORT_UNEQUAL_RAW_PTR_COUNTER(get_for_duplication_cnt)        \
59   }
60 
61 // Matcher used with `CountingRawPtr`. Provides slightly shorter
62 // boilerplate for verifying counts. This inner function is detached
63 // from the `MATCHER`.
CountersMatchImpl(const CountingRawPtrExpectations & arg,testing::MatchResultListener * result_listener)64 inline bool CountersMatchImpl(const CountingRawPtrExpectations& arg,
65                               testing::MatchResultListener* result_listener) {
66   bool result = true;
67   REPORT_UNEQUAL_RAW_PTR_COUNTERS(result);
68   return result;
69 }
70 
71 // Implicit `arg` has type `CountingRawPtrExpectations`, specialized for
72 // the specific counting impl.
73 MATCHER(CountersMatch, "counting impl has specified counters") {
74   return CountersMatchImpl(arg, result_listener);
75 }
76 
77 #undef REPORT_UNEQUAL_RAW_PTR_COUNTERS
78 #undef REPORT_UNEQUAL_RAW_PTR_COUNTER
79 
80 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_TEST_SUPPORT_H_
81