// Copyright 2021 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "src/core/lib/gprpp/table.h" #include #include #include #include "absl/types/optional.h" namespace grpc_core { namespace testing { TEST(Table, InstantiateEmpty) { Table<>(); } TEST(Table, NoOp) { Table t; EXPECT_EQ(t.get(), nullptr); EXPECT_EQ(t.get(), nullptr); EXPECT_EQ(t.get(), nullptr); EXPECT_EQ(t.get<0>(), nullptr); EXPECT_EQ(t.get<1>(), nullptr); EXPECT_EQ(t.get<2>(), nullptr); } TEST(Table, SetTheThings) { Table t; t.set(3); t.set(2.9); t.set("Hello world!"); EXPECT_EQ(*t.get(), 3); EXPECT_EQ(*t.get(), 2.9); EXPECT_EQ(*t.get(), "Hello world!"); EXPECT_EQ(*t.get<0>(), 3); EXPECT_EQ(*t.get<1>(), 2.9); EXPECT_EQ(*t.get<2>(), "Hello world!"); } TEST(Table, GetDefault) { Table t; EXPECT_EQ(*t.get_or_create(), ""); EXPECT_EQ(*t.get_or_create(), 0.0); EXPECT_EQ(*t.get_or_create(), 0); } TEST(Table, GetDefaultIndexed) { Table t; EXPECT_EQ(*t.get_or_create<2>(), ""); EXPECT_EQ(*t.get_or_create<1>(), 0.0); EXPECT_EQ(*t.get_or_create<0>(), 0); } TEST(Table, Copy) { Table t; t.set("abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(*t.get(), "abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(t.get(), nullptr); Table u(t); EXPECT_EQ(*u.get(), "abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(*t.get(), "abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(t.get(), nullptr); EXPECT_EQ(u.get(), nullptr); u.set("hello"); EXPECT_EQ(*u.get<1>(), "hello"); EXPECT_EQ(*t.get<1>(), "abcdefghijklmnopqrstuvwxyz"); t = u; EXPECT_EQ(*u.get(), "hello"); EXPECT_EQ(*t.get(), "hello"); } TEST(Table, Move) { Table t; t.set("abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(*t.get(), "abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(t.get(), nullptr); Table u(std::move(t)); EXPECT_NE(t.get(), nullptr); // NOLINT(bugprone-use-after-move) EXPECT_EQ(*u.get(), "abcdefghijklmnopqrstuvwxyz"); EXPECT_EQ(t.get(), nullptr); EXPECT_EQ(u.get(), nullptr); u.set("hello"); EXPECT_EQ(*u.get<1>(), "hello"); t = std::move(u); EXPECT_NE(u.get(), nullptr); // NOLINT(bugprone-use-after-move) EXPECT_EQ(*t.get(), "hello"); } TEST(Table, SameTypes) { Table t; // The following lines should not compile: // t.get(); // t.has<4>(); // t.get<4>(); // t.clear<4>(); EXPECT_EQ(t.get<0>(), nullptr); EXPECT_EQ(t.get<1>(), nullptr); EXPECT_EQ(t.get<2>(), nullptr); t.set<1>("Hello!"); EXPECT_EQ(t.get<0>(), nullptr); EXPECT_EQ(*t.get<1>(), "Hello!"); EXPECT_EQ(t.get<2>(), nullptr); } TEST(Table, ForEach) { Table t; t.set<0>(1); t.set<1>(2); t.set<2>(3); int i = 1; t.ForEach([&i](int x) { EXPECT_EQ(x, i); i++; }); } #if !defined(_MSC_VER) // Test suite proving this is memory efficient compared to // tuple...> // TODO(ctiller): determine why this test doesn't compile under MSVC. // For now whether it passes or not in that one environment is probably // immaterial. template struct TableSizeTest : public ::testing::Test {}; using SizeTests = ::testing::Types< std::tuple, std::tuple, std::tuple, std::tuple, std::tuple, std::tuple>; TYPED_TEST_SUITE(TableSizeTest, SizeTests); template int sizeof_tuple_of_optionals(std::tuple*) { return sizeof(std::tuple...>); } template int sizeof_table(std::tuple*) { return sizeof(Table); } TYPED_TEST(TableSizeTest, SmallerThanTupleOfOptionals) { EXPECT_GE(sizeof_tuple_of_optionals(static_cast(nullptr)), sizeof_table(static_cast(nullptr))); } #endif } // namespace testing } // namespace grpc_core int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }