From: asl Date: Fri, 16 Oct 2015 11:52:16 +0000 (+0300) Subject: new randomizer equivalent for 32 and 64 bits platform X-Git-Tag: v1.5~101^2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=c40bde9a5a0840614502cc0e56b253b2d3648236;p=modules%2Fhydro.git new randomizer equivalent for 32 and 64 bits platform --- diff --git a/src/HYDRO_tests/CMakeLists.txt b/src/HYDRO_tests/CMakeLists.txt index 8916171a..30e87f5b 100644 --- a/src/HYDRO_tests/CMakeLists.txt +++ b/src/HYDRO_tests/CMakeLists.txt @@ -18,6 +18,7 @@ set(PROJECT_HEADERS TestViewer.h TestLib_Listener.h TestLib_Runner.h + random.h ) set(PROJECT_SOURCES @@ -38,6 +39,7 @@ set(PROJECT_SOURCES TestViewer.cxx TestLib_Listener.cxx TestLib_Runner.cxx + random.cxx ) add_definitions( diff --git a/src/HYDRO_tests/TestViewer.cxx b/src/HYDRO_tests/TestViewer.cxx index 7f891922..1a5e0bf9 100644 --- a/src/HYDRO_tests/TestViewer.cxx +++ b/src/HYDRO_tests/TestViewer.cxx @@ -1,17 +1,35 @@ #include +#include #include +#ifdef WIN32 + #pragma warning ( disable: 4251 ) +#endif #include +#ifdef WIN32 + #pragma warning ( disable: 4251 ) +#endif #include +#ifdef WIN32 + #pragma warning ( disable: 4251 ) +#endif #include #include #include + #include #include +#include + +#ifdef WIN32 + #pragma warning ( default: 4251 ) +#endif + #include OCCViewer_ViewManager* TestViewer::myViewManager = 0; OCCViewer_ViewWindow* TestViewer::myViewWindow = 0; +QString TestViewer::myKey = ""; OCCViewer_ViewManager* TestViewer::viewManager() { @@ -47,41 +65,51 @@ Handle(AIS_InteractiveContext) context() return TestViewer::viewer()->getAISContext(); } -void TestViewer::show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode ) +QColor randomColor() { - QColor aColor = theColor; - if( !aColor.isValid() ) - { - // random color - int aHue = rand()%255; - aColor = QColor::fromHsl( aHue, 255, 128 ); - } + int aHue = rand()%255; + QColor aColor = QColor::fromHsl( aHue, 255, 128 ); + return aColor; +} - double r = aColor.red() / 255.0; - double g = aColor.green() / 255.0; - double b = aColor.blue() / 255.0; +void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor ) +{ + double r = theColor.red() / 255.0; + double g = theColor.green() / 255.0; + double b = theColor.blue() / 255.0; Handle(AIS_Shape) aShape = new AIS_Shape( theShape ); aShape->SetMaterial( Graphic3d_NOM_PLASTIC ); aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) ); context()->Display( aShape, theMode, 0, Standard_False ); + + if( isFitAll ) + { + viewWindow()->onTopView(); + viewWindow()->onFitAll(); + } } -void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll ) +void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, char* theKey ) { context()->EraseAll(); + myKey = theKey; + test_srand( qHash( theKey ) ); if( theShape.ShapeType()==TopAbs_COMPOUND ) { TopoDS_Iterator anIt( theShape ); for( ; anIt.More(); anIt.Next() ) - show( anIt.Value(), QColor(), theMode ); + show( anIt.Value(), theMode, false, randomColor() ); } else - show( theShape, QColor(), theMode ); + show( theShape, theMode, false, randomColor() ); - viewWindow()->onTopView(); - viewWindow()->onFitAll(); + if( isFitAll ) + { + viewWindow()->onTopView(); + viewWindow()->onFitAll(); + } } bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance ) @@ -106,21 +134,24 @@ bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double th return true; } -bool TestViewer::AssertEqual( const QString& theUseCaseName ) +bool TestViewer::AssertImages( QString& theMessage ) { QImage anActualImage = viewWindow()->dumpView(); QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" ); - anExpectedRefFilePath += "/" + theUseCaseName + ".png"; + anExpectedRefFilePath += "/" + myKey + ".png"; QImage anExpectedRefImage; anExpectedRefImage.load( anExpectedRefFilePath ); - if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.2 ) ) + if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) ) + { + theMessage = ""; return true; + } - QString aPath = QDir::tempPath() + "/" + theUseCaseName + ".png"; + QString aPath = QDir::tempPath() + "/" + myKey + ".png"; anActualImage.save( aPath ); - std::string aMessage = "The viewer contents does not correspond to the reference image: " + theUseCaseName.toStdString(); + theMessage = "The viewer contents does not correspond to the reference image: " + myKey; QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 ); QPainter aPainter( &aDiff ); @@ -128,9 +159,8 @@ bool TestViewer::AssertEqual( const QString& theUseCaseName ) aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination ); aPainter.drawImage( 0, 0, anActualImage ); - QString aDiffFilePath = QDir::tempPath() + "/" + theUseCaseName + "_diff.png"; + QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png"; aDiff.save( aDiffFilePath ); - CPPUNIT_FAIL( aMessage.c_str() ); return false; } diff --git a/src/HYDRO_tests/TestViewer.h b/src/HYDRO_tests/TestViewer.h index e825208b..669b5621 100644 --- a/src/HYDRO_tests/TestViewer.h +++ b/src/HYDRO_tests/TestViewer.h @@ -15,11 +15,23 @@ public: static OCCViewer_Viewer* viewer(); static OCCViewer_ViewWindow* viewWindow(); - static void show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode ); - static void show( const TopoDS_Shape& theShape, int theMode, bool isFitAll = true ); - static bool AssertEqual( const QString& theUseCaseName ); + static void show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor ); + static void show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, char* theKey ); + static bool AssertImages( QString& theMessage ); private: static OCCViewer_ViewManager* myViewManager; static OCCViewer_ViewWindow* myViewWindow; + static QString myKey; }; + +#define CPPUNIT_ASSERT_IMAGES \ + { \ + QString aMessage; \ + if( !TestViewer::AssertImages( aMessage ) ) \ + { \ + std::string aMessageStl = aMessage.toStdString(); \ + CPPUNIT_FAIL( aMessageStl.c_str() ); \ + } \ + } \ + diff --git a/src/HYDRO_tests/random.cxx b/src/HYDRO_tests/random.cxx new file mode 100644 index 00000000..da51deba --- /dev/null +++ b/src/HYDRO_tests/random.cxx @@ -0,0 +1,18 @@ + +#include + +int m_w = 1; +int m_z = 1; + +void test_srand( unsigned int theValue ) +{ + m_w = theValue + 1; + m_z = theValue / 2; +} + +unsigned int test_rand() +{ + m_z = 36969 * (m_z & 65535) + (m_z >> 16); + m_w = 18000 * (m_w & 65535) + (m_w >> 16); + return (m_z << 16) + m_w; /* 32-bit result */ +} diff --git a/src/HYDRO_tests/random.h b/src/HYDRO_tests/random.h new file mode 100644 index 00000000..bc8800b2 --- /dev/null +++ b/src/HYDRO_tests/random.h @@ -0,0 +1,5 @@ + +#pragma once + +void test_srand( unsigned int theValue ); +unsigned int test_rand(); diff --git a/src/HYDRO_tests/reference_data/LandCoverMap_Split_1.png b/src/HYDRO_tests/reference_data/LandCoverMap_Split_1.png index d76a7f7e..2ed3c282 100644 Binary files a/src/HYDRO_tests/reference_data/LandCoverMap_Split_1.png and b/src/HYDRO_tests/reference_data/LandCoverMap_Split_1.png differ diff --git a/src/HYDRO_tests/test_HYDROData_Bathymetry.cxx b/src/HYDRO_tests/test_HYDROData_Bathymetry.cxx index f72a456f..28a22cfe 100644 --- a/src/HYDRO_tests/test_HYDROData_Bathymetry.cxx +++ b/src/HYDRO_tests/test_HYDROData_Bathymetry.cxx @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -61,7 +62,7 @@ bool test_HYDROData_Bathymetry::createTestFile( const QString& theFileName ) { QTextStream anOutStream( &aTmpFile ); - srand( 4587 ); + test_srand( 4587 ); generateOne( anOutStream, 0, 5, 0, -5 ); generateOne( anOutStream, 10, 5, 10, -5 ); generateOne( anOutStream, 20, 5, 20, -5 ); @@ -108,7 +109,7 @@ bool test_HYDROData_Bathymetry::createTestFile( const QString& theFileName ) generateOne( anOutStream, 1040.079834, -441.303467, 1017.623413, -459.9102175 ); generateOne( anOutStream, 1055.478516, -456.060547, 1034.946899, -475.9505 ); generateOne( anOutStream, 1074.085327, -474.025665, 1058.044922, -493.274017 ); - srand( 0 ); + test_srand( 0 ); } aTmpFile.close(); @@ -134,27 +135,27 @@ void test_HYDROData_Bathymetry::testFileImport() gp_XY aTestPoint( 1, 1 ); double anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint ); - CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.9755, anAltitude, EPS ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.9186, anAltitude, EPS ); aTestPoint = gp_XY( 0.5, 0.5 ); anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint ); - CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0147, anAltitude, EPS ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.3452, anAltitude, EPS ); aTestPoint = gp_XY( 1.5, 1 ); anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint ); - CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.9534, anAltitude, EPS ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.9157, anAltitude, EPS ); aTestPoint = gp_XY( 1.5, 0.7 ); anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint ); - CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5032, anAltitude, EPS ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.8782, anAltitude, EPS ); aTestPoint = gp_XY( 1.5, -0.7 ); anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint ); - CPPUNIT_ASSERT_DOUBLES_EQUAL( 6.3088, anAltitude, EPS ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.9678, anAltitude, EPS ); aTestPoint = gp_XY( 2, 3.5 ); anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint ); - CPPUNIT_ASSERT_DOUBLES_EQUAL( 11.090, anAltitude, EPS ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 12.4364, anAltitude, EPS ); aDoc->Close(); } diff --git a/src/HYDRO_tests/test_HYDROData_LandCoverMap.cxx b/src/HYDRO_tests/test_HYDROData_LandCoverMap.cxx index 353e544b..721070bc 100644 --- a/src/HYDRO_tests/test_HYDROData_LandCoverMap.cxx +++ b/src/HYDRO_tests/test_HYDROData_LandCoverMap.cxx @@ -86,8 +86,8 @@ void test_HYDROData_LandCoverMap::test_add_2_objects() TopoDS_Face aLC2 = LandCover( QList() << 30 << 20 << 60 << 10 << 70 << 35 << 40 << 40 ); CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) ); - TestViewer::show( aMap->GetShape(), AIS_Shaded, true ); - TestViewer::AssertEqual( "LandCoverMap_Add_2_Objects" ); + TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Add_2_Objects" ); + CPPUNIT_ASSERT_IMAGES HYDROData_LandCoverMap::Iterator anIt( aMap ); CPPUNIT_ASSERT_EQUAL( true, anIt.More() ); @@ -120,9 +120,9 @@ void test_HYDROData_LandCoverMap::test_split() CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) ); - TestViewer::show( aMap->GetShape(), AIS_Shaded, true ); - TestViewer::show( aWire, QColor(), 0 ); - TestViewer::AssertEqual( "LandCoverMap_Split_1" ); + TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Split_1" ); + TestViewer::show( aWire, 0, true, Qt::blue ); + CPPUNIT_ASSERT_IMAGES HYDROData_LandCoverMap::Iterator anIt( aMap ); CPPUNIT_ASSERT_EQUAL( true, anIt.More() ); @@ -155,9 +155,9 @@ void test_HYDROData_LandCoverMap::test_incomplete_split() CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) ); - TestViewer::show( aMap->GetShape(), AIS_Shaded, true ); - TestViewer::show( aWire, QColor(), 0 ); - TestViewer::AssertEqual( "LandCoverMap_Split_2" ); + TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Split_2" ); + TestViewer::show( aWire, 0, true, Qt::blue ); + CPPUNIT_ASSERT_IMAGES HYDROData_LandCoverMap::Iterator anIt( aMap ); CPPUNIT_ASSERT_EQUAL( true, anIt.More() ); @@ -206,9 +206,9 @@ void test_HYDROData_LandCoverMap::test_merge() aList.Append( aMap->FindByPoint( aPnt2, aType2 ) ); CPPUNIT_ASSERT_EQUAL( true, aMap->Merge( aList, "new" ) ); - TestViewer::show( aMap->GetShape(), AIS_Shaded, true ); + TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Merge_1" ); //TestViewer::show( BRepBuilderAPI_MakeEdge( gp_Pnt(aPnt1.X(), aPnt1.Y(), 0), gp_Pnt(aPnt2.X(), aPnt2.Y(), 0) ).Edge(), QColor( Qt::blue ), AIS_Shaded ); - TestViewer::AssertEqual( "LandCoverMap_Merge_1" ); + CPPUNIT_ASSERT_IMAGES aDoc->Close(); } @@ -244,8 +244,8 @@ void test_HYDROData_LandCoverMap::test_remove() aList.Append( aMap->FindByPoint( aPnt2, aType2 ) ); CPPUNIT_ASSERT_EQUAL( true, aMap->Remove( aList ) ); - TestViewer::show( aMap->GetShape(), AIS_Shaded, true ); - TestViewer::AssertEqual( "LandCoverMap_Remove_1" ); + TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Remove_1" ); + CPPUNIT_ASSERT_IMAGES aDoc->Close(); } diff --git a/src/HYDRO_tests/test_HYDROData_Main.cxx b/src/HYDRO_tests/test_HYDROData_Main.cxx index 151bb2b5..40d7c4a3 100644 --- a/src/HYDRO_tests/test_HYDROData_Main.cxx +++ b/src/HYDRO_tests/test_HYDROData_Main.cxx @@ -28,11 +28,11 @@ #include #include #include -#include +#include int main( int argc, char* argv[] ) { - srand( 0 ); + test_srand( 0 ); QApplication anApp( argc, argv ); SUIT_Session aSession;