Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Sep 16, 2024
1 parent 8730188 commit 7facdd5
Showing 1 changed file with 78 additions and 20 deletions.
98 changes: 78 additions & 20 deletions src/programs/unit_tests/Components/GetObjectsTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,106 @@
// Subject to the terms in the LICENSE file found in the top-level directory.

#include <JANA/JEventSource.h>
#include <JANA/JEventProcessor.h>
#include <JANA/JApplication.h>
#include "JANA/JFactoryGenerator.h"
#include "catch.hpp"

struct Obj1 : public JObject { int data; };
struct Obj2 : public JObject { int data; };
struct Obj3 : public JObject { int data; };
struct Obj4 : public JObject { int data; };
namespace jana::components::getobjects_tests {

class Src : public JEventSource {
struct Obj : public JObject { int data; };

struct Src : public JEventSource {
Src() {
EnableGetObjects();
SetCallbackStyle(CallbackStyle::ExpertMode);
}
Result Emit(JEvent& event) override {
auto obj = new Obj1;
obj->data = 21;
event.Insert(obj);
Result Emit(JEvent&) override {
return Result::Success;
}
bool GetObjects(const std::shared_ptr<const JEvent>&, JFactory* fac) override {
if (fac->GetObjectName() == "Obj2") {
auto obj = new Obj2;

LOG_INFO(GetLogger()) << "GetObjects: Fac has object name '" << fac->GetObjectName() << "' and type name '" << fac->GetTypeName() << "'" << LOG_END;

//if (fac->GetObjectName() == "jana::components::getobjects_tests::Obj") {
auto typed_fac = dynamic_cast<JFactoryT<Obj>*>(fac);
if (typed_fac != nullptr) {
auto obj = new Obj;
obj->data = 22;
fac->Insert(obj);
typed_fac->Insert(obj);
return true;
}
return false;
}
};

class Fac : public JFactoryT<Obj3> {
struct Fac : public JFactoryT<Obj> {
void Process(const std::shared_ptr<const JEvent>&) override {
auto obj = new Obj;
obj->data = 23;
Insert(obj);
}
};

struct RFac : public JFactoryT<Obj> {
RFac() {
SetFactoryFlag(REGENERATE);
}
void Process(const std::shared_ptr<const JEvent>&) override {
auto obj = new Obj3;
auto obj = new Obj;
obj->data = 23;
Insert(obj);
}
};

TEST_CASE("GetObjectsTests") {
JEvent event;
JFactorySet* fs = new JFactorySet;
fs->Add(new Fac);
event.SetFactorySet(fs);
event.GetJCallGraphRecorder()->SetEnabled(true);
struct Proc : public JEventProcessor {
bool from_getobjects=true;
void Process(const std::shared_ptr<const JEvent>& event) override {
auto objs = event->Get<Obj>();
REQUIRE(objs.size() == 1);
if (from_getobjects) {
REQUIRE(objs[0]->data == 22);
}
else {
REQUIRE(objs[0]->data == 23);
}
}
};

TEST_CASE("GetObjectsTests_NoFac") {
JApplication app;
app.SetParameterValue("jana:loglevel", "warn");
app.SetParameterValue("jana:nevents", 1);
app.Add(new Src);
auto proc = new Proc;
proc->from_getobjects = true;
app.Add(proc);
app.Add(new JFactoryGeneratorT<JFactoryT<Obj>>);
app.Run();
}

TEST_CASE("GetObjectsTests_OverrideFac") {
JApplication app;
app.SetParameterValue("jana:loglevel", "warn");
app.SetParameterValue("jana:nevents", 1);
app.Add(new Src);
app.Add(new JFactoryGeneratorT<Fac>);
auto proc = new Proc;
proc->from_getobjects = true;
app.Add(proc);
app.Run();
}

TEST_CASE("GetObjectsTests_Regenerate") {
JApplication app;
app.SetParameterValue("jana:loglevel", "warn");
app.SetParameterValue("jana:nevents", 1);
app.Add(new Src);
app.Add(new JFactoryGeneratorT<RFac>);
auto proc = new Proc;
proc->from_getobjects = false;
app.Add(proc);
app.Run();
}

} // namespace ...

0 comments on commit 7facdd5

Please sign in to comment.