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"
16 #include <gtest/gtest.h>
19 #include "absl/types/optional.h"
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);
34 TEST(Table, SetTheThings) {
35 Table<int, double, std::string> t;
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!");
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);
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);
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");
75 EXPECT_EQ(*u.get<std::string>(), "hello");
76 EXPECT_EQ(*t.get<std::string>(), "hello");
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");
92 EXPECT_NE(u.get<std::string>(), nullptr); // NOLINT(bugprone-use-after-move)
93 EXPECT_EQ(*t.get<std::string>(), "hello");
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>();
103 EXPECT_EQ(t.get<0>(), nullptr);
104 EXPECT_EQ(t.get<1>(), nullptr);
105 EXPECT_EQ(t.get<2>(), nullptr);
107 EXPECT_EQ(t.get<0>(), nullptr);
108 EXPECT_EQ(*t.get<1>(), "Hello!");
109 EXPECT_EQ(t.get<2>(), nullptr);
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
119 template <typename T>
120 struct TableSizeTest : public ::testing::Test {};
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>>;
127 TYPED_TEST_SUITE(TableSizeTest, SizeTests);
129 template <typename... Ts>
130 int sizeof_tuple_of_optionals(std::tuple<Ts...>*) {
131 return sizeof(std::tuple<absl::optional<Ts>...>);
134 template <typename... Ts>
135 int sizeof_table(std::tuple<Ts...>*) {
136 return sizeof(Table<Ts...>);
139 TYPED_TEST(TableSizeTest, SmallerThanTupleOfOptionals) {
140 EXPECT_GE(sizeof_tuple_of_optionals(static_cast<TypeParam*>(nullptr)),
141 sizeof_table(static_cast<TypeParam*>(nullptr)));
145 } // namespace testing
146 } // namespace grpc_core
148 int main(int argc, char** argv) {
149 ::testing::InitGoogleTest(&argc, argv);
150 return RUN_ALL_TESTS();