Salome HOME
tests for Strickler table
authorasl <asl@opencascade.com>
Tue, 13 Oct 2015 10:36:34 +0000 (13:36 +0300)
committerasl <asl@opencascade.com>
Tue, 13 Oct 2015 10:36:34 +0000 (13:36 +0300)
src/HYDROData/HYDROData_StricklerTable.cxx
src/HYDROData/HYDROData_StricklerTable.h
src/HYDROGUI/HYDROGUI_DataModel.cxx
src/HYDROGUI/HYDROGUI_StricklerTableOp.cxx
src/HYDROGUI/resources/def_strickler_table.txt
src/HYDROPy/HYDROData_StricklerTable.sip
src/HYDRO_tests/CMakeLists.txt
src/HYDRO_tests/operators.h [new file with mode: 0644]
src/HYDRO_tests/test_HYDROData_Main.cxx
src/HYDRO_tests/test_HYDROData_StricklerTable.cxx
src/HYDRO_tests/test_HYDROData_StricklerTable.h

index 1cd277049952ee19fc29d6b8077c2f465eea8a85..d206c59cb2d5c8a5dc862a659d19847e89c5237c 100644 (file)
@@ -39,8 +39,6 @@
 IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
 
-const char ATTR_SUFFIX[] = " attr_value";
-
 HYDROData_StricklerTable::HYDROData_StricklerTable()
 {
 }
@@ -54,13 +52,13 @@ const ObjectKind HYDROData_StricklerTable::GetKind() const
   return KIND_STRICKLER_TABLE;
 }
 
-bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
+bool HYDROData_StricklerTable::Import( const QString& theFileName )
 {
   Handle(TDataStd_NamedData) aMap = Map();
   if( aMap.IsNull() )
     return false;
 
-  QFile aFile( theFileName.ToCString() );
+  QFile aFile( theFileName );
   if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
     return false;
 
@@ -99,31 +97,38 @@ bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileNam
   return true;
 }
 
-bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
+bool HYDROData_StricklerTable::Export( const QString& theFileName )
 {
   Handle(TDataStd_NamedData) aMap = Map();
   if( aMap.IsNull() )
     return false;
 
-  QFile aFile( theFileName.ToCString() );
+  QFile aFile( theFileName );
   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
     return false;
 
   QTextStream aStream( &aFile );
   aStream.setCodec( "UTF-8" );
+  aStream.setGenerateByteOrderMark( true );
 
   aStream << GetAttrName() << "\n";
 
   bool aRes = true;
-  for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
+  QStringList aTypes = GetTypes();
+  foreach( QString aType, aTypes )
   {
-    QString aType = toQString( it.Key() );
-    aStream << "\"" << aType << "\"  " << it.Value();
+    TCollection_ExtendedString aTypeExt = toExtString( aType );
+
+    aStream << "\"" << aType << "\" " << Get( aType, 0.0 );
 
-    QString aColor = QString::number( aMap->GetInteger( it.Key() ), 16 ).toUpper();
-    QString anAttrValue = toQString( aMap->GetString( it.Key() ) );
+    QString aColor = QString::number( aMap->GetInteger( aTypeExt ), 16 ).toUpper();
+    aColor = QString( 6-aColor.length(), '0' ) + aColor;
+    QString anAttrValue = toQString( aMap->GetString( aTypeExt ) );
 
-    aStream << " " << aColor << " " << anAttrValue << "\n";
+    aStream << " " << aColor;
+    if( !anAttrValue.isEmpty() )
+      aStream << " " << anAttrValue;
+    aStream << "\n";
   }
 
   aFile.close();
@@ -176,6 +181,7 @@ QStringList HYDROData_StricklerTable::GetTypes() const
     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
        aSeq.append( toQString( it.Key() ) );
   }
+  aSeq.sort();
   return aSeq;
 }
 
@@ -261,7 +267,7 @@ void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
+  TCollection_ExtendedString aType = toExtString( theType );
   if( aMap->HasString( aType ) )
     return toQString( aMap->GetString( aType ) );
   else
@@ -271,7 +277,7 @@ QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
+  TCollection_ExtendedString aType = toExtString( theType );
   aMap->SetString( aType, toExtString( theAttrValue ) );
 }
 
@@ -279,13 +285,11 @@ QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
   TCollection_ExtendedString anAttrValue = toExtString( theAttrValue );
-  int l = strlen( ATTR_SUFFIX );
   for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
   {
     if( it.Value() == anAttrValue )
     {
       QString aType = toQString( it.Key() );
-      aType.remove( aType.length() - l, l );
       return aType;
     }
   }
index 5cf2e7f57caa79e15b515a234ecf1d8336d3b29c..62323e328fae9465f95bff4f17cffff637735bcc 100644 (file)
@@ -46,8 +46,8 @@ public:
 
   HYDRODATA_EXPORT virtual const ObjectKind GetKind() const;
 
