From 8c883c8da32faf1245d257f1fc1fb39fb2b63efc Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Wed, 19 Feb 2020 18:48:32 +0100 Subject: QDom: use correct precision when converting float/double values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit d7cb21ac085117f879a8aa1d7727b2ca52d3353d change the way a double is converted which resulted in less precision. Fix it by explictily setting the precision in QString::setNum() Task-number: QTBUG-80068 Change-Id: I1fd9d00837155ceb707e84bfeb9deff03b5ab57e Reviewed-by: André Hartmann Reviewed-by: Konstantin Shegunov Reviewed-by: Lars Knoll --- tests/auto/xml/dom/qdom/tst_qdom.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'tests/auto/xml/dom/qdom') diff --git a/tests/auto/xml/dom/qdom/tst_qdom.cpp b/tests/auto/xml/dom/qdom/tst_qdom.cpp index 99639df5b0..229d2ba6e5 100644 --- a/tests/auto/xml/dom/qdom/tst_qdom.cpp +++ b/tests/auto/xml/dom/qdom/tst_qdom.cpp @@ -38,6 +38,7 @@ #include #include #include +#include QT_FORWARD_DECLARE_CLASS(QDomDocument) QT_FORWARD_DECLARE_CLASS(QDomNode) @@ -408,7 +409,9 @@ void tst_QDom::setGetAttributes() const int intVal = std::numeric_limits::min(); const uint uintVal = std::numeric_limits::max(); const float floatVal = 0.1234f; - const double doubleVal = 0.1234; + const double doubleVal1 = 1./6.; + const double doubleVal2 = std::nextafter(doubleVal1, 1.); + const double doubleVal3 = std::nextafter(doubleVal2, 1.); rootNode.setAttribute("qstringVal", qstringVal); rootNode.setAttribute("qlonglongVal", qlonglongVal); @@ -416,7 +419,9 @@ void tst_QDom::setGetAttributes() rootNode.setAttribute("intVal", intVal); rootNode.setAttribute("uintVal", uintVal); rootNode.setAttribute("floatVal", floatVal); - rootNode.setAttribute("doubleVal", doubleVal); + rootNode.setAttribute("doubleVal1", doubleVal1); + rootNode.setAttribute("doubleVal2", doubleVal2); + rootNode.setAttribute("doubleVal3", doubleVal3); QDomElement nsNode = doc.createElement("NS"); rootNode.appendChild(nsNode); @@ -426,7 +431,9 @@ void tst_QDom::setGetAttributes() nsNode.setAttributeNS("namespace", "intVal", intVal); nsNode.setAttributeNS("namespace", "uintVal", uintVal); nsNode.setAttributeNS("namespace", "floatVal", floatVal); // not available atm - nsNode.setAttributeNS("namespace", "doubleVal", doubleVal); + nsNode.setAttributeNS("namespace", "doubleVal1", doubleVal1); + nsNode.setAttributeNS("namespace", "doubleVal2", doubleVal2); + nsNode.setAttributeNS("namespace", "doubleVal3", doubleVal3); bool bOk; QCOMPARE(rootNode.attribute("qstringVal"), qstringVal); @@ -440,8 +447,10 @@ void tst_QDom::setGetAttributes() QVERIFY(bOk); QCOMPARE(rootNode.attribute("floatVal").toFloat(&bOk), floatVal); QVERIFY(bOk); - QCOMPARE(rootNode.attribute("doubleVal").toDouble(&bOk), doubleVal); - QVERIFY(bOk); + + QVERIFY(rootNode.attribute("doubleVal1").toDouble(&bOk) == doubleVal1 && bOk); + QVERIFY(rootNode.attribute("doubleVal2").toDouble(&bOk) == doubleVal2 && bOk); + QVERIFY(rootNode.attribute("doubleVal3").toDouble(&bOk) == doubleVal3 && bOk); QCOMPARE(nsNode.attributeNS("namespace", "qstringVal"), qstringVal); QCOMPARE(nsNode.attributeNS("namespace", "qlonglongVal").toLongLong(&bOk), qlonglongVal); @@ -454,8 +463,9 @@ void tst_QDom::setGetAttributes() QVERIFY(bOk); QCOMPARE(nsNode.attributeNS("namespace", "floatVal").toFloat(&bOk), floatVal); QVERIFY(bOk); - QCOMPARE(nsNode.attributeNS("namespace", "doubleVal").toDouble(&bOk), doubleVal); - QVERIFY(bOk); + QVERIFY(nsNode.attributeNS("namespace", "doubleVal1").toDouble(&bOk) == doubleVal1 && bOk); + QVERIFY(nsNode.attributeNS("namespace", "doubleVal2").toDouble(&bOk) == doubleVal2 && bOk); + QVERIFY(nsNode.attributeNS("namespace", "doubleVal3").toDouble(&bOk) == doubleVal3 && bOk); QLocale::setDefault(oldLocale); } -- cgit v1.2.3