File indexing completed on 2024-04-06 12:12:29
0001 #include "catch.hpp"
0002
0003 #include "FWCore/Framework/interface/ESHandle.h"
0004 #include "FWCore/Utilities/interface/EDMException.h"
0005
0006 namespace {
0007 class TestExceptionFactory : public edm::ESHandleExceptionFactory {
0008 public:
0009 std::exception_ptr make() const override { return std::make_exception_ptr(edm::Exception(edm::errors::OtherCMS)); }
0010 };
0011 }
0012
0013 TEST_CASE("test edm::ESHandle", "[ESHandle]") {
0014 SECTION("Default constructor") {
0015 edm::ESHandle<int> handle;
0016 REQUIRE(not handle.isValid());
0017 REQUIRE(not handle.failedToGet());
0018 REQUIRE_THROWS_AS(handle.description(), edm::Exception);
0019 REQUIRE(handle.product() == nullptr);
0020 }
0021
0022 SECTION("Valid construction") {
0023 int const value = 42;
0024
0025 SECTION("without ComponentDescription") {
0026 edm::ESHandle<int> handle(&value);
0027 REQUIRE(not handle.isValid());
0028 REQUIRE(not handle.failedToGet());
0029 REQUIRE_THROWS_AS(handle.description(), edm::Exception);
0030 REQUIRE(handle.product() != nullptr);
0031 REQUIRE(*handle == value);
0032 }
0033
0034 SECTION("Valid construction, with ComponentDescription") {
0035 edm::eventsetup::ComponentDescription const desc;
0036 edm::ESHandle<int> handle(&value, &desc);
0037 REQUIRE(handle.isValid());
0038 REQUIRE(not handle.failedToGet());
0039 REQUIRE(handle.description() == &desc);
0040 REQUIRE(handle.product() != nullptr);
0041 REQUIRE(*handle == value);
0042 }
0043 }
0044
0045 SECTION("Construction for a 'failure'") {
0046 SECTION("From temporary factory object") {
0047 edm::ESHandle<int> handle(std::make_shared<TestExceptionFactory>());
0048 REQUIRE(not handle.isValid());
0049 REQUIRE(handle.failedToGet());
0050 REQUIRE_THROWS_AS(handle.description(), edm::Exception);
0051 REQUIRE_THROWS_AS(handle.product(), edm::Exception);
0052 }
0053
0054 SECTION("From another factory object") {
0055 auto const factory = std::make_shared<TestExceptionFactory>();
0056 edm::ESHandle<int> handle(factory);
0057 REQUIRE(not handle.isValid());
0058 REQUIRE(handle.failedToGet());
0059 REQUIRE_THROWS_AS(handle.description(), edm::Exception);
0060 REQUIRE_THROWS_AS(handle.product(), edm::Exception);
0061 REQUIRE(handle.whyFailedFactory().get() == factory.get());
0062 }
0063
0064 SECTION("From another ESHandle") {
0065 auto const factory = std::make_shared<TestExceptionFactory>();
0066 edm::ESHandle<int> handleA(factory);
0067 edm::ESHandle<int> handle(handleA.whyFailedFactory());
0068 REQUIRE(not handle.isValid());
0069 REQUIRE(handle.failedToGet());
0070 REQUIRE_THROWS_AS(handle.description(), edm::Exception);
0071 REQUIRE_THROWS_AS(handle.product(), edm::Exception);
0072 REQUIRE(handle.whyFailedFactory().get() == factory.get());
0073 }
0074 }
0075
0076 SECTION("Copying") {
0077 int const valueA = 42;
0078 edm::eventsetup::ComponentDescription const descA;
0079
0080 SECTION("From valid ESHandle") {
0081 edm::ESHandle<int> const handleA(&valueA, &descA);
0082
0083 SECTION("Constructor") {
0084 edm::ESHandle<int> handleB(handleA);
0085 REQUIRE(handleA.isValid());
0086 REQUIRE(*handleA == valueA);
0087 REQUIRE(handleB.isValid());
0088 REQUIRE(*handleB == valueA);
0089 }
0090
0091 SECTION("Assignment") {
0092 edm::ESHandle<int> handleB;
0093 REQUIRE(not handleB.isValid());
0094
0095 handleB = handleA;
0096 REQUIRE(handleA.isValid());
0097 REQUIRE(*handleA == valueA);
0098 REQUIRE(handleB.isValid());
0099 REQUIRE(*handleB == valueA);
0100 }
0101 }
0102
0103 SECTION("From invalid ESHandle") {
0104 edm::ESHandle<int> const handleA(std::make_shared<TestExceptionFactory>());
0105
0106 SECTION("Constructor") {
0107 edm::ESHandle<int> handleB(handleA);
0108 REQUIRE(not handleA.isValid());
0109 REQUIRE(handleA.failedToGet());
0110 REQUIRE_THROWS_AS(handleA.description(), edm::Exception);
0111 REQUIRE_THROWS_AS(handleA.product(), edm::Exception);
0112
0113 REQUIRE(not handleB.isValid());
0114 REQUIRE(handleB.failedToGet());
0115 REQUIRE_THROWS_AS(handleB.description(), edm::Exception);
0116 REQUIRE_THROWS_AS(handleB.product(), edm::Exception);
0117 }
0118
0119 SECTION("Assignment") {
0120 edm::ESHandle<int> handleB(&valueA, &descA);
0121 REQUIRE(handleB.isValid());
0122
0123 handleB = handleA;
0124 REQUIRE(not handleA.isValid());
0125 REQUIRE(handleA.failedToGet());
0126 REQUIRE_THROWS_AS(handleA.description(), edm::Exception);
0127 REQUIRE_THROWS_AS(handleA.product(), edm::Exception);
0128
0129 REQUIRE(not handleB.isValid());
0130 REQUIRE(handleB.failedToGet());
0131 REQUIRE_THROWS_AS(handleB.description(), edm::Exception);
0132 REQUIRE_THROWS_AS(handleB.product(), edm::Exception);
0133 }
0134 }
0135 }
0136
0137 SECTION("Moving") {
0138 int const valueA = 42;
0139 edm::eventsetup::ComponentDescription const descA;
0140
0141 SECTION("From valid ESHandle") {
0142 edm::ESHandle<int> handleA(&valueA, &descA);
0143
0144 SECTION("Constructor") {
0145 edm::ESHandle<int> handleB(std::move(handleA));
0146 REQUIRE(handleB.isValid());
0147 REQUIRE(*handleB == valueA);
0148 }
0149
0150 SECTION("Assignment") {
0151 edm::ESHandle<int> handleB;
0152 REQUIRE(not handleB.isValid());
0153
0154 handleB = std::move(handleA);
0155 REQUIRE(handleB.isValid());
0156 REQUIRE(*handleB == valueA);
0157 }
0158 }
0159
0160 SECTION("From invalid ESHandle") {
0161 edm::ESHandle<int> handleA(std::make_shared<TestExceptionFactory>());
0162
0163 SECTION("Constructor") {
0164 edm::ESHandle<int> handleB(std::move(handleA));
0165
0166
0167
0168 REQUIRE(not handleA.failedToGet());
0169
0170 REQUIRE(not handleB.isValid());
0171 REQUIRE(handleB.failedToGet());
0172 REQUIRE_THROWS_AS(handleB.description(), edm::Exception);
0173 REQUIRE_THROWS_AS(handleB.product(), edm::Exception);
0174 }
0175
0176 SECTION("Assignment") {
0177 edm::ESHandle<int> handleB(&valueA, &descA);
0178 REQUIRE(handleB.isValid());
0179
0180 handleB = std::move(handleA);
0181
0182
0183
0184 REQUIRE(not handleA.failedToGet());
0185
0186 REQUIRE(not handleB.isValid());
0187 REQUIRE(handleB.failedToGet());
0188 REQUIRE_THROWS_AS(handleB.description(), edm::Exception);
0189 REQUIRE_THROWS_AS(handleB.product(), edm::Exception);
0190 }
0191 }
0192 }
0193
0194 SECTION("Swap") {
0195 int const valueA = 42;
0196 edm::eventsetup::ComponentDescription const descA;
0197 edm::ESHandle<int> handleA(&valueA, &descA);
0198
0199 SECTION("With value") {
0200 int const valueB = 3;
0201 edm::ESHandle<int> handleB(&valueB);
0202
0203 std::swap(handleA, handleB);
0204 REQUIRE(not handleA.isValid());
0205 REQUIRE(handleB.isValid());
0206 REQUIRE(*handleB == valueA);
0207 }
0208
0209 SECTION("With failure factory") {
0210 auto factory = std::make_shared<TestExceptionFactory>();
0211 edm::ESHandle<int> handleB(factory);
0212
0213 std::swap(handleA, handleB);
0214 REQUIRE(not handleA.isValid());
0215 REQUIRE(handleA.failedToGet());
0216 REQUIRE(handleA.whyFailedFactory().get() == factory.get());
0217 REQUIRE(handleB.isValid());
0218 REQUIRE(*handleB == valueA);
0219 }
0220 }
0221 }