From aa7a7a0eb893f230112026fe58fba24ec8d378ef Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 6 Jul 2023 22:45:38 +0200 Subject: QCOMPARE: restore compatibility with braced-init-lists a611a9f537f1187825339c2a2214c8ec4a23680f (in Qt 5) added support for mixed-type comparisons through QCOMPARE. That commit added a new overload for qCompare taking two types, T1 and T2; but it also left the same-type qCompare(T, T) overload around, guarded by a Qt 6 version check. The mixed-type version is however not a generalization of the same-type one, because it won't work if one of the arguments doesn't participate in FTAD. Case in point: braced-init-lists. In Qt 5 this worked: QCOMPARE(some_container, {42}); but in Qt 6 it does not work any more. The mixed-type overload fails deduction (can't deduce T2); in Qt 5 the same-type overload deduced T=SomeContainer, and {42} was used to select a constructor for SomeContainer. -- There's a partial, straightforward workaround for this: default T2 to T1 in the mized-type overload. In that case T2 has a "fallback" if it cannot be deduced. This is partial because of course doesn't address the case in which T1 cannot be deduced, but I don't think that is common at all. QList is special here, because it has qCompare overloads that makes it comparable with arrays, initializer lists and more. I don't like that very much -- we should probably have a qCompare(input_range, input_range) overload, but that's an exercise for C++20. Change-Id: I344ba33167829984978cd8d649a1904349a9edab Reviewed-by: Marc Mutz Reviewed-by: Milian Wolff Reviewed-by: Volker Hilsheimer (cherry picked from commit 5560723e9dd8f32a2fb2530b975ba0bb0f9f5ef7) Reviewed-by: Qt Cherry-pick Bot --- src/testlib/qtestcase.h | 2 +- .../auto/testlib/selftests/cmptest/tst_cmptest.cpp | 21 +++++++ .../testlib/selftests/expected_cmptest.junitxml | 3 +- .../testlib/selftests/expected_cmptest.lightxml | 4 ++ tests/auto/testlib/selftests/expected_cmptest.tap | 71 +++++++++++----------- .../testlib/selftests/expected_cmptest.teamcity | 2 + tests/auto/testlib/selftests/expected_cmptest.txt | 3 +- tests/auto/testlib/selftests/expected_cmptest.xml | 4 ++ 8 files changed, 72 insertions(+), 38 deletions(-) diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 02deff09f1..004380f53d 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -579,7 +579,7 @@ namespace QTest QTEST_COMPARE_DECL(bool) #endif - template + template inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, const char *file, int line) { diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp index 7a3817c63d..f8cf3845b1 100644 --- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp +++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp @@ -12,6 +12,8 @@ #include #include #endif +#include +#include using namespace Qt::StringLiterals; /* XPM test data for QPixmap, QImage tests (use drag cursors as example) */ @@ -127,6 +129,7 @@ private slots: void compareQListIntToInitializerList_data(); void compareQListIntToInitializerList(); void compareQListDouble(); + void compareContainerToInitializerList(); #ifdef QT_GUI_LIB void compareQColor_data(); void compareQColor(); @@ -506,6 +509,24 @@ void tst_Cmptest::compareQListDouble() QCOMPARE(double1, double2); } +void tst_Cmptest::compareContainerToInitializerList() +{ + // Protect ',' in the list +#define ARG(...) __VA_ARGS__ + QSet set{1, 2, 3}; + QCOMPARE(set, ARG({1, 2, 3})); + + std::vector vec{1, 2, 3}; + QCOMPARE(vec, ARG({1, 2, 3})); + + vec.clear(); + QCOMPARE(vec, {}); + + vec.push_back(42); + QCOMPARE(vec, {42}); +#undef ARG +} + #ifdef QT_GUI_LIB void tst_Cmptest::compareQColor_data() { diff --git a/tests/auto/testlib/selftests/expected_cmptest.junitxml b/tests/auto/testlib/selftests/expected_cmptest.junitxml index 7ec9e2e05f..3f042d6ac0 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.junitxml +++ b/tests/auto/testlib/selftests/expected_cmptest.junitxml @@ -1,5 +1,5 @@ - + @@ -173,6 +173,7 @@ Expected (double2): 1]]> + diff --git a/tests/auto/testlib/selftests/expected_cmptest.lightxml b/tests/auto/testlib/selftests/expected_cmptest.lightxml index 6328632c69..9a8e9b4b91 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.lightxml +++ b/tests/auto/testlib/selftests/expected_cmptest.lightxml @@ -234,6 +234,10 @@ + + + + diff --git a/tests/auto/testlib/selftests/expected_cmptest.tap b/tests/auto/testlib/selftests/expected_cmptest.tap index 368d3aafb4..3e460903b8 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.tap +++ b/tests/auto/testlib/selftests/expected_cmptest.tap @@ -303,8 +303,9 @@ not ok 38 - compareQListDouble() file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 39 - compareQColor(Qt::yellow vs "yellow") -not ok 40 - compareQColor(Qt::yellow vs Qt::green) +ok 39 - compareContainerToInitializerList() +ok 40 - compareQColor(Qt::yellow vs "yellow") +not ok 41 - compareQColor(Qt::yellow vs Qt::green) --- type: QCOMPARE message: Compared values are not the same @@ -316,7 +317,7 @@ not ok 40 - compareQColor(Qt::yellow vs Qt::green) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 41 - compareQColor(0x88ff0000 vs 0xffff0000) +not ok 42 - compareQColor(0x88ff0000 vs 0xffff0000) --- type: QCOMPARE message: Compared values are not the same @@ -328,8 +329,8 @@ not ok 41 - compareQColor(0x88ff0000 vs 0xffff0000) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 42 - compareQPixmaps(both null) -not ok 43 - compareQPixmaps(one null) +ok 43 - compareQPixmaps(both null) +not ok 44 - compareQPixmaps(one null) --- type: QCOMPARE message: Compared QPixmaps differ. @@ -341,7 +342,7 @@ not ok 43 - compareQPixmaps(one null) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 44 - compareQPixmaps(other null) +not ok 45 - compareQPixmaps(other null) --- type: QCOMPARE message: Compared QPixmaps differ. @@ -353,8 +354,8 @@ not ok 44 - compareQPixmaps(other null) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 45 - compareQPixmaps(equal) -not ok 46 - compareQPixmaps(different size) +ok 46 - compareQPixmaps(equal) +not ok 47 - compareQPixmaps(different size) --- type: QCOMPARE message: Compared QPixmaps differ in size. @@ -366,14 +367,14 @@ not ok 46 - compareQPixmaps(different size) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 47 - compareQPixmaps(different pixels) +not ok 48 - compareQPixmaps(different pixels) --- # Compared values are not the same at: tst_Cmptest::compareQPixmaps() (qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp:0) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 48 - compareQPixmaps(different dpr) +not ok 49 - compareQPixmaps(different dpr) --- type: QCOMPARE message: Compared QPixmaps differ in device pixel ratio. @@ -385,8 +386,8 @@ not ok 48 - compareQPixmaps(different dpr) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 49 - compareQImages(both null) -not ok 50 - compareQImages(one null) +ok 50 - compareQImages(both null) +not ok 51 - compareQImages(one null) --- type: QCOMPARE message: Compared QImages differ. @@ -398,7 +399,7 @@ not ok 50 - compareQImages(one null) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 51 - compareQImages(other null) +not ok 52 - compareQImages(other null) --- type: QCOMPARE message: Compared QImages differ. @@ -410,8 +411,8 @@ not ok 51 - compareQImages(other null) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 52 - compareQImages(equal) -not ok 53 - compareQImages(different size) +ok 53 - compareQImages(equal) +not ok 54 - compareQImages(different size) --- type: QCOMPARE message: Compared QImages differ in size. @@ -423,7 +424,7 @@ not ok 53 - compareQImages(different size) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 54 - compareQImages(different format) +not ok 55 - compareQImages(different format) --- type: QCOMPARE message: Compared QImages differ in format. @@ -435,14 +436,14 @@ not ok 54 - compareQImages(different format) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 55 - compareQImages(different pixels) +not ok 56 - compareQImages(different pixels) --- # Compared values are not the same at: tst_Cmptest::compareQImages() (qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp:0) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 56 - compareQImages(different dpr) +not ok 57 - compareQImages(different dpr) --- type: QCOMPARE message: Compared QImages differ in device pixel ratio. @@ -454,8 +455,8 @@ not ok 56 - compareQImages(different dpr) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 57 - compareQRegion(equal-empty) -not ok 58 - compareQRegion(1-empty) +ok 58 - compareQRegion(equal-empty) +not ok 59 - compareQRegion(1-empty) --- type: QCOMPARE message: Compared values are not the same @@ -467,8 +468,8 @@ not ok 58 - compareQRegion(1-empty) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 59 - compareQRegion(equal) -not ok 60 - compareQRegion(different lists) +ok 60 - compareQRegion(equal) +not ok 61 - compareQRegion(different lists) --- type: QCOMPARE message: Compared values are not the same @@ -480,7 +481,7 @@ not ok 60 - compareQRegion(different lists) file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 61 - compareQVector2D() +not ok 62 - compareQVector2D() --- type: QCOMPARE message: Compared values are not the same @@ -492,7 +493,7 @@ not ok 61 - compareQVector2D() file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 62 - compareQVector3D() +not ok 63 - compareQVector3D() --- type: QCOMPARE message: Compared values are not the same @@ -504,7 +505,7 @@ not ok 62 - compareQVector3D() file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 63 - compareQVector4D() +not ok 64 - compareQVector4D() --- type: QCOMPARE message: Compared values are not the same @@ -516,7 +517,7 @@ not ok 63 - compareQVector4D() file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 64 - tryCompare() +not ok 65 - tryCompare() --- type: QCOMPARE message: Compared values are not the same @@ -532,7 +533,7 @@ not ok 64 - tryCompare() - severity: info message: Should now time out and fail ... -not ok 65 - verify() +not ok 66 - verify() --- type: QVERIFY message: Verification failed @@ -544,7 +545,7 @@ not ok 65 - verify() file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 66 - verify2() +not ok 67 - verify2() --- type: QVERIFY message: 42 >= 2 (as expected, in fact) @@ -556,7 +557,7 @@ not ok 66 - verify2() file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -not ok 67 - tryVerify() +not ok 68 - tryVerify() --- type: QVERIFY message: Verification failed @@ -572,7 +573,7 @@ not ok 67 - tryVerify() - severity: info message: Should now time out and fail ... -not ok 68 - tryVerify2() +not ok 69 - tryVerify2() --- type: QVERIFY message: Should time out and fail @@ -584,9 +585,9 @@ not ok 68 - tryVerify2() file: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp line: 0 ... -ok 69 - verifyExplicitOperatorBool() -ok 70 - cleanupTestCase() -1..70 -# tests 70 -# pass 21 +ok 70 - verifyExplicitOperatorBool() +ok 71 - cleanupTestCase() +1..71 +# tests 71 +# pass 22 # fail 49 diff --git a/tests/auto/testlib/selftests/expected_cmptest.teamcity b/tests/auto/testlib/selftests/expected_cmptest.teamcity index dfc8cdb2ce..7c7e64b9f2 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.teamcity +++ b/tests/auto/testlib/selftests/expected_cmptest.teamcity @@ -101,6 +101,8 @@ ##teamcity[testStarted name='compareQListDouble()' flowId='tst_Cmptest'] ##teamcity[testFailed name='compareQListDouble()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(0)|]' details='Compared lists differ at index 0.|n Actual (double1): 1.5|n Expected (double2): 1' flowId='tst_Cmptest'] ##teamcity[testFinished name='compareQListDouble()' flowId='tst_Cmptest'] +##teamcity[testStarted name='compareContainerToInitializerList()' flowId='tst_Cmptest'] +##teamcity[testFinished name='compareContainerToInitializerList()' flowId='tst_Cmptest'] ##teamcity[testStarted name='compareQColor(Qt::yellow vs "yellow")' flowId='tst_Cmptest'] ##teamcity[testFinished name='compareQColor(Qt::yellow vs "yellow")' flowId='tst_Cmptest'] ##teamcity[testStarted name='compareQColor(Qt::yellow vs Qt::green)' flowId='tst_Cmptest'] diff --git a/tests/auto/testlib/selftests/expected_cmptest.txt b/tests/auto/testlib/selftests/expected_cmptest.txt index 602202bafc..9d96fb7643 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.txt +++ b/tests/auto/testlib/selftests/expected_cmptest.txt @@ -116,6 +116,7 @@ FAIL! : tst_Cmptest::compareQListDouble() Compared lists differ at index 0. Actual (double1): 1.5 Expected (double2): 1 Loc: [qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(0)] +PASS : tst_Cmptest::compareContainerToInitializerList() PASS : tst_Cmptest::compareQColor(Qt::yellow vs "yellow") FAIL! : tst_Cmptest::compareQColor(Qt::yellow vs Qt::green) Compared values are not the same Actual (colorA): #ffffff00 @@ -207,5 +208,5 @@ FAIL! : tst_Cmptest::tryVerify2() '!c' returned FALSE. (Should time out and fai Loc: [qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(0)] PASS : tst_Cmptest::verifyExplicitOperatorBool() PASS : tst_Cmptest::cleanupTestCase() -Totals: 21 passed, 49 failed, 0 skipped, 0 blacklisted, 0ms +Totals: 22 passed, 49 failed, 0 skipped, 0 blacklisted, 0ms ********* Finished testing of tst_Cmptest ********* diff --git a/tests/auto/testlib/selftests/expected_cmptest.xml b/tests/auto/testlib/selftests/expected_cmptest.xml index a0e8e76179..ba9d617aed 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xml +++ b/tests/auto/testlib/selftests/expected_cmptest.xml @@ -236,6 +236,10 @@ + + + + -- cgit v1.2.3