Skip to content

Commit adcc6ba

Browse files
committed
test: avoid tie-order assumptions in peer sort proxy assertions
1 parent 8e71060 commit adcc6ba

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

test/test_peerlistmodel.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <util/translation.h>
1111

1212
#include <algorithm>
13+
#include <map>
1314
#include <utility>
1415

1516
const TranslateFn G_TRANSLATION_FUN{nullptr};
@@ -233,6 +234,11 @@ void PeerListModelTests::sortProxySortsByRoles()
233234

234235
const QVector<CNodeStats> source_stats{stats_b, stats_c, stats_a};
235236
const auto stats{MakeStats({stats_b, stats_c, stats_a})};
237+
const std::map<qint64, CNodeStats> stats_by_id{
238+
{stats_a.nodeid, stats_a},
239+
{stats_b.nodeid, stats_b},
240+
{stats_c.nodeid, stats_c},
241+
};
236242

237243
NiceMock<MockNode> node;
238244
EXPECT_CALL(node, getNodesStats(_))
@@ -244,15 +250,6 @@ void PeerListModelTests::sortProxySortsByRoles()
244250
proxy.setSourceModel(&model);
245251

246252
const auto assert_sort = [&](const QString& role_name, auto less_than) {
247-
QVector<CNodeStats> expected = source_stats;
248-
std::sort(expected.begin(), expected.end(), less_than);
249-
250-
QVector<qint64> expected_ids;
251-
expected_ids.reserve(expected.size());
252-
for (const auto& node_stats : expected) {
253-
expected_ids.append(node_stats.nodeid);
254-
}
255-
256253
proxy.setSortBy(role_name);
257254

258255
QVector<qint64> actual_ids;
@@ -261,7 +258,26 @@ void PeerListModelTests::sortProxySortsByRoles()
261258
actual_ids.append(proxy.data(proxy.index(row, 0), PeerListModel::NetNodeId).toLongLong());
262259
}
263260

264-
QCOMPARE(actual_ids, expected_ids);
261+
QVector<qint64> expected_ids;
262+
expected_ids.reserve(source_stats.size());
263+
for (const auto& node_stats : source_stats) {
264+
expected_ids.append(node_stats.nodeid);
265+
}
266+
std::sort(expected_ids.begin(), expected_ids.end());
267+
268+
QVector<qint64> sorted_actual_ids = actual_ids;
269+
std::sort(sorted_actual_ids.begin(), sorted_actual_ids.end());
270+
QCOMPARE(sorted_actual_ids, expected_ids);
271+
272+
for (int i = 1; i < actual_ids.size(); ++i) {
273+
const auto prev_it = stats_by_id.find(actual_ids.at(i - 1));
274+
const auto cur_it = stats_by_id.find(actual_ids.at(i));
275+
QVERIFY(prev_it != stats_by_id.end());
276+
QVERIFY(cur_it != stats_by_id.end());
277+
278+
// Allow equal-key items in any order, but disallow an inversion.
279+
QVERIFY(!less_than(cur_it->second, prev_it->second));
280+
}
265281
};
266282

267283
assert_sort("nodeId", [](const CNodeStats& left, const CNodeStats& right) { return left.nodeid < right.nodeid; });

0 commit comments

Comments
 (0)