summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/global
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-02-26 12:50:31 +0100
committerLars Knoll <lars.knoll@qt.io>2020-06-25 06:20:18 +0200
commit95e84c0ea95d3d3b7556c5c12bb2a52f856675d5 (patch)
tree5f0bbb285e8683240d6131070b5ee6ac1830979f /tests/auto/corelib/global
parent0dbd2dd86389c0705dbe9f518aed12f609ed09a1 (diff)
Allow qMin, qMax and qBound for types that can be losslessly converted
Add overloads for qMin and friends where the arguments are of different type, but one can be easily promoted to the other. Return the promoted type. Promotions are only allowed if both types are either signed, unsigned or floating point numbers. This should simplify writing code in many case (as for example qMin(myint64, 1)) and also help reduce source incompatibilities between Qt 5 and Qt 6, where the return types for sizes of our containers changes from int to qsizetype. Change-Id: Ia6bcf16bef0469ea568063e7c32f532da610d1cd Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/global')
-rw-r--r--tests/auto/corelib/global/qglobal/tst_qglobal.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp
index 7787c00218..4cfbdbde37 100644
--- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp
+++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp
@@ -51,6 +51,7 @@ private slots:
void integerForSize();
void buildAbiEndianness();
void testqOverload();
+ void testqMinMax();
};
extern "C" { // functions in qglobal.c
@@ -575,6 +576,32 @@ void tst_QGlobal::testqOverload()
#endif
}
+// enforce that types are identical when comparing
+template<typename T>
+void compare(T a, T b)
+{ QCOMPARE(a, b); }
+
+void tst_QGlobal::testqMinMax()
+{
+ // signed types
+ compare(qMin(float(1), double(-1)), double(-1));
+ compare(qMin(double(1), float(-1)), double(-1));
+ compare(qMin(short(1), int(-1)), int(-1));
+ compare(qMin(short(1), long(-1)), long(-1));
+ compare(qMin(qint64(1), short(-1)), qint64(-1));
+
+ compare(qMax(float(1), double(-1)), double(1));
+ compare(qMax(short(1), long(-1)), long(1));
+ compare(qMax(qint64(1), short(-1)), qint64(1));
+
+ // unsigned types
+ compare(qMin(ushort(1), ulong(2)), ulong(1));
+ compare(qMin(quint64(1), ushort(2)), quint64(1));
+
+ compare(qMax(ushort(1), ulong(2)), ulong(2));
+ compare(qMax(quint64(1), ushort(2)), quint64(2));
+}
+
QTEST_APPLESS_MAIN(tst_QGlobal)
#include "tst_qglobal.moc"