Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:22

0001 
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 
0004 #include <cstdlib>
0005 #include <iomanip>
0006 #include <iostream>
0007 #include <memory>
0008 #include <string>
0009 
0010 struct Thing {
0011   Thing() : x() {}
0012   explicit Thing(int xx) : x(xx) {}
0013   int x;
0014 };
0015 
0016 std::ostream& operator<<(std::ostream& os, const Thing& t) {
0017   os << "Thing(" << t.x << ")";
0018   return os;
0019 }
0020 
0021 const char expected[] =
0022     "An exception of category 'InfiniteLoop' occurred.\n"
0023     "Exception Message:\n"
0024     "In func1\n"
0025     "This is just a test: \n"
0026     "double: 1.11111\n"
0027     "float:  2.22222\n"
0028     "uint:   75\n"
0029     "string: a string\n"
0030     "char*:  a nonconst pointer\n"
0031     "char[]: a c-style array\n"
0032     "Thing:  Thing(4)\n"
0033     "\n"
0034     "double: 1.111110e+00\n"
0035     "float:  2.22e+00\n"
0036     "char*:  ..a nonconst pointer\n"
0037     "\n"
0038     "Gave up\n";
0039 
0040 static void errorMessage(std::string const& iE, std::string const& iExpected) {
0041   std::cerr << "The exception message doesn't match expectation\n"
0042                "==exception==\n"
0043             << iE << "\n==expected==\n"
0044             << iExpected << "\n";
0045 }
0046 
0047 void func3() {
0048   double d = 1.11111;
0049   float f = 2.22222;
0050   unsigned int i = 75U;
0051   std::string s("a string");
0052   char* c1 = const_cast<char*>("a nonconst pointer");
0053   char c2[] = "a c-style array";
0054   Thing thing(4);
0055 
0056   //  throw cms::Exception("DataCorrupt")
0057   cms::Exception e("DataCorrupt");
0058   e << "This is just a test: \n"
0059     << "double: " << d << "\n"
0060     << "float:  " << f << "\n"
0061     << "uint:   " << i << "\n"
0062     << "string: " << s << "\n"
0063     << "char*:  " << c1 << "\n"
0064     << "char[]: " << c2 << "\n"
0065     << "Thing:  " << thing << "\n"
0066     << std::endl
0067     << "double: " << std::scientific << d << "\n"
0068     << "float:  " << std::setprecision(2) << f << "\n"
0069     << "char*:  " << std::setfill('.') << std::setw(20) << c1 << std::setfill(' ') << "\n"
0070     << std::endl;
0071 
0072   throw e;
0073 }
0074 
0075 void func2() { func3(); }
0076 
0077 void func1() {
0078   try {
0079     func2();
0080   } catch (cms::Exception& e) {
0081     cms::Exception toThrow("InfiniteLoop", "In func1", e);
0082     toThrow << "Gave up";
0083     throw toThrow;
0084   }
0085 }
0086 
0087 int main() {
0088   try {
0089     func1();
0090   } catch (cms::Exception& e) {
0091     if (e.explainSelf() != expected) {
0092       errorMessage(e.explainSelf(), expected);
0093       abort();
0094     }
0095     if (e.explainSelf() != std::string(e.what())) {
0096       std::cerr << "explainSelf and what do not agree"
0097                 << "\n==explainSelf==\n"
0098                 << e.explainSelf() << "\n==what==\n"
0099                 << e.what();
0100       abort();
0101     }
0102   }
0103 
0104   cms::Exception e1("ABC");
0105   if (e1.alreadyPrinted())
0106     abort();
0107   e1.setAlreadyPrinted(true);
0108   if (!e1.alreadyPrinted())
0109     abort();
0110 
0111   cms::Exception e1s(std::string("ABC"));
0112   if (e1s.alreadyPrinted())
0113     abort();
0114   {
0115     const std::string expected("An exception of category 'ABC' occurred.\n");
0116     if (e1.explainSelf() != expected) {
0117       errorMessage(e1.explainSelf(), expected);
0118       abort();
0119     }
0120   }
0121   if (e1.explainSelf() != e1s.explainSelf()) {
0122     abort();
0123   }
0124 
0125   cms::Exception e2("ABC", "foo");
0126   cms::Exception e2cs("ABC", std::string("foo"));
0127   cms::Exception e2sc(std::string("ABC"), "foo");
0128   cms::Exception e2ss(std::string("ABC"), std::string("foo"));
0129   if (e2.alreadyPrinted())
0130     abort();
0131   if (e2cs.alreadyPrinted())
0132     abort();
0133   if (e2sc.alreadyPrinted())
0134     abort();
0135   if (e2ss.alreadyPrinted())
0136     abort();
0137   e2 << "bar";
0138   e2cs << "bar";
0139   e2sc << "bar";
0140   e2ss << "bar";
0141   {
0142     const std::string expected(
0143         "An exception of category 'ABC' occurred.\n"
0144         "Exception Message:\n"
0145         "foo bar\n");
0146     if (e2.explainSelf() != expected) {
0147       errorMessage(e2.explainSelf(), expected);
0148       abort();
0149     }
0150   }
0151   if (e2.explainSelf() != e2cs.explainSelf()) {
0152     abort();
0153   }
0154   if (e2.explainSelf() != e2sc.explainSelf()) {
0155     abort();
0156   }
0157   if (e2.explainSelf() != e2ss.explainSelf()) {
0158     abort();
0159   }
0160 
0161   cms::Exception e3("ABC", "foo ");
0162   e3 << "bar\n";
0163   {
0164     const std::string expected(
0165         "An exception of category 'ABC' occurred.\n"
0166         "Exception Message:\n"
0167         "foo bar\n");
0168     if (e3.explainSelf() != expected) {
0169       errorMessage(e3.explainSelf(), expected);
0170       abort();
0171     }
0172   }
0173   cms::Exception e4("ABC", "foo\n");
0174   e4 << "bar";
0175   {
0176     const std::string expected(
0177         "An exception of category 'ABC' occurred.\n"
0178         "Exception Message:\n"
0179         "foo\nbar\n");
0180     if (e4.explainSelf() != expected) {
0181       errorMessage(e4.explainSelf(), expected);
0182       abort();
0183     }
0184   }
0185 
0186   e2.addContext("context1");
0187   e2.addContext(std::string("context2"));
0188   e2.addAdditionalInfo("info1");
0189   e2.addAdditionalInfo(std::string("info2"));
0190   {
0191     const std::string expected(
0192         "An exception of category 'ABC' occurred while\n"
0193         "   [0] context2\n"
0194         "   [1] context1\n"
0195         "Exception Message:\n"
0196         "foo bar\n"
0197         "   Additional Info:\n"
0198         "      [a] info2\n"
0199         "      [b] info1\n");
0200     if (e2.explainSelf() != expected) {
0201       errorMessage(e2.explainSelf(), expected);
0202       abort();
0203     }
0204   }
0205   cms::Exception e5("DEF", "start\n", e2);
0206   if (e5.alreadyPrinted())
0207     abort();
0208   cms::Exception e6("DEF", "start", e2);
0209   std::string expected5(
0210       "An exception of category 'DEF' occurred while\n"
0211       "   [0] context2\n"
0212       "   [1] context1\n"
0213       "Exception Message:\n"
0214       "start\n"
0215       "foo bar"
0216       "finish\n"
0217       "   Additional Info:\n"
0218       "      [a] info2\n"
0219       "      [b] info1\n");
0220   e5 << "finish";
0221   e6 << "finish";
0222   cms::Exception e7(e6);
0223   if (e7.alreadyPrinted())
0224     abort();
0225   e6.setAlreadyPrinted(true);
0226   cms::Exception e9(e6);
0227   if (!e9.alreadyPrinted())
0228     abort();
0229 
0230   if (e7.explainSelf() != expected5) {
0231     errorMessage(e7.explainSelf(), expected5);
0232     abort();
0233   }
0234 
0235   if (e7.category() != std::string("DEF")) {
0236     abort();
0237   }
0238   if (e7.message() != std::string("start\n"
0239                                   "foo bar"
0240                                   "finish")) {
0241     abort();
0242   }
0243   e7.clearContext();
0244   std::string expected7_1(
0245       "An exception of category 'DEF' occurred.\n"
0246       "Exception Message:\n"
0247       "start\n"
0248       "foo bar"
0249       "finish\n"
0250       "   Additional Info:\n"
0251       "      [a] info2\n"
0252       "      [b] info1\n");
0253   if (e7.explainSelf() != expected7_1) {
0254     errorMessage(e7.explainSelf(), expected7_1);
0255     abort();
0256   }
0257   std::list<std::string> newContext;
0258   newContext.push_back("new1");
0259   newContext.push_back("new2");
0260   newContext.push_back("new3");
0261   e7.setContext(newContext);
0262   if (e7.context() != newContext) {
0263     std::cerr << e7;
0264     abort();
0265   }
0266 
0267   e7.clearAdditionalInfo();
0268   std::string expected7_2(
0269       "An exception of category 'DEF' occurred while\n"
0270       "   [0] new3\n"
0271       "   [1] new2\n"
0272       "   [2] new1\n"
0273       "Exception Message:\n"
0274       "start\n"
0275       "foo bar"
0276       "finish\n");
0277   if (e7.explainSelf() != expected7_2) {
0278     errorMessage(e7.explainSelf(), expected7_2);
0279     abort();
0280   }
0281   std::list<std::string> newAdditionalInfo;
0282   newAdditionalInfo.push_back("newInfo1");
0283   newAdditionalInfo.push_back("newInfo2");
0284   newAdditionalInfo.push_back("newInfo3");
0285   e7.setAdditionalInfo(newAdditionalInfo);
0286   if (e7.additionalInfo() != newAdditionalInfo) {
0287     std::cerr << e7;
0288     abort();
0289   }
0290   std::string expected7_3(
0291       "An exception of category 'DEF' occurred while\n"
0292       "   [0] new3\n"
0293       "   [1] new2\n"
0294       "   [2] new1\n"
0295       "Exception Message:\n"
0296       "start\n"
0297       "foo bar"
0298       "finish\n"
0299       "   Additional Info:\n"
0300       "      [a] newInfo3\n"
0301       "      [b] newInfo2\n"
0302       "      [c] newInfo1\n");
0303   if (e7.explainSelf() != expected7_3) {
0304     errorMessage(e7.explainSelf(), expected7_3);
0305     abort();
0306   }
0307   if (e7.returnCode() != 8001) {
0308     abort();
0309   }
0310   e7.append(std::string(" X"));
0311   e7.append("Y");
0312   cms::Exception e8("ZZZ", "Z");
0313   e7.append(e8);
0314   std::string expected7_4(
0315       "An exception of category 'DEF' occurred while\n"
0316       "   [0] new3\n"
0317       "   [1] new2\n"
0318       "   [2] new1\n"
0319       "Exception Message:\n"
0320       "start\n"
0321       "foo bar"
0322       "finish XYZ \n"
0323       "   Additional Info:\n"
0324       "      [a] newInfo3\n"
0325       "      [b] newInfo2\n"
0326       "      [c] newInfo1\n");
0327   if (e7.explainSelf() != expected7_4) {
0328     errorMessage(e7.explainSelf(), expected7_4);
0329     abort();
0330   }
0331   std::unique_ptr<cms::Exception> ptr(e7.clone());
0332   e7.clearMessage();
0333   std::string expected7_5(
0334       "An exception of category 'DEF' occurred while\n"
0335       "   [0] new3\n"
0336       "   [1] new2\n"
0337       "   [2] new1\n"
0338       "   Additional Info:\n"
0339       "      [a] newInfo3\n"
0340       "      [b] newInfo2\n"
0341       "      [c] newInfo1\n");
0342   if (e7.explainSelf() != expected7_5) {
0343     errorMessage(e7.explainSelf(), expected7_5);
0344     abort();
0345   }
0346 
0347   try {
0348     ptr->raise();
0349   } catch (cms::Exception& ex) {
0350     ex << "last one ";
0351     std::string expected7_6(
0352         "An exception of category 'DEF' occurred while\n"
0353         "   [0] new3\n"
0354         "   [1] new2\n"
0355         "   [2] new1\n"
0356         "Exception Message:\n"
0357         "start\n"
0358         "foo bar"
0359         "finish XYZ last one \n"
0360         "   Additional Info:\n"
0361         "      [a] newInfo3\n"
0362         "      [b] newInfo2\n"
0363         "      [c] newInfo1\n");
0364     if (ex.explainSelf() != expected7_6) {
0365       errorMessage(ex.explainSelf(), expected7_6);
0366       abort();
0367     }
0368   }
0369   return 0;
0370 }