/Users/deen/code/yugabyte-db/src/yb/util/map-util-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | // This unit test belongs in gutil, but it depends on test_main which is |
34 | | // part of util. |
35 | | #include "yb/gutil/map-util.h" |
36 | | |
37 | | |
38 | | #include <gtest/gtest.h> |
39 | | |
40 | | using std::map; |
41 | | using std::string; |
42 | | using std::unique_ptr; |
43 | | |
44 | | namespace yb { |
45 | | |
46 | 1 | TEST(FloorTest, TestMapUtil) { |
47 | 1 | map<int, int> my_map; |
48 | | |
49 | 1 | ASSERT_EQ(nullptr, FindFloorOrNull(my_map, 5)); |
50 | | |
51 | 1 | my_map[5] = 5; |
52 | 1 | ASSERT_EQ(5, *FindFloorOrNull(my_map, 6)); |
53 | 1 | ASSERT_EQ(5, *FindFloorOrNull(my_map, 5)); |
54 | 1 | ASSERT_EQ(nullptr, FindFloorOrNull(my_map, 4)); |
55 | | |
56 | 1 | my_map[1] = 1; |
57 | 1 | ASSERT_EQ(5, *FindFloorOrNull(my_map, 6)); |
58 | 1 | ASSERT_EQ(5, *FindFloorOrNull(my_map, 5)); |
59 | 1 | ASSERT_EQ(1, *FindFloorOrNull(my_map, 4)); |
60 | 1 | ASSERT_EQ(1, *FindFloorOrNull(my_map, 1)); |
61 | 1 | ASSERT_EQ(nullptr, FindFloorOrNull(my_map, 0)); |
62 | | |
63 | 1 | } |
64 | | |
65 | 1 | TEST(EmplaceTest, TestEmplace) { |
66 | | // Map with move-only value type. |
67 | 1 | map<string, unique_ptr<string>> my_map; |
68 | 1 | unique_ptr<string> val(new string("foo")); |
69 | 1 | ASSERT_TRUE(EmplaceIfNotPresent(&my_map, "k", std::move(val))); |
70 | 1 | ASSERT_TRUE(ContainsKey(my_map, "k")); |
71 | 2 | ASSERT_FALSE(EmplaceIfNotPresent(&my_map, "k", nullptr)) |
72 | 2 | << "Should return false for already-present"; |
73 | 1 | } |
74 | | |
75 | | } // namespace yb |