Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_ModulusArray.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 #include "MEDMEMTest.hxx"
21 #include <cppunit/Message.h>
22 #include <cppunit/TestAssert.h>
23
24 #include "MEDMEM_ModulusArray.hxx"
25 #include "MEDMEM_define.hxx"
26
27 #include <sstream>
28 #include <cmath>
29
30 using namespace std;
31 using namespace MEDMEM;
32 using namespace MED_EN;
33
34 /*!
35  *  Check methods (4), defined in MEDMEM_ModulusArray.hxx:
36  *  class MEDMODULUSARRAY {
37  *   (+) MEDMODULUSARRAY(int length, const int * array);
38  *   (+) MEDMODULUSARRAY(int vertexLength, int totalLength, const int * array);
39  *   (+) ~MEDMODULUSARRAY();
40  *   (+) const int operator[](const int &i) const;
41  *   (+) int compare(const MEDMODULUSARRAY &modulusArray) const;
42  *  }
43  */
44 void MEDMEMTest::testModulusArray()
45 {
46
47   int array[5]={0,1,2,1,4} ;
48   MEDMODULUSARRAY modulusArray(5,array);
49
50   for(int i=-10, j=0; i<15; i++, j++)
51     CPPUNIT_ASSERT_EQUAL( array[ j%5 ], modulusArray[i] );
52
53   // test compare
54   int ret ;
55
56   int array2[5]={1,4,0,1,2} ;
57   MEDMODULUSARRAY modulusArray2(5,array2);
58
59   ret=modulusArray2.compare(modulusArray);
60
61   CPPUNIT_ASSERT_MESSAGE("Two identical arrays - KO", ret == 1);
62
63   int array3[5]={1,2,1,0,4} ;
64   MEDMODULUSARRAY modulusArray3(5,array3) ;
65   ret=modulusArray3.compare(modulusArray);
66
67   CPPUNIT_ASSERT_MESSAGE("Two arrays are in reverse order - KO",ret == -1);
68
69   int array4[6]={1,2,1,0} ;
70   MEDMODULUSARRAY modulusArray4(4,array4) ;
71   ret=modulusArray4.compare(modulusArray);
72
73   CPPUNIT_ASSERT_MESSAGE("Two arrays are have different size - KO",ret == 0);
74
75   int array5[5]={1,2,1,0,1} ;
76   MEDMODULUSARRAY modulusArray5(5,array5) ;
77
78   ret=modulusArray5.compare(modulusArray);
79
80   CPPUNIT_ASSERT_MESSAGE("Two different arrays, same size - KO",ret == 0);
81
82   // test small array :
83
84   // 1
85   int array6[1]={1} ;
86   MEDMODULUSARRAY modulusArray6(1,array6);
87
88   for(int i=-10;i<15;i++)
89     CPPUNIT_ASSERT_EQUAL( 1, modulusArray6[i] );
90
91
92   int array7[1]={1} ;
93   MEDMODULUSARRAY modulusArray7(1,array7);
94   ret=modulusArray6.compare(modulusArray7);
95
96   CPPUNIT_ASSERT_MESSAGE("Two identical arrays - KO", ret == 1);
97
98   int array8[1]={2} ;
99   MEDMODULUSARRAY modulusArray8(1,array8);
100   ret=modulusArray6.compare(modulusArray8);
101
102   CPPUNIT_ASSERT_MESSAGE("Two different arrays - KO",ret == 0);
103
104   // 2
105   int array60[2]={1,2} ;
106   MEDMODULUSARRAY modulusArray60(2,array60);
107
108   for(int i=-10, j = 0;i<15;i++,j++)
109     CPPUNIT_ASSERT_EQUAL( array60[ j%2 ], modulusArray60[i] );
110
111   int array70[2]={1,2} ;
112   MEDMODULUSARRAY modulusArray70(2,array70);
113
114   ret=modulusArray60.compare(modulusArray70);
115
116   CPPUNIT_ASSERT_MESSAGE("Same arrays, same order - KO",ret == 1);
117
118   int array80[2]={2,2} ;
119   MEDMODULUSARRAY modulusArray80(2,array80);
120   ret=modulusArray60.compare(modulusArray80);
121
122   CPPUNIT_ASSERT_MESSAGE("Different arrays - KO",ret == 0);
123
124   int array90[2]={2,1} ;
125   MEDMODULUSARRAY modulusArray90(2,array90);
126
127   ret=modulusArray60.compare(modulusArray90);
128
129   CPPUNIT_ASSERT_MESSAGE("Two arrays are in reverse order - KO",ret == -1);
130
131   //test not vertex nodes
132   int array100[2]={1,2} ;
133   MEDMODULUSARRAY modulusArray100(2,2,array100); // == MEDMODULUSARRAY(2,array100);
134
135   ret = modulusArray60.compare(modulusArray100);
136
137   CPPUNIT_ASSERT_MESSAGE("Same arrays, same order - KO", ret == 1);
138
139   // check comparison of equal arrays, containing non-vertex nodes
140
141   int array110[4] = {1,2,4,3};
142   MEDMODULUSARRAY modulusArray110(2,4,array110);
143
144   int array120[4] = {1,2,4,3};
145   MEDMODULUSARRAY modulusArray120(2,4,array120);
146
147   int array130[4] = {1,2,3,4};
148   MEDMODULUSARRAY modulusArray130(2,4,array130);
149
150   // same order of non-vertex nodes
151   CPPUNIT_ASSERT_NO_THROW(ret = modulusArray120.compare(modulusArray110));
152   CPPUNIT_ASSERT_MESSAGE("Same arrays, same order - KO", ret == 1);
153
154   // different order of non-vertex nodes
155   CPPUNIT_ASSERT_NO_THROW(ret = modulusArray130.compare(modulusArray110));
156   CPPUNIT_ASSERT_MESSAGE("Same arrays, same order - KO", ret == 1);
157
158   // check comparison of different arrays, containing non-vertex nodes
159
160   // difference is in vertex nodes
161   int array140[4] = {1,5,4,3};
162   MEDMODULUSARRAY modulusArray140 (2,4,array140);
163
164   CPPUNIT_ASSERT_NO_THROW(ret = modulusArray120.compare(modulusArray140));
165   CPPUNIT_ASSERT_MESSAGE("Different arrays - KO", ret == 0);
166
167   // difference is in non-vertex nodes
168   int array150[4] = {1,2,4,5};
169   MEDMODULUSARRAY modulusArray150 (2,4,array150);
170
171   CPPUNIT_ASSERT_NO_THROW(ret = modulusArray120.compare(modulusArray150));
172   CPPUNIT_ASSERT_MESSAGE("Different arrays - KO", ret == 0);
173
174   // check that operator[] returns only vertex nodes
175   CPPUNIT_ASSERT_EQUAL(1, modulusArray120[0]);
176   CPPUNIT_ASSERT_EQUAL(2, modulusArray120[1]);
177   CPPUNIT_ASSERT_EQUAL(1, modulusArray120[2]);
178   CPPUNIT_ASSERT_EQUAL(2, modulusArray120[3]);
179   CPPUNIT_ASSERT_EQUAL(1, modulusArray120[4]);
180   CPPUNIT_ASSERT_EQUAL(2, modulusArray120[5]);
181 }