A Discrete-Event Network Simulator
API
spectrum-interference-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 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: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #include <ns3/object.h>
22 #include <ns3/spectrum-interference.h>
23 #include <ns3/spectrum-error-model.h>
24 #include <ns3/log.h>
25 #include <ns3/test.h>
26 #include <ns3/simulator.h>
27 #include <ns3/packet.h>
28 #include <ns3/ptr.h>
29 #include <iostream>
30 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE ("SpectrumInterferenceTest");
34 
35 
36 
43 {
44 public:
52  SpectrumInterferenceTestCase (Ptr<SpectrumValue> s, uint32_t txBytes, bool rxCorrect, std::string name);
53  virtual ~SpectrumInterferenceTestCase ();
54  virtual void DoRun (void);
59  void RetrieveTestResult (SpectrumInterference* si);
60 
61 private:
63  uint32_t m_txBytes;
66 };
67 
68 
69 
70 SpectrumInterferenceTestCase::SpectrumInterferenceTestCase (Ptr<SpectrumValue> s, uint32_t txBytes, bool rxCorrect, std::string name)
71  : TestCase (name),
72  m_s (s),
73  m_txBytes (txBytes),
74  m_rxCorrectKnownOutcome (rxCorrect),
75  m_mySpectrumModel (s->GetSpectrumModel ())
76 {
77 }
78 
80 {
81 }
82 
83 
84 
85 void
87 {
88  Ptr<SpectrumValue> n = Create<SpectrumValue> (m_mySpectrumModel);
89  Ptr<SpectrumValue> i1 = Create<SpectrumValue> (m_mySpectrumModel);
90  Ptr<SpectrumValue> i2 = Create<SpectrumValue> (m_mySpectrumModel);
91  Ptr<SpectrumValue> i3 = Create<SpectrumValue> (m_mySpectrumModel);
92  Ptr<SpectrumValue> i4 = Create<SpectrumValue> (m_mySpectrumModel);
93 
94 
95  (*n)[0] = 5.000000000000e-19;
96  (*n)[1] = 4.545454545455e-19;
97 
98  (*i1)[0] = 5.000000000000e-18;
99  (*i2)[0] = 5.000000000000e-16;
100  (*i3)[0] = 1.581138830084e-16;
101  (*i4)[0] = 7.924465962306e-17;
102  (*i1)[1] = 1.437398936440e-18;
103  (*i2)[1] = 5.722388235428e-16;
104  (*i3)[1] = 7.204059965732e-17;
105  (*i4)[1] = 5.722388235428e-17;
106 
107 
109  si.SetErrorModel (CreateObject<ShannonSpectrumErrorModel> ());
111 
112  Time ts = Seconds (1);
113  Time ds = Seconds (1);
114  Time ti1 = Seconds (0);
115  Time di1 = Seconds (3);
116  Time ti2 = Seconds (0.7);
117  Time di2 = Seconds (1);
118  Time ti3 = Seconds (1.2);
119  Time di3 = Seconds (1);
120  Time ti4 = Seconds (1.5);
121  Time di4 = Seconds (0.1);
122 
123  Simulator::Schedule (ts, &SpectrumInterference::AddSignal, &si, m_s, ds);
124  Simulator::Schedule (ti1, &SpectrumInterference::AddSignal, &si, i1, di1);
125  Simulator::Schedule (ti2, &SpectrumInterference::AddSignal, &si, i2, di2);
126  Simulator::Schedule (ti3, &SpectrumInterference::AddSignal, &si, i3, di3);
127  Simulator::Schedule (ti4, &SpectrumInterference::AddSignal, &si, i4, di4);
128 
129  Ptr<Packet> p = Create<Packet> (m_txBytes);
130  Simulator::Schedule (ts, &SpectrumInterference::StartRx, &si, p, m_s);
131  Simulator::Schedule (ts + ds, &SpectrumInterferenceTestCase::RetrieveTestResult, this, &si);
132 
133  Simulator::Run ();
134  // the above will return and after RetrieveTestResults have
135  // been called and after all signals have expired
136  Simulator::Destroy ();
137 }
138 
139 
140 
141 void
143 {
144  NS_TEST_ASSERT_MSG_EQ (si->EndRx (), m_rxCorrectKnownOutcome, "no message");
145 }
146 
147 
148 
155 {
156 public:
158 };
159 
161  : TestSuite ("spectrum-interference", UNIT)
162 {
163 
164  NS_LOG_INFO ("creating SpectrumInterferenceTestSuite");
165 
167 
168  Bands bands;
169  struct BandInfo bi;
170 
171  bi.fl = 2.400e9;
172  bi.fc = 2.410e9;
173  bi.fh = 2.420e9;
174  bands.push_back (bi);
175 
176  bi.fl = 2.420e9;
177  bi.fc = 2.431e9;
178  bi.fh = 2.442e9;
179  bands.push_back (bi);
180 
181  m = Create<SpectrumModel> (bands);
182 
183 
184  double b; // max deliverable bytes
185 
186  const double e = 1e-5; // max tolerated relative error for
187  // deliverable bytes
188 
189  // Power Spectral Density of the signal of interest = [-46 -48] dBm;
190  Ptr<SpectrumValue> s1 = Create<SpectrumValue> (m);
191  (*s1)[0] = 1.255943215755e-15;
192  (*s1)[1] = 7.204059965732e-16;
193  b = 10067205.5632012;
194  AddTestCase (new SpectrumInterferenceTestCase (s1, 0, true, "sdBm = [-46 -48] tx bytes: 1"), TestCase::QUICK);
195  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * 0.5 + 0.5), true, "sdBm = [-46 -48] tx bytes: b*0.5"), TestCase::QUICK);
196  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * (1 - e) + 0.5), true, "sdBm = [-46 -48] tx bytes: b*(1-e)"), TestCase::QUICK);
197  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * (1 + e) + 0.5), false, "sdBm = [-46 -48] tx bytes: b*(1+e)"), TestCase::QUICK);
198  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * 1.5 + 0.5), false, "sdBm = [-46 -48] tx bytes: b*1.5"), TestCase::QUICK);
199  AddTestCase (new SpectrumInterferenceTestCase (s1, 0xffffffff, false, "sdBm = [-46 -48] tx bytes: 2^32-1"), TestCase::QUICK);
200 
201  // Power Spectral Density of the signal of interest = [-63 -61] dBm;
202  Ptr<SpectrumValue> s2 = Create<SpectrumValue> (m);
203  (*s2)[0] = 2.505936168136e-17;
204  (*s2)[1] = 3.610582885110e-17;
205  b = 882401.591840728;
206  AddTestCase (new SpectrumInterferenceTestCase (s2, 1, true, "sdBm = [-63 -61] tx bytes: 1"), TestCase::QUICK);
207  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * 0.5 + 0.5), true, "sdBm = [-63 -61] tx bytes: b*0.5"), TestCase::QUICK);
208  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * (1 - e) + 0.5), true, "sdBm = [-63 -61] tx bytes: b*(1-e)"), TestCase::QUICK);
209  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * (1 + e) + 0.5), false, "sdBm = [-63 -61] tx bytes: b*(1+e)"), TestCase::QUICK);
210  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * 1.5 + 0.5), false, "sdBm = [-63 -61] tx bytes: b*1.5"), TestCase::QUICK);
211  AddTestCase (new SpectrumInterferenceTestCase (s2, 0xffffffff, false, "sdBm = [-63 -61] tx bytes: 2^32-1"), TestCase::QUICK);
212 
213 }
214 
uint32_t m_rxCorrectKnownOutcome
extected Rx bytes
Ptr< SpectrumValue > m_s
Spectrum value.
virtual void DoRun(void)
Implementation to actually run this TestCase.
SpectrumInterferenceTestCase(Ptr< SpectrumValue > s, uint32_t txBytes, bool rxCorrect, std::string name)
Constructor.
uint32_t m_txBytes
number of bytes to transmit
void RetrieveTestResult(SpectrumInterference *si)
Retrieve the test results.
Ptr< const SpectrumModel > m_mySpectrumModel
Spectrum model pointer.
Spectrum Interference TestSuite.
This class implements a gaussian interference model, i.e., all incoming signals are added to the tota...
void SetNoisePowerSpectralDensity(Ptr< const SpectrumValue > noisePsd)
Set the Noise Power Spectral Density.
bool EndRx()
Notify that the RX attempt has ended.
void SetErrorModel(Ptr< SpectrumErrorModel > e)
Set the SpectrumErrorModel to be used.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
#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:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::vector< BandInfo > Bands
Container of BandInfo.
static SpectrumInterferenceTestSuite spectrumInterferenceTestSuite
Static variable for test initialization.
The building block of a SpectrumModel.
double fc
center frequency
double fl
lower limit of subband
double fh
upper limit of subband