summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/text/qstring/tst_qstring.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2020-07-26 08:31:46 -0700
committerThiago Macieira <thiago.macieira@intel.com>2020-07-31 10:36:33 -0700
commitfe9e6b2ceb77befb77e664cda9842ff95ddf08a3 (patch)
tree1d2cca0610862d590273a84fe662d1fcd621e570 /tests/auto/corelib/text/qstring/tst_qstring.cpp
parenta6b3a1e4598f829b1b60407b2e47417db6f0e442 (diff)
QString::vasprintf: fix 't' modifier for integers on 64-bit
‘t’ Specifies that the argument is a ‘ptrdiff_t’. This modifier was introduced in ISO C99. We use qsizetype, which is the same width as ptrdiff_t, so it makes no difference in va_arg(). Change-Id: Iea47e0f8fc8b40378df7fffd16255730109413a5 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/corelib/text/qstring/tst_qstring.cpp')
-rw-r--r--tests/auto/corelib/text/qstring/tst_qstring.cpp60
1 files changed, 35 insertions, 25 deletions
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp
index e0d5180b78..700284f043 100644
--- a/tests/auto/corelib/text/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp
@@ -580,7 +580,6 @@ private slots:
void truncateWithNegative() const;
void QCharRefMutableUnicode() const;
void QCharRefDetaching() const;
- void sprintfZU() const;
void repeatedSignature() const;
void repeated() const;
void repeated_data() const;
@@ -1257,6 +1256,33 @@ void tst_QString::asprintf()
QCOMPARE(QString::asprintf("%lf", 1.23456789), QLatin1String("1.234568"));
QCOMPARE(QString::asprintf("%p", ptrValue(0xbfffd350)), QLatin1String("0xbfffd350"));
QCOMPARE(QString::asprintf("%p", ptrValue(0)), QLatin1String("0x0"));
+ QCOMPARE(QString::asprintf("%td", ptrdiff_t(6)), QString::fromLatin1("6"));
+ QCOMPARE(QString::asprintf("%td", ptrdiff_t(-6)), QString::fromLatin1("-6"));
+ QCOMPARE(QString::asprintf("%zu", size_t(6)), QString::fromLatin1("6"));
+ QCOMPARE(QString::asprintf("%zu", size_t(1) << 31), QString::fromLatin1("2147483648"));
+
+ // cross z and t
+ using ssize_t = std::make_signed<size_t>::type; // should be ptrdiff_t
+ using uptrdiff_t = std::make_unsigned<ptrdiff_t>::type; // should be size_t
+ QCOMPARE(QString::asprintf("%tu", uptrdiff_t(6)), QString::fromLatin1("6"));
+ QCOMPARE(QString::asprintf("%tu", uptrdiff_t(1) << 31), QString::fromLatin1("2147483648"));
+ QCOMPARE(QString::asprintf("%zd", ssize_t(-6)), QString::fromLatin1("-6"));
+
+ if (sizeof(qsizetype) > sizeof(int)) {
+ // 64-bit test
+ QCOMPARE(QString::asprintf("%zu", SIZE_MAX), QString::fromLatin1("18446744073709551615"));
+ QCOMPARE(QString::asprintf("%td", PTRDIFF_MAX), QString::fromLatin1("9223372036854775807"));
+ QCOMPARE(QString::asprintf("%td", PTRDIFF_MIN), QString::fromLatin1("-9223372036854775808"));
+
+ // sign extension is easy, make sure we can get something middle-ground
+ // (24 + 8 = 32; addition used to avoid warning about shifting more
+ // than size type on 32-bit systems)
+ size_t ubig = size_t(1) << (24 + sizeof(size_t));
+ ptrdiff_t sbig = ptrdiff_t(1) << (24 + sizeof(ptrdiff_t));
+ QCOMPARE(QString::asprintf("%zu", ubig), QString::fromLatin1("4294967296"));
+ QCOMPARE(QString::asprintf("%td", sbig), QString::fromLatin1("4294967296"));
+ QCOMPARE(QString::asprintf("%td", -sbig), QString::fromLatin1("-4294967296"));
+ }
int i = 6;
long l = -2;
@@ -1265,6 +1291,14 @@ void tst_QString::asprintf()
double d = -514.25683;
QCOMPARE(QString::asprintf("%f", d), QLatin1String("-514.256830"));
+
+ {
+ /* This code crashed. I don't know how to reduce it further. In other words,
+ * both %zu and %s needs to be present. */
+ size_t s = 6;
+ QCOMPARE(QString::asprintf("%zu%s", s, "foo"), QString::fromLatin1("6foo"));
+ QCOMPARE(QString::asprintf("%zu %s\n", s, "foo"), QString::fromLatin1("6 foo\n"));
+ }
}
void tst_QString::asprintfS()
@@ -6368,30 +6402,6 @@ void tst_QString::QCharRefDetaching() const
}
}
-void tst_QString::sprintfZU() const
-{
- {
- size_t s = 6;
- QCOMPARE(QString::asprintf("%zu", s), QString::fromLatin1("6"));
- }
-
- {
- QCOMPARE(QString::asprintf("%s\n", "foo"), QString::fromLatin1("foo\n"));
- }
-
- {
- /* This code crashed. I don't know how to reduce it further. In other words,
- * both %zu and %s needs to be present. */
- size_t s = 6;
- QCOMPARE(QString::asprintf("%zu%s", s, "foo"), QString::fromLatin1("6foo"));
- }
-
- {
- size_t s = 6;
- QCOMPARE(QString::asprintf("%zu %s\n", s, "foo"), QString::fromLatin1("6 foo\n"));
- }
-}
-
void tst_QString::repeatedSignature() const
{
/* repated() should be a const member. */