1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "CounterMetric.h"
20
21 namespace android {
22
23 /**
24 * Unit tests for the CounterMetric class.
25 */
26 class CounterMetricTest : public ::testing::Test {
27 };
28
TEST_F(CounterMetricTest,IntDataTypeEmpty)29 TEST_F(CounterMetricTest, IntDataTypeEmpty) {
30 CounterMetric<int> metric("MyMetricName", "MetricAttributeName");
31
32 std::map<int, int64_t> values;
33
34 metric.ExportValues(
35 [&] (int attribute_value, int64_t value) {
36 values[attribute_value] = value;
37 });
38
39 EXPECT_TRUE(values.empty());
40 }
41
TEST_F(CounterMetricTest,IntDataType)42 TEST_F(CounterMetricTest, IntDataType) {
43 CounterMetric<int> metric("MyMetricName", "MetricAttributeName");
44
45 std::map<int, int64_t> values;
46
47 metric.Increment(7);
48 metric.Increment(8);
49 metric.Increment(8);
50
51 metric.ExportValues(
52 [&] (int attribute_value, int64_t value) {
53 values[attribute_value] = value;
54 });
55
56 ASSERT_EQ(2u, values.size());
57 EXPECT_EQ(1, values[7]);
58 EXPECT_EQ(2, values[8]);
59 }
60
TEST_F(CounterMetricTest,StringDataType)61 TEST_F(CounterMetricTest, StringDataType) {
62 CounterMetric<std::string> metric("MyMetricName", "MetricAttributeName");
63
64 std::map<std::string, int64_t> values;
65
66 metric.Increment("a");
67 metric.Increment("b");
68 metric.Increment("b");
69
70 metric.ExportValues(
71 [&] (std::string attribute_value, int64_t value) {
72 values[attribute_value] = value;
73 });
74
75 ASSERT_EQ(2u, values.size());
76 EXPECT_EQ(1, values["a"]);
77 EXPECT_EQ(2, values["b"]);
78 }
79
80 } // namespace android
81