a538012dcfa9a24224dcee8aa703c6061fd1348d
[folly.git] / folly / test / MergeTest.cpp
1 /*
2  * Copyright 2014 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/Merge.h"
18 #include <gtest/gtest.h>
19 #include <map>
20 #include <vector>
21
22 TEST(MergeTest, NonOverlapping) {
23   std::vector<int> a = {0, 2, 4, 6};
24   std::vector<int> b = {1, 3, 5, 7};
25   std::vector<int> c;
26
27   folly::merge(a.begin(), a.end(),
28                b.begin(), b.end(),
29                std::back_inserter(c));
30   EXPECT_EQ(8, c.size());
31   for (int i = 0; i < 8; ++i) {
32     EXPECT_EQ(i, c[i]);
33   }
34 }
35
36 TEST(MergeTest, OverlappingInSingleInputRange) {
37   std::vector<std::pair<int, int>> a = {{0, 0}, {0, 1}};
38   std::vector<std::pair<int, int>> b = {{2, 2}, {3, 3}};
39   std::map<int, int> c;
40
41   folly::merge(a.begin(), a.end(),
42                b.begin(), b.end(),
43                std::inserter(c, c.begin()));
44   EXPECT_EQ(3, c.size());
45
46   // First value is inserted, second is not
47   EXPECT_EQ(c[0], 0);
48
49   EXPECT_EQ(c[2], 2);
50   EXPECT_EQ(c[3], 3);
51 }
52
53 TEST(MergeTest, OverlappingInDifferentInputRange) {
54   std::vector<std::pair<int, int>> a = {{0, 0}, {1, 1}};
55   std::vector<std::pair<int, int>> b = {{0, 2}, {3, 3}};
56   std::map<int, int> c;
57
58   folly::merge(a.begin(), a.end(),
59                b.begin(), b.end(),
60                std::inserter(c, c.begin()));
61   EXPECT_EQ(3, c.size());
62
63   // Value from a is inserted, value from b is not.
64   EXPECT_EQ(c[0], 0);
65
66   EXPECT_EQ(c[1], 1);
67   EXPECT_EQ(c[3], 3);
68 }