1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3 * Copyright (c) 2011 CTTC
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Luis Pacheco <luisbelem@gmail.com>
19 */
20 #include <ns3/core-module.h>
21 #include <ns3/test.h>
22 #include <ns3/spectrum-module.h>
23
24
25 NS_LOG_COMPONENT_DEFINE ("WaveformGeneratorTest");
26
27 using namespace ns3;
28
29
30
31 class WaveformGeneratorTestCase : public TestCase
32 {
33 public:
34 WaveformGeneratorTestCase (double period, double dutyCycle, double stop);
35 virtual ~WaveformGeneratorTestCase ();
36
37 private:
38 virtual void DoRun (void);
39
40 void TraceWave (Ptr<const Packet> newPkt);
41 double m_period;
42 double m_dutyCycle;
43 double m_stop;
44 int m_fails;
45 };
46
47 void
TraceWave(Ptr<const Packet> newPkt)48 WaveformGeneratorTestCase::TraceWave (Ptr<const Packet> newPkt)
49 {
50 if (Now ().GetSeconds () > m_stop)
51 {
52 m_fails++;
53 }
54 }
55
WaveformGeneratorTestCase(double period,double dutyCycle,double stop)56 WaveformGeneratorTestCase::WaveformGeneratorTestCase (double period, double dutyCycle, double stop)
57 : TestCase ("Check stop method"),
58 m_period (period),
59 m_dutyCycle (dutyCycle),
60 m_stop (stop),
61 m_fails (0)
62 {
63 }
64
~WaveformGeneratorTestCase()65 WaveformGeneratorTestCase::~WaveformGeneratorTestCase ()
66 {
67 }
68
69
70 void
DoRun(void)71 WaveformGeneratorTestCase::DoRun (void)
72 {
73 Ptr<SpectrumValue> txPsd = MicrowaveOvenSpectrumValueHelper::CreatePowerSpectralDensityMwo1 ();
74
75 SpectrumChannelHelper channelHelper = SpectrumChannelHelper::Default ();
76 channelHelper.SetChannel ("ns3::SingleModelSpectrumChannel");
77 Ptr<SpectrumChannel> channel = channelHelper.Create ();
78
79 Ptr<Node> n = CreateObject<Node> ();
80
81 WaveformGeneratorHelper waveformGeneratorHelper;
82 waveformGeneratorHelper.SetTxPowerSpectralDensity (txPsd);
83 waveformGeneratorHelper.SetChannel (channel);
84 waveformGeneratorHelper.SetPhyAttribute ("Period", TimeValue (Seconds (m_period)));
85 waveformGeneratorHelper.SetPhyAttribute ("DutyCycle", DoubleValue (m_dutyCycle));
86 NetDeviceContainer waveformGeneratorDevices = waveformGeneratorHelper.Install (n);
87
88 Ptr<WaveformGenerator> wave = waveformGeneratorDevices.Get (0)->GetObject<NonCommunicatingNetDevice> ()->GetPhy ()->GetObject<WaveformGenerator> ();
89
90 wave->TraceConnectWithoutContext ("TxStart", MakeCallback (&WaveformGeneratorTestCase::TraceWave,this));
91
92 Simulator::Schedule (Seconds (1.0), &WaveformGenerator::Start, wave);
93 Simulator::Schedule (Seconds (m_stop), &WaveformGenerator::Stop, wave);
94
95 Simulator::Stop (Seconds (3.0));
96 Simulator::Run ();
97
98 NS_TEST_ASSERT_MSG_EQ (m_fails, 0, "Wave started after the stop method was called");
99
100 Simulator::Destroy ();
101 }
102
103
104 class WaveformGeneratorTestSuite : public TestSuite
105 {
106 public:
107 WaveformGeneratorTestSuite ();
108 };
109
WaveformGeneratorTestSuite()110 WaveformGeneratorTestSuite::WaveformGeneratorTestSuite ()
111 : TestSuite ("waveform-generator", SYSTEM)
112 {
113 NS_LOG_INFO ("creating WaveformGeneratorTestSuite");
114
115 // Stop while wave is active
116 AddTestCase (new WaveformGeneratorTestCase (1.0, 0.5, 1.2), TestCase::QUICK);
117 // Stop after wave
118 AddTestCase (new WaveformGeneratorTestCase (1.0, 0.5, 1.7), TestCase::QUICK);
119 }
120
121 static WaveformGeneratorTestSuite g_waveformGeneratorTestSuite;
122