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

@ -1211,8 +1211,8 @@ QDebug &QDebug::putTupleLikeImplImpl(const char *ns, const char *what,
\relates QDebug
\since 6.9
Writes the contents of \a map to \a debug. Both \c Key and
\c T need to support streaming into QDebug.
Writes the contents of \a map to \a debug. Both \c Key and
\c T need to support streaming into QDebug.
*/
/*!
@ -1220,11 +1220,10 @@ QDebug &QDebug::putTupleLikeImplImpl(const char *ns, const char *what,
\relates QDebug
\since 6.9
Writes the contents of \a unordered_set to \a debug. The \c Key type
needs to support streaming into QDebug.
Writes the contents of \a unordered_set to \a debug. The \c Key type
needs to support streaming into QDebug.
*/
/*!
\fn template <class Key, class T> QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)
\relates QDebug

View File

@ -25,11 +25,14 @@
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <set>
#include <tuple>
#include <QtCore/q20type_traits.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <unordered_map>
#include <unordered_set>
@ -446,7 +449,7 @@ inline QDebug operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc>
}
template <typename Key, typename Compare, typename Alloc>
inline QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc>& set)
inline QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)
{
return QtPrivate::printSequentialContainer(std::move(debug), "std::set", set);
}
@ -458,7 +461,7 @@ inline QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, Ke
}
template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
inline QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc>& unordered_set)
inline QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)
{
return QtPrivate::printSequentialContainer(std::move(debug), "std::unordered_set", unordered_set);
}

View File

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