1 /*
2  * Copyright 2020 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 <ftl/small_map.h>
18 #include <gtest/gtest.h>
19 
20 #include <cctype>
21 
22 namespace android::test {
23 
24 using ftl::SmallMap;
25 
26 // Keep in sync with example usage in header file.
TEST(SmallMap,Example)27 TEST(SmallMap, Example) {
28   ftl::SmallMap<int, std::string, 3> map;
29   EXPECT_TRUE(map.empty());
30   EXPECT_FALSE(map.dynamic());
31 
32   map = ftl::init::map<int, std::string>(123, "abc")(-1)(42, 3u, '?');
33   EXPECT_EQ(map.size(), 3u);
34   EXPECT_FALSE(map.dynamic());
35 
36   EXPECT_TRUE(map.contains(123));
37 
38   EXPECT_EQ(map.find(42, [](const std::string& s) { return s.size(); }), 3u);
39 
40   const auto opt = map.find(-1);
41   ASSERT_TRUE(opt);
42 
43   std::string& ref = *opt;
44   EXPECT_TRUE(ref.empty());
45   ref = "xyz";
46 
47   EXPECT_EQ(map, SmallMap(ftl::init::map(-1, "xyz")(42, "???")(123, "abc")));
48 }
49 
TEST(SmallMap,Construct)50 TEST(SmallMap, Construct) {
51   {
52     // Default constructor.
53     SmallMap<int, std::string, 2> map;
54 
55     EXPECT_TRUE(map.empty());
56     EXPECT_FALSE(map.dynamic());
57   }
58   {
59     // In-place constructor with same types.
60     SmallMap<int, std::string, 5> map =
61         ftl::init::map<int, std::string>(123, "abc")(456, "def")(789, "ghi");
62 
63     EXPECT_EQ(map.size(), 3u);
64     EXPECT_EQ(map.max_size(), 5u);
65     EXPECT_FALSE(map.dynamic());
66 
67     EXPECT_EQ(map, SmallMap(ftl::init::map(123, "abc")(456, "def")(789, "ghi")));
68   }
69   {
70     // In-place constructor with different types.
71     SmallMap<int, std::string, 5> map =
72         ftl::init::map<int, std::string>(123, "abc")(-1)(42, 3u, '?');
73 
74     EXPECT_EQ(map.size(), 3u);
75     EXPECT_EQ(map.max_size(), 5u);
76     EXPECT_FALSE(map.dynamic());
77 
78     EXPECT_EQ(map, SmallMap(ftl::init::map(42, "???")(123, "abc")(-1, "\0\0\0")));
79   }
80   {
81     // In-place constructor with implicit size.
82     SmallMap map = ftl::init::map<int, std::string>(123, "abc")(-1)(42, 3u, '?');
83 
84     static_assert(std::is_same_v<decltype(map), SmallMap<int, std::string, 3>>);
85     EXPECT_EQ(map.size(), 3u);
86     EXPECT_EQ(map.max_size(), 3u);
87     EXPECT_FALSE(map.dynamic());
88 
89     EXPECT_EQ(map, SmallMap(ftl::init::map(-1, "\0\0\0")(42, "???")(123, "abc")));
90   }
91 }
92 
TEST(SmallMap,Find)93 TEST(SmallMap, Find) {
94   {
95     // Constant reference.
96     const ftl::SmallMap map = ftl::init::map('a', 'A')('b', 'B')('c', 'C');
97 
98     const auto opt = map.find('b');
99     EXPECT_EQ(opt, 'B');
100 
101     const char d = 'D';
102     const auto ref = map.find('d').value_or(std::cref(d));
103     EXPECT_EQ(ref.get(), 'D');
104   }
105   {
106     // Mutable reference.
107     ftl::SmallMap map = ftl::init::map('a', 'A')('b', 'B')('c', 'C');
108 
109     const auto opt = map.find('c');
110     EXPECT_EQ(opt, 'C');
111 
112     char d = 'd';
113     const auto ref = map.find('d').value_or(std::ref(d));
114     ref.get() = 'D';
115     EXPECT_EQ(d, 'D');
116   }
117   {
118     // Constant unary operation.
119     const ftl::SmallMap map = ftl::init::map('a', 'x')('b', 'y')('c', 'z');
120     EXPECT_EQ(map.find('c', [](char c) { return std::toupper(c); }), 'Z');
121   }
122   {
123     // Mutable unary operation.
124     ftl::SmallMap map = ftl::init::map('a', 'x')('b', 'y')('c', 'z');
125     EXPECT_TRUE(map.find('c', [](char& c) { c = std::toupper(c); }));
126 
127     EXPECT_EQ(map, SmallMap(ftl::init::map('c', 'Z')('b', 'y')('a', 'x')));
128   }
129 }
130 
131 }  // namespace android::test
132