Back to index

salome-med  6.5.0
MEDMEMTest_Exception.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "MEDMEMTest.hxx"
00021 #include <cppunit/TestAssert.h>
00022 
00023 #include "MEDMEM_Exception.hxx"
00024 #include "MEDMEM_STRING.hxx"
00025 
00026 #include <sstream>
00027 #include <cmath>
00028 #include <cstring>
00029 
00030 using namespace std;
00031 using namespace MEDMEM;
00032 
00033 // #12: MEDMEM_Exception.hxx  }  MEDMEMTest_Exception.cxx
00034 
00054 void MEDMEMTest::testException()
00055 {
00056   {
00057     // test simple constructor
00058     MEDEXCEPTION a = MEDEXCEPTION("test med exception");
00059     CPPUNIT_ASSERT(a.what());
00060     CPPUNIT_ASSERT(strcmp(a.what(), "MED Exception : test med exception") == 0);
00061 
00062     // test copy constructor
00063     MEDEXCEPTION b (a);
00064     CPPUNIT_ASSERT(b.what());
00065     CPPUNIT_ASSERT(strcmp(b.what(), "MED Exception : test med exception") == 0);
00066 
00067     // test dumping of exception in stream
00068     ostringstream ostr;
00069     ostr << b;
00070     CPPUNIT_ASSERT(ostr.str() == "MED Exception : test med exception");
00071   }
00072 
00073   {
00074     // test constructor from three arguments
00075     MEDEXCEPTION c ("test med exception", "<file_name>.cxx", 14);
00076     CPPUNIT_ASSERT(c.what());
00077     CPPUNIT_ASSERT(strcmp(c.what(), "MED Exception in <file_name>.cxx [14] : test med exception") == 0);
00078   }
00079 
00080   {
00081     // Test macro LOCALIZED
00082     STRING msgErr;
00083     msgErr << "ESSAI::ESSAI()!  "<< 4 << "ieme essai ";
00084 
00085     const char* prefix = "MED Exception";
00086     const char* exctxt = static_cast<const char *>(msgErr);
00087     const char* filen = __FILE__;
00088     unsigned int linen = __LINE__ + 5;
00089     const size_t len = strlen(prefix) + strlen(exctxt) + strlen(filen) +
00090       (1 + int(log10(float(linen)))) + 10 + 1;
00091     char* excmsg = new char [len];
00092     sprintf(excmsg, "%s in %s [%u] : %s", prefix, filen, linen, exctxt);
00093     CPPUNIT_ASSERT(strcmp(MEDEXCEPTION(LOCALIZED(msgErr)).what(), excmsg) == 0);
00094 
00095     delete [] excmsg;
00096   }
00097 
00098   {
00099     // Test function STRING<<
00100     const char * LOC = "toto";
00101     CPPUNIT_ASSERT(MEDEXCEPTION(LOCALIZED(STRING(LOC) << " et titi")).what());
00102 
00103     // Test throw
00104     MEDEXCEPTION d (LOCALIZED(STRING(LOC) << " et titi"));
00105     const char* dtxt = d.what();
00106     try {
00107       throw d;
00108     }
00109     catch (MEDEXCEPTION dc) {
00110       CPPUNIT_ASSERT(strcmp(dc.what(), dtxt) == 0);
00111     }
00112     catch (...) {
00113       CPPUNIT_FAIL("Unknown exception");
00114     }
00115   }
00116 
00117   {
00118     // Test MED_DRIVER_NOT_FOUND_EXCEPTION
00119     MED_DRIVER_NOT_FOUND_EXCEPTION g ("mdnfe");
00120     try {
00121       throw g;
00122     }
00123     catch (MED_DRIVER_NOT_FOUND_EXCEPTION mdnfe) {
00124       CPPUNIT_ASSERT(strcmp(mdnfe.what(), g.what()) == 0);
00125     }
00126     catch (MEDEXCEPTION me) {
00127       CPPUNIT_FAIL(me.what());
00128     }
00129     catch (...) {
00130       CPPUNIT_FAIL("Unknown exception");
00131     }
00132 
00133     // Copy constructor
00134     MED_DRIVER_NOT_FOUND_EXCEPTION gcopy (g);
00135     CPPUNIT_ASSERT(gcopy.what());
00136     CPPUNIT_ASSERT(strcmp(gcopy.what(), g.what()) == 0);
00137   }
00138 }