Back to index

salome-med  6.5.0
MEDMEMTest_AsciiFieldDriver.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_AsciiFieldDriver.hxx"
00024 #include "MEDMEM_STRING.hxx"
00025 
00026 #include <stdio.h>
00027 
00028 #include <sstream>
00029 #include <fstream>
00030 #include <cmath>
00031 
00032 // use this define to enable lines, execution of which leads to Segmentation Fault
00033 //#define ENABLE_FAULTS
00034 
00035 // use this define to enable CPPUNIT asserts and fails, showing bugs
00036 //#define ENABLE_FORCED_FAILURES
00037 
00038 using namespace std;
00039 using namespace MEDMEM;
00040 
00041 // #4: MEDMEM_AsciiFieldDriver.hxx  }  MEDMEMTest_AsciiFieldDriver.cxx
00042 
00077 void MEDMEMTest::testAsciiFieldDriver()
00078 {
00079   // read a mesh from a MED file
00080   string filename  = getResourceFile("pointe.med");
00081   string meshname  = "maa1";
00082   string fieldname = "fieldcelldoublescalar";
00083 
00084   string anyfile1    = makeTmpFile( "anyfile1" );
00085   string SDFfilename = makeTmpFile( "myfile" );
00086   ofstream aFile(SDFfilename.c_str());
00087 
00088   // To remove tmp files from disk
00089   MEDMEMTest_TmpFilesRemover aRemover;
00090   aRemover.Register(anyfile1);
00091   aRemover.Register(SDFfilename);
00092 
00093   //Test SDForSorting class
00094   {
00095     double coord_1[10] = { 1.0, 2.0,
00096                            -1.0, 2.0,
00097                            3.6, -8.7,
00098                            10.0, -10.0,
00099                            12.3, 9.3};
00100 
00101     int comp_1[5] = {1, 3, 5, 7, 9};
00102     SDForSorting<int, 2, 48> aSDF_1(coord_1, comp_1, 5);
00103 
00104     SDForSorting<int, 2, 48> aSDFCpy_1 = SDForSorting<int, 2, 48>(aSDF_1);
00105     CPPUNIT_ASSERT_EQUAL(aSDFCpy_1 < aSDF_1, false);
00106     CPPUNIT_ASSERT_NO_THROW(aSDF_1.writeLine(aFile));
00107   }
00108 
00109   // Why functions
00110   // template<> void MEDMEM::fill<-1,0x3>(double *a, const double *b)
00111   // and
00112   // template<> bool MEDMEM::compare<-1>(const double *a, const double *b)
00113   // declared in MEDMEM_AsciiFieldDriver.hxx,
00114   // are implemented in MEDMEM_DriverFactory.cxx?
00115 
00116   // template<int N,unsigned int CODE> void fill(double *a, const double *b)
00117   {
00118     // 0x3 = 110000
00119     double aa[3];
00120     double bb[3] = {1,2,3};
00121 
00122     fill<2,198>(aa, bb); // ZYX // 11000110 // 012
00123     //MEDMEMTest_DumpArray<double>(cout, aa, 3, "aa filled with ZYX");
00124     CPPUNIT_ASSERT_EQUAL(aa[0], bb[0]);
00125     CPPUNIT_ASSERT_EQUAL(aa[1], bb[1]);
00126     CPPUNIT_ASSERT_EQUAL(aa[2], bb[2]);
00127 
00128     fill<2,210>(aa, bb); // ZXY // 11010010 // 102
00129     //MEDMEMTest_DumpArray<double>(cout, aa, 3, "aa filled with ZXY");
00130     CPPUNIT_ASSERT_EQUAL(aa[0], bb[1]);
00131     CPPUNIT_ASSERT_EQUAL(aa[1], bb[0]);
00132     CPPUNIT_ASSERT_EQUAL(aa[2], bb[2]);
00133 
00134     fill<2,228>(aa, bb); // XYZ // 11100100 // 210
00135     //MEDMEMTest_DumpArray<double>(cout, aa, 3, "aa filled with XYZ");
00136     CPPUNIT_ASSERT_EQUAL(aa[0], bb[2]);
00137     CPPUNIT_ASSERT_EQUAL(aa[1], bb[1]);
00138     CPPUNIT_ASSERT_EQUAL(aa[2], bb[0]);
00139   }
00140 
00141   // template<int N> bool compare(const double* a, const double* b)
00142   {
00143     {
00144       // aa < bb
00145       double aa[8] = {1,1,1,1,1,1,1,1};
00146       double bb[8] = {1,1,1,1,1,1,1,2};
00147       CPPUNIT_ASSERT(compare<7>(aa, bb));
00148     }
00149 
00150     {
00151       // aa < bb
00152       double aa[8] = {1,1,1,1,1,1,1,1};
00153       double bb[8] = {2,1,1,1,1,1,1,1};
00154       CPPUNIT_ASSERT(compare<7>(aa, bb));
00155     }
00156 
00157     {
00158       // aa > bb
00159       double aa[8] = {2,1,1,1,1,1,1,1};
00160       double bb[8] = {1,1,1,1,1,1,1,1};
00161       CPPUNIT_ASSERT(!compare<7>(aa, bb));
00162     }
00163 
00164     {
00165       // aa == bb
00166       double aa[8] = {1,1,1,1,1,1,1,1};
00167       double bb[8] = {1,1,1,1,1,1,1,1};
00168       CPPUNIT_ASSERT(!compare<7>(aa, bb));
00169     }
00170 
00171     {
00172       // compare<-1>
00173       double aa[8] = {2,1,1,1,1,1,1,1};
00174       double bb[8] = {1,1,1,1,1,1,1,1};
00175       CPPUNIT_ASSERT(!compare<-1>(aa, bb));
00176     }
00177   }
00178 
00179   // Test ASCII_FIELD_DRIVER
00180   FIELD<double> * aField1 = new FIELD<double> (MED_DRIVER, filename, fieldname);
00181   const SUPPORT * aSupport = aField1->getSupport();
00182   MESH * aMesh = new MESH(MED_DRIVER, filename, aSupport->getMeshName());
00183   aSupport->setMesh(aMesh);
00184 
00185   // create an ASCII driver for a field
00186   ASCII_FIELD_DRIVER<double> * aDriver1 =
00187     new ASCII_FIELD_DRIVER<double> (anyfile1, aField1, MED_EN::ASCENDING, "");
00188   CPPUNIT_ASSERT(aDriver1);
00189 
00190   CPPUNIT_ASSERT(aDriver1->getFileName() == anyfile1);
00191   CPPUNIT_ASSERT(aDriver1->getAccessMode() == MED_EN::WRONLY);
00192 
00193   // and write the field on disk
00194 
00195   // must throw because the file is not opened
00196 
00197   CPPUNIT_ASSERT_THROW(aDriver1->write(), MEDEXCEPTION);
00198 
00199   aDriver1->open();
00200 
00201   // must throw because the file is opened
00202   CPPUNIT_ASSERT_THROW(aDriver1->setFileName("anyfile2"), MEDEXCEPTION);
00203   CPPUNIT_ASSERT_THROW(aDriver1->setFileName(anyfile1), MEDEXCEPTION);
00204 
00205   CPPUNIT_ASSERT_THROW(aDriver1->open(), MEDEXCEPTION);
00206 
00207 
00208   // must throw because it is a writeonly driver
00209   CPPUNIT_ASSERT_THROW(aDriver1->read(), MEDEXCEPTION);
00210 
00211   aDriver1->write();
00212   aDriver1->close();
00213 
00214   // must throw because the file is not opened
00215 
00216   CPPUNIT_ASSERT_THROW(aDriver1->write(), MEDEXCEPTION);
00217 
00218   //CPPUNIT_ASSERT_THROW(aDriver1->close(), MEDEXCEPTION);
00219   CPPUNIT_ASSERT_NO_THROW(aDriver1->close()); // do not make troubles to the user
00220 
00221   // check priority definition
00222   int spaceDimension = aMesh->getSpaceDimension();
00223   if (spaceDimension == 3) {
00224     // good
00225     CPPUNIT_ASSERT_NO_THROW(ASCII_FIELD_DRIVER<double> aDriver2
00226                             ("anyfile2", aField1, MED_EN::ASCENDING, "XYZ"));
00227     // too long
00228     CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver3
00229                          ("anyfile3", aField1, MED_EN::ASCENDING, "XYZX"), MEDEXCEPTION);
00230     // too short
00231     CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver4
00232                          ("anyfile4", aField1, MED_EN::ASCENDING, "XY"), MEDEXCEPTION);
00233     // invalid
00234     CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver5
00235                          ("anyfile5", aField1, MED_EN::ASCENDING, "ABC"), MEDEXCEPTION);
00236   }
00237   else if (spaceDimension == 2) {
00238     // good
00239     CPPUNIT_ASSERT_NO_THROW(ASCII_FIELD_DRIVER<double> aDriver2
00240                             ("anyfile2", aField1, MED_EN::ASCENDING, "XY"));
00241     // too long
00242     CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver3
00243                          ("anyfile3", aField1, MED_EN::ASCENDING, "XYZ"), MEDEXCEPTION);
00244     // too short
00245     CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver4
00246                          ("anyfile4", aField1, MED_EN::ASCENDING, "X"), MEDEXCEPTION);
00247     // invalid
00248     CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver5
00249                          ("anyfile5", aField1, MED_EN::ASCENDING, "AB"), MEDEXCEPTION);
00250   }
00251   else {
00252     CPPUNIT_FAIL("Cannot test ASCII_FIELD_DRIVER because file pointe.med"
00253                  " contains mesh of wrong dimension: must be 2 or 3");
00254   }
00255 
00256   //Copy constructor
00257   ASCII_FIELD_DRIVER<double> aDriver1_Cpy1 = ASCII_FIELD_DRIVER<double> (*aDriver1);
00258 
00259   //Test copy() function
00260   ASCII_FIELD_DRIVER<double> *aDriver1_Cpy2 = (ASCII_FIELD_DRIVER<double>*)aDriver1->copy();
00261   CPPUNIT_ASSERT(aDriver1_Cpy2);
00262   delete aDriver1_Cpy2;
00263   // free memory
00264   delete aDriver1;
00265   aField1->removeReference();
00266   aMesh->removeReference();
00267 }