From 8f464e7f68fd9d50ed39b2866ef8dac9c837439d Mon Sep 17 00:00:00 2001 From: Victor Costan Date: Mon, 3 Jan 2022 20:57:56 +0000 Subject: [PATCH] Remove main() from most tests. This gives some flexibility to embedders. Currently, embedders have to build a binary for each test file. After this CL, embedders can still choose to have a binary for each test file, by linking each test file with a googletest target that includes main() (usually "gtest_main"). Embedders can also choose to build a single binary for almost all test files, and link with a googletest target that includes main(). The latter is more convenient for projects that have very few test binaries, like Chromium. PiperOrigin-RevId: 419470798 --- CMakeLists.txt | 88 ++++++++++++++++++++++++++----------------- db/autocompact_test.cc | 5 --- db/corruption_test.cc | 5 --- db/db_test.cc | 5 --- db/dbformat_test.cc | 5 --- db/fault_injection_test.cc | 5 --- db/filename_test.cc | 5 --- db/log_test.cc | 5 --- db/recovery_test.cc | 5 --- db/skiplist_test.cc | 5 --- db/version_edit_test.cc | 5 --- db/version_set_test.cc | 5 --- db/write_batch_test.cc | 5 --- helpers/memenv/memenv_test.cc | 5 --- issues/issue178_test.cc | 5 --- issues/issue200_test.cc | 5 --- issues/issue320_test.cc | 5 --- table/filter_block_test.cc | 5 --- table/table_test.cc | 5 --- util/arena_test.cc | 5 --- util/bloom_test.cc | 5 --- util/cache_test.cc | 5 --- util/coding_test.cc | 5 --- util/crc32c_test.cc | 5 --- util/env_test.cc | 5 --- util/hash_test.cc | 5 --- util/logging_test.cc | 5 --- util/no_destructor_test.cc | 5 --- util/status_test.cc | 5 --- 29 files changed, 54 insertions(+), 174 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7690302..b829c94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -306,6 +306,60 @@ if(LEVELDB_BUILD_TESTS) APPEND PROPERTY COMPILE_OPTIONS -Wno-missing-field-initializers) endif(LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS) + add_executable(leveldb_tests "") + target_sources(leveldb_tests + PRIVATE + # "db/fault_injection_test.cc" + # "issues/issue178_test.cc" + # "issues/issue200_test.cc" + # "issues/issue320_test.cc" + "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h" + # "util/env_test.cc" + "util/status_test.cc" + "util/no_destructor_test.cc" + "util/testutil.cc" + "util/testutil.h" + ) + if(NOT BUILD_SHARED_LIBS) + target_sources(leveldb_tests + PRIVATE + "db/autocompact_test.cc" + "db/corruption_test.cc" + "db/db_test.cc" + "db/dbformat_test.cc" + "db/filename_test.cc" + "db/log_test.cc" + "db/recovery_test.cc" + "db/skiplist_test.cc" + "db/version_edit_test.cc" + "db/version_set_test.cc" + "db/write_batch_test.cc" + "helpers/memenv/memenv_test.cc" + "table/filter_block_test.cc" + "table/table_test.cc" + "util/arena_test.cc" + "util/bloom_test.cc" + "util/cache_test.cc" + "util/coding_test.cc" + "util/crc32c_test.cc" + "util/hash_test.cc" + "util/logging_test.cc" + ) + endif(NOT BUILD_SHARED_LIBS) + target_link_libraries(leveldb_tests leveldb gmock gtest gtest_main) + target_compile_definitions(leveldb_tests + PRIVATE + ${LEVELDB_PLATFORM_NAME}=1 + ) + if (NOT HAVE_CXX17_HAS_INCLUDE) + target_compile_definitions(leveldb_tests + PRIVATE + LEVELDB_HAS_PORT_CONFIG_H=1 + ) + endif(NOT HAVE_CXX17_HAS_INCLUDE) + + add_test(NAME "leveldb_tests" COMMAND "leveldb_tests") + function(leveldb_test test_file) get_filename_component(test_target_name "${test_file}" NAME_WE) @@ -334,42 +388,8 @@ if(LEVELDB_BUILD_TESTS) endfunction(leveldb_test) leveldb_test("db/c_test.c") - leveldb_test("db/fault_injection_test.cc") - - leveldb_test("issues/issue178_test.cc") - leveldb_test("issues/issue200_test.cc") - leveldb_test("issues/issue320_test.cc") - - leveldb_test("util/env_test.cc") - leveldb_test("util/status_test.cc") - leveldb_test("util/no_destructor_test.cc") if(NOT BUILD_SHARED_LIBS) - leveldb_test("db/autocompact_test.cc") - leveldb_test("db/corruption_test.cc") - leveldb_test("db/db_test.cc") - leveldb_test("db/dbformat_test.cc") - leveldb_test("db/filename_test.cc") - leveldb_test("db/log_test.cc") - leveldb_test("db/recovery_test.cc") - leveldb_test("db/skiplist_test.cc") - leveldb_test("db/version_edit_test.cc") - leveldb_test("db/version_set_test.cc") - leveldb_test("db/write_batch_test.cc") - - leveldb_test("helpers/memenv/memenv_test.cc") - - leveldb_test("table/filter_block_test.cc") - leveldb_test("table/table_test.cc") - - leveldb_test("util/arena_test.cc") - leveldb_test("util/bloom_test.cc") - leveldb_test("util/cache_test.cc") - leveldb_test("util/coding_test.cc") - leveldb_test("util/crc32c_test.cc") - leveldb_test("util/hash_test.cc") - leveldb_test("util/logging_test.cc") - # TODO(costan): This test also uses # "util/env_{posix|windows}_test_helper.h" if (WIN32) diff --git a/db/autocompact_test.cc b/db/autocompact_test.cc index 3b7241b..69341e3 100644 --- a/db/autocompact_test.cc +++ b/db/autocompact_test.cc @@ -108,8 +108,3 @@ TEST_F(AutoCompactTest, ReadAll) { DoReads(kCount); } TEST_F(AutoCompactTest, ReadHalf) { DoReads(kCount / 2); } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/corruption_test.cc b/db/corruption_test.cc index a31f448..dc7da76 100644 --- a/db/corruption_test.cc +++ b/db/corruption_test.cc @@ -360,8 +360,3 @@ TEST_F(CorruptionTest, UnrelatedKeys) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/db_test.cc b/db/db_test.cc index 7f22688..9bd6e14 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -2295,8 +2295,3 @@ TEST_F(DBTest, Randomized) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/dbformat_test.cc b/db/dbformat_test.cc index 4a11c4a..7f3f81a 100644 --- a/db/dbformat_test.cc +++ b/db/dbformat_test.cc @@ -126,8 +126,3 @@ TEST(FormatTest, InternalKeyDebugString) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/fault_injection_test.cc b/db/fault_injection_test.cc index 6eebafa..ef864a4 100644 --- a/db/fault_injection_test.cc +++ b/db/fault_injection_test.cc @@ -548,8 +548,3 @@ TEST_F(FaultInjectionTest, FaultTestWithLogReuse) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/filename_test.cc b/db/filename_test.cc index f291d72..9ac0111 100644 --- a/db/filename_test.cc +++ b/db/filename_test.cc @@ -125,8 +125,3 @@ TEST(FileNameTest, Construction) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/log_test.cc b/db/log_test.cc index 346b19c..d55d4dd 100644 --- a/db/log_test.cc +++ b/db/log_test.cc @@ -556,8 +556,3 @@ TEST_F(LogTest, ReadPastEnd) { CheckOffsetPastEndReturnsNoRecords(5); } } // namespace log } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/recovery_test.cc b/db/recovery_test.cc index 6c5d42e..1d9f621 100644 --- a/db/recovery_test.cc +++ b/db/recovery_test.cc @@ -332,8 +332,3 @@ TEST_F(RecoveryTest, ManifestMissing) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/skiplist_test.cc b/db/skiplist_test.cc index 79a5b86..1d355cb 100644 --- a/db/skiplist_test.cc +++ b/db/skiplist_test.cc @@ -366,8 +366,3 @@ TEST(SkipTest, Concurrent4) { RunConcurrent(4); } TEST(SkipTest, Concurrent5) { RunConcurrent(5); } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/version_edit_test.cc b/db/version_edit_test.cc index acafab0..a108c15 100644 --- a/db/version_edit_test.cc +++ b/db/version_edit_test.cc @@ -39,8 +39,3 @@ TEST(VersionEditTest, EncodeDecode) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/version_set_test.cc b/db/version_set_test.cc index dee6b4c..64bb983 100644 --- a/db/version_set_test.cc +++ b/db/version_set_test.cc @@ -329,8 +329,3 @@ TEST_F(AddBoundaryInputsTest, TestDisjoinFilePointers) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/db/write_batch_test.cc b/db/write_batch_test.cc index 64df9b8..1a3ea8f 100644 --- a/db/write_batch_test.cc +++ b/db/write_batch_test.cc @@ -130,8 +130,3 @@ TEST(WriteBatchTest, ApproximateSize) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/helpers/memenv/memenv_test.cc b/helpers/memenv/memenv_test.cc index 3f03cb6..909a0ca 100644 --- a/helpers/memenv/memenv_test.cc +++ b/helpers/memenv/memenv_test.cc @@ -257,8 +257,3 @@ TEST_F(MemEnvTest, DBTest) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/issues/issue178_test.cc b/issues/issue178_test.cc index 8fa5bb9..5cd5862 100644 --- a/issues/issue178_test.cc +++ b/issues/issue178_test.cc @@ -83,8 +83,3 @@ TEST(Issue178, Test) { } } // anonymous namespace - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/issues/issue200_test.cc b/issues/issue200_test.cc index 4eba23a..959b371 100644 --- a/issues/issue200_test.cc +++ b/issues/issue200_test.cc @@ -52,8 +52,3 @@ TEST(Issue200, Test) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/issues/issue320_test.cc b/issues/issue320_test.cc index c08296a..9d7fa7b 100644 --- a/issues/issue320_test.cc +++ b/issues/issue320_test.cc @@ -124,8 +124,3 @@ TEST(Issue320, Test) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/table/filter_block_test.cc b/table/filter_block_test.cc index 91a6be2..3ee41cf 100644 --- a/table/filter_block_test.cc +++ b/table/filter_block_test.cc @@ -120,8 +120,3 @@ TEST_F(FilterBlockTest, MultiChunk) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/table/table_test.cc b/table/table_test.cc index 190dd0f..7f0f998 100644 --- a/table/table_test.cc +++ b/table/table_test.cc @@ -827,8 +827,3 @@ TEST(TableTest, ApproximateOffsetOfCompressed) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/arena_test.cc b/util/arena_test.cc index 90226fe..3e2011e 100644 --- a/util/arena_test.cc +++ b/util/arena_test.cc @@ -59,8 +59,3 @@ TEST(ArenaTest, Simple) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/bloom_test.cc b/util/bloom_test.cc index 520473e..9f11108 100644 --- a/util/bloom_test.cc +++ b/util/bloom_test.cc @@ -152,8 +152,3 @@ TEST_F(BloomTest, VaryingLengths) { // Different bits-per-byte } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/cache_test.cc b/util/cache_test.cc index 79cfc27..e68da34 100644 --- a/util/cache_test.cc +++ b/util/cache_test.cc @@ -222,8 +222,3 @@ TEST_F(CacheTest, ZeroSizeCache) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/coding_test.cc b/util/coding_test.cc index aa6c748..cceda14 100644 --- a/util/coding_test.cc +++ b/util/coding_test.cc @@ -191,8 +191,3 @@ TEST(Coding, Strings) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/crc32c_test.cc b/util/crc32c_test.cc index 647e561..2fe1c41 100644 --- a/util/crc32c_test.cc +++ b/util/crc32c_test.cc @@ -54,8 +54,3 @@ TEST(CRC, Mask) { } // namespace crc32c } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/env_test.cc b/util/env_test.cc index 491ef43..fc69d71 100644 --- a/util/env_test.cc +++ b/util/env_test.cc @@ -233,8 +233,3 @@ TEST_F(EnvTest, ReopenAppendableFile) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/hash_test.cc b/util/hash_test.cc index 6d6771f..0ea5977 100644 --- a/util/hash_test.cc +++ b/util/hash_test.cc @@ -39,8 +39,3 @@ TEST(HASH, SignedUnsignedIssue) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/logging_test.cc b/util/logging_test.cc index 24e1fe9..1746c57 100644 --- a/util/logging_test.cc +++ b/util/logging_test.cc @@ -138,8 +138,3 @@ TEST(Logging, ConsumeDecimalNumberNoDigits) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/no_destructor_test.cc b/util/no_destructor_test.cc index 68fdfee..e3602cc 100644 --- a/util/no_destructor_test.cc +++ b/util/no_destructor_test.cc @@ -42,8 +42,3 @@ TEST(NoDestructorTest, StaticInstance) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/util/status_test.cc b/util/status_test.cc index 914b386..dbf5faa 100644 --- a/util/status_test.cc +++ b/util/status_test.cc @@ -37,8 +37,3 @@ TEST(Status, MoveConstructor) { } } // namespace leveldb - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -}