File indexing completed on 2024-12-01 12:29:08

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #ifndef MARBLE_TESTUTILS_H
0007 #define MARBLE_TESTUTILS_H
0008 
0009 #include "GeoDataDocument.h"
0010 #include "GeoDataParser.h"
0011 #include "GeoDataCoordinates.h"
0012 #include "GeoDataLatLonAltBox.h"
0013 
0014 #include <QBuffer>
0015 #include <QTest>
0016 
0017 namespace QTest
0018 {
0019 
0020 bool qCompare(qreal val1, qreal val2, qreal epsilon, const char *actual, const char *expected, const char *file, int line)
0021 {
0022     return ( qAbs( val1 - val2 ) < epsilon )
0023         ? compare_helper( true, "COMPARE()", toString( val1 ), toString( val2 ), actual, expected, file, line )
0024         : compare_helper( false, "Compared qreals are not the same", toString( val1 ), toString( val2 ), actual, expected, file, line );
0025 }
0026 
0027 template<>
0028 char *toString(const Marble::GeoDataCoordinates &coordinates)
0029 {
0030     return qstrdup( coordinates.toString( Marble::GeoDataCoordinates::Decimal, 10 ).toLatin1().data() );
0031 }
0032 
0033 template<>
0034 char *toString(const Marble::GeoDataLatLonBox &box)
0035 {
0036     const QString string = QString( "North: %1, West: %2, South: %3, East: %4" )
0037         .arg( box.north( Marble::GeoDataCoordinates::Degree ) )
0038         .arg( box.west( Marble::GeoDataCoordinates::Degree ) )
0039         .arg( box.south( Marble::GeoDataCoordinates::Degree ) )
0040         .arg( box.east( Marble::GeoDataCoordinates::Degree ) );
0041 
0042     return qstrdup( string.toLatin1().data() );
0043 }
0044 
0045 template<>
0046 char *toString(const Marble::GeoDataLatLonAltBox &box)
0047 {
0048     const QString string = QString( "North: %1, West: %2, South: %3, East: %4, MinAlt: %5, MaxAlt: %6" )
0049         .arg( box.north( Marble::GeoDataCoordinates::Degree ) )
0050         .arg( box.west( Marble::GeoDataCoordinates::Degree ) )
0051         .arg( box.south( Marble::GeoDataCoordinates::Degree ) )
0052         .arg( box.east( Marble::GeoDataCoordinates::Degree ) )
0053         .arg( box.minAltitude() )
0054         .arg( box.maxAltitude() );
0055 
0056     return qstrdup( string.toLatin1().data() );
0057 }
0058 
0059 }
0060 
0061 #define QFUZZYCOMPARE(actual, expected, epsilon) \
0062 do {\
0063     if (!QTest::qCompare(actual, expected, epsilon, #actual, #expected, __FILE__, __LINE__))\
0064         return;\
0065 } while (0)
0066 
0067 #define addRow() QTest::newRow( QString("line %1").arg( __LINE__ ).toLatin1().data() )
0068 #define addNamedRow(testName) QTest::newRow( QString("line %1: %2").arg( __LINE__ ).arg(testName).toLatin1().data() )
0069 
0070 namespace Marble {
0071 
0072 GeoDataDocument *parseKml(const QString &content)
0073 {
0074     GeoDataParser parser( GeoData_KML );
0075 
0076     QByteArray array( content.toUtf8() );
0077     QBuffer buffer( &array );
0078     buffer.open( QIODevice::ReadOnly );
0079     //qDebug() << "Buffer content:" << endl << buffer.buffer();
0080     if ( !parser.read( &buffer ) ) {
0081         qFatal( "Could not parse data!" );
0082     }
0083     GeoDocument* document = parser.releaseDocument();
0084     Q_ASSERT( document );
0085     return static_cast<GeoDataDocument*>( document );
0086 }
0087 
0088 }
0089 
0090 #endif