diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-01-09 10:32:30 -0600 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2023-03-07 16:12:02 +0100 |
commit | b0d3f5df53486fde39fd4ed03606f987efd32874 (patch) | |
tree | 82883ca797bbff44580a838db9b96518178ce70c | |
parent | 52d0aa38e0a2298937b764c03a8c719a8996da23 (diff) |
tst_QAlgorithms: don't use random numbers in the test row names
Just describe the row instead. We'd lose the original input in case of
failure, so I added a class to print that value on destruction. Example:
FAIL! : tst_QAlgorithms::countLeading64(0) Compared values are not the
same
Actual (qCountLeadingZeroBits(value)): 63
Expected (expected) : 64
Loc: [tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp(374)]
QWARN : tst_QAlgorithms::countLeading64(0) Original value was 0x1
Fixes: QTBUG-109958
Change-Id: I69ecc04064514f939896fffd1738b1119cd80cf8
Reviewed-by: Dimitrios Apostolou <jimis@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit d0408b5f13f811f73cf2485b5b31d5ec4b7f2b62)
-rw-r--r-- | tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp index 5bce644331..4b7405cf0f 100644 --- a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp +++ b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp @@ -95,6 +95,18 @@ private: void countLeading_impl(); }; +template <typename T> struct PrintIfFailed +{ + T value; + PrintIfFailed(T v) : value(v) {} + ~PrintIfFailed() + { + if (!QTest::currentTestFailed()) + return; + qWarning() << "Original value was" << Qt::hex << Qt::showbase << T(value); + } +}; + void tst_QAlgorithms::swap() { { @@ -274,22 +286,22 @@ void tst_QAlgorithms::popCount_data_impl(size_t sizeof_T_Int) const uint bits = bitsSetInByte(byte); const quint64 value = static_cast<quint64>(byte); const quint64 input = value << ((i % sizeof_T_Int) * 8U); - QTest::addRow("0x%016llx", input) << input << bits; + QTest::addRow("%u-bits", i) << input << bits; } // and some random ones: if (sizeof_T_Int >= 8) { for (size_t i = 0; i < 1000; ++i) { const quint64 input = QRandomGenerator::global()->generate64(); - QTest::addRow("0x%016llx", input) << input << bitsSetInInt64(input); + QTest::addRow("random-%zu", i) << input << bitsSetInInt64(input); } } else if (sizeof_T_Int >= 2) { for (size_t i = 0; i < 1000 ; ++i) { const quint32 input = QRandomGenerator::global()->generate(); if (sizeof_T_Int >= 4) { - QTest::addRow("0x%08x", input) << quint64(input) << bitsSetInInt(input); + QTest::addRow("random-%zu", i) << quint64(input) << bitsSetInInt(input); } else { - QTest::addRow("0x%04x", quint16(input & 0xFFFF)) + QTest::addRow("random-%zu", i) << quint64(input & 0xFFFF) << bitsSetInShort(input & 0xFFFF); } } @@ -303,7 +315,7 @@ void tst_QAlgorithms::popCount_impl() QFETCH(uint, expected); const T_Int value = static_cast<T_Int>(input); - + PrintIfFailed pf(value); QCOMPARE(qPopulationCount(value), expected); } @@ -316,12 +328,10 @@ void tst_QAlgorithms::countTrailing_data_impl(size_t sizeof_T_Int) addColumn<quint64>("input"); addColumn<uint>("expected"); - int nibs = sizeof_T_Int*2; - - addRow("0x%0*llx", nibs, Q_UINT64_C(0)) << Q_UINT64_C(0) << uint(sizeof_T_Int*8); + addRow("0") << Q_UINT64_C(0) << uint(sizeof_T_Int*8); for (uint i = 0; i < sizeof_T_Int*8; ++i) { const quint64 input = Q_UINT64_C(1) << i; - addRow("0x%0*llx", nibs, input) << input << i; + addRow("bit-%u", i) << input << i; } quint64 type_mask; @@ -337,7 +347,7 @@ void tst_QAlgorithms::countTrailing_data_impl(size_t sizeof_T_Int) for (uint j = 0; j < sizeof_T_Int * casesPerOffset; ++j) { const quint64 r = QRandomGenerator::global()->generate64(); const quint64 input = (r&mask) | b; - addRow("0x%0*llx", nibs, input) << input << i; + addRow("%u-bits-random-%u", i, j) << input << i; } } } @@ -349,7 +359,7 @@ void tst_QAlgorithms::countTrailing_impl() QFETCH(uint, expected); const T_Int value = static_cast<T_Int>(input); - + PrintIfFailed pf(value); QCOMPARE(qCountTrailingZeroBits(value), expected); } @@ -359,12 +369,10 @@ void tst_QAlgorithms::countLeading_data_impl(size_t sizeof_T_Int) addColumn<quint64>("input"); addColumn<uint>("expected"); - int nibs = sizeof_T_Int*2; - - addRow("0x%0*llx", nibs, Q_UINT64_C(0)) << Q_UINT64_C(0) << uint(sizeof_T_Int*8); + addRow("0") << Q_UINT64_C(0) << uint(sizeof_T_Int*8); for (uint i = 0; i < sizeof_T_Int*8; ++i) { const quint64 input = Q_UINT64_C(1) << i; - addRow("0x%0*llx", nibs, input) << input << uint(sizeof_T_Int*8-i-1); + addRow("bit-%u", i) << input << uint(sizeof_T_Int*8-i-1); } // and some random ones: @@ -374,7 +382,7 @@ void tst_QAlgorithms::countLeading_data_impl(size_t sizeof_T_Int) for (uint j = 0; j < sizeof_T_Int * casesPerOffset; ++j) { const quint64 r = QRandomGenerator::global()->generate64(); const quint64 input = (r&mask) | b; - addRow("0x%0*llx", nibs, input) << input << uint(sizeof_T_Int*8-i-1); + addRow("%u-bits-random-%u", i, j) << input << uint(sizeof_T_Int*8-i-1); } } } @@ -386,7 +394,7 @@ void tst_QAlgorithms::countLeading_impl() QFETCH(uint, expected); const T_Int value = static_cast<T_Int>(input); - + PrintIfFailed pf(value); QCOMPARE(qCountLeadingZeroBits(value), expected); } |