0f3957bb13f1868c0a4b81a596c87349e268993e
[platform/upstream/grpc.git] / test / core / gprpp / table_test.cc
1 // Copyright 2021 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/table.h"
16 #include <gtest/gtest.h>
17 #include <string>
18 #include <tuple>
19 #include "absl/types/optional.h"
20
21 namespace grpc_core {
22 namespace testing {
23
24 TEST(Table, NoOp) {
25   Table<int, double, std::string> t;
26   EXPECT_EQ(t.get<int>(), nullptr);
27   EXPECT_EQ(t.get<double>(), nullptr);
28   EXPECT_EQ(t.get<std::string>(), nullptr);
29   EXPECT_EQ(t.get<0>(), nullptr);
30   EXPECT_EQ(t.get<1>(), nullptr);
31   EXPECT_EQ(t.get<2>(), nullptr);
32 }
33
34 TEST(Table, SetTheThings) {
35   Table<int, double, std::string> t;
36   t.set<int>(3);
37   t.set<double>(2.9);
38   t.set<std::string>("Hello world!");
39   EXPECT_EQ(*t.get<int>(), 3);
40   EXPECT_EQ(*t.get<double>(), 2.9);
41   EXPECT_EQ(*t.get<std::string>(), "Hello world!");
42   EXPECT_EQ(*t.get<0>(), 3);
43   EXPECT_EQ(*t.get<1>(), 2.9);
44   EXPECT_EQ(*t.get<2>(), "Hello world!");
45 }
46
47 TEST(Table, GetDefault) {
48   Table<int, double, std::string> t;
49   EXPECT_EQ(*t.get_or_create<std::string>(), "");
50   EXPECT_EQ(*t.get_or_create<double>(), 0.0);
51   EXPECT_EQ(*t.get_or_create<int>(), 0);
52 }
53
54 TEST(Table, GetDefaultIndexed) {
55   Table<int, double, std::string> t;
56   EXPECT_EQ(*t.get_or_create<2>(), "");
57   EXPECT_EQ(*t.get_or_create<1>(), 0.0);
58   EXPECT_EQ(*t.get_or_create<0>(), 0);
59 }
60
61 TEST(Table, Copy) {
62   Table<int, std::string> t;
63   t.set<std::string>("abcdefghijklmnopqrstuvwxyz");
64   EXPECT_EQ(*t.get<std::string>(), "abcdefghijklmnopqrstuvwxyz");
65   EXPECT_EQ(t.get<int>(), nullptr);
66   Table<int, std::string> u(t);
67   EXPECT_EQ(*u.get<std::string>(), "abcdefghijklmnopqrstuvwxyz");
68   EXPECT_EQ(*t.get<std::string>(), "abcdefghijklmnopqrstuvwxyz");
69   EXPECT_EQ(t.get<int>(), nullptr);
70   EXPECT_EQ(u.get<int>(), nullptr);
71   u.set<std::string>("hello");
72   EXPECT_EQ(*u.get<1>(), "hello");
73   EXPECT_EQ(*t.get<1>(), "abcdefghijklmnopqrstuvwxyz");
74   t = u;
75   EXPECT_EQ(*u.get<std::string>(), "hello");
76   EXPECT_EQ(*t.get<std::string>(), "hello");
77 }
78
79 TEST(Table, Move) {
80   Table<int, std::string> t;
81   t.set<std::string>("abcdefghijklmnopqrstuvwxyz");
82   EXPECT_EQ(*t.get<std::string>(), "abcdefghijklmnopqrstuvwxyz");
83   EXPECT_EQ(t.get<int>(), nullptr);
84   Table<int, std::string> u(std::move(t));
85   EXPECT_NE(t.get<std::string>(), nullptr);  // NOLINT(bugprone-use-after-move)
86   EXPECT_EQ(*u.get<std::string>(), "abcdefghijklmnopqrstuvwxyz");
87   EXPECT_EQ(t.get<int>(), nullptr);
88   EXPECT_EQ(u.get<int>(), nullptr);
89   u.set<std::string>("hello");
90   EXPECT_EQ(*u.get<1>(), "hello");
91   t = std::move(u);
92   EXPECT_NE(u.get<std::string>(), nullptr);  // NOLINT(bugprone-use-after-move)
93   EXPECT_EQ(*t.get<std::string>(), "hello");
94 }
95
96 TEST(Table, SameTypes) {
97   Table<std::string, std::string, std::string> t;
98   // The following lines should not compile:
99   // t.get<std::string>();
100   // t.has<4>();
101   // t.get<4>();
102   // t.clear<4>();
103   EXPECT_EQ(t.get<0>(), nullptr);
104   EXPECT_EQ(t.get<1>(), nullptr);
105   EXPECT_EQ(t.get<2>(), nullptr);
106   t.set<1>("Hello!");
107   EXPECT_EQ(t.get<0>(), nullptr);
108   EXPECT_EQ(*t.get<1>(), "Hello!");
109   EXPECT_EQ(t.get<2>(), nullptr);
110 }
111
112 #if !defined(_MSC_VER)
113 // Test suite proving this is memory efficient compared to
114 // tuple<optional<Ts>...>
115 // TODO(ctiller): determine why this test doesn't compile under MSVC.
116 // For now whether it passes or not in that one environment is probably
117 // immaterial.
118
119 template <typename T>
120 struct TableSizeTest : public ::testing::Test {};
121
122 using SizeTests = ::testing::Types<
123     std::tuple<char>, std::tuple<char, char>, std::tuple<char, char, char>,
124     std::tuple<int>, std::tuple<std::string>,
125     std::tuple<int, int, int, int, int, int, int, int, int, int>>;
126
127 TYPED_TEST_SUITE(TableSizeTest, SizeTests);
128
129 template <typename... Ts>
130 int sizeof_tuple_of_optionals(std::tuple<Ts...>*) {
131   return sizeof(std::tuple<absl::optional<Ts>...>);
132 }
133
134 template <typename... Ts>
135 int sizeof_table(std::tuple<Ts...>*) {
136   return sizeof(Table<Ts...>);
137 }
138
139 TYPED_TEST(TableSizeTest, SmallerThanTupleOfOptionals) {
140   EXPECT_GE(sizeof_tuple_of_optionals(static_cast<TypeParam*>(nullptr)),
141             sizeof_table(static_cast<TypeParam*>(nullptr)));
142 }
143 #endif
144
145 }  // namespace testing
146 }  // namespace grpc_core
147
148 int main(int argc, char** argv) {
149   ::testing::InitGoogleTest(&argc, argv);
150   return RUN_ALL_TESTS();
151 }