blob: ab0aab5cbd1726c5c7b9a6e99d3b4cd7d44be90e [file] [log] [blame]
Keir Mierle45fa7852020-08-10 21:09:54 -07001// 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 "gtest/gtest.h"
18#include "pw_log/log.h"
19#include "pw_metric/metric.h"
20
21namespace pw {
22namespace metric {
23
24// Count elements in an iterable.
25template <typename T>
26int Size(T& container) {
27 int num_elements = 0;
28 for (auto& element : container) {
29 PW_UNUSED(element);
30 num_elements++;
31 }
32 return num_elements;
33}
34
35// Create two global metrics; make sure they show up.
36PW_METRIC_GLOBAL(stat_x, "stat_x", 123u);
37PW_METRIC_GLOBAL(stat_y, "stat_y", 123u);
38
39TEST(Global, Metrics) {
40 Metric::Dump(global_metrics);
41 EXPECT_EQ(Size(global_metrics), 2);
42}
43
44// Create three global metric groups; make sure they show up.
45// Also, register some sub-metrics in the global groups.
46PW_METRIC_GROUP_GLOBAL(gyro_metrics, "gyro");
47PW_METRIC(gyro_metrics, max_velocity, "max_velocity", 5.0f);
48
49PW_METRIC_GROUP_GLOBAL(comms_metrics, "comms");
50PW_METRIC(comms_metrics, packet_drops, "packet_drops", 10u);
51PW_METRIC(comms_metrics, bandwidth, "bandwidth", 230.3f);
52
53PW_METRIC_GROUP_GLOBAL(power_metrics, "power");
54PW_METRIC(power_metrics, voltage, "voltage", 3.33f);
55PW_METRIC(power_metrics, battery_cycles, "battery_cycles", 550u);
56PW_METRIC(power_metrics, current_ma, "current_ma", 35.2f);
57
58TEST(Global, Groups) {
59 Group::Dump(global_groups);
Paul Mathieu990fab92020-08-31 17:55:44 +020060 EXPECT_EQ(Size(global_groups), 4);
Keir Mierle45fa7852020-08-10 21:09:54 -070061
62 EXPECT_EQ(Size(gyro_metrics.metrics()), 1);
63 EXPECT_EQ(Size(comms_metrics.metrics()), 2);
64 EXPECT_EQ(Size(power_metrics.metrics()), 3);
65}
66
67} // namespace metric
68} // namespace pw
Paul Mathieu990fab92020-08-31 17:55:44 +020069
70// this is a compilation test to make sure metrics can be defined outside of
71// ::pw::metric
72PW_METRIC_GROUP_GLOBAL(global_group, "global group");