Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*
0002  *  sortedcollection_t.cppunit.cc
0003  *  CMSSW
0004  *
0005  */
0006 
0007 // This is a serious hack! Remove it as soon as we get templated on 'key'.
0008 typedef int DetId;
0009 
0010 #include <algorithm>
0011 #include <iostream>
0012 #include <iterator>
0013 #include <ostream>
0014 #include <vector>
0015 
0016 #include "cppunit/extensions/HelperMacros.h"
0017 #include "DataFormats/Common/interface/SortedCollection.h"
0018 
0019 using namespace edm;
0020 
0021 //------------------------------------------------------
0022 // This is a sample VALUE class, almost the simplest possible.
0023 //------------------------------------------------------
0024 
0025 class Value {
0026 public:
0027   // The following is a hack, to make this class Value look like the
0028   // real use case.
0029   typedef int DetId;
0030 
0031   // VALUES must declare a key_type
0032   typedef DetId key_type;
0033 
0034   // VALUES must be default constructible
0035   Value() : d_(0.0), id_(0) {}
0036 
0037   // This constructor is used for testing; it is not required by the
0038   // concept VALUE.
0039   Value(double d, DetId anId) : d_(d), id_(anId) {}
0040 
0041   // This access function is used for testing; it is not required by
0042   // the concept VALUE.
0043   double val() const { return d_; }
0044 
0045   // VALUES must have a const member function id() that returns a
0046   // key_type. N.B.: here, DetId is key_type.
0047   DetId id() const { return id_; }
0048 
0049   // VALUES must be destructible
0050   ~Value() {}
0051 
0052   // The private stuff below is all implementation detail, and not
0053   // required by the concept VALUE.
0054 private:
0055   double d_;
0056   DetId id_;
0057 };
0058 
0059 //------------------------------------------------------
0060 // If one wants to test SortedCollections using operator==, then one
0061 // must use a VALUE that is equality comparable. Otherwise, VALUE need
0062 // not be equality comparable.
0063 //------------------------------------------------------
0064 
0065 bool operator==(Value const& a, Value const& b) { return (a.id() == b.id()) && (a.val() == b.val()); }
0066 
0067 //------------------------------------------------------
0068 // The stream insertion operator is not required; it is used here
0069 // for diagnostic output.
0070 //------------------------------------------------------
0071 std::ostream& operator<<(std::ostream& os, const Value& v) {
0072   os << "id: " << v.id() << " val: " << v.val();
0073   return os;
0074 }
0075 
0076 class testSortedCollection : public CppUnit::TestFixture {
0077   CPPUNIT_TEST_SUITE(testSortedCollection);
0078   CPPUNIT_TEST(constructTest);
0079   CPPUNIT_TEST(insertTest);
0080   CPPUNIT_TEST(accessTest);
0081   CPPUNIT_TEST(swapTest);
0082   CPPUNIT_TEST(frontbackTest);
0083   CPPUNIT_TEST(squarebracketTest);
0084   CPPUNIT_TEST_SUITE_END();
0085 
0086 public:
0087   void setUp() {}
0088   void tearDown() {}
0089 
0090   void constructTest();
0091   void insertTest();
0092   void accessTest();
0093   void swapTest();
0094   void frontbackTest();
0095   void squarebracketTest();
0096 };
0097 
0098 ///registration of the test so that the runner can find it
0099 CPPUNIT_TEST_SUITE_REGISTRATION(testSortedCollection);
0100 
0101 typedef edm::SortedCollection<Value, StrictWeakOrdering<Value> > scoll_type;
0102 
0103 void testSortedCollection::constructTest() {
0104   scoll_type c1;
0105   CPPUNIT_ASSERT(c1.size() == 0);
0106 
0107   scoll_type c2(20);
0108   CPPUNIT_ASSERT(c2.size() == 20);
0109 
0110   std::vector<Value> values(3);
0111   scoll_type c3(values);
0112   CPPUNIT_ASSERT(c3.size() == values.size());
0113 
0114   scoll_type c4(c3);
0115   CPPUNIT_ASSERT(c4.size() == c3.size());
0116   CPPUNIT_ASSERT(c3 == c4);
0117 }
0118 
0119 void testSortedCollection::insertTest() {
0120   scoll_type c;
0121   Value v1(1.1, 1);
0122   Value v2(2.2, 2);
0123 
0124   c.push_back(v1);
0125   CPPUNIT_ASSERT(c.size() == 1);
0126   c.push_back(v2);
0127   CPPUNIT_ASSERT(c.size() == 2);
0128 }
0129 
0130 template <class T, class SORT>
0131 void append_to_both(edm::SortedCollection<T, SORT>& sc, std::vector<T>& vec, const T& t) {
0132   sc.push_back(t);
0133   vec.push_back(t);
0134 }
0135 
0136 void testSortedCollection::accessTest() {
0137   scoll_type c;
0138   std::vector<Value> vec;
0139   append_to_both(c, vec, Value(1.5, 3));
0140   append_to_both(c, vec, Value(2.5, 200));
0141   append_to_both(c, vec, Value(3.5, 1));
0142   append_to_both(c, vec, Value(4.5, 1001));
0143   append_to_both(c, vec, Value(5.5, 2));
0144   //  append_to_both(c, vec, Value(10.5, 3));
0145 
0146   CPPUNIT_ASSERT(c.size() == vec.size());
0147   c.sort();
0148 
0149   // DO NOT ADD ANY MORE ITEMS TO c!
0150   CPPUNIT_ASSERT(c.size() == vec.size());
0151 
0152   {
0153     scoll_type::iterator i = c.find(DetId(100));  // does not exist!
0154     CPPUNIT_ASSERT(i == c.end());
0155   }
0156 
0157   {
0158     std::cerr << "Dumping SortedCollection" << std::endl;
0159     std::copy(c.begin(), c.end(), std::ostream_iterator<Value>(std::cerr, "\n"));
0160   }
0161 
0162   {
0163     std::vector<Value>::const_iterator i = vec.begin();
0164     std::vector<Value>::const_iterator e = vec.end();
0165     std::cerr << "There are " << vec.size() << " searches to do...\n";
0166     while (i != e) {
0167       DetId id = i->id();
0168       std::cerr << "Looking for id: " << id << "...   ";
0169       //scoll_type::iterator loc = c.find(i->id());
0170       scoll_type::iterator loc = c.find(id);
0171       if (loc == c.end())
0172         std::cerr << "Failed to find this id!\n";
0173       else
0174         std::cerr << "Found it, record is: " << *loc << '\n';
0175       CPPUNIT_ASSERT(loc != c.end());
0176       CPPUNIT_ASSERT(*loc == *i);
0177       ++i;
0178     }
0179   }
0180 }
0181 
0182 void testSortedCollection::swapTest() {
0183   scoll_type c;
0184   std::vector<Value> vec;
0185   append_to_both(c, vec, Value(1.5, 3));
0186   append_to_both(c, vec, Value(2.5, 200));
0187   append_to_both(c, vec, Value(3.5, 1));
0188   append_to_both(c, vec, Value(4.5, 1001));
0189   append_to_both(c, vec, Value(5.5, 2));
0190 
0191   {
0192     scoll_type copy(c);
0193     CPPUNIT_ASSERT(copy == c);
0194     scoll_type empty;
0195     CPPUNIT_ASSERT(empty.empty());
0196 
0197     empty.swap(copy);
0198     CPPUNIT_ASSERT(copy.empty());
0199     CPPUNIT_ASSERT(empty == c);
0200   }
0201 
0202   {
0203     std::vector<Value> copy(vec);
0204     scoll_type empty;
0205     CPPUNIT_ASSERT(empty.empty());
0206 
0207     empty.swap_contents(copy);
0208     CPPUNIT_ASSERT(copy.empty());
0209     CPPUNIT_ASSERT(empty == vec);
0210   }
0211 }
0212 
0213 void testSortedCollection::frontbackTest() {
0214   scoll_type c;
0215   std::vector<Value> vec;
0216   append_to_both(c, vec, Value(1.5, 3));
0217   append_to_both(c, vec, Value(2.5, 200));
0218   append_to_both(c, vec, Value(3.5, 1));
0219   append_to_both(c, vec, Value(4.5, 1001));
0220   append_to_both(c, vec, Value(5.5, 2));
0221 
0222   c.sort();
0223 
0224   CPPUNIT_ASSERT(c.front() == Value(3.5, 1));
0225   CPPUNIT_ASSERT(c.back() == Value(4.5, 1001));
0226 
0227   scoll_type const& cr = c;
0228   CPPUNIT_ASSERT(cr.front() == Value(3.5, 1));
0229   CPPUNIT_ASSERT(cr.back() == Value(4.5, 1001));
0230 }
0231 
0232 void testSortedCollection::squarebracketTest() {
0233   scoll_type c;
0234   std::vector<Value> vec;
0235   append_to_both(c, vec, Value(1.5, 3));
0236   append_to_both(c, vec, Value(2.5, 200));
0237   append_to_both(c, vec, Value(3.5, 1));
0238   append_to_both(c, vec, Value(4.5, 1001));
0239   append_to_both(c, vec, Value(5.5, 2));
0240 
0241   c.sort();
0242 
0243   CPPUNIT_ASSERT(c[0] == Value(3.5, 1));
0244   CPPUNIT_ASSERT(c[1] == Value(5.5, 2));
0245   CPPUNIT_ASSERT(c[2] == Value(1.5, 3));
0246   CPPUNIT_ASSERT(c[3] == Value(2.5, 200));
0247   CPPUNIT_ASSERT(c[4] == Value(4.5, 1001));
0248 
0249   scoll_type const& cr = c;
0250   CPPUNIT_ASSERT(cr[0] == Value(3.5, 1));
0251   CPPUNIT_ASSERT(cr[1] == Value(5.5, 2));
0252   CPPUNIT_ASSERT(cr[2] == Value(1.5, 3));
0253   CPPUNIT_ASSERT(cr[3] == Value(2.5, 200));
0254   CPPUNIT_ASSERT(cr[4] == Value(4.5, 1001));
0255 }