Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEMBinTest / tests / testUArray.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  File   : testUArray.cxx
21 //  Module : MED
22 //
23 #include <cstdlib>      // pour l'acces à EXIT_SUCCESS et EXIT_FAILURE
24 #include "MEDMEM_Utilities.hxx"
25 #include "MEDMEM_Array.hxx"
26
27 using namespace std;
28 using namespace MEDMEM;
29
30 static void imprime(string titre,const int * myValues,const int * myOthers, int lignes, int colonnes)
31 {
32   cout << "             " <<titre << endl<< endl;
33   cout << "Full Interlace : " << endl;
34   if (myValues != NULL)
35   {
36         for (int i=0; i<lignes; i++)
37         {
38                 cout << " - ";
39                 for (int k=0; k< colonnes; k++)
40                 {
41                         cout << myValues[k + i * colonnes]<< " " ; 
42                 }
43                 cout << endl;
44         }
45   }
46   else
47   {
48                 cout << "Pointeur Null : pas de valeur" << endl << endl;
49   }
50   cout << endl;
51   cout << "No Interlace : " << endl;
52
53   if (myOthers != NULL)
54   {
55         for (int k=0; k< colonnes; k++)
56         {
57                 cout << " - ";
58                 for (int i=0; i<lignes; i++)
59                 {
60                         cout << myOthers[i + k*lignes]<< " " ; 
61                 }
62                 cout << endl;
63         }
64   }
65   else
66   {
67                 cout << "Pointeur Null : pas de valeur" << endl << endl;
68   }
69   cout << endl;
70 }
71
72 int main (int argc, char ** argv) 
73 {
74
75   /* ---------------------------------------------------------- */
76   /*                                                            */
77   /*                    INITIALISATION                          */
78   /*                                                            */
79   /* - constructeur new MEDARRAY<int>(int,int,mode)             */
80   /* - get sur le pointeur MED_FULL_INTERLACE                   */
81   /* - initialisation des valeurs (coordonnées ieme elt = I+1)  */
82   /* - get sur le pointeur MED_NO_INTERLACE                     */
83   /* ---------------------------------------------------------- */
84
85   int SpaceDimension = 3;
86   int NumberOfNodes  = 4; 
87
88
89   
90   MEDARRAY<int> * myArrayfull= new MEDARRAY<int>(SpaceDimension,NumberOfNodes,MED_EN::MED_FULL_INTERLACE);
91   ASSERT_MED(myArrayfull != NULL);
92
93   const int * myValues = myArrayfull->get(MED_EN::MED_FULL_INTERLACE);
94   ASSERT_MED(myValues!= NULL);
95
96   for (int i=0; i<NumberOfNodes; i++)
97   {
98         for (int k=0; k<SpaceDimension; k++)
99         {
100           //myValues[i* SpaceDimension +k]=i + 1; 
101           myArrayfull->setIJ(i+1,k+1,i+1) ;
102         }
103   }
104   
105   const int * myOthers = myArrayfull->get(MED_EN::MED_NO_INTERLACE) ;
106   ASSERT_MED(myOthers != NULL);
107   
108   imprime("Initialisation full interlace (xi=yi=zi=i+1)",myValues,myOthers,NumberOfNodes,SpaceDimension);
109
110   MEDARRAY<int> * myArrayno= new MEDARRAY<int>(SpaceDimension,NumberOfNodes,MED_EN::MED_NO_INTERLACE);
111   ASSERT_MED(myArrayno != NULL);
112   const int * myValuesno = myArrayno->get(MED_EN::MED_NO_INTERLACE);
113   ASSERT_MED(myValuesno!= NULL);
114
115   for (int k=0; k<SpaceDimension; k++)
116   {
117         for (int i=0; i<NumberOfNodes; i++)
118         {
119           //myValuesno[i +k*NumberOfNodes]=k + 1; 
120           myArrayno->setIJ(i+1,k+1,k+1) ;
121         }
122   }
123   
124   const int * myOthersno = myArrayno->get(MED_EN::MED_FULL_INTERLACE) ;
125   ASSERT_MED(myOthersno != NULL);
126
127   imprime("Initialisation no interlace (xi=yi=zi=i+1)",myOthersno,myValuesno,NumberOfNodes,SpaceDimension);
128
129
130   /* ---------------------------------------------------------- */
131   /*                                                            */
132   /*                    Tests des Fonctions Set                 */
133   /*                                                            */
134   /* - setI sur l'element 1 avec des coordonnees a 100          */
135   /* - setJ sur l'element 1 avec des coordonnees a 100          */
136   /* - setIJ sur (1,2) avec une coordonnee = 1992               */
137   /* - set   avec l ensemble des coordonnes remises à i              */
138   /* ---------------------------------------------------------- */
139
140
141   int * myNewLine = new int[SpaceDimension];
142   for (int i = 0; i < SpaceDimension; i++) 
143         myNewLine[i] = myValues[i] * 100;
144   try
145   {
146         myArrayfull->setI(1, myNewLine);
147   }
148   catch ( const std::exception &e )
149   {
150         cout << "--------------" << endl;
151         cout << "   Pb au setI " << endl;
152         cout << "--------------" << endl;
153         MESSAGE_MED( "catched exception : " << e.what() ) ;
154         return EXIT_FAILURE ;
155   }
156   catch (...)
157   {
158         cout << "---------------" << endl;
159         cout << "   Pb au setI   " << endl;
160         cout << "---------------" << endl;
161   }
162
163   delete [] myNewLine;
164   imprime("1er element : coordonnees à 100",myValues,myOthers,NumberOfNodes,SpaceDimension);
165
166   int * myNewCol = new int[NumberOfNodes];
167   for (int i = 0; i < NumberOfNodes; i++) 
168         myNewCol[i] = 100;
169   try
170   {
171         myArrayno->setJ(1, myNewCol);
172   }
173   catch ( const std::exception &e )
174   {
175         cout << "--------------" << endl;
176         cout << "   Pb au setJ " << endl;
177         cout << "--------------" << endl;
178         MESSAGE_MED( "catched exception : " << e.what() ) ;
179         return EXIT_FAILURE ;
180   }
181   catch (...)
182   {
183         cout << "---------------" << endl;
184         cout << "   Pb au setJ   " << endl;
185         cout << "---------------" << endl;
186   }
187
188   delete [] myNewCol;
189   imprime("1eres  coordonnees à 100",myOthersno,myValuesno,NumberOfNodes,SpaceDimension);
190
191   try
192   {
193         myArrayfull->setIJ(1,2,1992);
194   }
195   catch ( const std::exception &e )
196   {
197         cout << "---------------------------" << endl;
198         cout << "   Pb au setIJ()  de 1 , 2 "  << endl;
199         cout << "---------------------------" << endl;
200         MESSAGE_MED( "catched exception : " << e.what() ) ;
201         return EXIT_FAILURE ;
202   }
203   catch (...)
204   {
205         cout << "-----------------------------" << endl;
206         cout << "   Pb au setIJ()  de 1 , 2 "  << endl;
207         cout << "-----------------------------" << endl;
208   }
209
210   imprime("1er element : 2ieme coordonnee = 1992",myValues,myOthers,NumberOfNodes,SpaceDimension);
211
212
213   try
214   {
215         myArrayno->setIJ(1,2,1992);
216   }
217   catch ( const std::exception &e )
218   {
219         cout << "---------------------------" << endl;
220         cout << "   Pb au setIJ()  de 1 , 2 "  << endl;
221         cout << "---------------------------" << endl;
222         MESSAGE_MED( "catched exception : " << e.what() ) ;
223         return EXIT_FAILURE ;
224   }
225   catch (...)
226   {
227         cout << "-----------------------------" << endl;
228         cout << "   Pb au setIJ()  de 1 , 2 "  << endl;
229         cout << "-----------------------------" << endl;
230   }
231
232   imprime("1er element : 2ieme coordonnee = 1992",myValues,myOthers,NumberOfNodes,SpaceDimension);
233
234   int * mynewvalues= new int [ NumberOfNodes*SpaceDimension ];
235   for (int i=0; i<NumberOfNodes*SpaceDimension; i++)
236   {
237         mynewvalues[i]=i;
238   }
239   try
240   {
241         myArrayfull->set(MED_EN::MED_FULL_INTERLACE,mynewvalues);
242         myValues = myArrayfull->get(MED_EN::MED_FULL_INTERLACE);
243         myOthers = myArrayfull->get(MED_EN::MED_NO_INTERLACE);
244   }
245   catch ( const std::exception &e )
246   {
247         cout << "-------------" << endl;
248         cout << "   Pb au set "  << endl;
249         cout << "-------------" << endl;
250         MESSAGE_MED( "catched exception : " << e.what() ) ;
251         return EXIT_FAILURE ;
252   }
253   catch (...)
254   {
255         cout << "--------------" << endl;
256         cout << "   Pb au set "  << endl;
257         cout << "--------------" << endl;
258   }
259   imprime("remise valeur a i sur myArrayfull med full interlace",myValues,myOthers,NumberOfNodes,SpaceDimension);
260
261   try
262   {
263         myArrayno->set(MED_EN::MED_FULL_INTERLACE,mynewvalues);
264         myValuesno = myArrayfull->get(MED_EN::MED_FULL_INTERLACE);
265         myOthersno = NULL;
266   }
267   catch ( const std::exception &e )
268   {
269         cout << "-------------" << endl;
270         cout << "   Pb au set "  << endl;
271         cout << "-------------" << endl;
272         MESSAGE_MED( "catched exception : " << e.what() ) ;
273         return EXIT_FAILURE ;
274   }
275   catch (...)
276   {
277         cout << "--------------" << endl;
278         cout << "   Pb au setI "  << endl;
279         cout << "--------------" << endl;
280   }
281   imprime("set full interlace de myArrayno",myValuesno,myOthersno,NumberOfNodes,SpaceDimension);
282
283   /* ---------------------------------------------------------- */
284   /*                                                            */
285   /*                Tests des constructeurs                     */
286   /*                Tests des Fonctions Get                     */
287   /*                                                            */
288   /*                                                            */
289   /* ---------------------------------------------------------- */
290
291   MEDARRAY<int> * myArrayShare = new MEDARRAY<int>( *myArrayfull);
292   const int * sharevalues = myArrayShare->get(MED_EN::MED_FULL_INTERLACE );
293   const int * shareno = myArrayShare->get(MED_EN::MED_NO_INTERLACE);
294   imprime("test contructeur par recopie non profonde",sharevalues,shareno,NumberOfNodes,SpaceDimension);
295
296   myArrayfull->setIJ(1,2,1992);
297   ASSERT_MED(myArrayShare->getIJ(1,2) == 1992);
298   imprime("change valeur tableau source, impression tableau cible",sharevalues,shareno,NumberOfNodes,SpaceDimension);
299
300   myArrayShare->setIJ(1,2,1995);
301   ASSERT_MED(myArrayfull->getIJ(1,2) == 1995);
302   imprime("change valeur tableau cible, impression tableau source",myValues,myOthers,NumberOfNodes,SpaceDimension);
303
304   delete myArrayShare;
305   imprime("tableau cible apres destruction tableau source",myValues,myOthers,NumberOfNodes,SpaceDimension);
306
307   MEDARRAY<int> * myArrayShare2 = new MEDARRAY<int>( *myArrayfull,true);
308   sharevalues = myArrayShare2->get(MED_EN::MED_FULL_INTERLACE );
309   shareno = myArrayShare2->get(MED_EN::MED_NO_INTERLACE );
310   imprime("test contructeur par recopie profonde",sharevalues,shareno,NumberOfNodes,SpaceDimension);
311
312   myArrayfull->setIJ(1,2,18);
313   imprime("change valeur tableau source, impression tableau cible",sharevalues,shareno,NumberOfNodes,SpaceDimension);
314
315   myArrayShare2->setIJ(1,2,19);
316   imprime("change valeur tableau cible, impression tableau source",myValues,myOthers,NumberOfNodes,SpaceDimension);
317
318   myArrayno->set(MED_EN::MED_NO_INTERLACE,mynewvalues);
319   myArrayno->setIJ(2,1,1);
320   myValuesno = myArrayno->get(MED_EN::MED_NO_INTERLACE);
321   myOthersno = myArrayno->get(MED_EN::MED_FULL_INTERLACE);
322   imprime("Initialisation no interlace (0...11)",myOthersno,myValuesno,NumberOfNodes,SpaceDimension);
323
324   MEDARRAY<int> * myArrayShare3 = new MEDARRAY<int>( *myArrayno);
325   sharevalues = myArrayShare3->get(MED_EN::MED_FULL_INTERLACE);
326   shareno = myArrayShare3->get(MED_EN::MED_NO_INTERLACE);
327   imprime("test contructeur par recopie non profonde",sharevalues,shareno,NumberOfNodes,SpaceDimension);
328
329   myArrayno->setIJ(1,2,1992);
330   ASSERT_MED(myArrayShare3->getIJ(1,2) == 1992);
331   imprime("change valeur tableau source, impression tableau cible",sharevalues,shareno,NumberOfNodes,SpaceDimension);
332
333   myArrayShare3->setIJ(1,2,1995);
334   ASSERT_MED(myArrayno->getIJ(1,2) == 1995);
335   imprime("change valeur tableau cible, impression tableau source",myValuesno,myOthersno,NumberOfNodes,SpaceDimension);
336
337   delete myArrayno;
338   delete [] mynewvalues;
339   delete myArrayfull;
340   delete myArrayShare2;
341   delete myArrayShare3;
342   MESSAGE_MED("FIN NORMALE DU TRAITEMENT");
343   return EXIT_SUCCESS ;
344 }
345 /*
346   inline medModeSwitch getMode() const ;
347 */