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"
0002 #include "cppunit/extensions/HelperMacros.h"
0003 
0004 #include "DataFormats/Common/interface/DataFrame.h"
0005 #include "DataFormats/Common/interface/DataFrameContainer.h"
0006 #include <vector>
0007 #include <algorithm>
0008 #include <cstdlib>
0009 #include <numeric>
0010 #include <cstring>
0011 
0012 class TestDataFrame : public CppUnit::TestFixture {
0013   CPPUNIT_TEST_SUITE(TestDataFrame);
0014   CPPUNIT_TEST(default_ctor);
0015   CPPUNIT_TEST(filling);
0016   CPPUNIT_TEST(iterator);
0017   CPPUNIT_TEST(sort);
0018 
0019   CPPUNIT_TEST_SUITE_END();
0020 
0021 public:
0022   TestDataFrame();
0023   ~TestDataFrame() {}
0024   void setUp() {}
0025   void tearDown() {}
0026 
0027   void default_ctor();
0028   void filling();
0029   void iterator();
0030   void sort();
0031 
0032 public:
0033   std::vector<edm::DataFrame::data_type> sv1;
0034   std::vector<edm::DataFrame::data_type> sv2;
0035 };
0036 
0037 CPPUNIT_TEST_SUITE_REGISTRATION(TestDataFrame);
0038 
0039 TestDataFrame::TestDataFrame() : sv1(10), sv2(10) {
0040   using std::placeholders::_1;
0041   edm::DataFrame::data_type v[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
0042   std::copy(v, v + 10, sv1.begin());
0043   std::transform(sv1.begin(), sv1.end(), sv2.begin(), std::bind(std::plus<edm::DataFrame::data_type>(), 10, _1));
0044 }
0045 
0046 void TestDataFrame::default_ctor() {
0047   edm::DataFrameContainer frames(10, 2);
0048   CPPUNIT_ASSERT(frames.stride() == 10);
0049   CPPUNIT_ASSERT(frames.subdetId() == 2);
0050   CPPUNIT_ASSERT(frames.size() == 0);
0051   frames.resize(3);
0052   CPPUNIT_ASSERT(frames.size() == 3);
0053   edm::DataFrame df = frames[1];
0054   CPPUNIT_ASSERT(df.size() == 10);
0055   CPPUNIT_ASSERT(df.m_data == &frames.m_data.front() + 10);
0056   df.set(frames, 2);
0057   CPPUNIT_ASSERT(df.size() == 10);
0058   CPPUNIT_ASSERT(df.m_data == &frames.m_data.front() + 20);
0059   frames.pop_back();
0060   CPPUNIT_ASSERT(frames.size() == 2);
0061   CPPUNIT_ASSERT(frames.m_data.size() == 20);
0062 }
0063 
0064 void TestDataFrame::filling() {
0065   edm::DataFrameContainer frames(10, 2);
0066   for (unsigned int n = 1; n < 5; ++n) {
0067     unsigned int id = 20 + n;
0068     frames.push_back(id);
0069     CPPUNIT_ASSERT(frames.size() == n);
0070     edm::DataFrame df = frames.back();
0071     CPPUNIT_ASSERT(df.size() == 10);
0072     CPPUNIT_ASSERT(df.id() == id);
0073 
0074     if (n % 2 == 0)
0075       std::copy(sv1.begin(), sv1.end(), df.begin());
0076     else
0077       ::memcpy(&df[0], &sv1[0], sizeof(edm::DataFrame::data_type) * frames.stride());
0078 
0079     std::vector<edm::DataFrame::data_type> v2(10);
0080     std::copy(frames.m_data.begin() + (n - 1) * 10, frames.m_data.begin() + n * 10, v2.begin());
0081     CPPUNIT_ASSERT(sv1 == v2);
0082   }
0083 }
0084 
0085 namespace {
0086   struct VerifyIter {
0087     VerifyIter(TestDataFrame* itest) : n(0), test(*itest) {}
0088 
0089     void operator()(edm::DataFrame const& df) {
0090       ++n;
0091       CPPUNIT_ASSERT(df.id() == 2000 + n);
0092       std::vector<edm::DataFrame::data_type> v2(10);
0093       std::copy(df.begin(), df.end(), v2.begin());
0094       if (n % 2 == 0)
0095         CPPUNIT_ASSERT(test.sv1 == v2);
0096       else
0097         CPPUNIT_ASSERT(test.sv2 == v2);
0098     }
0099 
0100     unsigned int n;
0101     TestDataFrame& test;
0102   };
0103 }  // namespace
0104 
0105 void TestDataFrame::iterator() {
0106   edm::DataFrameContainer frames(10, 2);
0107   for (int n = 1; n < 5; ++n) {
0108     int id = 2000 + n;
0109     frames.push_back(id);
0110     edm::DataFrame df = frames.back();
0111     if (n % 2 == 0)
0112       std::copy(sv1.begin(), sv1.end(), df.begin());
0113     else
0114       std::copy(sv2.begin(), sv2.end(), df.begin());
0115   }
0116   CPPUNIT_ASSERT(std::for_each(frames.begin(), frames.end(), VerifyIter(this)).n == 4);
0117 }
0118 
0119 void TestDataFrame::sort() {
0120   edm::DataFrameContainer frames(10, 2);
0121   std::vector<unsigned int> ids(100, 1);
0122   ids[0] = 2001;
0123   std::partial_sum(ids.begin(), ids.end(), ids.begin());
0124   std::random_shuffle(ids.begin(), ids.end());
0125 
0126   for (int n = 0; n < 100; ++n) {
0127     frames.push_back(ids[n]);
0128     edm::DataFrame df = frames.back();
0129     if (ids[n] % 2 == 0)
0130       std::copy(sv1.begin(), sv1.end(), df.begin());
0131     else
0132       std::copy(sv2.begin(), sv2.end(), df.begin());
0133   }
0134   frames.sort();
0135   CPPUNIT_ASSERT(std::for_each(frames.begin(), frames.end(), VerifyIter(this)).n == 100);
0136 }