QDebug: add refinements to qDebug support for stl containers

Refinements of the stl containers based on the comments.

Fixes: QTBUG-131766
Change-Id: Iddca031a7b63cc24a3d4a1fdede02b8968e95be9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Dheerendra Purohit 2024-12-02 17:02:46 +05:30
parent d991572a45
commit 51f702d402
3 changed files with 27 additions and 41 deletions

View File

@ -1224,7 +1224,6 @@ Writes the contents of \a unordered_set to \a debug. The \c Key type
needs to support streaming into QDebug. needs to support streaming into QDebug.
*/ */
/*! /*!
\fn template <class Key, class T> QDebug operator<<(QDebug debug, const QHash<Key, T> &hash) \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)
\relates QDebug \relates QDebug

View File

@ -25,11 +25,14 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <set>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <set> #include <set>
#include <tuple> #include <tuple>
#include <QtCore/q20type_traits.h> #include <QtCore/q20type_traits.h>
#include <unordered_map>
#include <unordered_set>
#include <utility> #include <utility>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>

View File

@ -773,8 +773,8 @@ void tst_QDebug::qDebugStdSet() const
{ {
QDebug d = qDebug(); QDebug d = qDebug();
std::set<int> Set{1, 2, 3, 2, 1}; std::set<int>set{1, 2, 3, 2, 1};
d.nospace().noquote() << Set; d.nospace().noquote() << set;
} }
#ifndef QT_NO_MESSAGELOGCONTEXT #ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO; file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
@ -785,15 +785,11 @@ void tst_QDebug::qDebugStdSet() const
QCOMPARE(s_line, line); QCOMPARE(s_line, line);
QCOMPARE(s_function, function); QCOMPARE(s_function, function);
{
qDebug() << std::set<std::string>{"apple", "banana", "cherry", "banana", "apple"}; qDebug() << std::set<std::string>{"apple", "banana", "cherry", "banana", "apple"};
}
QCOMPARE(s_msg, "std::set(\"apple\", \"banana\", \"cherry\")"_L1); QCOMPARE(s_msg, "std::set(\"apple\", \"banana\", \"cherry\")"_L1);
{
qDebug() << std::set<int>{}; qDebug() << std::set<int>{};
}
QCOMPARE(s_msg, "std::set()"_L1); QCOMPARE(s_msg, "std::set()"_L1);
} }
@ -858,25 +854,20 @@ void tst_QDebug::qDebugStdUnorderedMap() const
QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(s_msgType, QtDebugMsg);
QStringList expectedValues = {"std::unordered_map","std::pair(1, One)","std::pair(2, Two)","std::pair(3, Three)"}; QStringList expectedValues = {"std::unordered_map","std::pair(1, One)","std::pair(2, Two)","std::pair(3, Three)"};
for (const QString &expextedValue : expectedValues) {
for (const QString &expextedValue : expectedValues)
QVERIFY(s_msg.contains(expextedValue)); QVERIFY(s_msg.contains(expextedValue));
}
QCOMPARE(s_file, file); QCOMPARE(s_file, file);
QCOMPARE(s_line, line); QCOMPARE(s_line, line);
QCOMPARE(s_function, function); QCOMPARE(s_function, function);
{ qDebug() << std::unordered_map<std::string, float>{{"quarter", 0.25f}, {"half", 0.5f}, {"full", 1.0f}};
qDebug() << std::unordered_map<std::string, float>{{"quarter", 0.25f}, {"half", 0.5f}};
}
expectedValues= {"std::unordered_map","std::pair(\"quarter\", 0.25)","std::pair(\"half\", 0.5)"}; expectedValues = {"std::unordered_map","std::pair(\"quarter\", 0.25)","std::pair(\"half\", 0.5)","std::pair(\"full\", 1)"};
for (const QString &expextedValue : expectedValues) { for (const QString &expectedValue : expectedValues)
QVERIFY(s_msg.contains(expextedValue)); QVERIFY(s_msg.contains(expectedValue));
}
{
qDebug()<< std::unordered_map<int, QString> {}; qDebug()<< std::unordered_map<int, QString> {};
}
QCOMPARE(s_msg, "std::unordered_map()"_L1); QCOMPARE(s_msg, "std::unordered_map()"_L1);
} }
@ -898,25 +889,18 @@ void tst_QDebug::qDebugStdUnorderedSet() const
QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(s_msgType, QtDebugMsg);
QStringList expectedValues = {"std::unordered_set", "1", "2", "3"}; QStringList expectedValues = {"std::unordered_set", "1", "2", "3"};
for (const QString &expectedValue : expectedValues) {
QVERIFY(s_msg.contains(expectedValue));
}
QCOMPARE(s_file, file);
QCOMPARE(s_line, line);
QCOMPARE(s_function, function);
{ for (const QString &expectedValue : expectedValues)
QVERIFY(s_msg.contains(expectedValue));
qDebug() << std::unordered_set<std::string>{"apple", "banana", "cherry", "banana", "apple"}; qDebug() << std::unordered_set<std::string>{"apple", "banana", "cherry", "banana", "apple"};
}
expectedValues = {"std::unordered_set", "\"apple\"", "\"banana\"", "\"cherry\""}; expectedValues = {"std::unordered_set", "\"apple\"", "\"banana\"", "\"cherry\""};
for (const QString &expectedValue : expectedValues) { for (const QString &expectedValue : expectedValues)
QVERIFY(s_msg.contains(expectedValue)); QVERIFY(s_msg.contains(expectedValue));
}
{ qDebug() << std::unordered_set<int>{};
qDebug() << std::unordered_set<int>{}; // Empty set
}
QCOMPARE(s_msg, "std::unordered_set()"_L1); QCOMPARE(s_msg, "std::unordered_set()"_L1);
} }