1 /*
2 * Copyright 2021 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 "common/sync_map_count.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <cstddef>
23 #include <cstring>
24 #include <vector>
25
26 #include "os/log.h"
27
28 namespace testing {
29
30 const char* data[] = {
31 "One",
32 "Two",
33 "Two",
34 "Three",
35 "Three",
36 "Three",
37 "AAA",
38 "ZZZ",
39 nullptr,
40 };
41
42 namespace {
LoadStringMap(SyncMapCount<std::string> & map)43 void LoadStringMap(SyncMapCount<std::string>& map) {
44 for (const char** p = data; *p != nullptr; p++) {
45 map.Put(*p);
46 }
47 }
48 } // namespace
49
TEST(SyncMapCount,simple)50 TEST(SyncMapCount, simple) {
51 SyncMapCount<std::string> map;
52 LoadStringMap(map);
53
54 ASSERT_EQ(5ul, map.Size());
55
56 auto m = map.Get();
57 ASSERT_EQ(3ul, m["Three"]);
58 ASSERT_EQ(2ul, m["Two"]);
59 ASSERT_EQ(1ul, m["One"]);
60 }
61
TEST(SyncMapCount,sized)62 TEST(SyncMapCount, sized) {
63 SyncMapCount<std::string> map(2);
64 LoadStringMap(map);
65
66 ASSERT_EQ(2ul, map.Size());
67 }
68
TEST(SyncMapCount,sorted_string_value_low_to_high)69 TEST(SyncMapCount, sorted_string_value_low_to_high) {
70 SyncMapCount<std::string> map;
71 LoadStringMap(map);
72
73 auto entries = map.GetSortedLowToHigh();
74 ASSERT_EQ(3ul, entries[entries.size() - 1].count);
75 ASSERT_EQ(2ul, entries[entries.size() - 2].count);
76 }
77
TEST(SyncMapCount,sorted_string_value_high_to_low)78 TEST(SyncMapCount, sorted_string_value_high_to_low) {
79 SyncMapCount<std::string> map;
80 LoadStringMap(map);
81
82 auto entries = map.GetSortedHighToLow();
83 ASSERT_EQ(3ul, entries[0].count);
84 ASSERT_EQ(2ul, entries[1].count);
85 }
86
87 struct TestString {
TestStringtesting::TestString88 TestString(std::string string) : string_(string) {}
Stringtesting::TestString89 std::string String() const {
90 return string_;
91 }
92
operator <testing::TestString93 bool operator<(const TestString& other) const {
94 return (other.string_ > string_);
95 }
operator ==testing::TestString96 bool operator==(const TestString& other) const {
97 return (other.string_ == string_);
98 }
99
100 private:
101 std::string string_;
102 };
103
104 namespace {
LoadTestStringMap(SyncMapCount<TestString> & map)105 void LoadTestStringMap(SyncMapCount<TestString>& map) {
106 for (const char** p = data; *p != nullptr; p++) {
107 map.Put(TestString(*p));
108 }
109 }
110 } // namespace
111
TEST(SyncMapCount,simple_struct)112 TEST(SyncMapCount, simple_struct) {
113 SyncMapCount<TestString> map;
114 LoadTestStringMap(map);
115
116 ASSERT_EQ(5ul, map.Size());
117
118 auto m = map.Get();
119 ASSERT_EQ(3ul, m[TestString("Three")]);
120 ASSERT_EQ(2ul, m[TestString("Two")]);
121 ASSERT_EQ(1ul, m[TestString("One")]);
122 }
123
TEST(SyncMapCount,sorted_string_struct_value_low_to_high)124 TEST(SyncMapCount, sorted_string_struct_value_low_to_high) {
125 SyncMapCount<TestString> map;
126 LoadTestStringMap(map);
127
128 auto entries = map.GetSortedLowToHigh();
129 ASSERT_EQ(3ul, entries[entries.size() - 1].count);
130 ASSERT_EQ(2ul, entries[entries.size() - 2].count);
131 }
132
TEST(SyncMapCount,sorted_string_struct_value_high_to_low)133 TEST(SyncMapCount, sorted_string_struct_value_high_to_low) {
134 SyncMapCount<TestString> map;
135 LoadTestStringMap(map);
136
137 auto entries = map.GetSortedHighToLow();
138 ASSERT_EQ(3ul, entries[0].count);
139 ASSERT_EQ(2ul, entries[1].count);
140 }
141
TEST(SyncMapCount,locked_for_map_copy)142 TEST(SyncMapCount, locked_for_map_copy) {
143 SyncMapCount<TestString> map;
144 LoadTestStringMap(map);
145
146 ASSERT_EQ(5ul, map.Size());
147 std::vector<SyncMapCount<TestString>::Item> vec;
148 for (auto& it : map.Get()) {
149 map.Clear();
150 vec.push_back(SyncMapCount<TestString>::Item{it.first, it.second});
151 }
152 ASSERT_EQ(0ul, map.Size());
153 ASSERT_EQ(5ul, vec.size());
154 }
155
156 } // namespace testing
157