A Discrete-Event Network Simulator
API
spectrum-waveform-generator-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 CTTC
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Luis Pacheco <luisbelem@gmail.com>
18  */
19 #include <ns3/core-module.h>
20 #include <ns3/spectrum-module.h>
21 #include <ns3/test.h>
22 
23 NS_LOG_COMPONENT_DEFINE("WaveformGeneratorTest");
24 
25 using namespace ns3;
26 
33 {
34  public:
42  WaveformGeneratorTestCase(double period, double dutyCycle, double stop);
43  ~WaveformGeneratorTestCase() override;
44 
45  private:
46  void DoRun() override;
47 
52  void TraceWave(Ptr<const Packet> newPkt);
53  double m_period;
54  double m_dutyCycle;
55  double m_stop;
56  int m_fails;
57 };
58 
59 void
61 {
62  if (Now().GetSeconds() > m_stop)
63  {
64  m_fails++;
65  }
66 }
67 
68 WaveformGeneratorTestCase::WaveformGeneratorTestCase(double period, double dutyCycle, double stop)
69  : TestCase("Check stop method"),
70  m_period(period),
71  m_dutyCycle(dutyCycle),
72  m_stop(stop),
73  m_fails(0)
74 {
75 }
76 
78 {
79 }
80 
81 void
83 {
84  Ptr<SpectrumValue> txPsd = MicrowaveOvenSpectrumValueHelper::CreatePowerSpectralDensityMwo1();
85 
86  SpectrumChannelHelper channelHelper = SpectrumChannelHelper::Default();
87  channelHelper.SetChannel("ns3::SingleModelSpectrumChannel");
88  Ptr<SpectrumChannel> channel = channelHelper.Create();
89 
90  Ptr<Node> n = CreateObject<Node>();
91 
92  WaveformGeneratorHelper waveformGeneratorHelper;
93  waveformGeneratorHelper.SetTxPowerSpectralDensity(txPsd);
94  waveformGeneratorHelper.SetChannel(channel);
95  waveformGeneratorHelper.SetPhyAttribute("Period", TimeValue(Seconds(m_period)));
96  waveformGeneratorHelper.SetPhyAttribute("DutyCycle", DoubleValue(m_dutyCycle));
97  NetDeviceContainer waveformGeneratorDevices = waveformGeneratorHelper.Install(n);
98 
99  Ptr<WaveformGenerator> wave = waveformGeneratorDevices.Get(0)
100  ->GetObject<NonCommunicatingNetDevice>()
101  ->GetPhy()
103 
104  wave->TraceConnectWithoutContext("TxStart",
106 
107  Simulator::Schedule(Seconds(1.0), &WaveformGenerator::Start, wave);
108  Simulator::Schedule(Seconds(m_stop), &WaveformGenerator::Stop, wave);
109 
110  Simulator::Stop(Seconds(3.0));
111  Simulator::Run();
112 
113  NS_TEST_ASSERT_MSG_EQ(m_fails, 0, "Wave started after the stop method was called");
114 
115  Simulator::Destroy();
116 }
117 
124 {
125  public:
127 };
128 
130  : TestSuite("waveform-generator", SYSTEM)
131 {
132  NS_LOG_INFO("creating WaveformGeneratorTestSuite");
133 
134  // Stop while wave is active
135  AddTestCase(new WaveformGeneratorTestCase(1.0, 0.5, 1.2), TestCase::QUICK);
136  // Stop after wave
137  AddTestCase(new WaveformGeneratorTestCase(1.0, 0.5, 1.7), TestCase::QUICK);
138 }
139 
double m_period
waveform period (seconds)
void DoRun() override
Implementation to actually run this TestCase.
void TraceWave(Ptr< const Packet > newPkt)
Trace if the waveform is active.
WaveformGeneratorTestCase(double period, double dutyCycle, double stop)
Constructor.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
This class implements a device which does not communicate, in the sense that it does not interact wit...
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
void SetChannel(std::string type, Ts &&... args)
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Create a Waveform generator, which can be used to inject specific noise in the channel.
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
void SetPhyAttribute(std::string name, const AttributeValue &v)
Simple SpectrumPhy implementation that sends customizable waveform.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
#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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:704
channel
Definition: third.py:88
static WaveformGeneratorTestSuite g_waveformGeneratorTestSuite
Static variable for test initialization.