Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:27:14

0001 #include "RecoTauTag/RecoTau/interface/CombinatoricGenerator.h"
0002 #include <cppunit/extensions/HelperMacros.h>
0003 #include <sstream>
0004 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>
0005 #include <iostream>
0006 
0007 typedef std::vector<int> vint;
0008 
0009 // a list of the combo + remaining items
0010 typedef std::pair<vint, vint> ComboRemainder;
0011 
0012 // a list of the combo + remaining items
0013 typedef std::vector<ComboRemainder> VComboRemainder;
0014 
0015 template <typename G>
0016 VComboRemainder getAllCombinations(G& generator) {
0017   VComboRemainder output;
0018   typedef typename G::iterator iterator;
0019   typedef typename G::combo_iterator combo_iterator;
0020   for (iterator combo = generator.begin(); combo != generator.end(); ++combo) {
0021     ComboRemainder thisCombo;
0022     for (combo_iterator item = combo->combo_begin(); item != combo->combo_end(); ++item) {
0023       thisCombo.first.push_back(*item);
0024     }
0025 
0026     for (combo_iterator item = combo->remainder_begin(); item != combo->remainder_end(); ++item) {
0027       thisCombo.second.push_back(*item);
0028     }
0029 
0030     output.push_back(thisCombo);
0031   }
0032   return output;
0033 }
0034 
0035 class testCombGenerator : public CppUnit::TestFixture {
0036   CPPUNIT_TEST_SUITE(testCombGenerator);
0037   CPPUNIT_TEST(testEmptyCollection);
0038   CPPUNIT_TEST(testLessThanDesiredCollection);
0039   CPPUNIT_TEST(testNormalBehavior);
0040   CPPUNIT_TEST_SUITE_END();
0041 
0042 public:
0043   void setup() {}
0044   typedef reco::tau::CombinatoricGenerator<vint> Generator;
0045 
0046   void testNormalBehavior() {
0047     std::cout << "Testing normal behavior" << std::endl;
0048     vint toCombize;
0049     toCombize.push_back(1);
0050     toCombize.push_back(2);
0051     toCombize.push_back(3);
0052     toCombize.push_back(4);
0053 
0054     Generator pairGenerator(toCombize.begin(), toCombize.end(), 2);
0055 
0056     VComboRemainder pairResults = getAllCombinations(pairGenerator);
0057 
0058     // 4 choose 2 = 6
0059 
0060     CPPUNIT_ASSERT(pairResults.size() == 6);
0061 
0062     ComboRemainder firstExpResult;
0063     firstExpResult.first.push_back(1);
0064     firstExpResult.first.push_back(2);
0065     firstExpResult.second.push_back(3);
0066     firstExpResult.second.push_back(4);
0067 
0068     CPPUNIT_ASSERT(firstExpResult == pairResults[0]);
0069 
0070     ComboRemainder secondExpResult;
0071     secondExpResult.first.push_back(1);
0072     secondExpResult.first.push_back(3);
0073     secondExpResult.second.push_back(2);
0074     secondExpResult.second.push_back(4);
0075 
0076     CPPUNIT_ASSERT(secondExpResult == pairResults[1]);
0077 
0078     ComboRemainder lastExpResult;
0079     lastExpResult.first.push_back(3);
0080     lastExpResult.first.push_back(4);
0081     lastExpResult.second.push_back(1);
0082     lastExpResult.second.push_back(2);
0083 
0084     Generator tripletGen(toCombize.begin(), toCombize.end(), 3);
0085 
0086     VComboRemainder tripletResults = getAllCombinations(tripletGen);
0087     CPPUNIT_ASSERT(tripletResults.size() == 4);
0088 
0089     // This one should have only one combinatoric
0090     Generator quadGen(toCombize.begin(), toCombize.end(), 4);
0091     VComboRemainder quadResults = getAllCombinations(quadGen);
0092     CPPUNIT_ASSERT(quadResults.size() == 1);
0093     // All should be in the combinatoric, none in the remainder
0094     CPPUNIT_ASSERT(quadResults[0].first.size() == 4);
0095     CPPUNIT_ASSERT(quadResults[0].second.size() == 0);
0096   };
0097 
0098   void testLessThanDesiredCollection() {
0099     std::cout << "Testing less than behavior" << std::endl;
0100     vint toCombize;
0101     toCombize.push_back(1);
0102     toCombize.push_back(2);
0103 
0104     Generator tripletGen(toCombize.begin(), toCombize.end(), 3);
0105     VComboRemainder tripletResults = getAllCombinations(tripletGen);
0106     // Can't make any combos - 2 choose 3 doesn't make sense.
0107     CPPUNIT_ASSERT(tripletResults.size() == 0);
0108   }
0109 
0110   void testEmptyCollection() {
0111     std::cout << "Testing empty collection" << std::endl;
0112     // A requested empty collection should return 1 empty combo
0113     vint toCombize;
0114     toCombize.push_back(1);
0115     toCombize.push_back(2);
0116     toCombize.push_back(3);
0117     toCombize.push_back(4);
0118 
0119     Generator zeroGen(toCombize.begin(), toCombize.end(), 0);
0120     VComboRemainder zeroResults = getAllCombinations(zeroGen);
0121     CPPUNIT_ASSERT(zeroResults.size() == 1);
0122     CPPUNIT_ASSERT(zeroResults[0].first.size() == 0);
0123     CPPUNIT_ASSERT(zeroResults[0].second.size() == 4);
0124   }
0125 };
0126 
0127 CPPUNIT_TEST_SUITE_REGISTRATION(testCombGenerator);