B

Getter

T

TestDetSet

VerifyAlgos

VerifyIter

cmp10

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
#include "Utilities/Testing/interface/CppUnit_testdriver.icpp"  //gives main
#include "cppunit/extensions/HelperMacros.h"

#include "DataFormats/Common/interface/DetSetNew.h"
#include "DataFormats/Common/interface/DetSetVectorNew.h"
#include "DataFormats/Common/interface/DetSetAlgorithm.h"
#include "DataFormats/Common/interface/DetSet2RangeMap.h"

#include "FWCore/Utilities/interface/EDMException.h"

#include <algorithm>
#include <vector>

struct B {
  virtual ~B() {}
  virtual B *clone() const = 0;
};

struct T : public B {
  T(float iv = 0) : v(iv) {}
  float v;
  bool operator==(T t) const { return v == t.v; }

  T *clone() const final { return new T(*this); }
};

bool operator==(T const &t, B const &b) {
  T const *p = dynamic_cast<T const *>(&b);
  return p && p->v == t.v;
}

bool operator==(B const &b, T const &t) { return t == b; }

typedef edmNew::DetSetVector<T> DSTV;
typedef edmNew::DetSet<T> DST;
typedef edmNew::det_id_type det_id_type;
typedef DSTV::FastFiller FF;
typedef DSTV::TSFastFiller TSFF;

class TestDetSet : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(TestDetSet);
  CPPUNIT_TEST(default_ctor);
  CPPUNIT_TEST(inserting);
  CPPUNIT_TEST(filling);
  CPPUNIT_TEST(fillingTS);
  CPPUNIT_TEST(iterator);
  CPPUNIT_TEST(algorithm);
  CPPUNIT_TEST(onDemand);
  CPPUNIT_TEST(toRangeMap);

  CPPUNIT_TEST_SUITE_END();

public:
  TestDetSet();
  ~TestDetSet() {}
  void setUp() {}
  void tearDown() {}

  void default_ctor();
  void inserting();
  void filling();
  void fillingTS();
  void iterator();
  void algorithm();
  void onDemand();
  void toRangeMap();

public:
  std::vector<DSTV::data_type> sv;
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestDetSet);

TestDetSet::TestDetSet() : sv(10) {
  DSTV::data_type v[10] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
  std::copy(v, v + 10, sv.begin());
}

void TestDetSet::default_ctor() {
#ifdef CMS_NOCXX11
  CPPUNIT_ASSERT(false && "CMS_NOCXX11");
#endif

  DSTV detsets(2);
  CPPUNIT_ASSERT(!detsets.onDemand());
  CPPUNIT_ASSERT(detsets.subdetId() == 2);
  CPPUNIT_ASSERT(detsets.size() == 0);
  CPPUNIT_ASSERT(detsets.empty());
  CPPUNIT_ASSERT(detsets.dataSize() == 0);
  detsets.resize(3, 10);
  CPPUNIT_ASSERT(detsets.size() == 3);
  CPPUNIT_ASSERT(detsets.dataSize() == 10);
  CPPUNIT_ASSERT(!detsets.empty());
  // follow is nonsense still valid construct... (maybe it shall throw...)
  // now assert!
  /*
  DST df(detsets,detsets.item(1));
  CPPUNIT_ASSERT(df.id()==0);
  CPPUNIT_ASSERT(df.size()==0);
  CPPUNIT_ASSERT(df.data()+1==&detsets.m_data.front());
  df.set(detsets,detsets.item(2));
  CPPUNIT_ASSERT(df.size()==0);
  CPPUNIT_ASSERT(df.data()+1==&detsets.m_data.front());
  */

  // test swap
  DSTV detsets2(3);
  detsets.swap(detsets2);
  CPPUNIT_ASSERT(detsets.subdetId() == 3);
  CPPUNIT_ASSERT(detsets.size() == 0);
  CPPUNIT_ASSERT(detsets.dataSize() == 0);
  CPPUNIT_ASSERT(detsets2.subdetId() == 2);
  CPPUNIT_ASSERT(detsets2.size() == 3);
  CPPUNIT_ASSERT(detsets2.dataSize() == 10);
  CPPUNIT_ASSERT(!detsets2.onDemand());

  // test move
  DSTV detsets3(4);
  detsets = std::move(detsets3);
  CPPUNIT_ASSERT(detsets.subdetId() == 4);
  CPPUNIT_ASSERT(detsets.size() == 0);
  CPPUNIT_ASSERT(detsets.dataSize() == 0);
  DSTV detsets5(std::move(detsets));
  CPPUNIT_ASSERT(detsets5.subdetId() == 4);
  CPPUNIT_ASSERT(detsets5.size() == 0);
  CPPUNIT_ASSERT(detsets5.dataSize() == 0);

  // test copy
  DSTV detsets4(detsets5);
  CPPUNIT_ASSERT(detsets4.subdetId() == 4);
  CPPUNIT_ASSERT(detsets4.size() == 0);
  CPPUNIT_ASSERT(detsets4.dataSize() == 0);
}

