From ca5580a560492f372157852d9bcc1f383e7b2d0b Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Thu, 13 Mar 2025 09:53:59 +0100
Subject: [PATCH] test/util/TestIntrusiveTreeSet: do not add duplicates in the
 LargeRandom tests

This can fail randomly if the order of identical values is different
in the std::deque and the IntrusiveTreeSet.
---
 test/util/TestIntrusiveTreeSet.cxx | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/test/util/TestIntrusiveTreeSet.cxx b/test/util/TestIntrusiveTreeSet.cxx
index 13a14e881..e32981120 100644
--- a/test/util/TestIntrusiveTreeSet.cxx
+++ b/test/util/TestIntrusiveTreeSet.cxx
@@ -8,6 +8,7 @@
 
 #include <cstdlib> // for std::rand()
 #include <deque>
+#include <set>
 #include <string>
 
 namespace {
@@ -207,13 +208,24 @@ TEST(IntrusiveTreeSet, RandomOrder)
 
 TEST(IntrusiveTreeSet, LargeRandom)
 {
+	std::set<int> dedup;
 	std::deque<std::unique_ptr<IntItem>> items;
 	IntrusiveTreeSet<IntItem,
 			 IntrusiveTreeSetOperators<IntItem, IntItem::GetKey>> set;
 
 	std::srand(42);
 	for (unsigned i = 0; i < 1024; ++i) {
-		items.emplace_back(std::make_unique<IntItem>(std::rand()));
+		/* prevent duplicates to avoid different ordering of
+		   identical values in the std::deque and in the
+		   IntrusiveTreeSet */
+		int value;
+		while (true) {
+			value = std::rand();
+			if (dedup.insert(value).second)
+				break;
+		}
+
+		items.emplace_back(std::make_unique<IntItem>(value));
 		set.insert(*items.back());
 
 #ifndef NDEBUG