1 // Copyright 2021 gRPC authors.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "src/core/lib/gprpp/table.h"
20 #include <gtest/gtest.h>
22 #include "absl/types/optional.h"
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);
37 TEST(Table, SetTheThings) {
38 Table<int, double, std::string> t;
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!");
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);
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);
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");
78 EXPECT_EQ(*u.get<std::string>(), "hello");
79 EXPECT_EQ(*t.get<std::string>(), "hello");
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");
95 EXPECT_NE(u.get<std::string>(), nullptr); // NOLINT(bugprone-use-after-move)
96 EXPECT_EQ(*t.get<std::string>(), "hello");
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>();
106 EXPECT_EQ(t.get<0>(), nullptr);
107 EXPECT_EQ(t.get<1>(), nullptr);
108 EXPECT_EQ(t.get<2>(), nullptr);
110 EXPECT_EQ(t.get<0>(), nullptr);
111 EXPECT_EQ(*t.get<1>(), "Hello!");
112 EXPECT_EQ(t.get<2>(), nullptr);
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
122 template <typename T>
123 struct TableSizeTest : public ::testing::Test {};
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>>;
130 TYPED_TEST_SUITE(TableSizeTest, SizeTests);
132 template <typename... Ts>
133 int sizeof_tuple_of_optionals(std::tuple<Ts...>*) {
134 return sizeof(std::tuple<absl::optional<Ts>...>);
137 template <typename... Ts>
138 int sizeof_table(std::tuple<Ts...>*) {
139 return sizeof(Table<Ts...>);
142 TYPED_TEST(TableSizeTest, SmallerThanTupleOfOptionals) {
143 EXPECT_GE(sizeof_tuple_of_optionals(static_cast<TypeParam*>(nullptr)),
144 sizeof_table(static_cast<TypeParam*>(nullptr)));
148 } // namespace testing
149 } // namespace grpc_core
151 int main(int argc, char** argv) {
152 ::testing::InitGoogleTest(&argc, argv);
153 return RUN_ALL_TESTS();