void TestDetSet::inserting() {
  DSTV detsets(2);
  unsigned int ntot = 0;
  for (unsigned int n = 1; n < 5; ++n) {
    ntot += n;
    unsigned int id = 20 + n;
    DST df = detsets.insert(id, n);
    CPPUNIT_ASSERT(detsets.size() == n);
    CPPUNIT_ASSERT(detsets.dataSize() == ntot);
    CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == n);
    CPPUNIT_ASSERT(df.size() == n);
    CPPUNIT_ASSERT(df.id() == id);

    std::copy(sv.begin(), sv.begin() + n, df.begin());

    DSTV detsets2(detsets);
    CPPUNIT_ASSERT(detsets2.size() == n);
    CPPUNIT_ASSERT(detsets2.dataSize() == ntot);
    CPPUNIT_ASSERT(detsets2.detsetSize(n - 1) == n);

    std::vector<DST::data_type> v1(n);
    std::vector<DST::data_type> v2(n);
    std::vector<DST::data_type> v3(n);
    std::copy(detsets.m_data.begin() + ntot - n, detsets.m_data.begin() + ntot, v2.begin());
    std::copy(detsets2.m_data.begin() + ntot - n, detsets2.m_data.begin() + ntot, v3.begin());
    std::copy(sv.begin(), sv.begin() + n, v1.begin());
    CPPUNIT_ASSERT(v1 == v2);
    CPPUNIT_ASSERT(v1 == v3);
  }

  // test error conditions
  try {
    detsets.insert(22, 6);
    CPPUNIT_ASSERT("insert did not throw" == 0);
  } catch (edm::Exception const &err) {
    CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
  }
}

