File indexing completed on 2024-04-06 12:03:58
0001 #include "cppunit/extensions/HelperMacros.h"
0002 #include "DataFormats/Common/interface/RangeMap.h"
0003 #include "DataFormats/Common/interface/CopyPolicy.h"
0004
0005 class testRangeMap : public CppUnit::TestFixture {
0006 CPPUNIT_TEST_SUITE(testRangeMap);
0007 CPPUNIT_TEST(checkAll);
0008 CPPUNIT_TEST_SUITE_END();
0009
0010 public:
0011 void setUp() {}
0012 void tearDown() {}
0013 void checkAll();
0014 };
0015
0016 CPPUNIT_TEST_SUITE_REGISTRATION(testRangeMap);
0017
0018 #include <iostream>
0019
0020 struct MatchOddId {
0021 bool operator()(int i) const { return i % 2 == 1; }
0022 };
0023
0024 class IntComparator {
0025 public:
0026 bool operator()(int d1, int d2) const {
0027
0028
0029
0030 if ((d1 == 4 && d2 == 3) || (d1 == 3 && d2 == 4))
0031 return false;
0032 return d1 < d2;
0033 }
0034 };
0035
0036 void testRangeMap::checkAll() {
0037 typedef edm::RangeMap<int, std::vector<int>, edm::CopyPolicy<int> > map;
0038
0039 map m;
0040 int v3[] = {1, 2, 3, 4};
0041 int v4[] = {10, 11, 12};
0042 int v2[] = {5, 6, 7};
0043 int v1[] = {8, 9};
0044 int s1 = sizeof(v1) / sizeof(int);
0045 int s2 = sizeof(v2) / sizeof(int);
0046 int s3 = sizeof(v3) / sizeof(int);
0047 int s4 = sizeof(v4) / sizeof(int);
0048 m.put(3, v3, v3 + s3);
0049 m.put(2, v2, v2 + s2);
0050 m.put(1, v1, v1 + s1);
0051 m.put(4, v4, v4 + s4);
0052
0053 m.post_insert();
0054
0055 map::const_iterator i = m.begin();
0056 CPPUNIT_ASSERT(*i++ == 8);
0057 CPPUNIT_ASSERT(*i++ == 9);
0058 CPPUNIT_ASSERT(*i++ == 5);
0059 CPPUNIT_ASSERT(*i++ == 6);
0060 CPPUNIT_ASSERT(*i++ == 7);
0061 CPPUNIT_ASSERT(*i++ == 1);
0062 CPPUNIT_ASSERT(*i++ == 2);
0063 CPPUNIT_ASSERT(*i++ == 3);
0064 CPPUNIT_ASSERT(*i++ == 4);
0065 CPPUNIT_ASSERT(*i++ == 10);
0066 CPPUNIT_ASSERT(*i++ == 11);
0067 CPPUNIT_ASSERT(*i++ == 12);
0068 CPPUNIT_ASSERT(i == m.end());
0069
0070 CPPUNIT_ASSERT(*--i == 12);
0071 CPPUNIT_ASSERT(*--i == 11);
0072 CPPUNIT_ASSERT(*--i == 10);
0073 CPPUNIT_ASSERT(*--i == 4);
0074 CPPUNIT_ASSERT(*--i == 3);
0075 CPPUNIT_ASSERT(*--i == 2);
0076 CPPUNIT_ASSERT(*--i == 1);
0077 CPPUNIT_ASSERT(*--i == 7);
0078 CPPUNIT_ASSERT(*--i == 6);
0079 CPPUNIT_ASSERT(*--i == 5);
0080 CPPUNIT_ASSERT(*--i == 9);
0081 CPPUNIT_ASSERT(*--i == 8);
0082 CPPUNIT_ASSERT(i == m.begin());
0083
0084 map::id_iterator b = m.id_begin();
0085 CPPUNIT_ASSERT(m.id_size() == 4);
0086 CPPUNIT_ASSERT(*b++ == 1);
0087 CPPUNIT_ASSERT(*b++ == 2);
0088 CPPUNIT_ASSERT(*b++ == 3);
0089 CPPUNIT_ASSERT(*b++ == 4);
0090 CPPUNIT_ASSERT(b == m.id_end());
0091
0092 CPPUNIT_ASSERT(*--b == 4);
0093 CPPUNIT_ASSERT(*--b == 3);
0094 CPPUNIT_ASSERT(*--b == 2);
0095 CPPUNIT_ASSERT(*--b == 1);
0096 CPPUNIT_ASSERT(b == m.id_begin());
0097
0098
0099
0100 map::range r = m.get(1);
0101 CPPUNIT_ASSERT(r.second - r.first == 2);
0102 r = m.get(2);
0103 CPPUNIT_ASSERT(r.second - r.first == 3);
0104 r = m.get(3);
0105 CPPUNIT_ASSERT(r.second - r.first == 4);
0106 r = m.get(4);
0107 CPPUNIT_ASSERT(r.second - r.first == 3);
0108
0109
0110
0111
0112 r = m.get(3, IntComparator());
0113 CPPUNIT_ASSERT(r.second - r.first == 7);
0114 i = r.first;
0115
0116 CPPUNIT_ASSERT(*i++ == 1);
0117 CPPUNIT_ASSERT(*i++ == 2);
0118 CPPUNIT_ASSERT(*i++ == 3);
0119 CPPUNIT_ASSERT(*i++ == 4);
0120 CPPUNIT_ASSERT(*i++ == 10);
0121 CPPUNIT_ASSERT(*i++ == 11);
0122 CPPUNIT_ASSERT(*i++ == 12);
0123 CPPUNIT_ASSERT(i == r.second);
0124
0125 r = m.get(1, IntComparator());
0126 CPPUNIT_ASSERT(r.second - r.first == 2);
0127 }