Debian Codegraph @

test/core/gprpp/table_test.cc

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