1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
23 #include "MEDMEM_Exception.hxx"
24 #include "MEDMEM_STRING.hxx"
31 using namespace MEDMEM;
33 // #12: MEDMEM_Exception.hxx } MEDMEMTest_Exception.cxx
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 ();
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();
52 * Use code of MEDMEM/tests/testUMedException.cxx
54 void MEDMEMTest::testException()
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);
62 // test copy constructor
64 CPPUNIT_ASSERT(b.what());
65 CPPUNIT_ASSERT(strcmp(b.what(), "MED Exception : test med exception") == 0);
67 // test dumping of exception in stream
70 CPPUNIT_ASSERT(ostr.str() == "MED Exception : test med exception");
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);
81 // Test macro LOCALIZED
83 msgErr << "ESSAI::ESSAI()! "<< 4 << "ieme essai ";
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);
99 // Test function STRING<<
100 const char * LOC = "toto";
101 CPPUNIT_ASSERT(MEDEXCEPTION(LOCALIZED(STRING(LOC) << " et titi")).what());
104 MEDEXCEPTION d (LOCALIZED(STRING(LOC) << " et titi"));
105 const char* dtxt = d.what();
109 catch (MEDEXCEPTION dc) {
110 CPPUNIT_ASSERT(strcmp(dc.what(), dtxt) == 0);
113 CPPUNIT_FAIL("Unknown exception");
118 // Test MED_DRIVER_NOT_FOUND_EXCEPTION
119 MED_DRIVER_NOT_FOUND_EXCEPTION g ("mdnfe");
123 catch (MED_DRIVER_NOT_FOUND_EXCEPTION mdnfe) {
124 CPPUNIT_ASSERT(strcmp(mdnfe.what(), g.what()) == 0);
126 catch (MEDEXCEPTION me) {
127 CPPUNIT_FAIL(me.what());
130 CPPUNIT_FAIL("Unknown exception");
134 MED_DRIVER_NOT_FOUND_EXCEPTION gcopy (g);
135 CPPUNIT_ASSERT(gcopy.what());
136 CPPUNIT_ASSERT(strcmp(gcopy.what(), g.what()) == 0);