From 34b4cf1a30f091cdf555b3080b8240b56a123f9a Mon Sep 17 00:00:00 2001 From: Stefano Avallone Date: Thu, 23 May 2024 20:16:47 +0200 Subject: [PATCH] wifi: Use custom Shuffle implementation instead of std::shuffle std::shuffle implementation is not guaranteed to return the same permutation across different libraries --- src/core/test/random-variable-stream-test-suite.cc | 12 ++++++++---- src/wifi/model/txop.cc | 3 ++- src/wifi/model/wifi-mac.cc | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/core/test/random-variable-stream-test-suite.cc b/src/core/test/random-variable-stream-test-suite.cc index c2fd5015013..145bd49bd6f 100644 --- a/src/core/test/random-variable-stream-test-suite.cc +++ b/src/core/test/random-variable-stream-test-suite.cc @@ -2878,6 +2878,8 @@ BinomialAntitheticTestCase::DoRun() * \ingroup tests * * \brief Test the Shuffle function + * + * Check that the Shuffle function actually shuffles the elements and does so in a portable way. */ class ShuffleElementsTest : public TestCase { @@ -2896,7 +2898,11 @@ ShuffleElementsTest::ShuffleElementsTest() void ShuffleElementsTest::DoRun() { + RngSeedManager::SetSeed(1); + RngSeedManager::SetRun(1); + auto rv = CreateObject(); + rv->SetStream(1); // test empty vector std::vector vec{}; @@ -2916,7 +2922,6 @@ ShuffleElementsTest::DoRun() vec.push_back(1); Shuffle(vec.begin(), vec.end(), rv); - std::sort(vec.begin(), vec.end()); NS_TEST_EXPECT_MSG_EQ((vec == std::vector{1, 3}), true, "Expected vector {1, 3}"); @@ -2927,11 +2932,10 @@ ShuffleElementsTest::DoRun() vec.push_back(9); Shuffle(vec.begin(), vec.end(), rv); - std::sort(vec.begin(), vec.end()); - NS_TEST_EXPECT_MSG_EQ((vec == std::vector{1, 2, 3, 4, 7, 9}), + NS_TEST_EXPECT_MSG_EQ((vec == std::vector{4, 1, 9, 3, 2, 7}), true, - "Expected vector {1, 2, 3, 4, 7, 9}"); + "Expected vector {4, 1, 9, 3, 2, 7}"); } /** diff --git a/src/wifi/model/txop.cc b/src/wifi/model/txop.cc index 58f02cf513a..93c8e541824 100644 --- a/src/wifi/model/txop.cc +++ b/src/wifi/model/txop.cc @@ -30,6 +30,7 @@ #include "ns3/attribute-container.h" #include "ns3/log.h" #include "ns3/pointer.h" +#include "ns3/shuffle.h" #include "ns3/simulator.h" #include "ns3/socket.h" @@ -665,7 +666,7 @@ Txop::Queue(Ptr mpdu) // shuffle link IDs not to request channel access on links always in the same order std::vector shuffledLinkIds(linkIds.cbegin(), linkIds.cend()); - std::shuffle(shuffledLinkIds.begin(), shuffledLinkIds.end(), m_shuffleLinkIdsGen); + Shuffle(shuffledLinkIds.begin(), shuffledLinkIds.end(), m_shuffleLinkIdsGen.GetRv()); if (!linkIds.empty() && g_log.IsEnabled(ns3::LOG_DEBUG)) { diff --git a/src/wifi/model/wifi-mac.cc b/src/wifi/model/wifi-mac.cc index 9c27f1be546..393789671c1 100644 --- a/src/wifi/model/wifi-mac.cc +++ b/src/wifi/model/wifi-mac.cc @@ -36,6 +36,7 @@ #include "ns3/log.h" #include "ns3/packet.h" #include "ns3/pointer.h" +#include "ns3/shuffle.h" #include "ns3/string.h" #include "ns3/vht-configuration.h" @@ -1562,7 +1563,7 @@ WifiMac::UnblockUnicastTxOnLinks(WifiQueueBlockedReason reason, // shuffle link IDs not to unblock links always in the same order std::vector shuffledLinkIds(linkIds.cbegin(), linkIds.cend()); - std::shuffle(shuffledLinkIds.begin(), shuffledLinkIds.end(), m_shuffleLinkIdsGen); + Shuffle(shuffledLinkIds.begin(), shuffledLinkIds.end(), m_shuffleLinkIdsGen.GetRv()); std::stringstream ss; if (g_log.IsEnabled(ns3::LOG_FUNCTION))