-  HYDRODATA_EXPORT bool Import( const TCollection_AsciiString& theFileName );
-  HYDRODATA_EXPORT bool Export( const TCollection_AsciiString& theFileName );
+  HYDRODATA_EXPORT bool Import( const QString& theFileName );
+  HYDRODATA_EXPORT bool Export( const QString& theFileName );
 
   HYDRODATA_EXPORT double Get( const QString& theType, double theDefault ) const;
   HYDRODATA_EXPORT void Set( const QString& theType, double theCoefficient );
index 1478a7473c2deaedb6d26afbaf15645ea102e83e..2f05232531bec83b84f4750bddb4b1630e78f606 100644 (file)
@@ -807,7 +807,7 @@ void HYDROGUI_DataModel::createDefaultStricklerTable( const Handle(HYDROData_Doc
   {
     SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
     QString defTablePath = resMgr->path( "resources", module()->name(), tr( "DEFAULT_STRICKLER_TABLE_FILE" ) );
-    aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( defTablePath ) );
+    aStricklerTableObj->Import( defTablePath );
        // Set name
     QString aStricklerTableName;
     if ( aStricklerTableObj->GetName().isEmpty() )
index bfb3bb7de368bb0c32b81246d81c666517d11e07..31435e65069e112797f6704a74dc2ba5e2b6907b 100644 (file)
@@ -150,7 +150,7 @@ bool HYDROGUI_StricklerTableOp::processApply( int& theUpdateFlags, QString& theE
         bool res = false;
         QString aFilePath = aPanel->getFileName().simplified();
         if ( !aFilePath.isEmpty() )
-            res = aStricklerTableObj->Export( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
+            res = aStricklerTableObj->Export( aFilePath );
         return res;
     }
 
@@ -172,7 +172,7 @@ bool HYDROGUI_StricklerTableOp::processApply( int& theUpdateFlags, QString& theE
     else
     {
         // Import data from Strickler table file into data model object
-        aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
+        aStricklerTableObj->Import( aFilePath );
     }
 
     aStricklerTableObj->Update();
index 29c9c23195be04a9b801d4b3063075c8ee34f176..b26d58121a130bd3a19b52a1007a290f40e223b6 100644 (file)
@@ -1,9 +1,9 @@
 CODE_06
-"Zones de champs, prairies, sans cultures"     20.0          FF0000     511
-"Zones de champs cultivé à végétation basse"   17.5          FFFF00     512
-"Zones de champs cultivé à végétation haute"   12.5          00FF00
-"Zones d'arbustes, de sous-bois"               10.0          00FFFF
-"Zones à faible urbanization (bourg)"           9.0          0000FF
-"Zones à forte urbanization (agglomération)"    9.0          123456
-"Canaux naturels"                              35.0          FF00FF
-"Canaux artificiels en béton"                  65.0          888888
+"Canaux artificiels en béton" 65 888888
+"Canaux naturels" 35 FF00FF
+"Zones d'arbustes, de sous-bois" 10 00FFFF
+"Zones de champs cultivé à végétation basse" 17.5 FFFF00 512
+"Zones de champs cultivé à végétation haute" 12.5 00FF00
+"Zones de champs, prairies, sans cultures" 20 FF0000 511
+"Zones à faible urbanization (bourg)" 9 0000FF
+"Zones à forte urbanization (agglomération)" 9 123456
index 60a038048d11ea7df493fd7e94fbf7e19b228485..a6493486c68d7f06215f24f64c14f912d6c4f067 100644 (file)
@@ -43,8 +43,8 @@ class HYDROData_StricklerTable : public HYDROData_Entity
 
 public:      
 
-  bool Import( const TCollection_AsciiString& theFileName );
-  bool Export( const TCollection_AsciiString& theFileName );
+  bool Import( const QString& theFileName );
+  bool Export( const QString& theFileName );
 
   double Get( const QString& theType, double theDefault ) const;
   void Set( const QString& theType, double theCoefficient );
index ebc2802ce76e1724430452d7e2f58c5e4141aca3..7a9943cd3bf6df0cecd92c01ba3b462582ac9b81 100644 (file)
@@ -1,6 +1,7 @@
 #include(../../CMake/Common.cmake)
 
 set(PROJECT_HEADERS
+  operators.h
   test_HYDROData_Bathymetry.h
   test_HYDROData_Document.h
   test_HYDROData_Entity.h
diff --git a/src/HYDRO_tests/operators.h b/src/HYDRO_tests/operators.h
new file mode 100644 (file)
index 0000000..ebc16d7
--- /dev/null
@@ -0,0 +1,5 @@
+
+#pragma once
+
+std::ostream& operator<<( std::ostream& theStream, const QString& theText );
+std::ostream& operator<<( std::ostream& theStream, const QColor& theText );
\ No newline at end of file
index 1ffc9358766d418570fa85623de29d6c32716f96..b9590889429c7191810386ceceb066718a128617 100644 (file)
 #include <cppunit/TextTestProgressListener.h>
 #include <stdexcept>
 #include <QApplication>
+#include <QColor>
+
+std::ostream& operator<<( std::ostream& theStream, const QString& theText )
+{
+  theStream << theText.toStdString();
+  return theStream;
+}
+
+std::ostream& operator<<( std::ostream& theStream, const QColor& theColor )
+{
+  theStream << "[" << theColor.red() << ", " << theColor.green() << ", " << theColor.blue() << "]";
+  return theStream;
+}
 
 int main( int argc, char* argv[] )
 {
index 4cf579891f65d205661c11874810eea6c2198c79..4c0b5f80a3e17f4bcb607bd901e79d839737a635 100644 (file)
 
 #include <test_HYDROData_StricklerTable.h>
 #include <HYDROData_Document.h>
+#include <HYDROData_StricklerTable.h>
+#include <QStringList>
+#include <QColor>
+#include <QDir>
+#include <operators.h>
 
-void test_HYDROData_StricklerTable::testImport()
+const QString DEF_STR_PATH = qgetenv( "HYDRO_ROOT_DIR" ) + "/share/salome/resources/hydro/def_strickler_table.txt";
+
+void test_HYDROData_StricklerTable::test_import()
 {
   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
 
-  Handle(HYDROData_Entity) anObj = aDoc->CreateObject( KIND_STRICKLER_TABLE );
-  TCollection_AsciiString aFileName = "test_name";
-  
+  Handle(HYDROData_StricklerTable) aTable =
+    Handle(HYDROData_StricklerTable)::DownCast( aDoc->CreateObject( KIND_STRICKLER_TABLE ) );
+
+  CPPUNIT_ASSERT_EQUAL( true, aTable->Import( DEF_STR_PATH ) );
+
+  QStringList aTypes = aTable->GetTypes();
+  CPPUNIT_ASSERT_EQUAL( 8, aTypes.size() );
+  CPPUNIT_ASSERT_EQUAL( QString( "CODE_06" ), aTable->GetAttrName() );
+
+  QString aType = "Zones de champs cultivé à végétation basse";
+  CPPUNIT_ASSERT_EQUAL( aType, aTypes[3] );
+  CPPUNIT_ASSERT_EQUAL( QColor( 255, 255, 0 ), aTable->GetColor( aType ) );
+  CPPUNIT_ASSERT_EQUAL( QString( "512" ), aTable->GetAttrValue( aType ) );
+
+  aType = "Zones à forte urbanization (agglomération)";
+  CPPUNIT_ASSERT_EQUAL( aType, aTypes[7] );
+  CPPUNIT_ASSERT_EQUAL( QColor( 18, 52, 86 ), aTable->GetColor( aType ) );
+  CPPUNIT_ASSERT_EQUAL( QString( "" ), aTable->GetAttrValue( aType ) );
+
   aDoc->Close();
 }
+
+void test_HYDROData_StricklerTable::test_import_export_equivalence()
+{
+  Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
+
+  Handle(HYDROData_StricklerTable) aTable =
+    Handle(HYDROData_StricklerTable)::DownCast( aDoc->CreateObject( KIND_STRICKLER_TABLE ) );
+
+  CPPUNIT_ASSERT_EQUAL( true, aTable->Import( DEF_STR_PATH ) );
+  QString aTmpPath = QDir::tempPath() + "/stricker.txt";
+  CPPUNIT_ASSERT_EQUAL( true, aTable->Export( aTmpPath ) );
+
+  QFile aRefFile( DEF_STR_PATH ), aTmpFile( aTmpPath );
+  CPPUNIT_ASSERT_EQUAL( true, aRefFile.open( QFile::ReadOnly ) );
+  CPPUNIT_ASSERT_EQUAL( true, aTmpFile.open( QFile::ReadOnly ) );
+
+  QByteArray aRefContents = aRefFile.readAll();
+  QByteArray aTmpContents = aTmpFile.readAll();
+
+  bool isEqual = aRefContents.size()==aTmpContents.size();
+  CPPUNIT_ASSERT_EQUAL( true, isEqual );
+  for( int i=0, n=aRefContents.size(); isEqual && i<n; i++ )
+    if( aRefContents[i]!=aTmpContents[i] )
+      isEqual = false;
+
+  CPPUNIT_ASSERT_EQUAL( true, isEqual );
+
+  aRefFile.close();
+  aTmpFile.close();
+  aDoc->Close();
+}
+
index fcd1a3889931f57b3bd2600bc1e353c03125dfe9..c19705f0a443e33dac3193514b6423a821208d89 100644 (file)
 class test_HYDROData_StricklerTable : public CppUnit::TestFixture
 {
   CPPUNIT_TEST_SUITE( test_HYDROData_StricklerTable );
-  CPPUNIT_TEST( testImport );
+  CPPUNIT_TEST( test_import );
+  CPPUNIT_TEST( test_import_export_equivalence );
   CPPUNIT_TEST_SUITE_END();
 
 public:
-  void testImport();
+  void test_import();
+  void test_import_export_equivalence();
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION( test_HYDROData_StricklerTable );