1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_allocator/metrics.h"
16
17 #include "pw_unit_test/framework.h"
18
19 namespace {
20
21 using ::pw::allocator::NoMetrics;
22 using ::pw::allocator::internal::AllMetrics;
23
TEST(MetricsTest,NoMetricsPresent)24 TEST(MetricsTest, NoMetricsPresent) {
25 #define EXPECT_METRIC_MISSING(metric_name) \
26 EXPECT_FALSE(NoMetrics::has_##metric_name());
27
28 PW_ALLOCATOR_METRICS_FOREACH(EXPECT_METRIC_MISSING);
29
30 #undef EXPECT_METRIC_MISSING
31 }
32
TEST(MetricsTest,NoMetricsEnabled)33 TEST(MetricsTest, NoMetricsEnabled) {
34 #define EXPECT_METRIC_DISABLED(metric_name) \
35 EXPECT_FALSE(NoMetrics::metric_name##_enabled());
36
37 PW_ALLOCATOR_METRICS_FOREACH(EXPECT_METRIC_DISABLED);
38 EXPECT_FALSE(pw::allocator::internal::AnyEnabled<NoMetrics>());
39
40 #undef EXPECT_METRIC_DISABLED
41 }
42
TEST(MetricsTest,TraitsAreConstexpr)43 TEST(MetricsTest, TraitsAreConstexpr) {
44 #define FAIL_IF_PRESENT(metric_name) \
45 if constexpr (NoMetrics::has_##metric_name()) { \
46 FAIL(); \
47 }
48
49 PW_ALLOCATOR_METRICS_FOREACH(FAIL_IF_PRESENT);
50
51 #undef FAIL_IF_PRESENT
52 }
53
TEST(MetricsTest,GetMissingMetricsReturnsNotFound)54 TEST(MetricsTest, GetMissingMetricsReturnsNotFound) {
55 NoMetrics metrics;
56 pw::StatusWithSize result;
57
58 #define EXPECT_GET_MISSING_IS_NOT_FOUND(metric_name) \
59 result = metrics.get_##metric_name(); \
60 EXPECT_EQ(result.status(), pw::Status::NotFound());
61
62 PW_ALLOCATOR_METRICS_FOREACH(EXPECT_GET_MISSING_IS_NOT_FOUND);
63
64 #undef EXPECT_GET_MISSING_IS_NOT_FOUND
65 }
66
TEST(MetricsTest,AllMetricsPresent)67 TEST(MetricsTest, AllMetricsPresent) {
68 #define EXPECT_METRIC_PRESENT(metric_name) \
69 EXPECT_TRUE(AllMetrics::has_##metric_name());
70
71 PW_ALLOCATOR_METRICS_FOREACH(EXPECT_METRIC_PRESENT);
72
73 #undef EXPECT_METRIC_PRESENT
74 }
75
TEST(MetricsTest,AllMetricsEnabled)76 TEST(MetricsTest, AllMetricsEnabled) {
77 #define EXPECT_METRIC_ENABLED(metric_name) \
78 EXPECT_TRUE(AllMetrics::metric_name##_enabled());
79
80 PW_ALLOCATOR_METRICS_FOREACH(EXPECT_METRIC_ENABLED);
81 EXPECT_TRUE(pw::allocator::internal::AnyEnabled<AllMetrics>());
82
83 #undef EXPECT_METRIC_ENABLED
84 }
85
TEST(MetricsTest,GetPresentMetricsReturnsOk)86 TEST(MetricsTest, GetPresentMetricsReturnsOk) {
87 AllMetrics metrics;
88 pw::StatusWithSize result;
89
90 #define EXPECT_GET_PRESENT_IS_OK(metric_name) \
91 result = metrics.get_##metric_name(); \
92 EXPECT_EQ(result.status(), pw::OkStatus()); \
93 EXPECT_EQ(result.size(), 0u);
94
95 PW_ALLOCATOR_METRICS_FOREACH(EXPECT_GET_PRESENT_IS_OK);
96
97 #undef EXPECT_GET_PRESENT_IS_OK
98 }
99
100 } // namespace
101