A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
csma-link-change-test-suite.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7
*/
8
9
#include "ns3/callback.h"
10
#include "ns3/csma-channel.h"
11
#include "ns3/csma-net-device.h"
12
#include "ns3/mac48-address.h"
13
#include "ns3/node.h"
14
#include "ns3/simulator.h"
15
#include "ns3/test.h"
16
17
using namespace
ns3
;
18
19
/**
20
* @ingroup csma-test
21
* @ingroup tests
22
*
23
* @brief Verify that detaching a CsmaNetDevice from its channel fires the
24
* link-change callback and marks the link down (@issueid{234}).
25
*/
26
class
CsmaLinkChangeTestCase
:
public
TestCase
27
{
28
public
:
29
CsmaLinkChangeTestCase
();
30
31
private
:
32
void
DoRun
()
override
;
33
/// Link-change callback; counts invocations.
34
void
LinkChange
();
35
uint32_t
m_linkChangeCount
{0};
//!< number of link-change notifications
36
};
37
38
CsmaLinkChangeTestCase::CsmaLinkChangeTestCase
()
39
:
TestCase
(
"CsmaChannel::Detach fires the link-change callback (issue #234)"
)
40
{
41
}
42
43
void
44
CsmaLinkChangeTestCase::LinkChange
()
45
{
46
m_linkChangeCount
++;
47
}
48
49
void
50
CsmaLinkChangeTestCase::DoRun
()
51
{
52
// --- Detach by device pointer ---
53
{
54
m_linkChangeCount
= 0;
55
Ptr<Node>
node =
CreateObject<Node>
();
56
Ptr<CsmaChannel>
channel =
CreateObject<CsmaChannel>
();
57
Ptr<CsmaNetDevice>
dev =
CreateObject<CsmaNetDevice>
();
58
dev->SetAddress(
Mac48Address::Allocate
());
59
node->AddDevice(dev);
60
61
dev->Attach(channel);
62
NS_TEST_ASSERT_MSG_EQ
(dev->IsLinkUp(),
true
,
"link should be up after Attach"
);
63
64
dev->AddLinkChangeCallback(
MakeCallback
(&
CsmaLinkChangeTestCase::LinkChange
,
this
));
65
66
bool
detached = channel->Detach(dev);
67
NS_TEST_ASSERT_MSG_EQ
(detached,
true
,
"Detach(device) should succeed"
);
68
NS_TEST_ASSERT_MSG_EQ
(
m_linkChangeCount
,
69
1,
70
"link-change callback should fire on Detach(device)"
);
71
NS_TEST_ASSERT_MSG_EQ
(dev->IsLinkUp(),
false
,
"link should be down after Detach(device)"
);
72
}
73
74
// --- Detach by device id ---
75
{
76
m_linkChangeCount
= 0;
77
Ptr<Node>
node =
CreateObject<Node>
();
78
Ptr<CsmaChannel>
channel =
CreateObject<CsmaChannel>
();
79
Ptr<CsmaNetDevice>
dev =
CreateObject<CsmaNetDevice>
();
80
dev->SetAddress(
Mac48Address::Allocate
());
81
node->AddDevice(dev);
82
83
dev->Attach(channel);
84
dev->AddLinkChangeCallback(
MakeCallback
(&
CsmaLinkChangeTestCase::LinkChange
,
this
));
85
86
// The first (and only) attached device has channel device id 0.
87
bool
detached = channel->Detach(
uint32_t
(0));
88
NS_TEST_ASSERT_MSG_EQ
(detached,
true
,
"Detach(deviceId) should succeed"
);
89
NS_TEST_ASSERT_MSG_EQ
(
m_linkChangeCount
,
90
1,
91
"link-change callback should fire on Detach(deviceId)"
);
92
NS_TEST_ASSERT_MSG_EQ
(dev->IsLinkUp(),
false
,
"link should be down after Detach(deviceId)"
);
93
}
94
95
// The nodes register themselves with the NodeList on construction; this
96
// clears it and disposes them along with their devices and channels.
97
Simulator::Destroy
();
98
}
99
100
/**
101
* @ingroup csma-test
102
* @ingroup tests
103
*
104
* @brief CSMA link-change test suite.
105
*/
106
class
CsmaLinkChangeTestSuite
:
public
TestSuite
107
{
108
public
:
109
CsmaLinkChangeTestSuite
()
110
:
TestSuite
(
"csma-link-change"
,
Type
::
UNIT
)
111
{
112
AddTestCase
(
new
CsmaLinkChangeTestCase
(),
TestCase::Duration::QUICK
);
113
}
114
};
115
116
static
CsmaLinkChangeTestSuite
g_csmaLinkChangeTestSuite
;
//!< Static variable for test init
CsmaLinkChangeTestCase
Verify that detaching a CsmaNetDevice from its channel fires the link-change callback and marks the l...
Definition
csma-link-change-test-suite.cc:27
CsmaLinkChangeTestCase::LinkChange
void LinkChange()
Link-change callback; counts invocations.
Definition
csma-link-change-test-suite.cc:44
CsmaLinkChangeTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
csma-link-change-test-suite.cc:50
CsmaLinkChangeTestCase::m_linkChangeCount
uint32_t m_linkChangeCount
number of link-change notifications
Definition
csma-link-change-test-suite.cc:35
CsmaLinkChangeTestCase::CsmaLinkChangeTestCase
CsmaLinkChangeTestCase()
Definition
csma-link-change-test-suite.cc:38
CsmaLinkChangeTestSuite
CSMA link-change test suite.
Definition
csma-link-change-test-suite.cc:107
CsmaLinkChangeTestSuite::CsmaLinkChangeTestSuite
CsmaLinkChangeTestSuite()
Definition
csma-link-change-test-suite.cc:109
ns3::Mac48Address::Allocate
static Mac48Address Allocate()
Allocate a new Mac48Address.
Definition
mac48-address.cc:95
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:70
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:125
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:296
ns3::TestCase::Duration::QUICK
@ QUICK
Fast test.
Definition
test.h:1057
ns3::TestCase::TestCase
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1271
ns3::TestSuite::Type::UNIT
@ UNIT
This test suite implements a Unit Test.
Definition
test.h:1273
ns3::TestSuite::TestSuite
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition
test.cc:494
uint32_t
g_csmaLinkChangeTestSuite
static CsmaLinkChangeTestSuite g_csmaLinkChangeTestSuite
Static variable for test init.
Definition
csma-link-change-test-suite.cc:116
ns3::MakeCallback
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition
callback.h:690
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:627
NS_TEST_ASSERT_MSG_EQ
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition
test.h:133
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
csma
test
csma-link-change-test-suite.cc
Generated on
for ns-3 by
1.16.1