xref: /aosp_15_r20/external/pigweed/pw_metric/global_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 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_metric/global.h"
16 
17 #include "pw_log/log.h"
18 #include "pw_metric/metric.h"
19 #include "pw_unit_test/framework.h"
20 
21 namespace pw {
22 namespace metric {
23 namespace {
24 
25 // Create two global metrics; make sure they show up.
26 PW_METRIC_GLOBAL(stat_x, "stat_x", 123u);
27 PW_METRIC_GLOBAL(stat_y, "stat_y", 123u);
28 
TEST(Global,Metrics)29 TEST(Global, Metrics) {
30   Metric::Dump(global_metrics);
31   EXPECT_EQ(global_metrics.size(), 2u);
32 }
33 
34 // Create three global metric groups; make sure they show up.
35 // Also, register some sub-metrics in the global groups.
36 PW_METRIC_GROUP_GLOBAL(gyro_metrics, "gyro");
37 PW_METRIC(gyro_metrics, max_velocity, "max_velocity", 5.0f);
38 
39 PW_METRIC_GROUP_GLOBAL(comms_metrics, "comms");
40 PW_METRIC(comms_metrics, packet_drops, "packet_drops", 10u);
41 PW_METRIC(comms_metrics, bandwidth, "bandwidth", 230.3f);
42 
43 PW_METRIC_GROUP_GLOBAL(power_metrics, "power");
44 PW_METRIC(power_metrics, voltage, "voltage", 3.33f);
45 PW_METRIC(power_metrics, battery_cycles, "battery_cycles", 550u);
46 PW_METRIC(power_metrics, current_ma, "current_ma", 35.2f);
47 
TEST(Global,Groups)48 TEST(Global, Groups) {
49   Group::Dump(global_groups);
50   EXPECT_EQ(global_groups.size(), 4u);
51 
52   EXPECT_EQ(gyro_metrics.metrics().size(), 1u);
53   EXPECT_EQ(comms_metrics.metrics().size(), 2u);
54   EXPECT_EQ(power_metrics.metrics().size(), 3u);
55 }
56 
57 }  // namespace
58 }  // namespace metric
59 }  // namespace pw
60 
61 namespace {
62 
63 // this is a compilation test to make sure metrics can be defined outside of
64 // ::pw::metric
65 PW_METRIC_GROUP_GLOBAL(global_group, "global group");
66 
67 }  // namespace
68