From f64a3ec454c0826cd41e7bff2d2b64bccebb1cd4 Mon Sep 17 00:00:00 2001 From: asl Date: Tue, 13 Oct 2015 13:36:34 +0300 Subject: [PATCH] tests for Strickler table --- src/HYDROData/HYDROData_StricklerTable.cxx | 36 ++++++----- src/HYDROData/HYDROData_StricklerTable.h | 4 +- src/HYDROGUI/HYDROGUI_DataModel.cxx | 2 +- src/HYDROGUI/HYDROGUI_StricklerTableOp.cxx | 4 +- .../resources/def_strickler_table.txt | 16 ++--- src/HYDROPy/HYDROData_StricklerTable.sip | 4 +- src/HYDRO_tests/CMakeLists.txt | 1 + src/HYDRO_tests/operators.h | 5 ++ src/HYDRO_tests/test_HYDROData_Main.cxx | 13 ++++ .../test_HYDROData_StricklerTable.cxx | 63 +++++++++++++++++-- .../test_HYDROData_StricklerTable.h | 6 +- 11 files changed, 117 insertions(+), 37 deletions(-) create mode 100644 src/HYDRO_tests/operators.h diff --git a/src/HYDROData/HYDROData_StricklerTable.cxx b/src/HYDROData/HYDROData_StricklerTable.cxx index 1cd27704..d206c59c 100644 --- a/src/HYDROData/HYDROData_StricklerTable.cxx +++ b/src/HYDROData/HYDROData_StricklerTable.cxx @@ -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; } } diff --git a/src/HYDROData/HYDROData_StricklerTable.h b/src/HYDROData/HYDROData_StricklerTable.h index 5cf2e7f5..62323e32 100644 --- a/src/HYDROData/HYDROData_StricklerTable.h +++ b/src/HYDROData/HYDROData_StricklerTable.h @@ -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 ); diff --git a/src/HYDROGUI/HYDROGUI_DataModel.cxx b/src/HYDROGUI/HYDROGUI_DataModel.cxx index 1478a747..2f052325 100644 --- a/src/HYDROGUI/HYDROGUI_DataModel.cxx +++ b/src/HYDROGUI/HYDROGUI_DataModel.cxx @@ -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() ) diff --git a/src/HYDROGUI/HYDROGUI_StricklerTableOp.cxx b/src/HYDROGUI/HYDROGUI_StricklerTableOp.cxx index bfb3bb7d..31435e65 100644 --- a/src/HYDROGUI/HYDROGUI_StricklerTableOp.cxx +++ b/src/HYDROGUI/HYDROGUI_StricklerTableOp.cxx @@ -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(); diff --git a/src/HYDROGUI/resources/def_strickler_table.txt b/src/HYDROGUI/resources/def_strickler_table.txt index 29c9c231..b26d5812 100644 --- a/src/HYDROGUI/resources/def_strickler_table.txt +++ b/src/HYDROGUI/resources/def_strickler_table.txt @@ -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 diff --git a/src/HYDROPy/HYDROData_StricklerTable.sip b/src/HYDROPy/HYDROData_StricklerTable.sip index 60a03804..a6493486 100644 --- a/src/HYDROPy/HYDROData_StricklerTable.sip +++ b/src/HYDROPy/HYDROData_StricklerTable.sip @@ -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 ); diff --git a/src/HYDRO_tests/CMakeLists.txt b/src/HYDRO_tests/CMakeLists.txt index ebc2802c..7a9943cd 100644 --- a/src/HYDRO_tests/CMakeLists.txt +++ b/src/HYDRO_tests/CMakeLists.txt @@ -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 index 00000000..ebc16d7e --- /dev/null +++ b/src/HYDRO_tests/operators.h @@ -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 diff --git a/src/HYDRO_tests/test_HYDROData_Main.cxx b/src/HYDRO_tests/test_HYDROData_Main.cxx index 1ffc9358..b9590889 100644 --- a/src/HYDRO_tests/test_HYDROData_Main.cxx +++ b/src/HYDRO_tests/test_HYDROData_Main.cxx @@ -24,6 +24,19 @@ #include #include #include +#include + +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[] ) { diff --git a/src/HYDRO_tests/test_HYDROData_StricklerTable.cxx b/src/HYDRO_tests/test_HYDROData_StricklerTable.cxx index 4cf57989..4c0b5f80 100644 --- a/src/HYDRO_tests/test_HYDROData_StricklerTable.cxx +++ b/src/HYDRO_tests/test_HYDROData_StricklerTable.cxx @@ -18,13 +18,68 @@ #include #include +#include +#include +#include +#include +#include -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 && iClose(); +} + diff --git a/src/HYDRO_tests/test_HYDROData_StricklerTable.h b/src/HYDRO_tests/test_HYDROData_StricklerTable.h index fcd1a388..c19705f0 100644 --- a/src/HYDRO_tests/test_HYDROData_StricklerTable.h +++ b/src/HYDRO_tests/test_HYDROData_StricklerTable.h @@ -25,11 +25,13 @@ 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 ); -- 2.39.2