void TestDetSet::filling() {
  DSTV detsets(2);
  unsigned int ntot = 0;
  for (unsigned int n = 1; n < 5; ++n) {
    unsigned int id = 20 + n;
    FF ff(detsets, id);
    CPPUNIT_ASSERT(detsets.size() == n);
    CPPUNIT_ASSERT(detsets.dataSize() == ntot);
    CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == 0);
    CPPUNIT_ASSERT(ff.m_item.offset == int(detsets.dataSize()));
    CPPUNIT_ASSERT(ff.m_item.size == 0);
    CPPUNIT_ASSERT(ff.m_item.id == id);
    ntot += 1;
    ff.push_back(3.14);
    CPPUNIT_ASSERT(detsets.dataSize() == ntot);
    CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == 1);
    CPPUNIT_ASSERT(detsets.m_data.back().v == 3.14f);
    CPPUNIT_ASSERT(ff.m_item.offset == int(detsets.dataSize()) - 1);
    CPPUNIT_ASSERT(ff.m_item.size == 1);
    ntot += n - 1;
    ff.resize(n);
    CPPUNIT_ASSERT(detsets.dataSize() == ntot);
    CPPUNIT_ASSERT(detsets.detsetSize(n - 1) == n);
    CPPUNIT_ASSERT(ff.m_item.offset == int(detsets.dataSize() - n));
    CPPUNIT_ASSERT(ff.m_item.size == n);

    std::copy(sv.begin(), sv.begin() + n, ff.begin());

    std::vector<DST::data_type> v1(n);
    std::vector<DST::data_type> v2(n);
    std::copy(detsets.m_data.begin() + ntot - n, detsets.m_data.begin() + ntot, v2.begin());
    std::copy(sv.begin(), sv.begin() + n, v1.begin());
    CPPUNIT_ASSERT(v1 == v2);
  }

  // test abort and empty
  {
    FF ff1(detsets, 30);
    CPPUNIT_ASSERT(detsets.size() == 5);
    CPPUNIT_ASSERT(detsets.exists(30));
    CPPUNIT_ASSERT(detsets.isValid(30));
  }
  CPPUNIT_ASSERT(detsets.size() == 4);
  CPPUNIT_ASSERT(!detsets.exists(30));
  {
    FF ff1(detsets, 30, true);
    CPPUNIT_ASSERT(detsets.size() == 5);
    CPPUNIT_ASSERT(detsets.exists(30));
    CPPUNIT_ASSERT(detsets.isValid(30));
  }
  CPPUNIT_ASSERT(detsets.size() == 5);
  CPPUNIT_ASSERT(detsets.exists(30));

  {
    DSTV detsets2(detsets);
    CPPUNIT_ASSERT(detsets2.size() == 5);
    CPPUNIT_ASSERT(detsets2.exists(30));
  }

  {
    unsigned int cs = detsets.dataSize();
    FF ff1(detsets, 31);
    ff1.resize(4);
    CPPUNIT_ASSERT(detsets.size() == 6);
    CPPUNIT_ASSERT(detsets.dataSize() == cs + 4);
    CPPUNIT_ASSERT(detsets.exists(31));
    CPPUNIT_ASSERT(detsets.isValid(31));
    ff1.abort();
    CPPUNIT_ASSERT(detsets.size() == 5);
    CPPUNIT_ASSERT(detsets.dataSize() == cs);
    CPPUNIT_ASSERT(!detsets.exists(31));
  }
  CPPUNIT_ASSERT(detsets.size() == 5);
  CPPUNIT_ASSERT(!detsets.exists(31));
  { FF ff1(detsets, 32, true); }
  CPPUNIT_ASSERT(detsets.size() == 6);

  DSTV detsets2(detsets);
  CPPUNIT_ASSERT(detsets2.size() == 6);

  detsets.clean();
  CPPUNIT_ASSERT(detsets.size() == 4);
  CPPUNIT_ASSERT(!detsets.exists(30));
  CPPUNIT_ASSERT(!detsets.exists(32));

  CPPUNIT_ASSERT(detsets2.size() == 6);
  CPPUNIT_ASSERT(detsets2.exists(30));
  CPPUNIT_ASSERT(detsets2.exists(32));
  detsets2.clean();
  CPPUNIT_ASSERT(detsets2.size() == 4);
  CPPUNIT_ASSERT(!detsets2.exists(30));
  CPPUNIT_ASSERT(!detsets2.exists(32));

  // test error conditions
  try {
    FF ff1(detsets, 22);
    CPPUNIT_ASSERT(" fast filler did not throw" == 0);
  } catch (edm::Exception const &err) {
    CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
  }

  try {
    FF ff1(detsets, 44);
    FF ff2(detsets, 45);
    CPPUNIT_ASSERT(" fast filler did not throw" == 0);
  } catch (edm::Exception const &err) {
    CPPUNIT_ASSERT(err.categoryCode() == edm::errors::LogicError);
  }
}

void TestDetSet::fillingTS() {
  DSTV detsets(2);
  detsets.reserve(5, 100);
  unsigned int ntot = 0;
  for (unsigned int n = 1; n < 5; ++n) {
    unsigned int id = 20 + n;
    {
      TSFF ff(detsets, id);
      CPPUNIT_ASSERT(detsets.size() == n);
      CPPUNIT_ASSERT(detsets.dataSize() == ntot);
      CPPUNIT_ASSERT(ff.m_item.size == 0);
      CPPUNIT_ASSERT(ff.id() == id);
      ntot += 1;
      ff.push_back(3.14);
      CPPUNIT_ASSERT(detsets.dataSize() == ntot - 1);
      CPPUNIT_ASSERT(ff.m_item.size == 0);
      CPPUNIT_ASSERT(ff.size() == 1);
      ntot += n - 1;
      ff.resize(n);
      CPPUNIT_ASSERT(ff.size() == n);
      std::copy(sv.begin(), sv.begin() + n, ff.begin());
    }
    CPPUNIT_ASSERT(detsets.size() == n);
    CPPUNIT_ASSERT(detsets.dataSize() == ntot);

    std::vector<DST::data_type> v1(n);
    std::vector<DST::data_type> v2(n);
    std::copy(detsets.m_data.begin() + ntot - n, detsets.m_data.begin() + ntot, v2.begin());
    std::copy(sv.begin(), sv.begin() + n, v1.begin());
    CPPUNIT_ASSERT(v1 == v2);
  }

  // test abort and empty
  {
    TSFF ff1(detsets, 30);
    CPPUNIT_ASSERT(detsets.exists(30));
  }
  CPPUNIT_ASSERT(detsets.size() == 5);
  CPPUNIT_ASSERT(detsets.exists(30));
  detsets.clean();
  CPPUNIT_ASSERT(detsets.size() == 4);
  CPPUNIT_ASSERT(!detsets.exists(30));

  unsigned int cs = detsets.dataSize();
  {
    TSFF ff1(detsets, 31);
    ff1.resize(4);
    ff1.abort();
  }
  CPPUNIT_ASSERT(detsets.size() == 5);
  CPPUNIT_ASSERT(detsets.dataSize() == cs);
  CPPUNIT_ASSERT(detsets.exists(31));
  detsets.clean();
  CPPUNIT_ASSERT(detsets.size() == 4);
  CPPUNIT_ASSERT(!detsets.exists(31));

  // test error conditions
  try {
    TSFF ff1(detsets, 22);
    CPPUNIT_ASSERT(" fast filler did not throw" == 0);
  } catch (edm::Exception const &err) {
    CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
  }

  try {
    TSFF ff1(detsets, 44);
    TSFF ff2(detsets, 45);
    CPPUNIT_ASSERT(" fast filler did not throw" == 0);
  } catch (edm::Exception const &err) {
    CPPUNIT_ASSERT(err.categoryCode() == edm::errors::LogicError);
  }
}

