1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/gprpp/sorted_pack.h"
16
17 #include <vector>
18
19 #include "gtest/gtest.h"
20
21 using grpc_core::WithSortedPack;
22
23 template <int I>
24 struct Int {
valueInt25 int value() const { return I; }
26 };
27
28 template <typename A, typename B>
29 struct Cmp;
30
31 template <int A, int B>
32 struct Cmp<Int<A>, Int<B>> {
33 static constexpr bool kValue = A < B;
34 };
35
36 template <typename... Args>
37 struct VecMaker {
MakeVecMaker38 static std::vector<int> Make() { return {Args().value()...}; }
39 };
40
41 template <int... Args>
TestVec()42 std::vector<int> TestVec() {
43 return WithSortedPack<VecMaker, Cmp, Int<Args>...>::Type::Make();
44 }
45
TEST(SortedPackTest,Empty)46 TEST(SortedPackTest, Empty) { EXPECT_EQ(TestVec<>(), std::vector<int>{}); }
47
TEST(SortedPackTest,LenOne)48 TEST(SortedPackTest, LenOne) {
49 EXPECT_EQ((TestVec<1>()), (std::vector<int>{1}));
50 EXPECT_EQ((TestVec<2>()), (std::vector<int>{2}));
51 }
52
TEST(SortedPackTest,Len2)53 TEST(SortedPackTest, Len2) {
54 EXPECT_EQ((TestVec<1, 2>()), (std::vector<int>{1, 2}));
55 EXPECT_EQ((TestVec<2, 1>()), (std::vector<int>{1, 2}));
56 }
57
TEST(SortedPackTest,Len3)58 TEST(SortedPackTest, Len3) {
59 EXPECT_EQ((TestVec<1, 2, 3>()), (std::vector<int>{1, 2, 3}));
60 EXPECT_EQ((TestVec<1, 3, 2>()), (std::vector<int>{1, 2, 3}));
61 EXPECT_EQ((TestVec<2, 1, 3>()), (std::vector<int>{1, 2, 3}));
62 EXPECT_EQ((TestVec<2, 3, 1>()), (std::vector<int>{1, 2, 3}));
63 EXPECT_EQ((TestVec<3, 1, 2>()), (std::vector<int>{1, 2, 3}));
64 EXPECT_EQ((TestVec<3, 2, 1>()), (std::vector<int>{1, 2, 3}));
65 }
66
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68 ::testing::InitGoogleTest(&argc, argv);
69 return RUN_ALL_TESTS();
70 }
71