Skip to content

Commit

Permalink
test(shared_ptr): FieldLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcakircali committed Aug 15, 2024
1 parent 365b0ab commit c509fec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/fdb/database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
65 changes: 65 additions & 0 deletions tests/fdb/database/test_fieldlocation.cc
Original file line number Diff line number Diff line change
@@ -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 <memory>

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::FieldLocation>(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::FieldLocation>(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);
}

0 comments on commit c509fec

Please sign in to comment.