Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:41

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 
0006 #include "catch.hpp"
0007 
0008 #include <JANA/JEvent.h>
0009 #include "JEventTests.h"
0010 
0011 
0012 TEST_CASE("JEventInsertTests") {
0013 
0014 
0015     auto event = std::make_shared<JEvent>();
0016 
0017     SECTION("Single-item JEvent::Insert() can be retrieved via JEvent::Get()") {
0018         auto input = new FakeJObject(22);
0019         event->Insert(input);
0020         auto output = event->GetSingle<FakeJObject>();
0021         REQUIRE(output->datum == input->datum);
0022 
0023         const FakeJObject* alternative;
0024         event->Get(&alternative);
0025         REQUIRE(output->datum == alternative->datum);
0026 
0027     }
0028 
0029     SECTION("Multi-item JEvent::Insert() can be retrieved via JEvent::Get()") {
0030         std::vector<FakeJObject*> input;          // const is optional for input
0031         input.push_back(new FakeJObject(1));
0032         input.push_back(new FakeJObject(2));
0033         input.push_back(new FakeJObject(3));
0034         event->Insert(input);
0035 
0036         std::vector<const FakeJObject*> output;   // const is required for output
0037         event->Get(output);
0038 
0039         REQUIRE(output.size() == input.size());
0040         for (size_t i=0; i<output.size(); ++i) {
0041             REQUIRE(output[i]->datum == input[i]->datum);
0042         }
0043     }
0044 
0045     SECTION("JEvent::Insert() respects both tag and typeid") {
0046         event->Insert(new FakeJObject(22), "first_tag");
0047         event->Insert(new FakeJObject(49), "second_tag");
0048         event->Insert(new DifferentFakeJObject(7.6), "first_tag");
0049 
0050         std::vector<const FakeJObject*> output;
0051 
0052         event->Get(output, "first_tag");
0053         REQUIRE(output.size() == 1);
0054         REQUIRE(output[0]->datum == 22);
0055 
0056         output.clear();
0057         event->Get(output, "second_tag");
0058         REQUIRE(output.size() == 1);
0059         REQUIRE(output[0]->datum == 49);
0060 
0061         std::vector<const DifferentFakeJObject*> different_output;
0062 
0063         event->Get(different_output, "first_tag");
0064         REQUIRE(different_output.size() == 1);
0065         REQUIRE(different_output[0]->sample == 7.6);
0066     }
0067 
0068     SECTION("Repeated calls to JEvent::Insert() aggregate") {
0069 
0070         event->Insert(new FakeJObject(22), "first_tag");
0071         event->Insert(new FakeJObject(49), "first_tag");
0072         event->Insert(new FakeJObject(618), "first_tag");
0073 
0074         std::vector<const FakeJObject*> output;
0075         event->Get(output, "first_tag");
0076         REQUIRE(output.size() == 3);
0077         REQUIRE(output[0]->datum == 22);
0078         REQUIRE(output[1]->datum == 49);
0079         REQUIRE(output[2]->datum == 618);
0080     }
0081 
0082     SECTION("Inserted JObjects get deleted when enclosing JEvent does") {
0083         FakeJObject* obj = new FakeJObject(618);
0084         bool deleted = false;
0085         obj->deleted = &deleted;
0086         JEvent* event_ptr = new JEvent();
0087         event_ptr->Insert(obj, "tag");
0088         REQUIRE(deleted == false);
0089         delete event_ptr;
0090         REQUIRE(deleted == true);
0091     }
0092 
0093 
0094     SECTION("JEvent::GetFactory<T> handles missing factories correctly") {
0095 
0096         // When present, GetFactory<T> returns a pointer to the correct factory (dummy or otherwise)
0097         event->Insert(new FakeJObject(22), "present_tag");
0098         auto present_factory = event->GetFactory<FakeJObject>("present_tag");
0099         REQUIRE(present_factory != nullptr);
0100 
0101         // By default, return nullptr for compatibility with older code
0102         auto missing_factory = event->GetFactory<FakeJObject>("absent_tag");
0103         REQUIRE(missing_factory == nullptr);
0104 
0105         // This is effected via the throw_on_missing parameter
0106         missing_factory = event->GetFactory<FakeJObject>("absent_tag", false);
0107         REQUIRE(missing_factory == nullptr);
0108 
0109         // GetFactory<T> can conveniently throw an exception if factory is missing.
0110         // This is useful when said data is required
0111         try {
0112             event->GetFactory<FakeJObject>("absent_tag", true);
0113             REQUIRE(0 == 1); // Shouldn't reach this point
0114         }
0115         catch (const JException& ex) {
0116             // Hide the stack trace by default if the error can be trivially traced back to a JFactory
0117             REQUIRE(ex.show_stacktrace == false);
0118         }
0119     }
0120 
0121     // -----------------
0122     // C-style GetSingle
0123     // -----------------
0124     SECTION("JEvent::Get(Single) updates destination to null when factory is present but empty") {
0125         std::vector<FakeJObject*> objects;
0126         event->Insert(objects);
0127         const FakeJObject* result;
0128         JFactory* factory = event->Get(&result);
0129         REQUIRE(result == nullptr);
0130         REQUIRE(factory != nullptr);
0131     }
0132 
0133     SECTION("JEvent::Get(Single) throws an exception when factory is missing") {
0134         const FakeJObject* result;
0135         REQUIRE_THROWS(event->Get(&result));
0136     }
0137 
0138     SECTION("JEvent::Get(Single) updates destination to point to the only object when the factory contains only one object") {
0139         auto inserted = new FakeJObject(22);
0140         event->Insert(inserted);
0141         const FakeJObject* result;
0142         auto factory = event->Get(&result);
0143         REQUIRE(result->datum == inserted->datum);      // Same contents
0144         REQUIRE(result == inserted);                    // Same pointer
0145         REQUIRE(factory != nullptr);                    // Factory exists
0146     }
0147 
0148     SECTION("JEvent::Get(Single) returns the first object when the factory contains multiple objects") {
0149         auto first = new FakeJObject(22);
0150         event->Insert(first);
0151         event->Insert(new FakeJObject(99));
0152         event->Insert(new FakeJObject(42));
0153         const FakeJObject* result;
0154         auto factory = event->Get(&result);
0155         REQUIRE(result->datum == first->datum);      // Same contents
0156         REQUIRE(result == first);                    // Same pointer
0157         REQUIRE(factory != nullptr);                    // Factory exists
0158     }
0159 
0160         // ---------
0161         // GetSingle
0162     // ---------
0163 
0164     SECTION("JEvent::GetSingle returns null when factory is present but empty") {
0165         std::vector<FakeJObject*> objects;
0166         event->Insert(objects);
0167         auto object = event->GetSingle<FakeJObject>();
0168         REQUIRE(object == nullptr);
0169     }
0170 
0171     SECTION("JEvent::GetSingle throws an exception when factory is missing") {
0172         REQUIRE_THROWS(event->GetSingle<FakeJObject>());
0173     }
0174 
0175     SECTION("JEvent::GetSingle returns the only object when the factory contains only one object") {
0176         auto inserted = new FakeJObject(22);
0177         event->Insert(inserted);
0178         auto retrieved = event->GetSingle<FakeJObject>();
0179         REQUIRE(retrieved->datum == inserted->datum);      // Same contents
0180         REQUIRE(retrieved == inserted);                    // Same pointer
0181     }
0182 
0183     SECTION("JEvent::GetSingle returns the first object when the factory contains multiple objects") {
0184         auto first = new FakeJObject(22);
0185         event->Insert(first);
0186         event->Insert(new FakeJObject(99));
0187         event->Insert(new FakeJObject(42));
0188         auto retrieved = event->GetSingle<FakeJObject>();
0189         REQUIRE(retrieved->datum == first->datum);      // Same contents
0190         REQUIRE(retrieved == first);                    // Same pointer
0191     }
0192 
0193     // ---------------
0194     // GetSingleStrict
0195     // ---------------
0196 
0197     SECTION("JEvent::GetSingleStrict throws an exception when factory is present but empty") {
0198         std::vector<FakeJObject*> objects; // empty
0199         event->Insert(objects);  // creates an empty dummy factory
0200         REQUIRE_THROWS(event->GetSingleStrict<FakeJObject>());
0201     }
0202 
0203     SECTION("JEvent::GetSingleStrict throws an exception when factory is missing") {
0204         REQUIRE_THROWS(event->GetSingleStrict<FakeJObject>());
0205     }
0206 
0207     SECTION("JEvent::GetSingleStrict returns the only object when the factory contains only one object") {
0208         auto inserted = new FakeJObject(22);
0209         event->Insert(inserted);
0210         auto retrieved = event->GetSingle<FakeJObject>();
0211         REQUIRE(retrieved->datum == inserted->datum);      // Same contents
0212         REQUIRE(retrieved == inserted);                    // Same pointer
0213     }
0214 
0215     SECTION("JEvent::GetSingleStrict throws an exception when the factory contains multiple objects") {
0216         event->Insert(new FakeJObject(22));
0217         event->Insert(new FakeJObject(99));
0218         event->Insert(new FakeJObject(42));
0219         REQUIRE_THROWS(event->GetSingleStrict<FakeJObject>());
0220     }
0221 
0222 }
0223