Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright 2020, Jefferson Science Associates, LLC.
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 
0004 #define CATCH_CONFIG_MAIN
0005 #include <catch.hpp>
0006 #include <JANA/JApplication.h>
0007 #include <JANA/JEvent.h>
0008 #include "../Tutorial/Hit.h"
0009 #include "../Tutorial/SimpleClusterFactory.h"
0010 
0011 TEST_CASE("SimpleClusterFactoryTests") {
0012 
0013     // We need to fire up the JApplication so that our Factory can access all of its JServices.
0014     // However, for unit testing, we don't need (or want!) to set up an event source or actually call JApplication::Run().
0015     JApplication app;
0016     app.SetParameterValue("jana:loglevel", "off");
0017     // Add any plugins you need here
0018     // app.AddPlugin("myPlugin");
0019     app.Initialize();  // Load the plugins
0020 
0021     // Create an instance of the JFactory we are testing
0022     // Unlike with regular JANA, we can hold on to a pointer to the factory, and ask it questions directly.
0023     auto factory = new SimpleClusterFactory;
0024 
0025     // Create a JEvent and give it the factory
0026     auto event = std::make_shared<JEvent>();
0027     event->SetJApplication(&app);
0028     event->GetFactorySet()->Add(factory);
0029 
0030     // We can now set up different independent scenarios and see if they behave according to our expectations.
0031     // Note that Catch2 will re-run the code above for each SECTION it sees below, so the test cases stay
0032     // isolated from each other.
0033 
0034     SECTION("One hit => One cluster") {
0035         // We set up a dummy scenario for each test case, craft some inputs manually, and insert them into the event.
0036         // If we want, we can add other factories, as long as we insert _their_ inputs as well.
0037         // However, the fewer moving pieces in the test, the better. So right now our event only knows about the
0038         // factory being tested, and all of its inputs are just inserted data.
0039         std::vector<Hit*> hits;
0040         hits.push_back(new Hit(5,5,1.0,0.0));
0041         event->Insert(hits);
0042 
0043         auto clusters = event->Get<Cluster>();
0044         // This is the best way to call the factory being tested because it makes sure Initialize(), BeginRun(), etc
0045         // have been called as needed.
0046 
0047         // Finally we make some assertions about our output data.
0048         REQUIRE(clusters.size() == 1);
0049     }
0050 
0051     SECTION("Cluster is centered on hits") {
0052         std::vector<Hit*> hits;
0053         hits.push_back(new Hit(0,10,1.0,0.0));
0054         hits.push_back(new Hit(6,12,1.0,0.0));
0055         event->Insert(hits);
0056 
0057         auto clusters = event->Get<Cluster>(); // This ultimately calls your factory
0058         REQUIRE(clusters.size() == 1);
0059         REQUIRE(clusters[0]->x_center == 3);
0060         REQUIRE(clusters[0]->y_center == 11);
0061     }
0062 
0063     SECTION("Every hit is part of at least one cluster") {
0064         // This brings up the idea of property-based testing. This would be more
0065         // like an integration test. The idea is that you create a set of test cases that
0066         // each test some property that you expect to hold no matter what the inputs. Instead of
0067         // hand-crafting test inputs for each property, you just put the property assertions in one JEventProcessor.
0068         // This way, you can add the JEventProcessor at any time, and it will verify that the property holds
0069         // for whatever event source and factory plugins you provide.
0070         REQUIRE(true == true);
0071     }
0072 
0073 }