Skip to content

Commit

Permalink
wifi: Use custom Shuffle implementation instead of std::shuffle
Browse files Browse the repository at this point in the history
std::shuffle implementation is not guaranteed to return the same
permutation across different libraries
  • Loading branch information
Stefano Avallone committed May 24, 2024
1 parent 556e901 commit 34b4cf1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/core/test/random-variable-stream-test-suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -2896,7 +2898,11 @@ ShuffleElementsTest::ShuffleElementsTest()
void
ShuffleElementsTest::DoRun()
{
RngSeedManager::SetSeed(1);
RngSeedManager::SetRun(1);

auto rv = CreateObject<UniformRandomVariable>();
rv->SetStream(1);

// test empty vector
std::vector<uint8_t> vec{};
Expand All @@ -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<uint8_t>{1, 3}), true, "Expected vector {1, 3}");
Expand All @@ -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<uint8_t>{1, 2, 3, 4, 7, 9}),
NS_TEST_EXPECT_MSG_EQ((vec == std::vector<uint8_t>{4, 1, 9, 3, 2, 7}),
true,
"Expected vector {1, 2, 3, 4, 7, 9}");
"Expected vector {4, 1, 9, 3, 2, 7}");
}
/**
Expand Down
3 changes: 2 additions & 1 deletion src/wifi/model/txop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -665,7 +666,7 @@ Txop::Queue(Ptr<WifiMpdu> mpdu)

// shuffle link IDs not to request channel access on links always in the same order
std::vector<uint8_t> 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))
{
Expand Down
3 changes: 2 additions & 1 deletion src/wifi/model/wifi-mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -1562,7 +1563,7 @@ WifiMac::UnblockUnicastTxOnLinks(WifiQueueBlockedReason reason,

// shuffle link IDs not to unblock links always in the same order
std::vector<uint8_t> 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))
Expand Down

0 comments on commit 34b4cf1

Please sign in to comment.