1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pw_metric/global.h"
17 #include "gtest/gtest.h"
18 #include "pw_log/log.h"
19 #include "pw_metric/metric.h"
24 // Count elements in an iterable.
26 int Size(T& container) {
28 for (auto& element : container) {
35 // Create two global metrics; make sure they show up.
36 PW_METRIC_GLOBAL(stat_x, "stat_x", 123u);
37 PW_METRIC_GLOBAL(stat_y, "stat_y", 123u);
39 TEST(Global, Metrics) {
40 Metric::Dump(global_metrics);
41 EXPECT_EQ(Size(global_metrics), 2);
44 // Create three global metric groups; make sure they show up.
45 // Also, register some sub-metrics in the global groups.
46 PW_METRIC_GROUP_GLOBAL(gyro_metrics, "gyro");
47 PW_METRIC(gyro_metrics, max_velocity, "max_velocity", 5.0f);
49 PW_METRIC_GROUP_GLOBAL(comms_metrics, "comms");
50 PW_METRIC(comms_metrics, packet_drops, "packet_drops", 10u);
51 PW_METRIC(comms_metrics, bandwidth, "bandwidth", 230.3f);
53 PW_METRIC_GROUP_GLOBAL(power_metrics, "power");
54 PW_METRIC(power_metrics, voltage, "voltage", 3.33f);
55 PW_METRIC(power_metrics, battery_cycles, "battery_cycles", 550u);
56 PW_METRIC(power_metrics, current_ma, "current_ma", 35.2f);
58 TEST(Global, Groups) {
59 Group::Dump(global_groups);
60 EXPECT_EQ(Size(global_groups), 4);
62 EXPECT_EQ(Size(gyro_metrics.metrics()), 1);
63 EXPECT_EQ(Size(comms_metrics.metrics()), 2);
64 EXPECT_EQ(Size(power_metrics.metrics()), 3);
70 // this is a compilation test to make sure metrics can be defined outside of
72 PW_METRIC_GROUP_GLOBAL(global_group, "global group");