File indexing completed on 2025-01-18 10:17:41
0001
0002
0003
0004
0005
0006 #include "catch.hpp"
0007 #include <JANA/JEvent.h>
0008
0009 struct Base {
0010 double base;
0011 Base(double base) : base(base) {};
0012 };
0013
0014 struct Derived : public Base {
0015 double derived;
0016 Derived(double base, double derived) : Base(base), derived(derived) {};
0017 };
0018
0019 struct Unrelated {
0020 double unrelated;
0021 Unrelated(double unrelated) : unrelated(unrelated) {};
0022 };
0023
0024 struct Multiple : public Derived, Unrelated {
0025 double multiple;
0026 Multiple(double base, double derived, double unrelated, double multiple)
0027 : Derived(base, derived), Unrelated(unrelated), multiple(multiple) {};
0028 };
0029
0030 class DerivedFactory : public JFactoryT<Derived> {
0031 public:
0032 DerivedFactory() {
0033 EnableGetAs<Base>();
0034 }
0035 };
0036
0037 class OtherDerivedFactory : public JFactoryT<Derived> {
0038 public:
0039 OtherDerivedFactory() {
0040 EnableGetAs<Base>();
0041 }
0042
0043 void Process(const std::shared_ptr<const JEvent>&) override {
0044 Insert(new Derived(19, 13));
0045 Insert(new Derived(29, 23));
0046 }
0047 };
0048
0049 class UnrelatedFactory : public JFactoryT<Unrelated> {};
0050
0051 class MultipleFactory : public JFactoryT<Multiple> {
0052 public:
0053 MultipleFactory() {
0054 EnableGetAs<Base>();
0055 EnableGetAs<Derived>();
0056 EnableGetAs<Unrelated>();
0057 }
0058 };
0059
0060 class MultipleFactoryMissing : public JFactoryT<Multiple> {
0061 public:
0062 MultipleFactoryMissing() {
0063 EnableGetAs<Derived>();
0064 }
0065 };
0066
0067 TEST_CASE("JFactoryGetAs") {
0068
0069 SECTION("Upcast Derived to Base (single item inserted)") {
0070 DerivedFactory f;
0071 f.Insert(new Derived (7, 22));
0072 auto bases = f.GetAs<Base>();
0073 REQUIRE(bases.size() == 1);
0074 REQUIRE(bases[0]->base == 7);
0075 }
0076
0077 SECTION("Upcast Derived to Base (multiple items inserted)") {
0078 DerivedFactory f;
0079 f.Insert(new Derived (7, 22));
0080 f.Insert(new Derived (8, 23));
0081 f.Insert(new Derived (9, 24));
0082 auto bases = f.GetAs<Base>();
0083
0084 REQUIRE(bases.size() == 3);
0085 REQUIRE(bases[0]->base == 7);
0086 REQUIRE(bases[1]->base == 8);
0087 REQUIRE(bases[2]->base == 9);
0088 }
0089
0090 SECTION("Upcast Derived to Unrelated (single item inserted)") {
0091 DerivedFactory f;
0092 f.Insert(new Derived (7, 22));
0093 auto bases = f.GetAs<Unrelated>();
0094 REQUIRE(bases.size() == 0);
0095 REQUIRE(bases.size() == 0);
0096 }
0097
0098 SECTION("Upcast Derived to Derived (single item inserted)") {
0099 DerivedFactory f;
0100 f.Insert(new Derived(22, 18));
0101 auto deriveds = f.GetAs<Derived>();
0102 REQUIRE(deriveds.size() == 1);
0103 REQUIRE(deriveds[0]->derived == 18);
0104 }
0105
0106 SECTION("GetAs doesn't trigger Process()") {
0107 OtherDerivedFactory f;
0108 auto deriveds_before = f.GetAs<Derived>();
0109 REQUIRE(deriveds_before.size() == 0);
0110 f.Process(std::make_shared<JEvent>());
0111 auto deriveds_after = f.GetAs<Derived>();
0112 REQUIRE(deriveds_after.size() == 2);
0113 }
0114
0115 SECTION("Upcast Multiple to each of its bases") {
0116 MultipleFactory f;
0117 f.Insert(new Multiple(22, 27, 42, 49));
0118
0119 auto bases = f.GetAs<Base>();
0120 REQUIRE(bases.size() == 1);
0121 REQUIRE(bases[0]->base == 22);
0122
0123 auto deriveds = f.GetAs<Derived>();
0124 REQUIRE(deriveds.size() == 1);
0125 REQUIRE(deriveds[0]->derived == 27);
0126
0127 auto unrelateds = f.GetAs<Unrelated>();
0128 REQUIRE(unrelateds.size() == 1);
0129 REQUIRE(unrelateds[0]->unrelated == 42);
0130
0131 auto multiples = f.GetAs<Multiple>();
0132 REQUIRE(multiples.size() == 1);
0133 REQUIRE(multiples[0]->multiple == 49);
0134 }
0135
0136 SECTION("Upcast only succeeds if bases have been enabled") {
0137 MultipleFactoryMissing f;
0138 f.Insert(new Multiple(22, 27, 42, 49));
0139
0140 auto bases = f.GetAs<Base>();
0141 REQUIRE(bases.size() == 0);
0142
0143 auto deriveds = f.GetAs<Derived>();
0144 REQUIRE(deriveds.size() == 1);
0145 REQUIRE(deriveds[0]->derived == 27);
0146
0147 auto unrelateds = f.GetAs<Unrelated>();
0148 REQUIRE(unrelateds.size() == 0);
0149
0150 auto multiples = f.GetAs<Multiple>();
0151 REQUIRE(multiples.size() == 1);
0152 REQUIRE(multiples[0]->base == 22);
0153 REQUIRE(multiples[0]->derived == 27);
0154 REQUIRE(multiples[0]->unrelated == 42);
0155 REQUIRE(multiples[0]->multiple == 49);
0156 }
0157 }
0158
0159 TEST_CASE("JEventGetAllChildren") {
0160
0161 auto event = std::make_shared<JEvent>();
0162
0163 SECTION("Single-item JEvent::Insert() can be retrieved via JEvent::GetAllChildren()") {
0164 auto b = new Base(22);
0165 auto d = new Derived(33,44);
0166 auto u = new Unrelated(19);
0167 auto m = new Multiple(1,2,3,4);
0168
0169 event->Insert(b);
0170 event->Insert(u);
0171 event->Insert(d)->EnableGetAs<Base>();
0172
0173 auto f = event->Insert(m);
0174 f->EnableGetAs<Base>();
0175 f->EnableGetAs<Derived>();
0176 f->EnableGetAs<Unrelated>();
0177
0178 auto base_map = event->GetAllChildren<Base>();
0179 REQUIRE(base_map.size() == 3);
0180 REQUIRE(base_map[{"Base",""}].size() == 1);
0181 REQUIRE(base_map[{"Base",""}][0]->base == 22);
0182 REQUIRE(base_map[{"Derived",""}].size() == 1);
0183 REQUIRE(base_map[{"Derived",""}][0]->base == 33);
0184 REQUIRE(base_map[{"Multiple",""}].size() == 1);
0185 REQUIRE(base_map[{"Multiple",""}][0]->base == 1);
0186 }
0187
0188 }