Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_PointerOf.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "MEDMEMTest.hxx"
23 #include "MEDMEM_PointerOf.hxx"
24 #include "MEDMEM_define.hxx"
25
26 #include <cppunit/TestAssert.h>
27
28 #include <sstream>
29 #include <cmath>
30
31 // use this define to enable lines, execution of which leads to Segmentation Fault
32 //#define ENABLE_FAULTS
33
34 // use this define to enable CPPUNIT asserts and fails, showing bugs
35 //#define ENABLE_FORCED_FAILURES
36
37 using namespace std;
38 using namespace MEDMEM;
39
40 /*!
41  *  Check methods (12), defined in MEDMEM_PointerOf.hxx:
42  *  template <typename T> class PointerOf {
43  *   (+) PointerOf();
44  *   (+) ~PointerOf();
45  *   (+) PointerOf(const int &size);
46  *   (+) PointerOf(const T *pointer);
47  *   (+) PointerOf(const int &size, const T *pointer);
48  *   (+) PointerOf(const PointerOf<T> & pointerOf);
49  *   (NOT COMPILABLE!!!) PointerOf(const int &size, const PointerOf<T> & pointerOf);
50  *   (+) operator T*();
51  *   (+) operator const T*() const;
52  *   (+) void set(const int &size);
53  *   (+) void set(const T *pointer);
54  *   (+) void set(const int &size, const T *pointer);
55  *   (+) void setShallowAndOwnership(const T *pointer);
56  *   (+) PointerOf<T>& operator=(const PointerOf<T> &pointer);
57  *  }
58  *
59  */
60 void MEDMEMTest::testPointerOf()
61 {
62   const int size=10;
63   PointerOf<int> P;
64
65   try
66   {
67     P.set(0);
68     CPPUNIT_ASSERT((int *)P == NULL);
69   }
70   catch ( const std::exception &e )
71   {
72     CPPUNIT_FAIL(e.what());
73   }
74
75   try
76   {
77     P.set(-1 * size);
78     CPPUNIT_ASSERT((int *)P == NULL);
79   }
80   catch ( const std::exception &e )
81   {
82     CPPUNIT_FAIL(e.what());
83   }
84
85   try
86   {
87     P.set(size);
88     CPPUNIT_ASSERT((int *)P != NULL);
89   }
90   catch ( const std::exception &e )
91   {
92     CPPUNIT_FAIL(e.what());
93   }
94
95   for (int i=0; i < size; i++)
96   {
97     P[i]=i;
98   }
99
100   PointerOf<int> P2(10);
101
102   P2=P;
103   for (int i=0; i < size; i++)
104   {
105     SCRUTE_MED(P2[i]);
106   }
107
108   int * p=new int [size];
109   for (int i=0; i < size; i++)
110   {
111     p[i]=i*10;
112   }
113
114   P2.set(p);
115   PointerOf<int> P3(p);
116
117   for (int i=0; i < size; i++)
118   {
119     CPPUNIT_ASSERT_EQUAL(P2[i],P3[i]);
120   }
121
122   const PointerOf<int> P4(p);
123   for (int i=0; i < size; i++)
124   {
125     CPPUNIT_ASSERT_EQUAL(P4[i],p[i]);
126   }
127
128   const PointerOf<int> P5(P4);
129   CPPUNIT_ASSERT((const int*)P5 != NULL);
130
131   const PointerOf<int> P7(10, p);
132
133   //{
134     //#ifdef ENABLE_COMPILATION_ERRORS
135     //PointerOf<int> PP1 (10, p);
136     //PointerOf<int> PP2 (10, PP1);
137     //#endif
138     //#ifdef ENABLE_FORCED_FAILURES
139     //CPPUNIT_FAIL("Can not create: PointerOf(const int &size, const PointerOf<T> & pointerOf)");
140     //#endif
141   //}
142
143   //CPPUNIT_ASSERT_THROW(P3.set(0, p),MEDEXCEPTION);
144   CPPUNIT_ASSERT_NO_THROW(P3.set(0, p));
145
146   P.setShallowAndOwnership( p );
147   for (int i=0; i < size; i++)
148   {
149     CPPUNIT_ASSERT_EQUAL(P[i],p[i]);
150   }
151
152   //delete [] p; not needed - P already owns p and will delete it when the scope is over
153 }