namespace {
  struct VerifyIter {
    VerifyIter(TestDetSet *itest, unsigned int in = 1, int iincr = 1) : n(in), incr(iincr), test(*itest) {}

    void operator()(DST const &df) {
      if (df.id() > 1000) {
        CPPUNIT_ASSERT(df.size() == 0);
        return;
      }
      CPPUNIT_ASSERT(df.id() == 20 + n);
      CPPUNIT_ASSERT(df.size() == n);
      std::vector<DST::data_type> v1(n);
      std::vector<DST::data_type> v2(n);
      std::copy(df.begin(), df.end(), v2.begin());
      std::copy(test.sv.begin(), test.sv.begin() + n, v1.begin());
      CPPUNIT_ASSERT(v1 == v2);
      n += incr;
    }

    unsigned int n;
    int incr;
    TestDetSet &test;
  };

  struct Getter final : public DSTV::Getter {
    Getter(TestDetSet *itest) : ntot(0), test(*itest) {}

    void fill(TSFF &ff) const override {
      aborted = false;
      try {
        const int n = ff.id() - 20;
        CPPUNIT_ASSERT(n > 0);
        ff.resize(n);
        int nCopied = n;
        if (static_cast<size_t>(n) > test.sv.size()) {
          nCopied = test.sv.size();
        }
        std::copy(test.sv.begin(), test.sv.begin() + nCopied, ff.begin());
        if (ff.full()) {
          ff.abort();
          aborted = true;
        } else {
          ntot += n;
        }
      } catch (edmNew::CapacityExaustedException const &) {
        CPPUNIT_ASSERT("cannot be here" == 0);
      }
    }

    mutable unsigned int ntot;
    mutable bool aborted = false;
    TestDetSet &test;
  };

  struct cmp10 {
    bool operator()(DSTV::id_type i1, DSTV::id_type i2) const { return i1 / 10 < i2 / 10; }
  };
}  // namespace

void TestDetSet::iterator() {
  DSTV detsets(2);
  for (unsigned int n = 1; n < 5; ++n) {
    unsigned int id = 20 + n;
    FF ff(detsets, id);
    ff.resize(n);
    std::copy(sv.begin(), sv.begin() + n, ff.begin());
  }
  //  CPPUNIT_ASSERT(std::for_each(detsets.begin(),detsets.end(),VerifyIter(this)).n==5);
  CPPUNIT_ASSERT(std::for_each(detsets.begin(true), detsets.end(true), VerifyIter(this)).n == 5);
  {
    FF ff(detsets, 31);
    ff.resize(2);
    std::copy(sv.begin(), sv.begin() + 2, ff.begin());
  }
  {
    FF ff(detsets, 11);
    ff.resize(2);
    std::copy(sv.begin(), sv.begin() + 2, ff.begin());
  }
  {
    FF ff(detsets, 34);
    ff.resize(4);
    std::copy(sv.begin(), sv.begin() + 4, ff.begin());
  }

  DSTV::Range r = detsets.equal_range(30, cmp10());
  CPPUNIT_ASSERT(r.second - r.first == 2);
  r = detsets.equal_range(40, cmp10());
  CPPUNIT_ASSERT(r.second - r.first == 0);

  try {
    {
      CPPUNIT_ASSERT(detsets.exists(22));
      CPPUNIT_ASSERT(!detsets.exists(44));
      DST df = *detsets.find(22);
      //      DST df = *detsets.find(22,true);
      CPPUNIT_ASSERT(df.id() == 22);
      CPPUNIT_ASSERT(df.size() == 2);
    }
    {
      DST df = detsets[22];
      CPPUNIT_ASSERT(df.id() == 22);
      CPPUNIT_ASSERT(df.size() == 2);
    }
  } catch (edm::Exception const &) {
    CPPUNIT_ASSERT("DetSetVector threw when not expected" == 0);
  }

  try {
    DSTV::const_iterator p = detsets.find(44);
    //    DSTV::const_iterator p = detsets.find(44,true);
    CPPUNIT_ASSERT(p == detsets.end());
    //    CPPUNIT_ASSERT(p==detsets.end(true));
  } catch (edm::Exception const &) {
    CPPUNIT_ASSERT("find thrown edm exception when not expected" == 0);
  }
  try {
    detsets[44];
    CPPUNIT_ASSERT("[] did not throw" == 0);
  } catch (edm::Exception const &err) {
    CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
  }
}

