diff --git a/tests/TestGeoDataCoordinates.cpp b/tests/TestGeoDataCoordinates.cpp --- a/tests/TestGeoDataCoordinates.cpp +++ b/tests/TestGeoDataCoordinates.cpp @@ -48,10 +48,10 @@ void testNormalizeLon(); void testNormalize_data(); void testNormalize(); -// void testFromStringDMS_data(); -// void testFromStringDMS(); -// void testFromStringDM_data(); -// void testFromStringDM(); + void testFromStringDMS_data(); + void testFromStringDMS(); + void testFromStringDM_data(); + void testFromStringDM(); void testFromStringD_data(); void testFromStringD(); void testFromLocaleString_data(); @@ -71,6 +71,9 @@ void TestGeoDataCoordinates::initTestCase() { QLocale::setDefault( QLocale::c() ); // needed for testing toString* conversions + + QTime time = QTime::currentTime(); + qsrand((uint)time.msec()); } /* @@ -515,51 +518,61 @@ enum SpacesType {NoSpaces, WithSpaces}; enum LocaleType {CLocale, SystemLocale}; -//static QString -//createDegreeString(SignType signType, -// int degreeValue, int minutesValue, qreal secondsValue, -// UnitsType unitsType, SpacesType spacesType) -//{ -// QString string; +static QString +createDegreeString(SignType signType, + int degreeValue, int minutesValue, qreal secondsValue, + LocaleType locale, + UnitsType unitsType, SpacesType spacesType) +{ + QString string; -// // add degree -// if (signType != NoSign) string.append(QLatin1Char(signType==PositiveSign?'+':'-')); -// string.append(QString::number(degreeValue)); -// if (unitsType == WithUnits) string.append(QChar(0xb0)); + // add degree + if (signType != NoSign) string.append(QLatin1Char(signType==PositiveSign?'+':'-')); + string.append(QString::number(degreeValue)); + if (unitsType == WithUnits) string.append(QChar(0xb0)); -// // add minutes -// string.append(QLatin1Char(' ') + QString::number(minutesValue)); -// if (unitsType == WithUnits) string.append(QLatin1Char('\'')); + // add minutes + string.append(QLatin1Char(' ') + QString::number(minutesValue)); + if (unitsType == WithUnits) string.append(QLatin1Char('\'')); -// // add seconds -// string.append(QString::fromLatin1(" %L1").arg(secondsValue, 0, 'f', 10)); -// if (unitsType == WithUnits) string.append(QLatin1Char('"')); + // add seconds + if (locale == CLocale) { + string.append(QLatin1Char(' ') + QString::number(secondsValue, 'f', 10)); + } else { + string.append(QLatin1Char(' ') + QLocale::system().toString(secondsValue, 'f', 10)); + } + if (unitsType == WithUnits) string.append(QLatin1Char('"')); -// if (spacesType == WithSpaces) string.append(QLatin1Char(' ')); + if (spacesType == WithSpaces) string.append(QLatin1Char(' ')); -// return string; -//} + return string; +} -//static QString -//createDegreeString(SignType signType, -// int degreeValue, qreal minutesValue, -// UnitsType unitsType, SpacesType spacesType) -//{ -// QString string; +static QString +createDegreeString(SignType signType, + int degreeValue, qreal minutesValue, + LocaleType locale, + UnitsType unitsType, SpacesType spacesType) +{ + QString string; -// // add degree -// if (signType != NoSign) string.append(QLatin1Char(signType==PositiveSign?'+':'-')); -// string.append(QString::number(degreeValue)); -// if (unitsType == WithUnits) string.append(QChar(0xb0)); + // add degree + if (signType != NoSign) string.append(QLatin1Char(signType==PositiveSign?'+':'-')); + string.append(QString::number(degreeValue)); + if (unitsType == WithUnits) string.append(QChar(0xb0)); -// // add minutes -// string.append(QString::fromLatin1(" %L1").arg(minutesValue, 0, 'f', 10)); -// if (unitsType == WithUnits) string.append(QLatin1Char('\'')); + // add minutes + if (locale == CLocale) { + string.append(QLatin1Char(' ') + QString::number(minutesValue, 'f', 10)); + } else { + string.append(QLatin1Char(' ') + QLocale::system().toString(minutesValue, 'f', 10)); + } + if (unitsType == WithUnits) string.append(QLatin1Char('\'')); -// if (spacesType == WithSpaces) string.append(QLatin1Char(' ')); + if (spacesType == WithSpaces) string.append(QLatin1Char(' ')); -// return string; -//} + return string; +} static QString createDegreeString(SignType signType, @@ -586,245 +599,271 @@ /* * test data for testStringDMS() */ -//void TestGeoDataCoordinates::testFromStringDMS_data() -//{ -// QTest::addColumn("string"); -// QTest::addColumn("lon"); -// QTest::addColumn("lat"); - -// const QVector signTypes = QVector() -// << NoSign << PositiveSign << NegativeSign; -// const QVector sphereTypes = QVector() -// << PosSphere << NegSphere; -// const QVector unitsTypes = QVector() -// << NoUnits << WithUnits; -// const QVector spacesTypes = QVector() -// << NoSpaces << WithSpaces; - -// const QVector degreeSamples = QVector() -// << 0 << 140 << 180; -// const QVector minutesSamples = QVector() -// << 0 << 23 << 59; -// const QVector secondsSamples = QVector() -// << 0.0 << 3.14159 << 59.9999999; - -// foreach(const UnitsType unitsType, unitsTypes) { -// foreach(const SpacesType spacesType, spacesTypes) { -// // lon -// foreach(const SphereType lonSphere, sphereTypes) { -// foreach(const SignType lonSignType, signTypes) { -// const bool lonIsPositive = -// (lonSphere==PosSphere && lonSignType!=NegativeSign) || -// (lonSphere==NegSphere && lonSignType==NegativeSign); -// foreach(const uint lonDegree, degreeSamples) { -// foreach(const uint lonMinutes, minutesSamples) { -// if(lonDegree == 180 && lonMinutes != 0) continue; -// foreach(const qreal lonSeconds, secondsSamples) { -// if(lonDegree == 180 && lonSeconds != 0.0) continue; -// // lat -// foreach(const SphereType latSphere, sphereTypes) { -// foreach(const SignType latSignType, signTypes) { -// const bool latIsPositive = -// (latSphere==PosSphere && latSignType!=NegativeSign) || -// (latSphere==NegSphere && latSignType==NegativeSign); -// foreach(const uint latDegree, degreeSamples) { -// foreach(const uint latMinutes, minutesSamples) { -// if(latDegree == 180 && latMinutes != 0) continue; -// foreach(const qreal latSeconds, secondsSamples) { -// if(latDegree == 180 && latSeconds != 0.0) continue; - -// // actual construction -// // Create lon & lat values -// qreal lon = (qreal)lonDegree + lonMinutes*MIN2HOUR + lonSeconds*SEC2HOUR; -// if( ! lonIsPositive ) -// lon *= -1; -// qreal lat = (qreal)latDegree + latMinutes*MIN2HOUR + latSeconds*SEC2HOUR; -// if( ! latIsPositive ) -// lat *= -1; - -// // Create string -// QString string; -// string.append(createDegreeString(latSignType, -// latDegree, latMinutes, latSeconds, -// unitsType, spacesType)); -// string.append(QLatin1Char(latSphere==PosSphere?'N':'S')); -// string.append(QLatin1Char(' ')); -// string.append(createDegreeString(lonSignType, -// lonDegree, lonMinutes, lonSeconds, -// unitsType, spacesType)); -// string.append(QLatin1Char(lonSphere==PosSphere?'E':'W')); - -// // Create row title -// QString rowTitle; -// rowTitle.append(QLatin1String(spacesType==WithSpaces?"spaced dir":"unspaced dir")) -// .append(QLatin1String(unitsType==WithUnits?"|units":"|no units")) -// .append(QLatin1String("|lon:")) -// .append(QLatin1Char(lonIsPositive?'+':'-')) -// .append(QString::number(lonDegree)+QChar(0xb0)) -// .append(QString::number(lonMinutes)+QLatin1Char('\'')) -// .append(QString::fromLatin1("%L1").arg(lonSeconds, 0, 'f', 10)+QLatin1Char('"')) -// .append(QLatin1Char(lonSphere==PosSphere?'P':'N')) -// .append(QLatin1String("|lat:")) -// .append(QLatin1Char(latIsPositive?'+':'-')) -// .append(QString::number(latDegree)+QChar(0xb0)) -// .append(QString::number(latMinutes)+QLatin1Char('\'')) -// .append(QString::fromLatin1("%L1").arg(latSeconds, 0, 'f', 10)+QLatin1Char('"')) -// .append(QLatin1Char(latSphere==PosSphere?'P':'N')) -// .append(QLatin1Char('|')).append(string).append(QLatin1Char('|')); -// QTest::newRow(rowTitle.toLatin1()) -// << string -// << lon -// << lat; -// } -// } -// } -// } -// } -// } -// } -// } -// } -// } -// } -// } -//} - -///* -// * test fromString() with DMS notation -// */ -//void TestGeoDataCoordinates::testFromStringDMS() -//{ -// QFETCH(QString, string); -// QFETCH(qreal, lon); -// QFETCH(qreal, lat); - -// bool succeeded = false; -// const GeoDataCoordinates coords = GeoDataCoordinates::fromString(string, succeeded); - -// if(! succeeded) -// qWarning() << "Could not parse"<("string"); -// QTest::addColumn("lon"); -// QTest::addColumn("lat"); - -// const QVector signTypes = QVector() -// << NoSign << PositiveSign << NegativeSign; -// const QVector sphereTypes = QVector() -// << PosSphere << NegSphere; -// const QVector unitsTypes = QVector() -// << NoUnits << WithUnits; -// const QVector spacesTypes = QVector() -// << NoSpaces << WithSpaces; - -// const QVector degreeSamples = QVector() -// << 0 << 140 << 180; -// const QVector minutesSamples = QVector() -// << 0.0 << 3.14159 << 59.9999999; - -// foreach(const UnitsType unitsType, unitsTypes) { -// foreach(const SpacesType spacesType, spacesTypes) { -// // lon -// foreach(const SphereType lonSphere, sphereTypes) { -// foreach(const SignType lonSignType, signTypes) { -// const bool lonIsPositive = -// (lonSphere==PosSphere && lonSignType!=NegativeSign) || -// (lonSphere==NegSphere && lonSignType==NegativeSign); -// foreach(const uint lonDegree, degreeSamples) { -// foreach(const qreal lonMinutes, minutesSamples) { -// if(lonDegree == 180 && lonMinutes != 0.0) continue; -// // lat -// foreach(const SphereType latSphere, sphereTypes) { -// foreach(const SignType latSignType, signTypes) { -// const bool latIsPositive = -// (latSphere==PosSphere && latSignType!=NegativeSign) || -// (latSphere==NegSphere && latSignType==NegativeSign); -// foreach(const uint latDegree, degreeSamples) { -// foreach(const qreal latMinutes, minutesSamples) { -// if(latDegree == 180 && latMinutes != 0.0) continue; - -// // actual construction -// // Create lon & lat values -// qreal lon = (qreal)lonDegree + lonMinutes*MIN2HOUR; -// if( ! lonIsPositive ) -// lon *= -1; -// qreal lat = (qreal)latDegree + latMinutes*MIN2HOUR; -// if( ! latIsPositive ) -// lat *= -1; - -// // Create string -// QString string; -// string.append(createDegreeString(latSignType, -// latDegree, latMinutes, -// unitsType, spacesType)); -// string.append(QLatin1Char(latSphere==PosSphere?'N':'S')); -// string.append(QLatin1Char(' ')); -// string.append(createDegreeString(lonSignType, -// lonDegree, lonMinutes, -// unitsType, spacesType)); -// string.append(QLatin1Char(lonSphere==PosSphere?'E':'W')); - -// // Create row title -// QString rowTitle; -// rowTitle.append(QLatin1String(spacesType==WithSpaces?"spaced dir":"unspaced dir")) -// .append(QLatin1String(unitsType==WithUnits?"|units":"|no units")) -// .append(QLatin1String("|lon:")) -// .append(QLatin1Char(lonIsPositive?'+':'-')) -// .append(QString::number(lonDegree)+QChar(0xb0)) -// .append(QString::fromLatin1("%L1").arg(lonMinutes, 0, 'f', 10)+QLatin1Char('\'')) -// .append(QLatin1Char(lonSphere==PosSphere?'P':'N')) -// .append(QLatin1String("|lat:")) -// .append(QLatin1Char(latIsPositive?'+':'-')) -// .append(QString::number(latDegree)+QChar(0xb0)) -// .append(QString::fromLatin1("%L1").arg(latMinutes, 0, 'f', 10)+QLatin1Char('\'')) -// .append(QLatin1Char(latSphere==PosSphere?'P':'N')) -// .append(QLatin1Char('|')).append(string).append(QLatin1Char('|')); -// QTest::newRow(rowTitle.toLatin1()) -// << string -// << lon -// << lat; -// } -// } -// } -// } -// } -// } -// } -// } -// } -// } -//} - -///* -// * test fromString() with DM notation -// */ -//void TestGeoDataCoordinates::testFromStringDM() -//{ -// QFETCH(QString, string); -// QFETCH(qreal, lon); -// QFETCH(qreal, lat); - -// bool succeeded = false; -// const GeoDataCoordinates coords = GeoDataCoordinates::fromString(string, succeeded); - -// if(! succeeded) -// qWarning() << "Could not parse"<("string"); + QTest::addColumn("lon"); + QTest::addColumn("lat"); + + const QVector signTypes = QVector() + << NoSign << PositiveSign << NegativeSign; + const QVector sphereTypes = QVector() + << PosSphere << NegSphere; + const QVector unitsTypes = QVector() + << NoUnits << WithUnits; + const QVector spacesTypes = QVector() + << NoSpaces << WithSpaces; + const QVector localeTypes = QVector() + << CLocale << SystemLocale; + + const QVector degreeSamples = QVector() + << 0 << 140 << 180; + const QVector minutesSamples = QVector() + << 0 << 23 << 59; + const QVector secondsSamples = QVector() + << 0.0 << 3.14159 << 59.9999999; + + foreach(const UnitsType unitsType, unitsTypes) { + foreach(const SpacesType spacesType, spacesTypes) { + // lon + foreach(const SphereType lonSphere, sphereTypes) { + foreach(const SignType lonSignType, signTypes) { + const bool lonIsPositive = + (lonSphere==PosSphere && lonSignType!=NegativeSign) || + (lonSphere==NegSphere && lonSignType==NegativeSign); + foreach(const uint lonDegree, degreeSamples) { + foreach(const uint lonMinutes, minutesSamples) { + if(lonDegree == 180 && lonMinutes != 0) continue; + foreach(const qreal lonSeconds, secondsSamples) { + if(lonDegree == 180 && lonSeconds != 0.0) continue; + // lat + foreach(const SphereType latSphere, sphereTypes) { + foreach(const SignType latSignType, signTypes) { + const bool latIsPositive = + (latSphere==PosSphere && latSignType!=NegativeSign) || + (latSphere==NegSphere && latSignType==NegativeSign); + foreach(const uint latDegree, degreeSamples) { + foreach(const uint latMinutes, minutesSamples) { + if(latDegree == 180 && latMinutes != 0) continue; + foreach(const qreal latSeconds, secondsSamples) { + if(latDegree == 180 && latSeconds != 0.0) continue; + // locale + foreach(const LocaleType locale, localeTypes) { + + // actual construction + // Create lon & lat values + qreal lon = (qreal)lonDegree + lonMinutes*MIN2HOUR + lonSeconds*SEC2HOUR; + if( ! lonIsPositive ) + lon *= -1; + qreal lat = (qreal)latDegree + latMinutes*MIN2HOUR + latSeconds*SEC2HOUR; + if( ! latIsPositive ) + lat *= -1; + + // Create string + QString string; + string.append(createDegreeString(latSignType, + latDegree, latMinutes, latSeconds, + locale, + unitsType, spacesType)); + string.append(QLatin1Char(latSphere==PosSphere?'N':'S')); + string.append(QLatin1Char(' ')); + string.append(createDegreeString(lonSignType, + lonDegree, lonMinutes, lonSeconds, + locale, + unitsType, spacesType)); + string.append(QLatin1Char(lonSphere==PosSphere?'E':'W')); + + // Create row title + QString rowTitle; + rowTitle.append(QLatin1String(spacesType==WithSpaces?"spaced dir":"unspaced dir")) + .append(QLatin1String(unitsType==WithUnits?"|units":"|no units")) + .append(QLatin1String("|lon:")) + .append(QLatin1Char(lonIsPositive?'+':'-')) + .append(QString::number(lonDegree)+QChar(0xb0)) + .append(QString::number(lonMinutes)+QLatin1Char('\'')) + .append(QString::number(lonSeconds, 'f', 10)+QLatin1Char('"')) + .append(QLatin1Char(lonSphere==PosSphere?'P':'N')) + .append(QLatin1String("|lat:")) + .append(QLatin1Char(latIsPositive?'+':'-')) + .append(QString::number(latDegree)+QChar(0xb0)) + .append(QString::number(latMinutes)+QLatin1Char('\'')) + .append(QString::number(latSeconds, 'f', 10)+QLatin1Char('"')) + .append(QLatin1Char(latSphere==PosSphere?'P':'N')) + .append(QLatin1Char('|')).append(QLatin1Char(locale==CLocale?'C':'L')) + .append(QLatin1Char('|')).append(string).append(QLatin1Char('|')); + QTest::newRow(rowTitle.toLatin1().constData()) + << string + << lon + << lat; + } + } + } + } + } + } + } + } + } + } + } + } + } +} + +/* + * test fromString() with DMS notation + */ +void TestGeoDataCoordinates::testFromStringDMS() +{ + // only run random 5% of all possible permutations + if ((qreal(qrand()) / RAND_MAX) > 0.05) { + QSKIP("Not picked for this run."); + } + + QFETCH(QString, string); + QFETCH(qreal, lon); + QFETCH(qreal, lat); + + bool succeeded = false; + const GeoDataCoordinates coords = GeoDataCoordinates::fromString(string, succeeded); + + if(! succeeded) + qWarning() << "Could not parse"<("string"); + QTest::addColumn("lon"); + QTest::addColumn("lat"); + + const QVector signTypes = QVector() + << NoSign << PositiveSign << NegativeSign; + const QVector sphereTypes = QVector() + << PosSphere << NegSphere; + const QVector unitsTypes = QVector() + << NoUnits << WithUnits; + const QVector spacesTypes = QVector() + << NoSpaces << WithSpaces; + const QVector localeTypes = QVector() + << CLocale << SystemLocale; + + const QVector degreeSamples = QVector() + << 0 << 140 << 180; + const QVector minutesSamples = QVector() + << 0.0 << 3.14159 << 59.9999999; + + foreach(const UnitsType unitsType, unitsTypes) { + foreach(const SpacesType spacesType, spacesTypes) { + // lon + foreach(const SphereType lonSphere, sphereTypes) { + foreach(const SignType lonSignType, signTypes) { + const bool lonIsPositive = + (lonSphere==PosSphere && lonSignType!=NegativeSign) || + (lonSphere==NegSphere && lonSignType==NegativeSign); + foreach(const uint lonDegree, degreeSamples) { + foreach(const qreal lonMinutes, minutesSamples) { + if(lonDegree == 180 && lonMinutes != 0.0) continue; + // lat + foreach(const SphereType latSphere, sphereTypes) { + foreach(const SignType latSignType, signTypes) { + const bool latIsPositive = + (latSphere==PosSphere && latSignType!=NegativeSign) || + (latSphere==NegSphere && latSignType==NegativeSign); + foreach(const uint latDegree, degreeSamples) { + foreach(const qreal latMinutes, minutesSamples) { + if(latDegree == 180 && latMinutes != 0.0) continue; + // locale + foreach(const LocaleType locale, localeTypes) { + + // actual construction + // Create lon & lat values + qreal lon = (qreal)lonDegree + lonMinutes*MIN2HOUR; + if( ! lonIsPositive ) + lon *= -1; + qreal lat = (qreal)latDegree + latMinutes*MIN2HOUR; + if( ! latIsPositive ) + lat *= -1; + + // Create string + QString string; + string.append(createDegreeString(latSignType, + latDegree, latMinutes, + locale, + unitsType, spacesType)); + string.append(QLatin1Char(latSphere==PosSphere?'N':'S')); + string.append(QLatin1Char(' ')); + string.append(createDegreeString(lonSignType, + lonDegree, lonMinutes, + locale, + unitsType, spacesType)); + string.append(QLatin1Char(lonSphere==PosSphere?'E':'W')); + + // Create row title + QString rowTitle; + rowTitle.append(QLatin1String(spacesType==WithSpaces?"spaced dir":"unspaced dir")) + .append(QLatin1String(unitsType==WithUnits?"|units":"|no units")) + .append(QLatin1String("|lon:")) + .append(QLatin1Char(lonIsPositive?'+':'-')) + .append(QString::number(lonDegree)+QChar(0xb0)) + .append(QString::number(lonMinutes, 'f', 10)+QLatin1Char('\'')) + .append(QLatin1Char(lonSphere==PosSphere?'P':'N')) + .append(QLatin1String("|lat:")) + .append(QLatin1Char(latIsPositive?'+':'-')) + .append(QString::number(latDegree)+QChar(0xb0)) + .append(QString::number(latMinutes, 'f', 10)+QLatin1Char('\'')) + .append(QLatin1Char(latSphere==PosSphere?'P':'N')) + .append(QLatin1Char('|')).append(QLatin1Char(locale==CLocale?'C':'L')) + .append(QLatin1Char('|')).append(string).append(QLatin1Char('|')); + QTest::newRow(rowTitle.toLatin1().constData()) + << string + << lon + << lat; + } + } + } + } + } + } + } + } + } + } + } +} + +/* + * test fromString() with DM notation + */ +void TestGeoDataCoordinates::testFromStringDM() +{ + // only run random 5% of all possible permutations + if ((qreal(qrand()) / RAND_MAX) > 0.05) { + QSKIP("Not picked for this run."); + } + + + QFETCH(QString, string); + QFETCH(qreal, lon); + QFETCH(qreal, lat); + + bool succeeded = false; + const GeoDataCoordinates coords = GeoDataCoordinates::fromString(string, succeeded); + + if(! succeeded) + qWarning() << "Could not parse"<