Skip to content

Commit

Permalink
QDebug: add refinements to qDebug support for stl containers
Browse files Browse the repository at this point in the history
Refinements of the stl containers based on the comments.

Fixes: QTBUG-131766
Change-Id: Iddca031a7b63cc24a3d4a1fdede02b8968e95be9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
QtDheeru committed Dec 12, 2024
1 parent d991572 commit 51f702d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
9 changes: 4 additions & 5 deletions src/corelib/io/qdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,20 +1211,19 @@ 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.
*/

/*!
\fn template <typename Key, typename Hash, typename KeyEqual, typename Alloc> QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)
\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
Expand Down
7 changes: 5 additions & 2 deletions src/corelib/io/qdebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
50 changes: 17 additions & 33 deletions tests/auto/corelib/io/qdebug/tst_qdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {

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"};
}
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);
}
Expand Down

0 comments on commit 51f702d

Please sign in to comment.