Back to index

salome-med  6.5.0
MEDMEMTest_PointerOf.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 "MEDMEM_PointerOf.hxx"
00022 #include "MEDMEM_define.hxx"
00023 
00024 #include <cppunit/TestAssert.h>
00025 
00026 #include <sstream>
00027 #include <cmath>
00028 
00029 // use this define to enable lines, execution of which leads to Segmentation Fault
00030 //#define ENABLE_FAULTS
00031 
00032 // use this define to enable CPPUNIT asserts and fails, showing bugs
00033 //#define ENABLE_FORCED_FAILURES
00034 
00035 using namespace std;
00036 using namespace MEDMEM;
00037 
00058 void MEDMEMTest::testPointerOf()
00059 {
00060   const int size=10;
00061   PointerOf<int> P;
00062 
00063   try
00064   {
00065     P.set(0);
00066     CPPUNIT_ASSERT((int *)P == NULL);
00067   }
00068   catch ( const std::exception &e )
00069   {
00070     CPPUNIT_FAIL(e.what());
00071   }
00072 
00073   try
00074   {
00075     P.set(-1 * size);
00076     CPPUNIT_ASSERT((int *)P == NULL);
00077   }
00078   catch ( const std::exception &e )
00079   {
00080     CPPUNIT_FAIL(e.what());
00081   }
00082 
00083   try
00084   {
00085     P.set(size);
00086     CPPUNIT_ASSERT((int *)P != NULL);
00087   }
00088   catch ( const std::exception &e )
00089   {
00090     CPPUNIT_FAIL(e.what());
00091   }
00092 
00093   for (int i=0; i < size; i++)
00094   {
00095     P[i]=i;
00096   }
00097 
00098   PointerOf<int> P2(10);
00099 
00100   P2=P;
00101   for (int i=0; i < size; i++)
00102   {
00103     SCRUTE_MED(P2[i]);
00104   }
00105 
00106   int * p=new int [size];
00107   for (int i=0; i < size; i++)
00108   {
00109     p[i]=i*10;
00110   }
00111 
00112   P2.set(p);
00113   PointerOf<int> P3(p);
00114 
00115   for (int i=0; i < size; i++)
00116   {
00117     CPPUNIT_ASSERT_EQUAL(P2[i],P3[i]);
00118   }
00119 
00120   const PointerOf<int> P4(p);
00121   for (int i=0; i < size; i++)
00122   {
00123     CPPUNIT_ASSERT_EQUAL(P4[i],p[i]);
00124   }
00125 
00126   const PointerOf<int> P5(P4);
00127   CPPUNIT_ASSERT((const int*)P5 != NULL);
00128 
00129   const PointerOf<int> P7(10, p);
00130 
00131   //{
00132     //#ifdef ENABLE_COMPILATION_ERRORS
00133     //PointerOf<int> PP1 (10, p);
00134     //PointerOf<int> PP2 (10, PP1);
00135     //#endif
00136     //#ifdef ENABLE_FORCED_FAILURES
00137     //CPPUNIT_FAIL("Can not create: PointerOf(const int &size, const PointerOf<T> & pointerOf)");
00138     //#endif
00139   //}
00140 
00141   //CPPUNIT_ASSERT_THROW(P3.set(0, p),MEDEXCEPTION);
00142   CPPUNIT_ASSERT_NO_THROW(P3.set(0, p));
00143 
00144   P.setShallowAndOwnership( p );
00145   for (int i=0; i < size; i++)
00146   {
00147     CPPUNIT_ASSERT_EQUAL(P[i],p[i]);
00148   }
00149 
00150   //delete [] p; not needed - P already owns p and will delete it when the scope is over
00151 }