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