Salome HOME
MEDMEM suppression
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_Exception.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
22
23 #include "MEDMEM_Exception.hxx"
24 #include "MEDMEM_STRING.hxx"
25
26 #include <sstream>
27 #include <cmath>
28 #include <cstring>
29
30 using namespace std;
31 using namespace MEDMEM;
32
33 // #12: MEDMEM_Exception.hxx  }  MEDMEMTest_Exception.cxx
34
35 /*!
36  *  Check methods (not in spec), defined in MEDMEM_Exception.hxx:
37  *   (+)     #define LOCALIZED(message) static_cast<const char *> (message) , __FILE__ , __LINE__
38  *  class MEDEXCEPTION : public std::exception {
39  *   (+)     MEDEXCEPTION(const char *text, const char *fileName=0, const unsigned int lineNumber=0);
40  *   (+)     MEDEXCEPTION(const MEDEXCEPTION &ex);
41  *   (+)     ~MEDEXCEPTION() throw ();
42  *   (+)     friend ostream & operator<< (ostream &os, const MEDEXCEPTION &ex);
43  *   (+)     virtual const char *what(void) const throw ();
44  *  }
45  *  class MED_DRIVER_NOT_FOUND_EXCEPTION : public MEDEXCEPTION {
46  *   (+)     MED_DRIVER_NOT_FOUND_EXCEPTION(const char *text, const char *fileName=0,
47  *                                          const unsigned int lineNumber=0);
48  *   (+)     MED_DRIVER_NOT_FOUND_EXCEPTION(const MED_DRIVER_NOT_FOUND_EXCEPTION &ex);
49  *   (+)     ~MED_DRIVER_NOT_FOUND_EXCEPTION() throw();
50  *  }
51  *
52  *  Use code of MEDMEM/tests/testUMedException.cxx
53  */
54 void MEDMEMTest::testException()
55 {
56   {
57     // test simple constructor
58     MEDEXCEPTION a = MEDEXCEPTION("test med exception");
59     CPPUNIT_ASSERT(a.what());
60     CPPUNIT_ASSERT(strcmp(a.what(), "MED Exception : test med exception") == 0);
61
62     // test copy constructor
63     MEDEXCEPTION b (a);
64     CPPUNIT_ASSERT(b.what());
65     CPPUNIT_ASSERT(strcmp(b.what(), "MED Exception : test med exception") == 0);
66
67     // test dumping of exception in stream
68     ostringstream ostr;
69     ostr << b;
70     CPPUNIT_ASSERT(ostr.str() == "MED Exception : test med exception");
71   }
72
73   {
74     // test constructor from three arguments
75     MEDEXCEPTION c ("test med exception", "<file_name>.cxx", 14);
76     CPPUNIT_ASSERT(c.what());
77     CPPUNIT_ASSERT(strcmp(c.what(), "MED Exception in <file_name>.cxx [14] : test med exception") == 0);
78   }
79
80   {
81     // Test macro LOCALIZED
82     STRING msgErr;
83     msgErr << "ESSAI::ESSAI()!  "<< 4 << "ieme essai ";
84
85     const char* prefix = "MED Exception";
86     const char* exctxt = static_cast<const char *>(msgErr);
87     const char* filen = __FILE__;
88     unsigned int linen = __LINE__ + 5;
89     const size_t len = strlen(prefix) + strlen(exctxt) + strlen(filen) +
90       (1 + int(log10(float(linen)))) + 10 + 1;
91     char* excmsg = new char [len];
92     sprintf(excmsg, "%s in %s [%u] : %s", prefix, filen, linen, exctxt);
93     CPPUNIT_ASSERT(strcmp(MEDEXCEPTION(LOCALIZED(msgErr)).what(), excmsg) == 0);
94
95     delete [] excmsg;
96   }
97
98   {
99     // Test function STRING<<
100     const char * LOC = "toto";
101     CPPUNIT_ASSERT(MEDEXCEPTION(LOCALIZED(STRING(LOC) << " et titi")).what());
102
103     // Test throw
104     MEDEXCEPTION d (LOCALIZED(STRING(LOC) << " et titi"));
105     const char* dtxt = d.what();
106     try {
107       throw d;
108     }
109     catch (MEDEXCEPTION dc) {
110       CPPUNIT_ASSERT(strcmp(dc.what(), dtxt) == 0);
111     }
112     catch (...) {
113       CPPUNIT_FAIL("Unknown exception");
114     }
115   }
116
117   {
118     // Test MED_DRIVER_NOT_FOUND_EXCEPTION
119     MED_DRIVER_NOT_FOUND_EXCEPTION g ("mdnfe");
120     try {
121       throw g;
122     }
123     catch (MED_DRIVER_NOT_FOUND_EXCEPTION mdnfe) {
124       CPPUNIT_ASSERT(strcmp(mdnfe.what(), g.what()) == 0);
125     }
126     catch (MEDEXCEPTION me) {
127       CPPUNIT_FAIL(me.what());
128     }
129     catch (...) {
130       CPPUNIT_FAIL("Unknown exception");
131     }
132
133     // Copy constructor
134     MED_DRIVER_NOT_FOUND_EXCEPTION gcopy (g);
135     CPPUNIT_ASSERT(gcopy.what());
136     CPPUNIT_ASSERT(strcmp(gcopy.what(), g.what()) == 0);
137   }
138 }