Warning, file /jana2/src/programs/unit_tests/Components/JMultiFactoryTests.cc was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005 #include "catch.hpp"
0006 #include "JANA/JFactoryGenerator.h"
0007 #include <JANA/JMultifactory.h>
0008 #include <JANA/Services/JComponentManager.h>
0009 #include <JANA/JEvent.h>
0010
0011 namespace multifactory_tests {
0012
0013 struct A {
0014 float x, y;
0015 };
0016
0017 struct B {
0018 int en, rn;
0019 };
0020
0021 struct MyMultifactory : public JMultifactory {
0022
0023 bool m_set_wrong_output = false;
0024 int m_process_call_count = 0;
0025 int m_init_call_count = 0;
0026
0027 public:
0028 MyMultifactory(bool set_wrong_output=false) : m_set_wrong_output(set_wrong_output) {
0029 DeclareOutput<A>("first");
0030 DeclareOutput<B>("second");
0031 }
0032
0033 void Init() override {
0034 auto app = GetApplication();
0035 REQUIRE(app != nullptr);
0036 m_init_call_count += 1;
0037 }
0038
0039 void Process(const std::shared_ptr<const JEvent>&) override {
0040 REQUIRE(GetApplication() != nullptr);
0041 m_process_call_count += 1;
0042 std::vector<A*> as;
0043 std::vector<B*> bs;
0044 as.push_back(new A {3.3, 4.4});
0045 as.push_back(new A {5.5, 6.6});
0046 bs.push_back(new B {1,1});
0047 SetData("first", as);
0048 SetData("second", bs);
0049 if (m_set_wrong_output) SetData("third", bs);
0050 }
0051 };
0052
0053 TEST_CASE("MultiFactoryTests") {
0054 JApplication app;
0055
0056
0057 SECTION("Calling from JEvent") {
0058 auto sut = new MyMultifactory(false);
0059 sut->SetApplication(&app);
0060 auto event = std::make_shared<JEvent>(&app);
0061 auto fs = event->GetFactorySet();
0062 fs->Add(sut);
0063 auto as = event->Get<A>("first");
0064 REQUIRE(as.size() == 2);
0065 REQUIRE(as[1]->x == 5.5);
0066
0067 auto bs = event->Get<B>("second");
0068 REQUIRE(bs.size() == 1);
0069 REQUIRE(bs[0]->en == 1);
0070 }
0071
0072 SECTION("Multifactory sets the wrong data") {
0073 auto sut = new MyMultifactory(true);
0074 sut->SetApplication(&app);
0075 auto event = std::make_shared<JEvent>(&app);
0076 auto fs = event->GetFactorySet();
0077 fs->Add(sut);
0078 REQUIRE_THROWS(event->Get<A>("first"));
0079 }
0080
0081 SECTION("Multifactories work with JFactoryGeneratorT") {
0082 app.Add(new JFactoryGeneratorT<MyMultifactory>());
0083 auto event = std::make_shared<JEvent>(&app);
0084 auto as = event->Get<A>("first");
0085 REQUIRE(as.size() == 2);
0086 REQUIRE(as[1]->x == 5.5);
0087 }
0088
0089 SECTION("Test that multifactory Process() is only called once") {
0090 app.Add(new JFactoryGeneratorT<MyMultifactory>());
0091 auto event = std::make_shared<JEvent>(&app);
0092
0093 auto sut = dynamic_cast<MyMultifactory*>(event->GetFactory("multifactory_tests::A", "first"));
0094 REQUIRE(sut != nullptr);
0095
0096 REQUIRE(sut->m_process_call_count == 0);
0097 REQUIRE(sut->m_init_call_count == 0);
0098
0099 auto as = event->Get<A>("first");
0100 REQUIRE(as.size() == 2);
0101 REQUIRE(as[1]->x == 5.5);
0102 REQUIRE(sut->m_init_call_count == 1);
0103 REQUIRE(sut->m_process_call_count == 1);
0104
0105 auto bs = event->Get<B>("second");
0106 REQUIRE(bs.size() == 1);
0107 REQUIRE(bs[0]->en == 1);
0108 REQUIRE(sut->m_init_call_count == 1);
0109 REQUIRE(sut->m_process_call_count == 1);
0110
0111 as = event->Get<A>("first");
0112 REQUIRE(as.size() == 2);
0113 REQUIRE(as[1]->x == 5.5);
0114 REQUIRE(sut->m_init_call_count == 1);
0115 REQUIRE(sut->m_process_call_count == 1);
0116 }
0117
0118 }
0119
0120 }