summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2018-11-16 15:44:44 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2018-11-23 10:04:56 +0000
commit704137f8de49a55a1b21bb6d612d2594562f51ce (patch)
tree9d689d41ab36fb747bb6f50e09c859fc19319162 /tests
parent108c9015b960fccd79efc6de4459dbf05f6ced54 (diff)
tst_QLocale: Add tests for toFloat()
Mirror those for toDouble(). Change-Id: Ide0ef3cd99528d575f6a578ef19547f3b1119c5d Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/tools/qlocale/tst_qlocale.cpp55
1 files changed, 54 insertions, 1 deletions
diff --git a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp
index 4803451399..fa1a64a045 100644
--- a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp
+++ b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp
@@ -83,6 +83,8 @@ private slots:
void matchingLocales();
void stringToDouble_data();
void stringToDouble();
+ void stringToFloat_data() { stringToDouble_data(); }
+ void stringToFloat();
void doubleToString_data();
void doubleToString();
void strtod_data();
@@ -801,7 +803,7 @@ void tst_QLocale::stringToDouble_data()
void tst_QLocale::stringToDouble()
{
-#define MY_DOUBLE_EPSILON (2.22045e-16)
+#define MY_DOUBLE_EPSILON (2.22045e-16) // 1/2^{52}; double has a 53-bit mantissa
QFETCH(QString, locale_name);
QFETCH(QString, num_str);
@@ -824,6 +826,8 @@ void tst_QLocale::stringToDouble()
}
if (ok) {
+ // First use fuzzy-compare, then a more precise check:
+ QCOMPARE(d, num);
double diff = d - num;
if (diff < 0)
diff = -diff;
@@ -834,11 +838,60 @@ void tst_QLocale::stringToDouble()
QCOMPARE(ok, good);
if (ok) {
+ QCOMPARE(d, num);
double diff = d - num;
if (diff < 0)
diff = -diff;
QVERIFY(diff <= MY_DOUBLE_EPSILON);
}
+#undef MY_DOUBLE_EPSILON
+}
+
+void tst_QLocale::stringToFloat()
+{
+#define MY_FLOAT_EPSILON (2.384e-7) // 1/2^{22}; float has a 23-bit mantissa
+
+ QFETCH(QString, locale_name);
+ QFETCH(QString, num_str);
+ QFETCH(bool, good);
+ QFETCH(double, num);
+ QStringRef num_strRef = num_str.leftRef(-1);
+ float fnum = num;
+
+ QLocale locale(locale_name);
+ QCOMPARE(locale.name(), locale_name);
+
+ bool ok;
+ float f = locale.toFloat(num_str, &ok);
+ QCOMPARE(ok, good);
+
+ {
+ // Make sure result is independent of locale:
+ TransientLocale ignoreme(LC_ALL, "ar_SA");
+ QCOMPARE(locale.toFloat(num_str, &ok), f);
+ QCOMPARE(ok, good);
+ }
+
+ if (ok) {
+ // First use fuzzy-compare, then a more precise check:
+ QCOMPARE(f, fnum);
+ float diff = f - fnum;
+ if (diff < 0)
+ diff = -diff;
+ QVERIFY(diff <= MY_FLOAT_EPSILON);
+ }
+
+ f = locale.toFloat(num_strRef, &ok);
+ QCOMPARE(ok, good);
+
+ if (ok) {
+ QCOMPARE(f, fnum);
+ float diff = f - fnum;
+ if (diff < 0)
+ diff = -diff;
+ QVERIFY(diff <= MY_FLOAT_EPSILON);
+ }
+#undef MY_FLOAT_EPSILON
}
void tst_QLocale::doubleToString_data()