Salome HOME
Invalidate method added.
[modules/hydro.git] / src / HYDROData / test_HYDROData_Bathymetry.cxx
1 #include <test_HYDROData_Bathymetry.h>
2
3 #include <HYDROData_Document.h>
4 #include <HYDROData_Tool.h>
5 #include <HYDROData_Bathymetry.h>
6
7 #include <gp_XY.hxx>
8 #include <gp_XYZ.hxx>
9
10 #include <QDir>
11 #include <QFile>
12 #include <QTextStream>
13
14 bool test_HYDROData_Bathymetry::createTestFile( const QString& theFileName )
15 {
16   QFile aTmpFile( theFileName );
17   if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
18     return false;
19
20   {
21     QTextStream anOutStream( &aTmpFile );
22     
23     anOutStream << "0  0     0  \n";
24     anOutStream << "0  1     10 \n";
25     anOutStream << "0  2     20 \n";
26     anOutStream << "0  3     30 \n";
27
28     anOutStream << "1  0     0  \n";
29     anOutStream << "1  1     10 \n";
30     anOutStream << "1  2     20 \n";
31     anOutStream << "1  3     30 \n";
32
33     anOutStream << "2  1     10 \n";
34     anOutStream << "2  2     20 \n";
35     anOutStream << "2  3     30 \n";
36     anOutStream << "2  4     40 \n";
37
38     anOutStream << "3  0     0  \n";
39     anOutStream << "3  1     10 \n";
40     anOutStream << "3  2     20 \n";
41     anOutStream << "3  3     30 \n";
42
43     anOutStream << "4  0     0  \n";
44     anOutStream << "4  1     10 \n";
45     anOutStream << "4  2     20 \n";
46     anOutStream << "4  3     30 \n";
47   }
48
49   aTmpFile.close();
50
51   return true;
52 }
53
54 void test_HYDROData_Bathymetry::testFileImport()
55 {
56   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
57
58   Handle(HYDROData_Bathymetry) aBathymetry = 
59     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
60
61   QString aFileName = QDir::tempPath() + QDir::separator() + "test.xyz";
62   if ( !createTestFile( aFileName ) )
63     return; // No file has been created
64
65   CPPUNIT_ASSERT( aBathymetry->ImportFromFile( aFileName ) );
66
67   HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry->GetAltitudePoints();
68   CPPUNIT_ASSERT( anAltitudePoints.length() == 20 );
69
70   gp_XY aTestPoint( 1, 1 );
71   double anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
72   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 10.0 ) );
73
74   aTestPoint = gp_XY( 0.5, 0.5 );
75   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
76   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 5.0 ) );
77
78   aTestPoint = gp_XY( 1.5, 1 );
79   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
80   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 10.0 ) );
81
82   aTestPoint = gp_XY( 1.5, 0.7 );
83   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
84   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 7.0 ) );
85
86   aTestPoint = gp_XY( 1.5, -0.7 );
87   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
88   CPPUNIT_ASSERT( ValuesEquals( anAltitude, HYDROData_Bathymetry::GetInvalidAltitude() ) );
89
90   aTestPoint = gp_XY( 2, 3.5 );
91   anAltitude = aBathymetry->GetAltitudeForPoint( aTestPoint );
92   CPPUNIT_ASSERT( ValuesEquals( anAltitude, 35.0 ) );
93
94   aDoc->Close();
95 }
96
97
98 void test_HYDROData_Bathymetry::testCopy()
99 {
100   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
101   
102   Handle(HYDROData_Bathymetry) aBathymetry1 = 
103     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
104
105   QString aFileName = QDir::tempPath() + QDir::separator() + "test.xyz";
106
107   bool anIsFileCreated = createTestFile( aFileName );
108   
109   if ( anIsFileCreated )
110   {
111     CPPUNIT_ASSERT( aBathymetry1->ImportFromFile( aFileName ) );
112
113     HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry1->GetAltitudePoints();
114     CPPUNIT_ASSERT( anAltitudePoints.length() == 20 );
115   }
116
117   Handle(HYDROData_Bathymetry) aBathymetry2 = 
118     Handle(HYDROData_Bathymetry)::DownCast( aDoc->CreateObject( KIND_BATHYMETRY ) );
119
120   aBathymetry1->CopyTo( aBathymetry2 );
121
122   if ( anIsFileCreated )
123   {
124     HYDROData_Bathymetry::AltitudePoints anAltitudePoints = aBathymetry2->GetAltitudePoints();
125     CPPUNIT_ASSERT( anAltitudePoints.length() == 20 );
126   }
127
128   aDoc->Close();
129 }