Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:56

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