From 94a5d323436a4ae6e3d1b8cea62b07115e855d55 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 8 Aug 2023 19:12:25 +0200 Subject: [PATCH] tst_QImageReader: it's a rotate^Wadjacent_find! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of copying a list, sorting it just to check it's sorted, and making a QSet out of it just to check the size is the same as that of the list (thereby checking there were no duplicates), simply apply adjacent_find with greater_equal. If none of the elements is ≥ their successor, that means all elements are < their successor, and _that_ means the range is sorted and has no duplicates. q.e.d. Change-Id: Id73c674ad4e29117370e8fc6af9fdfc690a3fba9 Reviewed-by: Fabian Kosmale Reviewed-by: Edward Welbourne (cherry picked from commit 29d07101e5dd7efbb0ffbab1acb5d8d4ef0ed731) Reviewed-by: Qt Cherry-pick Bot --- .../image/qimagereader/tst_qimagereader.cpp | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp b/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp index c8ef8ff839e..10f3a813c30 100644 --- a/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp +++ b/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -595,41 +594,31 @@ void tst_QImageReader::multiWordNamedColorXPM() QCOMPARE(image.pixel(0, 2), qRgb(255, 250, 205)); // lemon chiffon } +namespace { +template +bool is_sorted_unique(ForwardIterator first, ForwardIterator last) +{ + // a range is sorted with no dups iff each *i < *(i+1), so check that none are >=: + return std::adjacent_find(first, last, std::greater_equal<>{}) == last; +} +} + void tst_QImageReader::supportedFormats() { - QList formats = QImageReader::supportedImageFormats(); - QList sortedFormats = formats; - std::sort(sortedFormats.begin(), sortedFormats.end()); - - // check that the list is sorted - QCOMPARE(formats, sortedFormats); - - QSet formatSet; - foreach (QByteArray format, formats) - formatSet << format; - - // check that the list does not contain duplicates - QCOMPARE(formatSet.size(), formats.size()); + const QList formats = QImageReader::supportedImageFormats(); + auto printOnFailure = qScopeGuard([&] { qDebug() << formats; }); + QVERIFY(is_sorted_unique(formats.begin(), formats.end())); + printOnFailure.dismiss(); } void tst_QImageReader::supportedMimeTypes() { - QList mimeTypes = QImageReader::supportedMimeTypes(); - QList sortedMimeTypes = mimeTypes; - std::sort(sortedMimeTypes.begin(), sortedMimeTypes.end()); - - // check that the list is sorted - QCOMPARE(mimeTypes, sortedMimeTypes); - - QSet mimeTypeSet; - foreach (QByteArray mimeType, mimeTypes) - mimeTypeSet << mimeType; - + const QList mimeTypes = QImageReader::supportedMimeTypes(); + auto printOnFailure = qScopeGuard([&] { qDebug() << mimeTypes; }); + QVERIFY(is_sorted_unique(mimeTypes.begin(), mimeTypes.end())); // check the list as a minimum contains image/bmp - QVERIFY(mimeTypeSet.contains("image/bmp")); - - // check that the list does not contain duplicates - QCOMPARE(mimeTypeSet.size(), mimeTypes.size()); + QVERIFY(mimeTypes.contains("image/bmp")); + printOnFailure.dismiss(); } void tst_QImageReader::setBackgroundColor_data()