From c509fec00cfd3cd6e3832a048b1583365f81fd60 Mon Sep 17 00:00:00 2001 From: Metin Cakircali Date: Thu, 15 Aug 2024 10:59:05 +0200 Subject: [PATCH] test(shared_ptr): FieldLocation --- tests/fdb/database/CMakeLists.txt | 5 ++ tests/fdb/database/test_fieldlocation.cc | 65 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/fdb/database/test_fieldlocation.cc diff --git a/tests/fdb/database/CMakeLists.txt b/tests/fdb/database/CMakeLists.txt index 1bc08100f..e8486544b 100644 --- a/tests/fdb/database/CMakeLists.txt +++ b/tests/fdb/database/CMakeLists.txt @@ -7,3 +7,8 @@ ecbuild_add_test( TARGET test_fdb5_database_indexaxis INCLUDES ${PMEM_INCLUDE_DIRS} LIBS fdb5 ENVIRONMENT "${_test_environment}") + +ecbuild_add_test( TARGET test_fdb5_fieldlocation + SOURCES test_fieldlocation.cc + LIBS fdb5 + ENVIRONMENT "${_test_environment}") diff --git a/tests/fdb/database/test_fieldlocation.cc b/tests/fdb/database/test_fieldlocation.cc new file mode 100644 index 000000000..a9df50af6 --- /dev/null +++ b/tests/fdb/database/test_fieldlocation.cc @@ -0,0 +1,65 @@ +/* + * (C) Copyright 1996- ECMWF. + * + * This software is licensed under the terms of the Apache Licence Version 2.0 + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. + * In applying this licence, ECMWF does not waive the privileges and immunities + * granted to it by virtue of its status as an intergovernmental organisation nor + * does it submit to any jurisdiction. + */ + +#include "fdb5/database/FieldLocation.h" + +#include "eckit/filesystem/URI.h" +#include "eckit/testing/Test.h" + +#include + +using namespace eckit; +using namespace eckit::testing; + +namespace fdb::test { + +//---------------------------------------------------------------------------------------------------------------------- + +CASE("FieldLocation - shared_ptr") { + URI uri {"dummy_uri"}; + + // GOOD (not best) + { + auto location = std::shared_ptr(fdb5::FieldLocationFactory::instance().build("file", uri)); + + auto loc1 = location->sharedPtr(); + EXPECT_EQUAL(loc1.use_count(), 2); + + auto loc2 = location->sharedPtr(); + EXPECT_EQUAL(loc2.use_count(), 3); + + // check that the shared pointers are the same + EXPECT_EQUAL(loc2, loc1); + + { + auto loc3 = location->sharedPtr(); + EXPECT_EQUAL(loc3.use_count(), 4); + EXPECT_EQUAL(loc3, loc1); + } + + auto loc4 = location->sharedPtr(); + EXPECT_EQUAL(loc4.use_count(), 4); + EXPECT_EQUAL(loc4, loc1); + } + + // BAD: this is a how NOT to use shared_ptr on FieldLocation + { + auto location = std::unique_ptr(fdb5::FieldLocationFactory::instance().build("file", uri)); + EXPECT_THROWS_AS(location->sharedPtr(), std::bad_weak_ptr); + } +} + +//---------------------------------------------------------------------------------------------------------------------- + +} // namespace fdb::test + +int main(int argc, char** argv) { + return run_tests(argc, argv); +}