Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DataFormats/Common/interface/OwnVector.h"
0002 #include "DataFormats/Common/interface/PtrVector.h"
0003 #include "DataFormats/Common/interface/RefVector.h"
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 
0006 #include "boost/range.hpp"
0007 
0008 #include <algorithm>
0009 #include <cassert>
0010 #include <iostream>
0011 #include <vector>
0012 #include "catch.hpp"
0013 
0014 struct Dummy {
0015   Dummy() : id() {}
0016   Dummy(int i) : id(i) {}
0017   Dummy* clone() const { return new Dummy(*this); }
0018   int id;
0019   bool operator==(int const& i) const { return id == i; }
0020 };
0021 typedef std::vector<Dummy> Coll;
0022 Coll dummies_;
0023 
0024 template <typename T>
0025 void testFill(T& r) {
0026   for (int i = 0; i < 12; ++i) {
0027     r.push_back(typename T::value_type(i));
0028   }
0029 }
0030 template <>
0031 void testFill<edm::RefVector<Coll> >(edm::RefVector<Coll>& r) {
0032   edm::TestHandle<Coll> h(&dummies_, edm::ProductID(1));
0033   for (int i = 0; i < 12; ++i) {
0034     r.push_back(edm::Ref<Coll>(h, i));
0035   }
0036 }
0037 template <>
0038 void testFill<edm::PtrVector<Dummy> >(edm::PtrVector<Dummy>& r) {
0039   edm::TestHandle<Coll> h(&dummies_, edm::ProductID(1));
0040   for (int i = 0; i < 12; ++i) {
0041     r.push_back(edm::Ptr<Dummy>(h, i));
0042   }
0043 }
0044 //template<>
0045 //void testFill<edm::OwnVector<Dummy> >(edm::OwnVector<Dummy> &r) {
0046 //    for(int i = 0; i < 12; ++i) { r.push_back(std::make_unique<Dummy>(i)); }
0047 //}
0048 
0049 bool operator==(edm::Ref<Coll> const& ref, int i) { return (int(ref.key()) == i) && (ref->id == i); }
0050 bool operator==(edm::Ptr<Dummy> const& ref, int i) { return (int(ref.key()) == i) && (ref->id == i); }
0051 std::ostream& operator<<(std::ostream& o, Dummy const& v) {
0052   o << "Dummy(" << v.id << ")";
0053   return o;
0054 }
0055 std::ostream& operator<<(std::ostream& o, edm::Ref<Coll> const& v) {
0056   o << "DummyRef(" << v.key() << " -> " << v->id << ")";
0057   return o;
0058 }
0059 std::ostream& operator<<(std::ostream& o, edm::Ptr<Dummy> const& v) {
0060   o << "DummyPtr(" << v.key() << " -> " << v->id << ")";
0061   return o;
0062 }
0063 
0064 template <typename R>
0065 void testEquals(typename R::value_type const& v, int i, char const* name) {
0066   if (!(v == i)) {
0067     std::cout << "Error: " << v << " != " << i << " for " << typeid(R).name() << " at " << name << std::endl;
0068   }
0069 }
0070 
0071 template <typename R>
0072 void test_read_value_fb(R const& r) {
0073   typename R::value_type v1 = r.front();
0074   typename R::value_type v2 = r.back();
0075   testEquals<R>(v1, 4, __func__);
0076   testEquals<R>(v2, 7, __func__);
0077 }
0078 
0079 template <typename R>
0080 void test_read_value_brackets(R const& r) {
0081   typename R::value_type v1 = r[0];
0082   typename R::value_type v2 = r[3];
0083   testEquals<R>(v1, 4, __func__);
0084   testEquals<R>(v2, 7, __func__);
0085 }
0086 
0087 template <typename R>
0088 void test_read_iter(R const& r) {
0089   int check = 4;
0090   for (typename R::const_iterator it = r.begin(), ed = r.end(); it != ed; ++it, ++check) {
0091     typename R::value_type v = *it;
0092     testEquals<R>(v, check, __func__);
0093   }
0094 }
0095 
0096 template <typename R>
0097 void test_size(R const& r) {
0098   size_t i = r.size();
0099   if (i != 4)
0100     std::cout << "Error: r.size() = " << i << " != 4 for " << typeid(R).name() << " at " << __func__ << std::endl;
0101 }
0102 
0103 template <typename R>
0104 void test_empty(R const& r) {
0105   bool b = r.empty();
0106   if (b)
0107     std::cout << "Error: r.empty() is true for " << typeid(R).name() << " at " << __func__ << std::endl;
0108 }
0109 
0110 struct DummySorter {
0111   bool operator()(Dummy const& t1, Dummy const& t2) const { return t1.id > t2.id; }
0112   bool operator()(edm::Ref<Coll> const& t1, edm::Ref<Coll> const& t2) const { return t1->id > t2->id; }
0113   bool operator()(edm::Ptr<Dummy> const& t1, edm::Ptr<Dummy> const& t2) const { return t1->id > t2->id; }
0114 };
0115 template <typename R>
0116 void test_sort(R r) {
0117   //std::cout << "Check sort for " << typeid(R).name() << std::endl;
0118   //std::cout << "Before sort: " << std::endl;
0119   REQUIRE(!std::is_sorted(r.begin(), r.end(), DummySorter()));
0120   //for(typename R::const_iterator it = r.begin(), ed = r.end(); it != ed; ++it) { std::cout << " - " << *it << std::endl; }
0121   std::sort(r.begin(), r.end(), DummySorter());
0122   //std::cout << "After sort: " << std::endl;
0123   //for(typename R::const_iterator it = r.begin(), ed = r.end(); it != ed; ++it) { std::cout << " - " << *it << std::endl; }
0124   //std::cout << "End check " << std::endl;
0125   REQUIRE(std::is_sorted(r.begin(), r.end(), DummySorter()));
0126 }
0127 
0128 template <typename T>
0129 void test_subrange(T t) {
0130   typedef boost::sub_range<T const> R;
0131   testFill(t);
0132   R r(t.begin() + 4, t.begin() + 8);
0133   test_empty(r);
0134   test_size(r);
0135   test_read_iter(r);
0136   test_read_value_fb(r);
0137   test_read_value_brackets(r);
0138 }
0139 
0140 template <typename T>
0141 void test_itrange(T t) {
0142   typedef boost::iterator_range<typename T::const_iterator> R;
0143   testFill(t);
0144   R r(t.begin() + 4, t.begin() + 8);
0145   test_empty(r);
0146   test_size(r);
0147   test_read_iter(r);
0148   test_read_value_fb(r);
0149   test_read_value_brackets(r);
0150   test_sort(r);
0151 }
0152 
0153 template <typename T>
0154 void test_const_itr_is_const(T const& t) {
0155   typename T::const_iterator itr = t.begin();
0156   *itr = t[0];
0157 }
0158 
0159 template <typename T>
0160 void test(T t) {
0161   testFill(t);
0162   test_const_itr_is_const(t);
0163   test_sort(t);
0164   test_itrange(t);
0165   test_subrange(t);
0166 }
0167 
0168 using namespace std;
0169 using namespace edm;
0170 
0171 #define DISABLE_SORT_IT(Y) \
0172   template <>              \
0173   void test_sort<boost::iterator_range<Y::const_iterator> >(boost::iterator_range<Y::const_iterator>) {}
0174 #define DISABLE_SORT_SUB(Y) \
0175   template <>               \
0176   void test_sort<boost::sub_range<Y const> >(boost::sub_range<Y const>) {}
0177 #define DISABLE_SORT_BARE(Y) \
0178   template <>                \
0179   void test_sort<Y>(Y) {}
0180 #define DISABLE_SORT_ALL(Y) \
0181   DISABLE_SORT_IT(Y)        \
0182   DISABLE_SORT_SUB(Y)
0183 #define DISABLE_CONST_ITR_IS_CONST(Y) \
0184   template <>                         \
0185   void test_const_itr_is_const<Y>(Y const&) {}
0186 
0187 // Comment out to check that std::sort does not compile for boost ranges of const X
0188 DISABLE_SORT_ALL(std::vector<Dummy>)
0189 DISABLE_SORT_ALL(edm::OwnVector<Dummy>)
0190 DISABLE_SORT_ALL(edm::PtrVector<Dummy>)
0191 DISABLE_SORT_ALL(edm::RefVector<Coll>)
0192 // Comment out to check that you can't assign a value to the '*' of a const_iterator
0193 DISABLE_CONST_ITR_IS_CONST(std::vector<Dummy>)
0194 DISABLE_CONST_ITR_IS_CONST(edm::OwnVector<Dummy>)
0195 DISABLE_CONST_ITR_IS_CONST(edm::PtrVector<Dummy>)
0196 DISABLE_CONST_ITR_IS_CONST(edm::RefVector<Coll>)
0197 // Comment out to check that std::sort doesn't compile or runs properly
0198 DISABLE_SORT_BARE(edm::PtrVector<Dummy>)
0199 DISABLE_SORT_BARE(edm::RefVector<Coll>)
0200 
0201 TEST_CASE("test boost::range", "[boost::range]") {
0202   dummies_.clear();
0203   for (int i = 0; i < 12; ++i)
0204     dummies_.push_back(Dummy(i));
0205   test(vector<Dummy>());
0206   test(RefVector<Coll>());
0207   test(PtrVector<Dummy>());
0208   test(OwnVector<Dummy>());
0209 }