Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:30

0001 #include <iostream>
0002 #include <stdexcept>
0003 #include <limits>
0004 
0005 #include "CondFormats/Serialization/interface/Equal.h"
0006 
0007 template <typename T>
0008 void same(const T &first, const T &second) {
0009   std::cout << "Same " << typeid(T).name() << std::endl;
0010   if (not cond::serialization::equal(first, second))
0011     throw std::logic_error("Objects are not equal.");
0012 }
0013 
0014 template <typename T>
0015 void diff(const T &first, const T &second) {
0016   std::cout << "Diff " << typeid(T).name() << std::endl;
0017   if (cond::serialization::equal(first, second))
0018     throw std::logic_error("Objects are not different.");
0019 }
0020 
0021 template <typename T>
0022 void checkFloatingPoint() {
0023   const T zero(0);
0024   const T nzero(-zero);
0025   const T first(1);
0026   const T second(2);
0027   const T inf(std::numeric_limits<T>::infinity());
0028   const T ninf(-inf);
0029   const T qnan(std::numeric_limits<T>::quiet_NaN());
0030   const T nqnan(-qnan);
0031   const T snan(std::numeric_limits<T>::signaling_NaN());
0032   const T nsnan(-snan);
0033 
0034   auto positive = [](T x) {
0035     if (std::signbit(x))
0036       throw std::logic_error("Object is not positive.");
0037   };
0038   auto negative = [](T x) {
0039     if (not std::signbit(x))
0040       throw std::logic_error("Object is not negative.");
0041   };
0042 
0043   // Check if we got the signbit right
0044   positive(zero);
0045   negative(nzero);
0046   positive(first);
0047   positive(second);
0048   positive(inf);
0049   negative(ninf);
0050   positive(qnan);
0051   negative(nqnan);
0052   positive(snan);
0053   negative(nsnan);
0054 
0055   same(zero, zero);
0056   same(zero, nzero);
0057 
0058   same(first, first);
0059   diff(first, second);
0060 
0061   same(inf, inf);
0062   same(ninf, ninf);
0063   diff(inf, ninf);
0064 
0065   // see notes in SerializationEqual.h
0066   same(qnan, qnan);
0067   same(qnan, nqnan);
0068   same(qnan, snan);
0069   same(qnan, nsnan);
0070   same(nqnan, nqnan);
0071   same(nqnan, snan);
0072   same(nqnan, nsnan);
0073   same(snan, snan);
0074   same(snan, nsnan);
0075   same(nsnan, nsnan);
0076 
0077   diff(zero, first);
0078   diff(zero, inf);
0079   diff(zero, qnan);
0080   diff(first, inf);
0081   diff(first, qnan);
0082   diff(inf, qnan);
0083 }
0084 
0085 template <typename T>
0086 void checkSequence() {
0087   // empty
0088   same(T({}), T({}));
0089 
0090   // diff size
0091   diff(T({}), T({{1}, {2}, {3}}));
0092   diff(T({{1}, {2}}), T({{1}, {2}, {3}}));
0093 
0094   // same size
0095   same(T({{1}, {2}, {3}}), T({{1}, {2}, {3}}));
0096   diff(T({{1}, {2}, {3}}), T({{4}, {2}, {3}}));
0097   diff(T({{1}, {2}, {3}}), T({{1}, {4}, {3}}));
0098   diff(T({{1}, {2}, {3}}), T({{1}, {2}, {4}}));
0099 }
0100 
0101 // This applies for both ordered and unordered mappings
0102 template <typename T>
0103 void checkCommonMapping() {
0104   // empty
0105   same(T({}), T({}));
0106 
0107   // same
0108   same(T({{1, 2}, {2, 3}}), T({{1, 2}, {2, 3}}));
0109 
0110   // diff size
0111   diff(T({}), T({{1, 2}, {2, 3}}));
0112   diff(T({{1, 2}}), T({{1, 2}, {2, 3}}));
0113 
0114   // diff keys
0115   diff(T({{1, 2}, {2, 3}}), T({{2, 2}, {2, 3}}));
0116   diff(T({{1, 2}, {2, 3}}), T({{1, 2}, {3, 3}}));
0117 
0118   // diff values
0119   diff(T({{1, 2}, {2, 3}}), T({{1, 3}, {2, 3}}));
0120   diff(T({{1, 2}, {2, 3}}), T({{1, 2}, {2, 4}}));
0121 }
0122 
0123 int main() {
0124   // integral
0125   same(false, false);
0126   diff(false, true);
0127   same('1', '1');
0128   diff('1', '2');
0129   same(static_cast<unsigned char>('1'), static_cast<unsigned char>('1'));
0130   diff(static_cast<unsigned char>('1'), static_cast<unsigned char>('2'));
0131   same(L'1', L'1');
0132   diff(L'1', L'2');
0133   same(u'1', u'1');
0134   diff(u'1', u'2');
0135   same(U'1', U'1');
0136   diff(U'1', U'2');
0137   same(static_cast<short>(1), static_cast<short>(1));
0138   diff(static_cast<short>(1), static_cast<short>(2));
0139   same(static_cast<unsigned short>(1), static_cast<unsigned short>(1));
0140   diff(static_cast<unsigned short>(1), static_cast<unsigned short>(2));
0141   same(1, 1);
0142   diff(1, 2);
0143   same(1u, 1u);
0144   diff(1u, 2u);
0145   same(1l, 1l);
0146   diff(1l, 2l);
0147   same(1ul, 1ul);
0148   diff(1ul, 2ul);
0149   same(1ll, 1ll);
0150   diff(1ll, 2ll);
0151   same(1ull, 1ull);
0152   diff(1ull, 2ull);
0153 
0154   // enum
0155   enum enums { enum1, enum2 };
0156   same(enum1, enum1);
0157   diff(enum1, enum2);
0158 
0159   // floating point
0160   checkFloatingPoint<float>();
0161   checkFloatingPoint<double>();
0162   checkFloatingPoint<long double>();
0163 
0164   // string
0165   same(std::string("hi"), std::string("hi"));
0166   diff(std::string("hi"), std::string("hj"));
0167   diff(std::string("hi"), std::string("hi2"));
0168 
0169   // bitset
0170   same(std::bitset<3>("101"), std::bitset<3>("101"));
0171   diff(std::bitset<3>("101"), std::bitset<3>("001"));
0172   diff(std::bitset<3>("101"), std::bitset<3>("111"));
0173   diff(std::bitset<3>("101"), std::bitset<3>("100"));
0174 
0175   // pair
0176   same(std::make_pair(1, '1'), std::make_pair(1, '1'));
0177   diff(std::make_pair(1, '1'), std::make_pair(2, '1'));
0178   diff(std::make_pair(1, '1'), std::make_pair(1, '2'));
0179 
0180   // tuple
0181   same(std::make_tuple(1, '1', 1.f), std::make_tuple(1, '1', 1.f));
0182   diff(std::make_tuple(1, '1', 1.f), std::make_tuple(2, '1', 1.f));
0183   diff(std::make_tuple(1, '1', 1.f), std::make_tuple(1, '2', 1.f));
0184   diff(std::make_tuple(1, '1', 1.f), std::make_tuple(1, '1', 2.f));
0185 
0186   // pointer
0187   int i1 = 1, i1b = 1, i2 = 2;
0188   int *pi1 = &i1, *pi1b = &i1b, *pi2 = &i2, *pin = nullptr;
0189   same(pi1, pi1);   // same addr
0190   same(pi1, pi1b);  // diff addr, same value
0191   diff(pi1, pi2);   // diff addr, diff value
0192   same(pin, pin);   // nullptr, same
0193   diff(pin, pi1);   // nullptr, diff, avoid dereferenciation, first
0194   diff(pi1, pin);   // nullptr, diff, avoid dereferenciation, second
0195 
0196   std::unique_ptr<int> ui1(pi1), u2i1(pi1), ui1b(pi1b), ui2(pi2), uin(pin);
0197   same(ui1, ui1);
0198   same(ui1, u2i1);  // diff object, same addr (even if it should not happen)
0199   same(ui1, ui1b);
0200   diff(ui1, ui2);
0201   same(uin, uin);
0202   diff(uin, ui1);
0203   diff(ui1, uin);
0204   ui1.release();
0205   u2i1.release();
0206   ui1b.release();
0207   ui2.release();
0208   uin.release();
0209 
0210   auto deleter = [](int *) {};
0211   std::shared_ptr<int> si1(pi1, deleter), s2i1(pi1, deleter), si1b(pi1b, deleter), si2(pi2, deleter), sin(pin, deleter);
0212   same(si1, si1);
0213   same(si1, s2i1);  // diff object, same addr (may happen since it is a shared_ptr)
0214   same(si1, si1b);
0215   diff(si1, si2);
0216   same(sin, sin);
0217   diff(sin, si1);
0218   diff(si1, sin);
0219   si1.reset();
0220   s2i1.reset();
0221   si1b.reset();
0222   si2.reset();
0223   sin.reset();
0224 
0225   std::shared_ptr<int> bsi1(pi1, deleter), bs2i1(pi1, deleter), bsi1b(pi1b, deleter), bsi2(pi2, deleter),
0226       bsin(pin, deleter);
0227   same(bsi1, bsi1);
0228   same(bsi1, bs2i1);  // diff object, same addr (may happen since it is a shared_ptr)
0229   same(bsi1, bsi1b);
0230   diff(bsi1, bsi2);
0231   same(bsin, bsin);
0232   diff(bsin, bsi1);
0233   diff(bsi1, bsin);
0234   bsi1.reset();
0235   bs2i1.reset();
0236   bsi1b.reset();
0237   bsi2.reset();
0238   bsin.reset();
0239 
0240   // C-style array
0241   int a123[] = {1, 2, 3}, a123b[] = {1, 2, 3}, a223[] = {2, 2, 3}, a133[] = {1, 3, 3}, a124[] = {1, 2, 4};
0242   same(a123, a123);
0243   same(a123, a123b);
0244   diff(a123, a223);
0245   diff(a123, a133);
0246   diff(a123, a124);
0247   same("hi", "hi");
0248   diff("hi", "hj");
0249 
0250   // array
0251   same(std::array<int, 3>{{1, 2, 3}}, std::array<int, 3>{{1, 2, 3}});
0252   diff(std::array<int, 3>{{1, 2, 3}}, std::array<int, 3>{{2, 2, 3}});
0253   diff(std::array<int, 3>{{1, 2, 3}}, std::array<int, 3>{{1, 3, 3}});
0254   diff(std::array<int, 3>{{1, 2, 3}}, std::array<int, 3>{{1, 2, 4}});
0255 
0256   // sequence
0257   checkSequence<std::vector<int>>();
0258   checkSequence<std::deque<int>>();
0259   checkSequence<std::forward_list<int>>();
0260   checkSequence<std::list<int>>();
0261   checkSequence<std::set<int>>();
0262   checkSequence<std::multiset<int>>();
0263 
0264   // mapping
0265   checkCommonMapping<std::map<int, int>>();
0266 
0267   // unordered mapping
0268   checkCommonMapping<std::unordered_map<int, int>>();
0269 
0270   return 0;
0271 }