2017
[folly.git] / folly / test / MapUtilTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
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 <folly/MapUtil.h>
18
19 #include <map>
20
21 #include <folly/portability/GTest.h>
22
23 using namespace folly;
24
25 TEST(MapUtil, get_default) {
26   std::map<int, int> m;
27   m[1] = 2;
28   EXPECT_EQ(2, get_default(m, 1, 42));
29   EXPECT_EQ(42, get_default(m, 2, 42));
30   EXPECT_EQ(0, get_default(m, 3));
31 }
32
33 TEST(MapUtil, get_default_function) {
34   std::map<int, int> m;
35   m[1] = 2;
36   EXPECT_EQ(2, get_default(m, 1, [] { return 42; }));
37   EXPECT_EQ(42, get_default(m, 2, [] { return 42; }));
38   EXPECT_EQ(0, get_default(m, 3));
39 }
40
41 TEST(MapUtil, get_or_throw) {
42   std::map<int, int> m;
43   m[1] = 2;
44   EXPECT_EQ(2, get_or_throw(m, 1));
45   EXPECT_THROW(get_or_throw(m, 2), std::out_of_range);
46   EXPECT_EQ(&m[1], &get_or_throw(m, 1));
47   get_or_throw(m, 1) = 3;
48   EXPECT_EQ(3, get_or_throw(m, 1));
49   const auto& cm = m;
50   EXPECT_EQ(&m[1], &get_or_throw(cm, 1));
51   EXPECT_EQ(3, get_or_throw(cm, 1));
52   EXPECT_THROW(get_or_throw(cm, 2), std::out_of_range);
53 }
54
55 TEST(MapUtil, get_or_throw_specified) {
56   std::map<int, int> m;
57   m[1] = 2;
58   EXPECT_EQ(2, get_or_throw<std::runtime_error>(m, 1));
59   EXPECT_THROW(get_or_throw<std::runtime_error>(m, 2), std::runtime_error);
60 }
61
62 TEST(MapUtil, get_optional) {
63   std::map<int, int> m;
64   m[1] = 2;
65   EXPECT_TRUE(get_optional(m, 1).hasValue());
66   EXPECT_EQ(2, get_optional(m, 1).value());
67   EXPECT_FALSE(get_optional(m, 2).hasValue());
68 }
69
70 TEST(MapUtil, get_ref_default) {
71   std::map<int, int> m;
72   m[1] = 2;
73   const int i = 42;
74   EXPECT_EQ(2, get_ref_default(m, 1, i));
75   EXPECT_EQ(42, get_ref_default(m, 2, i));
76   EXPECT_EQ(std::addressof(i), std::addressof(get_ref_default(m, 2, i)));
77 }
78
79 TEST(MapUtil, get_ref_default_function) {
80   std::map<int, int> m;
81   m[1] = 2;
82   const int i = 42;
83   EXPECT_EQ(2, get_ref_default(m, 1, [&i]() -> const int& { return i; }));
84   EXPECT_EQ(42, get_ref_default(m, 2, [&i]() -> const int& { return i; }));
85   EXPECT_EQ(
86       std::addressof(i),
87       std::addressof(
88           get_ref_default(m, 2, [&i]() -> const int& { return i; })));
89   // statically disallowed:
90   // get_ref_default(m, 2, [] { return 7; });
91 }
92
93 TEST(MapUtil, get_ptr) {
94   std::map<int, int> m;
95   m[1] = 2;
96   EXPECT_EQ(2, *get_ptr(m, 1));
97   EXPECT_TRUE(get_ptr(m, 2) == nullptr);
98   *get_ptr(m, 1) = 4;
99   EXPECT_EQ(4, m.at(1));
100 }