]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
new randomizer equivalent for 32 and 64 bits platform
authorasl <asl@opencascade.com>
Fri, 16 Oct 2015 11:52:16 +0000 (14:52 +0300)
committerasl <asl@opencascade.com>
Fri, 16 Oct 2015 11:52:16 +0000 (14:52 +0300)
src/HYDRO_tests/CMakeLists.txt
src/HYDRO_tests/TestViewer.cxx
src/HYDRO_tests/TestViewer.h
src/HYDRO_tests/random.cxx [new file with mode: 0644]
src/HYDRO_tests/random.h [new file with mode: 0644]
src/HYDRO_tests/reference_data/LandCoverMap_Split_1.png
src/HYDRO_tests/test_HYDROData_Bathymetry.cxx
src/HYDRO_tests/test_HYDROData_LandCoverMap.cxx
src/HYDRO_tests/test_HYDROData_Main.cxx

index 8916171a50ea7a892ec1e935be4b59ac7648c54c..30e87f5bf2abc4db6a4f82edbbab14eb7f9ec02b 100644 (file)
@@ -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(
index 7f891922ce71ed235ff56c2de38143bdea297d17..1a5e0bf983efc645354aa5aa12cafc2bec062280 100644 (file)
@@ -1,17 +1,35 @@
 
 #include <TestViewer.h>
+#include <random.h>
 #include <OCCViewer_ViewManager.h>
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
 #include <OCCViewer_ViewModel.h>
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
 #include <OCCViewer_ViewWindow.h>
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_Shape.hxx>
 #include <TopoDS_Iterator.hxx>
+
 #include <QDir>
 #include <QPainter>
+#include <QHash>
+
+#ifdef WIN32
+  #pragma warning ( default: 4251 )
+#endif
+
 #include <cppunit/TestAssert.h>
 
 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;
 }
index e825208bd147763461d40ebfa20e2a2db636a7c4..669b5621b5ad1e9dede3a4bd536cf15f437d4ddc 100644 (file)
@@ -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 (file)
index 0000000..da51deb
--- /dev/null
@@ -0,0 +1,18 @@
+
+#include <random.h>
+
+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 (file)
index 0000000..bc8800b
--- /dev/null
@@ -0,0 +1,5 @@
+
+#pragma once
+
+void test_srand( unsigned int theValue );
+unsigned int test_rand();
index d76a7f7e848083ab27c3d36fe1c1245e055feaf4..2ed3c2827f62bac13594bf11ce5e130612406e70 100644 (file)
Binary files a/src/HYDRO_tests/reference_data/LandCoverMap_Split_1.png and b/src/HYDRO_tests/reference_data/LandCoverMap_Split_1.png differ
index f72a456fa1210c51f146d3a2d4559aefff5f573e..28a22cfe4d62f2eec8a5e115673576a9219bbeb7 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <gp_XY.hxx>
 #include <gp_XYZ.hxx>
+#include <random.h>
 
 #include <QDir>
 #include <QFile>
@@ -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();
 }
index 353e544b22fcff407b4e03c0fc1dd6bf0a8c455d..721070bcb8f5616105b2ac832af3e3f5b7a5817e 100644 (file)
@@ -86,8 +86,8 @@ void test_HYDROData_LandCoverMap::test_add_2_objects()
   TopoDS_Face aLC2 = LandCover( QList<double>() << 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();
 }
index 151bb2b561eaefb87d4793b2e15ae7fc9cc2a2d1..40d7c4a3f0d763458f7441b301c744bb284da2df 100644 (file)
 #include <SUIT_Session.h>
 #include <QApplication>
 #include <QColor>
-#include <time.h>
+#include <random.h>
 
 int main( int argc, char* argv[] )
 {
-  srand( 0 );
+  test_srand( 0 );
 
   QApplication anApp( argc, argv );
   SUIT_Session aSession;