Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_metric / global_test.cc
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 "gtest/gtest.h"
18 #include "pw_log/log.h"
19 #include "pw_metric/metric.h"
20
21 namespace pw {
22 namespace metric {
23
24 // Count elements in an iterable.
25 template <typename T>
26 int 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.
36 PW_METRIC_GLOBAL(stat_x, "stat_x", 123u);
37 PW_METRIC_GLOBAL(stat_y, "stat_y", 123u);
38
39 TEST(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.
46 PW_METRIC_GROUP_GLOBAL(gyro_metrics, "gyro");
47 PW_METRIC(gyro_metrics, max_velocity, "max_velocity", 5.0f);
48
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);
52
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);
57
58 TEST(Global, Groups) {
59   Group::Dump(global_groups);
60   EXPECT_EQ(Size(global_groups), 4);
61
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
69
70 // this is a compilation test to make sure metrics can be defined outside of
71 // ::pw::metric
72 PW_METRIC_GROUP_GLOBAL(global_group, "global group");