Salome HOME
ccbb8fad8c1249413f160f9117f8800f72285931
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_ShapeFile.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <test_HYDROData_ShapeFile.h>
20 #include <HYDROData_ShapeFile.h>
21 #include <QStringList>
22 #include <vector>
23
24 const QString REF_PATH = qgetenv( "HYDRO_REFERENCE_DATA" );
25
26 void test_HYDROData_ShapeFile::test_openDbf()
27 {
28   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
29   HYDROData_ShapeFile aSHPFile;
30   bool Stat = aSHPFile.DBF_OpenDBF(aDBFPath);
31   CPPUNIT_ASSERT_EQUAL( true, Stat );  
32   aSHPFile.DBF_CloseDBF();
33 }
34
35 void test_HYDROData_ShapeFile::test_NbFieldsDbf()
36 {
37   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
38   HYDROData_ShapeFile aSHPFile;
39   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
40   int NbF = aSHPFile.DBF_GetNbFields();
41   CPPUNIT_ASSERT_EQUAL( 2, NbF );  
42   aSHPFile.DBF_CloseDBF();
43 }
44
45 void test_HYDROData_ShapeFile::test_FieldListDbf()
46 {
47   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
48   HYDROData_ShapeFile aSHPFile;
49   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
50
51   QStringList FL = aSHPFile.DBF_GetFieldList();
52   CPPUNIT_ASSERT_EQUAL(true, "NAME" == FL[0]);
53   CPPUNIT_ASSERT_EQUAL(true, "TYPE" == FL[1]);
54   aSHPFile.DBF_CloseDBF();
55 }
56
57 void test_HYDROData_ShapeFile::test_FieldTypeListDbf()
58 {
59   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
60   HYDROData_ShapeFile aSHPFile;
61   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
62
63   std::vector<HYDROData_ShapeFile::DBF_FieldType> FTVect;
64   aSHPFile.DBF_GetFieldTypeList(FTVect);
65   CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[0]);
66   CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[1]);
67   aSHPFile.DBF_CloseDBF();
68 }
69
70 void test_HYDROData_ShapeFile::test_NbRecordsDbf()
71 {
72   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
73   HYDROData_ShapeFile aSHPFile;
74   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
75   int NbR = aSHPFile.DBF_GetNbRecords();
76   CPPUNIT_ASSERT_EQUAL( 268, NbR );  
77   aSHPFile.DBF_CloseDBF();
78 }
79
80 void test_HYDROData_ShapeFile::test_GetAttrListIndex()
81 {
82   { //cyprus_natural
83     QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
84     HYDROData_ShapeFile aSHPFile;
85     CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
86     std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
87     aSHPFile.DBF_GetAttributeList(0, theAttrV ); //get all records take from the first field
88
89     //Check Values
90
91     //Non-null attr
92     CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() ); 
93     CPPUNIT_ASSERT_EQUAL( true, std::string("Ovgos Dam") == theAttrV[22].myRawValue);
94     CPPUNIT_ASSERT_EQUAL( true, QString("Ovgos Dam") == theAttrV[22].myStrVal);
95     CPPUNIT_ASSERT_EQUAL( false, theAttrV[22].myIsNull);
96     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[22].myFieldType);
97
98     //Null attr
99     CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myIsNull);
100     CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myRawValue.empty());
101     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[35].myFieldType);
102
103     theAttrV.clear();
104     aSHPFile.DBF_GetAttributeList(1, theAttrV ); //second field
105     CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() ); 
106     CPPUNIT_ASSERT_EQUAL( true, std::string("water") == theAttrV[3].myRawValue);
107     CPPUNIT_ASSERT_EQUAL( true, QString("water") == theAttrV[3].myStrVal);
108     CPPUNIT_ASSERT_EQUAL( false, theAttrV[3].myIsNull);
109     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[3].myFieldType);
110
111     aSHPFile.DBF_CloseDBF();
112   }
113   { //CLC06
114     QString aDBFPath = REF_PATH + "/CLC06.dbf";
115     HYDROData_ShapeFile aSHPFile;
116     CPPUNIT_ASSERT_EQUAL (true, aSHPFile.DBF_OpenDBF(aDBFPath));
117     std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
118
119     aSHPFile.DBF_GetAttributeList(0, theAttrV ); 
120     CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() ); 
121     CPPUNIT_ASSERT_EQUAL( true, std::string("FR-184045") == theAttrV[2].myRawValue);
122     CPPUNIT_ASSERT_EQUAL( true, QString("FR-184045") == theAttrV[2].myStrVal);
123     CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
124     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[2].myFieldType);
125     //
126     theAttrV.clear();
127     aSHPFile.DBF_GetAttributeList(2, theAttrV ); 
128     CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() ); 
129     CPPUNIT_ASSERT_EQUAL( true, std::string("6621.1654324936000000000000000000") == theAttrV[2].myRawValue);
130     CPPUNIT_ASSERT_EQUAL( true, QString("6621.1654324935998375") == theAttrV[2].myStrVal);
131     CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
132     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_Double, theAttrV[2].myFieldType);
133
134     aSHPFile.DBF_CloseDBF();
135   }
136 }