1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDCalculatorBasicsTest.hxx"
21 #include "MEDCalculatorBrowserLiteStruct.hxx"
22 #include "MEDCalculatorDBRangeSelection.hxx"
23 #include "MEDCalculatorDBField.hxx"
25 #include "SPythonParser.hxx"
26 #include "SPythonInterpreter.hxx"
28 #include "MEDLoader.hxx"
30 #include "MEDCouplingMemArray.hxx"
31 #include "MEDCouplingUMesh.hxx"
32 #include "MEDCouplingFieldDouble.hxx"
36 void ParaMEDMEM::MEDCalculatorBasicsTest::testLightStruct1()
38 const char fname[]="/export/home/geay/MEDStdAlone/Salome5/V5_1_main/MED/MED_INSTALL_DEBUG/share/salome/resources/med/TimeStamps_import22.med";
39 MEDCalculatorBrowserLiteStruct lt(fname);
41 //std::cout << lt.str() << std::endl;
44 void ParaMEDMEM::MEDCalculatorBasicsTest::testRangeSelection1()
46 MEDCalculatorDBRangeSelection sel1(":");
47 std::vector<int> v=sel1.getIds(6);
48 CPPUNIT_ASSERT_EQUAL(6,(int)v.size());
49 const int expected1[6]={0,1,2,3,4,5};
50 CPPUNIT_ASSERT(std::equal(expected1,expected1+6,v.begin()));
51 MEDCalculatorDBRangeSelection sel2(":-1");
53 CPPUNIT_ASSERT_EQUAL(5,(int)v.size());
54 CPPUNIT_ASSERT(std::equal(expected1,expected1+5,v.begin()));
55 MEDCalculatorDBRangeSelection sel3("2:4");
57 CPPUNIT_ASSERT_EQUAL(2,(int)v.size());
58 CPPUNIT_ASSERT(std::equal(expected1+2,expected1+4,v.begin()));
59 MEDCalculatorDBRangeSelection sel4(":3");
61 CPPUNIT_ASSERT_EQUAL(3,(int)v.size());
62 CPPUNIT_ASSERT(std::equal(expected1,expected1+3,v.begin()));
65 MEDCalculatorDBRangeSelection sel5("2");
66 try { sel5="3:1"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
67 CPPUNIT_ASSERT(exc); exc=false;
68 try { sel5="4:5:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
69 CPPUNIT_ASSERT(exc); exc=false;
70 try { sel5="4:5r"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
71 CPPUNIT_ASSERT(exc); exc=false;
72 try { sel5="4:ry5"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
73 CPPUNIT_ASSERT(exc); exc=false;
74 try { sel5="4r:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
75 CPPUNIT_ASSERT(exc); exc=false;
76 try { sel5="r4:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
77 CPPUNIT_ASSERT(exc); exc=false;
79 try { v=sel5.getIds(6); } catch(INTERP_KERNEL::Exception& e) { exc=true; }
80 CPPUNIT_ASSERT(exc); exc=false;
83 void ParaMEDMEM::MEDCalculatorBasicsTest::testDBField1()
85 const char fName[]="hfile1.med";
86 generateAFile1(fName);
87 MEDCalculatorBrowserLiteStruct lt(fName);
89 MEDCalculatorDBFieldReal *Power=new MEDCalculatorDBFieldReal(lt.getField(0));
90 MEDCalculatorDBFieldReal *Power2=new MEDCalculatorDBFieldReal(lt.getField(0));
91 MEDCalculatorDBFieldReal *Power3=(MEDCalculatorDBFieldReal *)((*Power)+(*Power2));
96 MEDCalculatorDBRangeSelection t1("2:4"),t2("6:8");
97 MEDCalculatorDBRangeSelection p;
98 MEDCalculatorDBRangeSelection c1,c2;
100 Power=new MEDCalculatorDBFieldReal(lt.getField(0));
101 Power2=(*Power)(t1,p,c1);
102 Power3=(*Power)(t2,p,c1);
103 MEDCalculatorDBField *Power4=(*Power2)+(*Power3);
110 void ParaMEDMEM::MEDCalculatorBasicsTest::generateAFile1(const char *fName)
112 double targetCoords[18]={-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 };
113 int targetConn[18]={0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4};
114 MEDCouplingUMesh *m=MEDCouplingUMesh::New();
116 m->setMeshDimension(2);
118 m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn);
119 m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+4);
120 m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+7);
121 m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+10);
122 m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+14);
123 m->finishInsertingCells();
124 DataArrayDouble *myCoords=DataArrayDouble::New();
125 myCoords->alloc(9,2);
126 myCoords->setInfoOnComponent(0,"x [cm]");
127 myCoords->setInfoOnComponent(1,"y [cm]");
128 std::copy(targetCoords,targetCoords+18,myCoords->getPointer());
129 m->setCoords(myCoords);
132 MEDLoader::WriteUMesh(fName,m,true);
133 static const int nbOfTimeSteps=10;
134 static const int nbOfComponents=7;
135 MEDCouplingFieldDouble *f=MEDCouplingFieldDouble::New(ON_CELLS,ONE_TIME);
138 DataArrayDouble *da=DataArrayDouble::New();
141 da->setInfoOnComponent(0,"aaa [a]"); da->setInfoOnComponent(1,"bbb [b]"); da->setInfoOnComponent(2,"ccc [c]");
142 da->setInfoOnComponent(3,"ddd [d]"); da->setInfoOnComponent(4,"eee [e]"); da->setInfoOnComponent(5,"fff [f]");
143 da->setInfoOnComponent(6,"ggg [g]");
145 for(int i=0;i<nbOfTimeSteps;i++)
147 double *pt=da->getPointer();
149 for(int k=0;k<nbOfComponents;k++,pt++)
150 *pt=(i+1)*100.+(j+1)*10.+k+1;
151 f->setTime(i*0.1,i,-i);
152 MEDLoader::WriteFieldUsingAlreadyWrittenMesh(fName,f);
159 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython1()
164 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
166 CPPUNIT_ASSERT_EQUAL(s2,s1);
168 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
170 CPPUNIT_ASSERT_EQUAL(s2,s1);
172 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
174 CPPUNIT_ASSERT_EQUAL(s2,s1);
176 CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
178 CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
180 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
182 CPPUNIT_ASSERT_EQUAL(s2,s1);
184 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
186 CPPUNIT_ASSERT_EQUAL(s2,s1);
188 CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
190 CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
193 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
195 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
197 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
199 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
201 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
203 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
205 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
207 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
209 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
211 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
214 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython2()
218 CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isParenthesisMatching(s1,s2));
219 CPPUNIT_ASSERT_EQUAL(s2,std::string("\"12:3\",\":\",4"));
222 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython3()
225 s1="( 12:3 , : ,4 )";
226 s2=ParaMEDMEM::SPythonInterpreter::strip(s1);
227 CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));
228 s1=" ( 12:3 , : ,4 ) ";
229 s2=ParaMEDMEM::SPythonInterpreter::strip(s1);
230 CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));