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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <test_HYDROData_ShapeFile.h>
20 #include <HYDROData_ShapeFile.h>
21 #include <QStringList>
24 const QString REF_PATH = qgetenv( "HYDRO_REFERENCE_DATA" );
26 bool test_HYDROData_ShapeFile::compare_two_files(const QString& File1, const QString& File2)
29 fp1 = fopen(File1.toStdString().c_str(), "r");
30 fp2 = fopen(File2.toStdString().c_str(), "r");
34 while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
49 void test_HYDROData_ShapeFile::test_openDbf()
51 QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
52 HYDROData_ShapeFile aSHPFile;
53 bool Stat = aSHPFile.DBF_OpenDBF(aDBFPath);
54 CPPUNIT_ASSERT_EQUAL( true, Stat );
55 aSHPFile.DBF_CloseDBF();
58 void test_HYDROData_ShapeFile::test_NbFieldsDbf()
60 QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
61 HYDROData_ShapeFile aSHPFile;
62 CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
63 int NbF = aSHPFile.DBF_GetNbFields();
64 CPPUNIT_ASSERT_EQUAL( 2, NbF );
65 aSHPFile.DBF_CloseDBF();
68 void test_HYDROData_ShapeFile::test_FieldListDbf()
70 QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
71 HYDROData_ShapeFile aSHPFile;
72 CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
74 QStringList FL = aSHPFile.DBF_GetFieldList();
75 CPPUNIT_ASSERT_EQUAL(true, "NAME" == FL[0]);
76 CPPUNIT_ASSERT_EQUAL(true, "TYPE" == FL[1]);
77 aSHPFile.DBF_CloseDBF();
80 void test_HYDROData_ShapeFile::test_FieldTypeListDbf()
82 QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
83 HYDROData_ShapeFile aSHPFile;
84 CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
86 std::vector<HYDROData_ShapeFile::DBF_FieldType> FTVect;
87 aSHPFile.DBF_GetFieldTypeList(FTVect);
88 CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[0]);
89 CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[1]);
90 aSHPFile.DBF_CloseDBF();
93 void test_HYDROData_ShapeFile::test_NbRecordsDbf()
95 QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
96 HYDROData_ShapeFile aSHPFile;
97 CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
98 int NbR = aSHPFile.DBF_GetNbRecords();
99 CPPUNIT_ASSERT_EQUAL( 268, NbR );
100 aSHPFile.DBF_CloseDBF();
103 void test_HYDROData_ShapeFile::test_GetAttrListIndex()
106 QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
107 HYDROData_ShapeFile aSHPFile;
108 CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
109 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
110 aSHPFile.DBF_GetAttributeList(0, theAttrV ); //get all records take from the first field
115 CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() );
116 CPPUNIT_ASSERT_EQUAL( true, std::string("Ovgos Dam") == theAttrV[22].myRawValue);
117 CPPUNIT_ASSERT_EQUAL( true, QString("Ovgos Dam") == theAttrV[22].myStrVal);
118 CPPUNIT_ASSERT_EQUAL( false, theAttrV[22].myIsNull);
119 CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[22].myFieldType);
122 CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myIsNull);
123 CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myRawValue.empty());
124 CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[35].myFieldType);
127 aSHPFile.DBF_GetAttributeList(1, theAttrV ); //second field
128 CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() );
129 CPPUNIT_ASSERT_EQUAL( true, std::string("water") == theAttrV[3].myRawValue);
130 CPPUNIT_ASSERT_EQUAL( true, QString("water") == theAttrV[3].myStrVal);
131 CPPUNIT_ASSERT_EQUAL( false, theAttrV[3].myIsNull);
132 CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[3].myFieldType);
134 aSHPFile.DBF_CloseDBF();
137 QString aDBFPath = REF_PATH + "/CLC06.dbf";
138 HYDROData_ShapeFile aSHPFile;
139 CPPUNIT_ASSERT_EQUAL (true, aSHPFile.DBF_OpenDBF(aDBFPath));
140 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
142 aSHPFile.DBF_GetAttributeList(0, theAttrV );
143 CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() );
144 CPPUNIT_ASSERT_EQUAL( true, std::string("FR-184045") == theAttrV[2].myRawValue);
145 CPPUNIT_ASSERT_EQUAL( true, QString("FR-184045") == theAttrV[2].myStrVal);
146 CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
147 CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[2].myFieldType);
150 aSHPFile.DBF_GetAttributeList(2, theAttrV );
151 CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() );
152 CPPUNIT_ASSERT_EQUAL( true, std::string("6621.1654324936000000000000000000") == theAttrV[2].myRawValue);
153 CPPUNIT_ASSERT_EQUAL( 6621.1654324935998375, theAttrV[2].myDoubleVal);
154 CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
155 CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_Double, theAttrV[2].myFieldType);
157 aSHPFile.DBF_CloseDBF();
162 void test_HYDROData_ShapeFile::test_DbfWrite()
165 HYDROData_ShapeFile aSHPFile;
166 QString tempFN = REF_PATH + "/temp_dbf1.dbf";
167 QString refFN = REF_PATH + "/ref_dbf1.dbf";
168 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
169 HYDROData_ShapeFile::DBF_AttrValue theAttr1;
170 theAttr1.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
171 theAttr1.myIsNull = false;
172 theAttr1.myRawValue = "test_value1";
173 theAttr1.myStrVal = "test_value1";
174 theAttrV.push_back(theAttr1);
175 HYDROData_ShapeFile::DBF_AttrValue theAttr2;
176 theAttr2.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
177 theAttr2.myIsNull = false;
178 theAttr2.myRawValue = "test_value2";
179 theAttr2.myStrVal = "test_value2";
180 theAttrV.push_back(theAttr2);
182 CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, true));
184 CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
185 CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));
189 HYDROData_ShapeFile aSHPFile;
190 QString tempFN = REF_PATH + "/temp_dbf2.dbf";
191 QString refFN = REF_PATH + "/ref_dbf2.dbf";
192 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
193 HYDROData_ShapeFile::DBF_AttrValue theAttr1;
194 theAttr1.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
195 theAttr1.myIsNull = false;
196 theAttr1.myRawValue = "test_value1";
197 theAttr1.myStrVal = "test_value1";
198 theAttrV.push_back(theAttr1);
200 HYDROData_ShapeFile::DBF_AttrValue theAttr2;
201 theAttr2.myIsNull = false;
202 theAttr2.myRawValue = "test_value_K";
203 theAttr2.myStrVal = "test_value_K";
204 theAttr2.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
205 for (int ii = 0; ii < 100; ii++)
206 theAttrV.push_back(theAttr2);
208 HYDROData_ShapeFile::DBF_AttrValue theAttr3;
209 theAttr3.myIsNull = true;
210 theAttr3.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
211 for (int ii = 0; ii < 100; ii++)
212 theAttrV.push_back(theAttr3);
214 CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, true));
216 CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
217 CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));
221 HYDROData_ShapeFile aSHPFile;
222 QString tempFN = REF_PATH + "/temp_dbf3.dbf";
223 QString refFN = REF_PATH + "/ref_dbf3.dbf";
224 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
225 HYDROData_ShapeFile::DBF_AttrValue theAttr1;
226 theAttr1.myFieldType = HYDROData_ShapeFile::DBF_FieldType_Integer;
227 theAttr1.myIsNull = false;
229 for (int ii = 0; ii < 100; ii++)
231 theAttr1.myIntVal = ii;
232 theAttrV.push_back(theAttr1);
236 CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_Integer, theAttrV, false));
238 CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
239 CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));