namespace {

  std::pair<unsigned int, cmp10> acc(unsigned int i) { return std::make_pair(i * 10, cmp10()); }

  struct VerifyAlgos {
    VerifyAlgos(std::vector<DSTV::data_type const *> &iv) : n(0), v(iv) {}

    void operator()(DSTV::data_type const &d) {
      CPPUNIT_ASSERT(d == *v[n]);
      CPPUNIT_ASSERT(&d == v[n]);
      ++n;
    }

    int n;
    std::vector<DSTV::data_type const *> const &v;
  };

}  // namespace

void TestDetSet::algorithm() {
  DSTV detsets(2);
  for (unsigned int n = 1; n < 5; ++n) {
    unsigned int id = 20 + n;
    FF ff(detsets, id);
    ff.resize(n);
    std::copy(sv.begin(), sv.begin() + n, ff.begin());
  }
  {
    FF ff(detsets, 31);
    ff.resize(2);
    std::copy(sv.begin(), sv.begin() + 2, ff.begin());
  }
  {
    FF ff(detsets, 11);
    ff.resize(2);
    std::copy(sv.begin(), sv.begin() + 2, ff.begin());
  }
  {
    FF ff(detsets, 34);
    ff.resize(4);
    std::copy(sv.begin(), sv.begin() + 4, ff.begin());
  }

  DSTV::Range r = detsetRangeFromPair(detsets, acc(3));
  CPPUNIT_ASSERT(r.second - r.first == 2);
  r = edmNew::detsetRangeFromPair(detsets, acc(4));
  CPPUNIT_ASSERT(r.second - r.first == 0);

  std::vector<DSTV::data_type const *> v;
  edmNew::copyDetSetRange(detsets, v, acc(3));
  VerifyAlgos va(v);
  edmNew::foreachDetSetObject(detsets, acc(3), va);
}

