1 /*
2 * Copyright (C) 2024 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 "androidfw/CombinedIterator.h"
18
19 #include <algorithm>
20 #include <string>
21 #include <strstream>
22 #include <utility>
23 #include <vector>
24
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27
28 namespace android {
29
30 template <class Coll>
toString(const Coll & coll)31 std::string toString(const Coll& coll) {
32 std::stringstream res;
33 res << "(" << std::size(coll) << ")";
34 if (std::size(coll)) {
35 res << "{" << coll[0];
36 for (int i = 1; i != std::size(coll); ++i) {
37 res << "," << coll[i];
38 }
39 res << "}";
40 }
41 return res.str();
42 }
43
44 template <class Coll>
AssertCollectionEq(const Coll & first,const Coll & second)45 void AssertCollectionEq(const Coll& first, const Coll& second) {
46 ASSERT_EQ(std::size(first), std::size(second))
47 << "first: " << toString(first) << ", second: " << toString(second);
48 for (int i = 0; i != std::size(first); ++i) {
49 ASSERT_EQ(first[i], second[i])
50 << "index: " << i << " first: " << toString(first) << ", second: " << toString(second);
51 }
52 }
53
TEST(CombinedIteratorTest,Sorting)54 TEST(CombinedIteratorTest, Sorting) {
55 std::vector<int> v1 = {2, 1, 3, 4, 0};
56 std::vector<int> v2 = {20, 10, 30, 40, 0};
57
58 std::sort(CombinedIterator(v1.begin(), v2.begin()), CombinedIterator(v1.end(), v2.end()));
59
60 ASSERT_EQ(v1.size(), v2.size());
61 ASSERT_TRUE(std::is_sorted(v1.begin(), v1.end()));
62 ASSERT_TRUE(std::is_sorted(v2.begin(), v2.end()));
63 AssertCollectionEq(v1, {0, 1, 2, 3, 4});
64 AssertCollectionEq(v2, {0, 10, 20, 30, 40});
65 }
66
TEST(CombinedIteratorTest,Removing)67 TEST(CombinedIteratorTest, Removing) {
68 std::vector<int> v1 = {1, 2, 3, 4, 5, 5, 5, 6};
69 std::vector<int> v2 = {10, 20, 30, 40, 50, 50, 50, 60};
70
71 auto newEnd =
72 std::remove_if(CombinedIterator(v1.begin(), v2.begin()), CombinedIterator(v1.end(), v2.end()),
73 [](auto&& pair) { return pair.first >= 3 && pair.first <= 5; });
74
75 ASSERT_EQ(newEnd.it1, v1.begin() + 3);
76 ASSERT_EQ(newEnd.it2, v2.begin() + 3);
77
78 v1.erase(newEnd.it1, v1.end());
79 AssertCollectionEq(v1, {1, 2, 6});
80 v2.erase(newEnd.it2, v2.end());
81 AssertCollectionEq(v2, {10, 20, 60});
82 }
83
TEST(CombinedIteratorTest,InplaceMerge)84 TEST(CombinedIteratorTest, InplaceMerge) {
85 std::vector<int> v1 = {1, 3, 4, 7, 2, 5, 6};
86 std::vector<int> v2 = {10, 30, 40, 70, 20, 50, 60};
87
88 std::inplace_merge(CombinedIterator(v1.begin(), v2.begin()),
89 CombinedIterator(v1.begin() + 4, v2.begin() + 4),
90 CombinedIterator(v1.end(), v2.end()));
91 ASSERT_TRUE(std::is_sorted(v1.begin(), v1.end()));
92 ASSERT_TRUE(std::is_sorted(v2.begin(), v2.end()));
93
94 AssertCollectionEq(v1, {1, 2, 3, 4, 5, 6, 7});
95 AssertCollectionEq(v2, {10, 20, 30, 40, 50, 60, 70});
96 }
97
98 } // namespace android
99