Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:05

0001 #include "Utilities/Testing/interface/CppUnit_testdriver.icpp"  //gives main
0002 #include "cppunit/extensions/HelperMacros.h"
0003 
0004 #include "DataFormats/Common/interface/DetSetNew.h"
0005 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0006 #include "DataFormats/Common/interface/DetSetAlgorithm.h"
0007 #include "DataFormats/Common/interface/DetSet2RangeMap.h"
0008 
0009 #include "FWCore/Utilities/interface/EDMException.h"
0010 
0011 #include <vector>
0012 #include <algorithm>
0013 
0014 struct B {
0015   virtual ~B() {}
0016   virtual B *clone() const = 0;
0017 };
0018 
0019 struct T : public B {
0020   T(float iv = 0) : v(iv) {}
0021   float v;
0022   bool operator==(T t) const { return v == t.v; }
0023 
0024   T *clone() const final { return new T(*this); }
0025 };
0026 
0027 bool operator==(T const &t, B const &b) {
0028   T const *p = dynamic_cast<T const *>(&b);
0029   return p && p->v == t.v;
0030 }
0031 
0032 bool operator==(B const &b, T const &t) { return t == b; }
0033 
0034 typedef edmNew::DetSetVector<T> DSTV;
0035 typedef edmNew::DetSet<T> DST;
0036 typedef edmNew::det_id_type det_id_type;
0037 typedef DSTV::FastFiller FF;
0038 typedef DSTV::TSFastFiller TSFF;
0039 
0040 class TestDetSet : public CppUnit::TestFixture {
0041   CPPUNIT_TEST_SUITE(TestDetSet);
0042   CPPUNIT_TEST(default_ctor);
0043   CPPUNIT_TEST(inserting);
0044   CPPUNIT_TEST(filling);
0045   CPPUNIT_TEST(fillingTS);
0046   CPPUNIT_TEST(iterator);
0047   CPPUNIT_TEST(algorithm);
0048   CPPUNIT_TEST(onDemand);
0049   CPPUNIT_TEST(toRangeMap);
0050 
0051   CPPUNIT_TEST_SUITE_END();
0052 
0053 public:
0054   TestDetSet();
0055   ~TestDetSet() {}
0056   void setUp() {}
0057   void tearDown() {}
0058 
0059   void default_ctor();
0060   void inserting();
0061   void filling();
0062   void fillingTS();
0063   void iterator();
0064   void algorithm();
0065   void onDemand();
0066   void toRangeMap();
0067 
0068 public:
0069   std::vector<DSTV::data_type> sv;
0070 };
0071 
0072 CPPUNIT_TEST_SUITE_REGISTRATION(TestDetSet);
0073 
0074 TestDetSet::TestDetSet() : sv(10) {
0075   DSTV::data_type v[10] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
0076   std::copy(v, v + 10, sv.begin());
0077 }
0078 
0079 void TestDetSet::default_ctor() {
0080 #ifdef CMS_NOCXX11
0081   CPPUNIT_ASSERT(false && "CMS_NOCXX11");
0082 #endif
0083 
0084   DSTV detsets(2);
0085   CPPUNIT_ASSERT(!detsets.onDemand());
0086   CPPUNIT_ASSERT(detsets.subdetId() == 2);
0087   CPPUNIT_ASSERT(detsets.size() == 0);
0088   CPPUNIT_ASSERT(detsets.empty());
0089   CPPUNIT_ASSERT(detsets.dataSize() == 0);
0090   detsets.resize(3, 10);
0091   CPPUNIT_ASSERT(detsets.size() == 3);
0092   CPPUNIT_ASSERT(detsets.dataSize() == 10);
0093   CPPUNIT_ASSERT(!detsets.empty());
0094   // follow is nonsense still valid construct... (maybe it shall throw...)
0095   // now assert!
0096   /*
0097   DST df(detsets,detsets.item(1));
0098   CPPUNIT_ASSERT(df.id()==0);
0099   CPPUNIT_ASSERT(df.size()==0);
0100   CPPUNIT_ASSERT(df.data()+1==&detsets.m_data.front());
0101   df.set(detsets,detsets.item(2));
0102   CPPUNIT_ASSERT(df.size()==0);
0103   CPPUNIT_ASSERT(df.data()+1==&detsets.m_data.front());
0104   */
0105 
0106   // test swap
0107   DSTV detsets2(3);
0108   detsets.swap(detsets2);
0109   CPPUNIT_ASSERT(detsets.subdetId() == 3);
0110   CPPUNIT_ASSERT(detsets.size() == 0);
0111   CPPUNIT_ASSERT(detsets.dataSize() == 0);
0112   CPPUNIT_ASSERT(detsets2.subdetId() == 2);
0113   CPPUNIT_ASSERT(detsets2.size() == 3);
0114   CPPUNIT_ASSERT(detsets2.dataSize() == 10);
0115   CPPUNIT_ASSERT(!detsets2.onDemand());
0116 
0117   // test move
0118   DSTV detsets3(4);
0119   detsets = std::move(detsets3);
0120   CPPUNIT_ASSERT(detsets.subdetId() == 4);
0121   CPPUNIT_ASSERT(detsets.size() == 0);
0122   CPPUNIT_ASSERT(detsets.dataSize() == 0);
0123   DSTV detsets5(std::move(detsets));
0124   CPPUNIT_ASSERT(detsets5.subdetId() == 4);
0125   CPPUNIT_ASSERT(detsets5.size() == 0);
0126   CPPUNIT_ASSERT(detsets5.dataSize() == 0);
0127 
0128   // test copy
0129   DSTV detsets4(detsets5);
0130   CPPUNIT_ASSERT(detsets4.subdetId() == 4);
0131   CPPUNIT_ASSERT(detsets4.size() == 0);
0132   CPPUNIT_ASSERT(detsets4.dataSize() == 0);
0133 }
0134 
0135 void TestDetSet::inserting() {
0136   DSTV detsets(2);
0137   unsigned int ntot = 0;
0138   for (unsigned int n = 1; n < 5; ++n) {
0139     ntot += n;
0140     unsigned int id = 20 + n;
0141     DST df = detsets.insert(id, n);
0142     CPPUNIT_ASSERT(detsets.size() == n);
0143     CPPUNIT_ASSERT(detsets.dataSize() == ntot);
0144     CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == n);
0145     CPPUNIT_ASSERT(df.size() == n);
0146     CPPUNIT_ASSERT(df.id() == id);
0147 
0148     std::copy(sv.begin(), sv.begin() + n, df.begin());
0149 
0150     DSTV detsets2(detsets);
0151     CPPUNIT_ASSERT(detsets2.size() == n);
0152     CPPUNIT_ASSERT(detsets2.dataSize() == ntot);
0153     CPPUNIT_ASSERT(detsets2.detsetSize(n - 1) == n);
0154 
0155     std::vector<DST::data_type> v1(n);
0156     std::vector<DST::data_type> v2(n);
0157     std::vector<DST::data_type> v3(n);
0158     std::copy(detsets.m_data.begin() + ntot - n, detsets.m_data.begin() + ntot, v2.begin());
0159     std::copy(detsets2.m_data.begin() + ntot - n, detsets2.m_data.begin() + ntot, v3.begin());
0160     std::copy(sv.begin(), sv.begin() + n, v1.begin());
0161     CPPUNIT_ASSERT(v1 == v2);
0162     CPPUNIT_ASSERT(v1 == v3);
0163   }
0164 
0165   // test error conditions
0166   try {
0167     detsets.insert(22, 6);
0168     CPPUNIT_ASSERT("insert did not throw" == 0);
0169   } catch (edm::Exception const &err) {
0170     CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
0171   }
0172 }
0173 
0174 void TestDetSet::filling() {
0175   DSTV detsets(2);
0176   unsigned int ntot = 0;
0177   for (unsigned int n = 1; n < 5; ++n) {
0178     unsigned int id = 20 + n;
0179     FF ff(detsets, id);
0180     CPPUNIT_ASSERT(detsets.size() == n);
0181     CPPUNIT_ASSERT(detsets.dataSize() == ntot);
0182     CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == 0);
0183     CPPUNIT_ASSERT(ff.m_item.offset == int(detsets.dataSize()));
0184     CPPUNIT_ASSERT(ff.m_item.size == 0);
0185     CPPUNIT_ASSERT(ff.m_item.id == id);
0186     ntot += 1;
0187     ff.push_back(3.14);
0188     CPPUNIT_ASSERT(detsets.dataSize() == ntot);
0189     CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == 1);
0190     CPPUNIT_ASSERT(detsets.m_data.back().v == 3.14f);
0191     CPPUNIT_ASSERT(ff.m_item.offset == int(detsets.dataSize()) - 1);
0192     CPPUNIT_ASSERT(ff.m_item.size == 1);
0193     ntot += n - 1;
0194     ff.resize(n);
0195     CPPUNIT_ASSERT(detsets.dataSize() == ntot);
0196     CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == n);
0197     CPPUNIT_ASSERT(ff.m_item.offset == int(detsets.dataSize() - n));
0198     CPPUNIT_ASSERT(ff.m_item.size == n);
0199 
0200     std::copy(sv.begin(), sv.begin() + n, ff.begin());
0201 
0202     std::vector<DST::data_type> v1(n);
0203     std::vector<DST::data_type> v2(n);
0204     std::copy(detsets.m_data.begin() + ntot - n, detsets.m_data.begin() + ntot, v2.begin());
0205     std::copy(sv.begin(), sv.begin() + n, v1.begin());
0206     CPPUNIT_ASSERT(v1 == v2);
0207   }
0208 
0209   // test abort and empty
0210   {
0211     FF ff1(detsets, 30);
0212     CPPUNIT_ASSERT(detsets.size() == 5);
0213     CPPUNIT_ASSERT(detsets.exists(30));
0214     CPPUNIT_ASSERT(detsets.isValid(30));
0215   }
0216   CPPUNIT_ASSERT(detsets.size() == 4);
0217   CPPUNIT_ASSERT(!detsets.exists(30));
0218   {
0219     FF ff1(detsets, 30, true);
0220     CPPUNIT_ASSERT(detsets.size() == 5);
0221     CPPUNIT_ASSERT(detsets.exists(30));
0222     CPPUNIT_ASSERT(detsets.isValid(30));
0223   }
0224   CPPUNIT_ASSERT(detsets.size() == 5);
0225   CPPUNIT_ASSERT(detsets.exists(30));
0226 
0227   {
0228     DSTV detsets2(detsets);
0229     CPPUNIT_ASSERT(detsets2.size() == 5);
0230     CPPUNIT_ASSERT(detsets2.exists(30));
0231   }
0232 
0233   {
0234     unsigned int cs = detsets.dataSize();
0235     FF ff1(detsets, 31);
0236     ff1.resize(4);
0237     CPPUNIT_ASSERT(detsets.size() == 6);
0238     CPPUNIT_ASSERT(detsets.dataSize() == cs + 4);
0239     CPPUNIT_ASSERT(detsets.exists(31));
0240     CPPUNIT_ASSERT(detsets.isValid(31));
0241     ff1.abort();
0242     CPPUNIT_ASSERT(detsets.size() == 5);
0243     CPPUNIT_ASSERT(detsets.dataSize() == cs);
0244     CPPUNIT_ASSERT(!detsets.exists(31));
0245   }
0246   CPPUNIT_ASSERT(detsets.size() == 5);
0247   CPPUNIT_ASSERT(!detsets.exists(31));
0248   { FF ff1(detsets, 32, true); }
0249   CPPUNIT_ASSERT(detsets.size() == 6);
0250 
0251   DSTV detsets2(detsets);
0252   CPPUNIT_ASSERT(detsets2.size() == 6);
0253 
0254   detsets.clean();
0255   CPPUNIT_ASSERT(detsets.size() == 4);
0256   CPPUNIT_ASSERT(!detsets.exists(30));
0257   CPPUNIT_ASSERT(!detsets.exists(32));
0258 
0259   CPPUNIT_ASSERT(detsets2.size() == 6);
0260   CPPUNIT_ASSERT(detsets2.exists(30));
0261   CPPUNIT_ASSERT(detsets2.exists(32));
0262   detsets2.clean();
0263   CPPUNIT_ASSERT(detsets2.size() == 4);
0264   CPPUNIT_ASSERT(!detsets2.exists(30));
0265   CPPUNIT_ASSERT(!detsets2.exists(32));
0266 
0267   // test error conditions
0268   try {
0269     FF ff1(detsets, 22);
0270     CPPUNIT_ASSERT(" fast filler did not throw" == 0);
0271   } catch (edm::Exception const &err) {
0272     CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
0273   }
0274 
0275   try {
0276     FF ff1(detsets, 44);
0277     FF ff2(detsets, 45);
0278     CPPUNIT_ASSERT(" fast filler did not throw" == 0);
0279   } catch (edm::Exception const &err) {
0280     CPPUNIT_ASSERT(err.categoryCode() == edm::errors::LogicError);
0281   }
0282 }
0283 
0284 void TestDetSet::fillingTS() {
0285   DSTV detsets(2);
0286   detsets.reserve(5, 100);
0287   unsigned int ntot = 0;
0288   for (unsigned int n = 1; n < 5; ++n) {
0289     unsigned int id = 20 + n;
0290     {
0291       TSFF ff(detsets, id);
0292       CPPUNIT_ASSERT(detsets.size() == n);
0293       CPPUNIT_ASSERT(detsets.dataSize() == ntot);
0294       CPPUNIT_ASSERT(ff.m_item.size == 0);
0295       CPPUNIT_ASSERT(ff.id() == id);
0296       ntot += 1;
0297       ff.push_back(3.14);
0298       CPPUNIT_ASSERT(detsets.dataSize() == ntot - 1);
0299       CPPUNIT_ASSERT(ff.m_item.size == 0);
0300       CPPUNIT_ASSERT(ff.size() == 1);
0301       ntot += n - 1;
0302       ff.resize(n);
0303       CPPUNIT_ASSERT(ff.size() == n);
0304       std::copy(sv.begin(), sv.begin() + n, ff.begin());
0305     }
0306     CPPUNIT_ASSERT(detsets.size() == n);
0307     CPPUNIT_ASSERT(detsets.dataSize() == ntot);
0308 
0309     std::vector<DST::data_type> v1(n);
0310     std::vector<DST::data_type> v2(n);
0311     std::copy(detsets.m_data.begin() + ntot - n, detsets.m_data.begin() + ntot, v2.begin());
0312     std::copy(sv.begin(), sv.begin() + n, v1.begin());
0313     CPPUNIT_ASSERT(v1 == v2);
0314   }
0315 
0316   // test abort and empty
0317   {
0318     TSFF ff1(detsets, 30);
0319     CPPUNIT_ASSERT(detsets.exists(30));
0320   }
0321   CPPUNIT_ASSERT(detsets.size() == 5);
0322   CPPUNIT_ASSERT(detsets.exists(30));
0323   detsets.clean();
0324   CPPUNIT_ASSERT(detsets.size() == 4);
0325   CPPUNIT_ASSERT(!detsets.exists(30));
0326 
0327   unsigned int cs = detsets.dataSize();
0328   {
0329     TSFF ff1(detsets, 31);
0330     ff1.resize(4);
0331     ff1.abort();
0332   }
0333   CPPUNIT_ASSERT(detsets.size() == 5);
0334   CPPUNIT_ASSERT(detsets.dataSize() == cs);
0335   CPPUNIT_ASSERT(detsets.exists(31));
0336   detsets.clean();
0337   CPPUNIT_ASSERT(detsets.size() == 4);
0338   CPPUNIT_ASSERT(!detsets.exists(31));
0339 
0340   // test error conditions
0341   try {
0342     TSFF ff1(detsets, 22);
0343     CPPUNIT_ASSERT(" fast filler did not throw" == 0);
0344   } catch (edm::Exception const &err) {
0345     CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
0346   }
0347 
0348   try {
0349     TSFF ff1(detsets, 44);
0350     TSFF ff2(detsets, 45);
0351     CPPUNIT_ASSERT(" fast filler did not throw" == 0);
0352   } catch (edm::Exception const &err) {
0353     CPPUNIT_ASSERT(err.categoryCode() == edm::errors::LogicError);
0354   }
0355 }
0356 
0357 namespace {
0358   struct VerifyIter {
0359     VerifyIter(TestDetSet *itest, unsigned int in = 1, int iincr = 1) : n(in), incr(iincr), test(*itest) {}
0360 
0361     void operator()(DST const &df) {
0362       if (df.id() > 1000) {
0363         CPPUNIT_ASSERT(df.size() == 0);
0364         return;
0365       }
0366       CPPUNIT_ASSERT(df.id() == 20 + n);
0367       CPPUNIT_ASSERT(df.size() == n);
0368       std::vector<DST::data_type> v1(n);
0369       std::vector<DST::data_type> v2(n);
0370       std::copy(df.begin(), df.end(), v2.begin());
0371       std::copy(test.sv.begin(), test.sv.begin() + n, v1.begin());
0372       CPPUNIT_ASSERT(v1 == v2);
0373       n += incr;
0374     }
0375 
0376     unsigned int n;
0377     int incr;
0378     TestDetSet &test;
0379   };
0380 
0381   struct Getter final : public DSTV::Getter {
0382     Getter(TestDetSet *itest) : ntot(0), test(*itest) {}
0383 
0384     void fill(TSFF &ff) override {
0385       aborted = false;
0386       try {
0387         const int n = ff.id() - 20;
0388         CPPUNIT_ASSERT(n > 0);
0389         ff.resize(n);
0390         int nCopied = n;
0391         if (static_cast<size_t>(n) > test.sv.size()) {
0392           nCopied = test.sv.size();
0393         }
0394         std::copy(test.sv.begin(), test.sv.begin() + nCopied, ff.begin());
0395         if (ff.full()) {
0396           ff.abort();
0397           aborted = true;
0398         } else {
0399           ntot += n;
0400         }
0401       } catch (edmNew::CapacityExaustedException const &) {
0402         CPPUNIT_ASSERT("cannot be here" == 0);
0403       }
0404     }
0405 
0406     unsigned int ntot;
0407     bool aborted = false;
0408     TestDetSet &test;
0409   };
0410 
0411   struct cmp10 {
0412     bool operator()(DSTV::id_type i1, DSTV::id_type i2) const { return i1 / 10 < i2 / 10; }
0413   };
0414 }  // namespace
0415 
0416 void TestDetSet::iterator() {
0417   DSTV detsets(2);
0418   for (unsigned int n = 1; n < 5; ++n) {
0419     unsigned int id = 20 + n;
0420     FF ff(detsets, id);
0421     ff.resize(n);
0422     std::copy(sv.begin(), sv.begin() + n, ff.begin());
0423   }
0424   //  CPPUNIT_ASSERT(std::for_each(detsets.begin(),detsets.end(),VerifyIter(this)).n==5);
0425   CPPUNIT_ASSERT(std::for_each(detsets.begin(true), detsets.end(true), VerifyIter(this)).n == 5);
0426   {
0427     FF ff(detsets, 31);
0428     ff.resize(2);
0429     std::copy(sv.begin(), sv.begin() + 2, ff.begin());
0430   }
0431   {
0432     FF ff(detsets, 11);
0433     ff.resize(2);
0434     std::copy(sv.begin(), sv.begin() + 2, ff.begin());
0435   }
0436   {
0437     FF ff(detsets, 34);
0438     ff.resize(4);
0439     std::copy(sv.begin(), sv.begin() + 4, ff.begin());
0440   }
0441 
0442   DSTV::Range r = detsets.equal_range(30, cmp10());
0443   CPPUNIT_ASSERT(r.second - r.first == 2);
0444   r = detsets.equal_range(40, cmp10());
0445   CPPUNIT_ASSERT(r.second - r.first == 0);
0446 
0447   try {
0448     {
0449       CPPUNIT_ASSERT(detsets.exists(22));
0450       CPPUNIT_ASSERT(!detsets.exists(44));
0451       DST df = *detsets.find(22);
0452       //      DST df = *detsets.find(22,true);
0453       CPPUNIT_ASSERT(df.id() == 22);
0454       CPPUNIT_ASSERT(df.size() == 2);
0455     }
0456     {
0457       DST df = detsets[22];
0458       CPPUNIT_ASSERT(df.id() == 22);
0459       CPPUNIT_ASSERT(df.size() == 2);
0460     }
0461   } catch (edm::Exception const &) {
0462     CPPUNIT_ASSERT("DetSetVector threw when not expected" == 0);
0463   }
0464 
0465   try {
0466     DSTV::const_iterator p = detsets.find(44);
0467     //    DSTV::const_iterator p = detsets.find(44,true);
0468     CPPUNIT_ASSERT(p == detsets.end());
0469     //    CPPUNIT_ASSERT(p==detsets.end(true));
0470   } catch (edm::Exception const &) {
0471     CPPUNIT_ASSERT("find thrown edm exception when not expected" == 0);
0472   }
0473   try {
0474     detsets[44];
0475     CPPUNIT_ASSERT("[] did not throw" == 0);
0476   } catch (edm::Exception const &err) {
0477     CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
0478   }
0479 }
0480 
0481 namespace {
0482 
0483   std::pair<unsigned int, cmp10> acc(unsigned int i) { return std::make_pair(i * 10, cmp10()); }
0484 
0485   struct VerifyAlgos {
0486     VerifyAlgos(std::vector<DSTV::data_type const *> &iv) : n(0), v(iv) {}
0487 
0488     void operator()(DSTV::data_type const &d) {
0489       CPPUNIT_ASSERT(d == *v[n]);
0490       CPPUNIT_ASSERT(&d == v[n]);
0491       ++n;
0492     }
0493 
0494     int n;
0495     std::vector<DSTV::data_type const *> const &v;
0496   };
0497 
0498 }  // namespace
0499 
0500 void TestDetSet::algorithm() {
0501   DSTV detsets(2);
0502   for (unsigned int n = 1; n < 5; ++n) {
0503     unsigned int id = 20 + n;
0504     FF ff(detsets, id);
0505     ff.resize(n);
0506     std::copy(sv.begin(), sv.begin() + n, ff.begin());
0507   }
0508   {
0509     FF ff(detsets, 31);
0510     ff.resize(2);
0511     std::copy(sv.begin(), sv.begin() + 2, ff.begin());
0512   }
0513   {
0514     FF ff(detsets, 11);
0515     ff.resize(2);
0516     std::copy(sv.begin(), sv.begin() + 2, ff.begin());
0517   }
0518   {
0519     FF ff(detsets, 34);
0520     ff.resize(4);
0521     std::copy(sv.begin(), sv.begin() + 4, ff.begin());
0522   }
0523 
0524   DSTV::Range r = detsetRangeFromPair(detsets, acc(3));
0525   CPPUNIT_ASSERT(r.second - r.first == 2);
0526   r = edmNew::detsetRangeFromPair(detsets, acc(4));
0527   CPPUNIT_ASSERT(r.second - r.first == 0);
0528 
0529   std::vector<DSTV::data_type const *> v;
0530   edmNew::copyDetSetRange(detsets, v, acc(3));
0531   VerifyAlgos va(v);
0532   edmNew::foreachDetSetObject(detsets, acc(3), va);
0533 }
0534 
0535 void TestDetSet::onDemand() {
0536   auto pg = std::make_shared<Getter>(this);
0537   Getter &g = *pg;
0538   assert(!g.aborted);
0539   std::vector<unsigned int> v = {21, 23, 25, 27, 1020};
0540   DSTV detsets(pg, v, 2);
0541   CPPUNIT_ASSERT(g.ntot == 0);
0542   CPPUNIT_ASSERT(detsets.onDemand());
0543   try {
0544     {
0545       CPPUNIT_ASSERT(detsets.exists(21));
0546       CPPUNIT_ASSERT(!detsets.exists(22));
0547       CPPUNIT_ASSERT(!detsets.isValid(21));
0548       CPPUNIT_ASSERT(!detsets.isValid(22));
0549       //      DST df = *detsets.find(21);
0550       detsets.reserve(5, 100);
0551       DST df = *detsets.find(21, true);
0552       CPPUNIT_ASSERT(df.id() == 21);
0553       CPPUNIT_ASSERT(df.size() == 1);
0554       CPPUNIT_ASSERT(detsets.isValid(21));
0555       CPPUNIT_ASSERT(!detsets.isValid(23));
0556       CPPUNIT_ASSERT(g.ntot == 1);
0557       assert(!g.aborted);
0558     }
0559     {
0560       DST df = detsets[25];
0561       CPPUNIT_ASSERT(df.id() == 25);
0562       CPPUNIT_ASSERT(df.size() == 5);
0563       CPPUNIT_ASSERT(g.ntot == 1 + 5);
0564       assert(!g.aborted);
0565     }
0566     {
0567       DST df = detsets[1020];
0568       CPPUNIT_ASSERT(df.id() == 1020);
0569       CPPUNIT_ASSERT(df.size() == 0);
0570       CPPUNIT_ASSERT(g.ntot == 1 + 5);
0571       assert(g.aborted);
0572     }
0573 
0574   } catch (edm::Exception const &) {
0575     CPPUNIT_ASSERT("DetSetVector threw when not expected" == 0);
0576   }
0577   // no onDemand!
0578   int i = 0;
0579   //  for (auto di = detsets.begin(false); di!=detsets.end(false); ++di) {
0580   for (auto di = detsets.begin(); di != detsets.end(); ++di) {
0581     ++i;
0582     auto ds = *di;
0583     auto id = ds.id();
0584     CPPUNIT_ASSERT(id == 1020 || (id > 20 && id < 28 && id % 2 == 1));
0585     if (1020 == id || 21 == id || 25 == id)
0586       CPPUNIT_ASSERT(ds.isValid());
0587     else
0588       CPPUNIT_ASSERT(!ds.isValid());
0589   }
0590   CPPUNIT_ASSERT(5 == i);
0591   CPPUNIT_ASSERT(g.ntot == 1 + 5);
0592 
0593   //  CPPUNIT_ASSERT(std::for_each(detsets.begin(),detsets.end(),VerifyIter(this,1,2)).n==9);
0594   CPPUNIT_ASSERT(std::for_each(detsets.begin(true), detsets.end(true), VerifyIter(this, 1, 2)).n == 9);
0595 
0596   //  CPPUNIT_ASSERT(std::for_each(detsets.begin(),detsets.end(),VerifyIter(this,1,2)).n==9);
0597   CPPUNIT_ASSERT(std::for_each(detsets.begin(true), detsets.end(true), VerifyIter(this, 1, 2)).n == 9);
0598   CPPUNIT_ASSERT(g.ntot == 1 + 3 + 5 + 7);
0599 
0600   try {
0601     DSTV::const_iterator p = detsets.find(22);
0602     CPPUNIT_ASSERT(p == detsets.end());
0603   } catch (edm::Exception const &) {
0604     CPPUNIT_ASSERT("find thrown edm exception when not expected" == 0);
0605   }
0606   try {
0607     detsets[22];
0608     CPPUNIT_ASSERT("[] did not throw" == 0);
0609   } catch (edm::Exception const &err) {
0610     CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
0611   }
0612 
0613   DSTV detsets2;
0614   detsets2.swap(detsets);
0615   CPPUNIT_ASSERT(detsets2.onDemand());
0616 
0617   DSTV detsets3;
0618   detsets3 = std::move(detsets2);
0619   CPPUNIT_ASSERT(detsets3.onDemand());
0620   DSTV detsets5(std::move(detsets3));
0621   CPPUNIT_ASSERT(detsets5.onDemand());
0622 
0623   DSTV detsets4(detsets5);
0624   CPPUNIT_ASSERT(detsets4.onDemand());
0625 }
0626 
0627 void TestDetSet::toRangeMap() {
0628   DSTV detsets(2);
0629   for (unsigned int n = 1; n < 5; ++n) {
0630     unsigned int id = 20 + n;
0631     FF ff(detsets, id);
0632     ff.resize(n);
0633     std::copy(sv.begin(), sv.begin() + n, ff.begin());
0634   }
0635   {
0636     FF ff(detsets, 31);
0637     ff.resize(2);
0638     std::copy(sv.begin(), sv.begin() + 2, ff.begin());
0639   }
0640   {
0641     FF ff(detsets, 11);
0642     ff.resize(2);
0643     std::copy(sv.begin(), sv.begin() + 2, ff.begin());
0644   }
0645   {
0646     FF ff(detsets, 34);
0647     ff.resize(4);
0648     std::copy(sv.begin(), sv.begin() + 4, ff.begin());
0649   }
0650 
0651   typedef edm::RangeMap<det_id_type, edm::OwnVector<B> > RM;
0652   edm::RangeMap<det_id_type, edm::OwnVector<B> > rm;
0653   try {
0654     edmNew::copy(detsets, rm);
0655     rm.post_insert();
0656     std::vector<det_id_type> ids = rm.ids();
0657     CPPUNIT_ASSERT(ids.size() == detsets.size());
0658     CPPUNIT_ASSERT(rm.size() == detsets.dataSize());
0659     for (int i = 0; i < int(ids.size()); i++) {
0660       RM::range r = rm.get(ids[i]);
0661       DST df = *detsets.find(ids[i]);
0662       //      DST df = *detsets.find(ids[i],true);
0663       CPPUNIT_ASSERT(static_cast<unsigned long>(r.second - r.first) == df.size());
0664       CPPUNIT_ASSERT(std::equal(r.first, r.second, df.begin()));
0665     }
0666   } catch (edm::Exception const &err) {
0667     std::cout << err.what() << std::endl;
0668     CPPUNIT_ASSERT(err.what() == 0);
0669   }
0670 }