void TestDetSet::onDemand() {
  auto pg = std::make_shared<Getter>(this);
  Getter &g = *pg;
  assert(!g.aborted);
  std::vector<unsigned int> v = {21, 23, 25, 27, 1020};
  DSTV detsets(pg, v, 2);
  CPPUNIT_ASSERT(g.ntot == 0);
  CPPUNIT_ASSERT(detsets.onDemand());
  try {
    {
      CPPUNIT_ASSERT(detsets.exists(21));
      CPPUNIT_ASSERT(!detsets.exists(22));
      CPPUNIT_ASSERT(!detsets.isValid(21));
      CPPUNIT_ASSERT(!detsets.isValid(22));
      //      DST df = *detsets.find(21);
      detsets.reserve(5, 100);
      DST df = *detsets.find(21, true);
      CPPUNIT_ASSERT(df.id() == 21);
      CPPUNIT_ASSERT(df.size() == 1);
      CPPUNIT_ASSERT(detsets.isValid(21));
      CPPUNIT_ASSERT(!detsets.isValid(23));
      CPPUNIT_ASSERT(g.ntot == 1);
      assert(!g.aborted);
    }
    {
      DST df = detsets[25];
      CPPUNIT_ASSERT(df.id() == 25);
      CPPUNIT_ASSERT(df.size() == 5);
      CPPUNIT_ASSERT(g.ntot == 1 + 5);
      assert(!g.aborted);
    }
    {
      DST df = detsets[1020];
      CPPUNIT_ASSERT(df.id() == 1020);
      CPPUNIT_ASSERT(df.size() == 0);
      CPPUNIT_ASSERT(g.ntot == 1 + 5);
      assert(g.aborted);
    }

  } catch (edm::Exception const &) {
    CPPUNIT_ASSERT("DetSetVector threw when not expected" == 0);
  }
  // no onDemand!
  int i = 0;
  //  for (auto di = detsets.begin(false); di!=detsets.end(false); ++di) {
  for (auto di = detsets.begin(); di != detsets.end(); ++di) {
    ++i;
    auto ds = *di;
    auto id = ds.id();
    CPPUNIT_ASSERT(id == 1020 || (id > 20 && id < 28 && id % 2 == 1));
    if (1020 == id || 21 == id || 25 == id)
      CPPUNIT_ASSERT(ds.isValid());
    else
      CPPUNIT_ASSERT(!ds.isValid());
  }
  CPPUNIT_ASSERT(5 == i);
  CPPUNIT_ASSERT(g.ntot == 1 + 5);

  //  CPPUNIT_ASSERT(std::for_each(detsets.begin(),detsets.end(),VerifyIter(this,1,2)).n==9);
  CPPUNIT_ASSERT(std::for_each(detsets.begin(true), detsets.end(true), VerifyIter(this, 1, 2)).n == 9);

  //  CPPUNIT_ASSERT(std::for_each(detsets.begin(),detsets.end(),VerifyIter(this,1,2)).n==9);
  CPPUNIT_ASSERT(std::for_each(detsets.begin(true), detsets.end(true), VerifyIter(this, 1, 2)).n == 9);
  CPPUNIT_ASSERT(g.ntot == 1 + 3 + 5 + 7);

  try {
    DSTV::const_iterator p = detsets.find(22);
    CPPUNIT_ASSERT(p == detsets.end());
  } catch (edm::Exception const &) {
    CPPUNIT_ASSERT("find thrown edm exception when not expected" == 0);
  }
  try {
    detsets[22];
    CPPUNIT_ASSERT("[] did not throw" == 0);
  } catch (edm::Exception const &err) {
    CPPUNIT_ASSERT(err.categoryCode() == edm::errors::InvalidReference);
  }

  DSTV detsets2;
  detsets2.swap(detsets);
  CPPUNIT_ASSERT(detsets2.onDemand());

  DSTV detsets3;
  detsets3 = std::move(detsets2);
  CPPUNIT_ASSERT(detsets3.onDemand());
  DSTV detsets5(std::move(detsets3));
  CPPUNIT_ASSERT(detsets5.onDemand());

  DSTV detsets4(detsets5);
  CPPUNIT_ASSERT(detsets4.onDemand());
}

void TestDetSet::toRangeMap() {
  DSTV detsets(2);
  for (unsigned int n = 1; n < 5; ++n) {
    unsigned int id = 20 + n;
    FF ff(detsets, id);
    ff.resize(n);
    std::copy(sv.begin(), sv.begin() + n, ff.begin());
  }
  {
    FF ff(detsets, 31);
    ff.resize(2);
    std::copy(sv.begin(), sv.begin() + 2, ff.begin());
  }
  {
    FF ff(detsets, 11);
    ff.resize(2);
    std::copy(sv.begin(), sv.begin() + 2, ff.begin());
  }
  {
    FF ff(detsets, 34);
    ff.resize(4);
    std::copy(sv.begin(), sv.begin() + 4, ff.begin());
  }

  typedef edm::RangeMap<det_id_type, edm::OwnVector<B> > RM;
  edm::RangeMap<det_id_type, edm::OwnVector<B> > rm;
  try {
    edmNew::copy(detsets, rm);
    rm.post_insert();
    std::vector<det_id_type> ids = rm.ids();
    CPPUNIT_ASSERT(ids.size() == detsets.size());
    CPPUNIT_ASSERT(rm.size() == detsets.dataSize());
    for (int i = 0; i < int(ids.size()); i++) {
      RM::range r = rm.get(ids[i]);
      DST df = *detsets.find(ids[i]);
      //      DST df = *detsets.find(ids[i],true);
      CPPUNIT_ASSERT(static_cast<unsigned long>(r.second - r.first) == df.size());
      CPPUNIT_ASSERT(std::equal(r.first, r.second, df.begin()));
    }
  } catch (edm::Exception const &err) {
    std::cout << err.what() << std::endl;
    CPPUNIT_ASSERT(err.what() == 0);
  }
}