A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
38 {
39 public:
40  SpectrumInterferenceTestCase (Ptr<SpectrumValue> s, uint32_t txBytes, bool rxCorrect, std::string name);
41  virtual ~SpectrumInterferenceTestCase ();
42  virtual void DoRun (void);
43  void RetrieveTestResult (SpectrumInterference* si);
44 
45 private:
47  uint32_t m_txBytes;
50 };
51 
52 
53 
54 SpectrumInterferenceTestCase::SpectrumInterferenceTestCase (Ptr<SpectrumValue> s, uint32_t txBytes, bool rxCorrect, std::string name)
55  : TestCase (name),
56  m_s (s),
57  m_txBytes (txBytes),
58  m_rxCorrectKnownOutcome (rxCorrect),
59  m_mySpectrumModel (s->GetSpectrumModel ())
60 {
61 }
62 
64 {
65 }
66 
67 
68 
69 void
71 {
72  Ptr<SpectrumValue> n = Create<SpectrumValue> (m_mySpectrumModel);
73  Ptr<SpectrumValue> i1 = Create<SpectrumValue> (m_mySpectrumModel);
74  Ptr<SpectrumValue> i2 = Create<SpectrumValue> (m_mySpectrumModel);
75  Ptr<SpectrumValue> i3 = Create<SpectrumValue> (m_mySpectrumModel);
76  Ptr<SpectrumValue> i4 = Create<SpectrumValue> (m_mySpectrumModel);
77 
78 
79  (*n)[0] = 5.000000000000e-19;
80  (*n)[1] = 4.545454545455e-19;
81 
82  (*i1)[0] = 5.000000000000e-18;
83  (*i2)[0] = 5.000000000000e-16;
84  (*i3)[0] = 1.581138830084e-16;
85  (*i4)[0] = 7.924465962306e-17;
86  (*i1)[1] = 1.437398936440e-18;
87  (*i2)[1] = 5.722388235428e-16;
88  (*i3)[1] = 7.204059965732e-17;
89  (*i4)[1] = 5.722388235428e-17;
90 
91 
93  si.SetErrorModel (CreateObject<ShannonSpectrumErrorModel> ());
95 
96  Time ts = Seconds (1);
97  Time ds = Seconds (1);
98  Time ti1 = Seconds (0);
99  Time di1 = Seconds (3);
100  Time ti2 = Seconds (0.7);
101  Time di2 = Seconds (1);
102  Time ti3 = Seconds (1.2);
103  Time di3 = Seconds (1);
104  Time ti4 = Seconds (1.5);
105  Time di4 = Seconds (0.1);
106 
107  Simulator::Schedule (ts, &SpectrumInterference::AddSignal, &si, m_s, ds);
108  Simulator::Schedule (ti1, &SpectrumInterference::AddSignal, &si, i1, di1);
109  Simulator::Schedule (ti2, &SpectrumInterference::AddSignal, &si, i2, di2);
110  Simulator::Schedule (ti3, &SpectrumInterference::AddSignal, &si, i3, di3);
111  Simulator::Schedule (ti4, &SpectrumInterference::AddSignal, &si, i4, di4);
112 
113  Ptr<Packet> p = Create<Packet> (m_txBytes);
114  Simulator::Schedule (ts, &SpectrumInterference::StartRx, &si, p, m_s);
115  Simulator::Schedule (ts + ds, &SpectrumInterferenceTestCase::RetrieveTestResult, this, &si);
116 
117  Simulator::Run ();
118  // the above will return and after RetrieveTestResults have
119  // been called and after all signals have expired
120  Simulator::Destroy ();
121 }
122 
123 
124 
125 void
127 {
128  NS_TEST_ASSERT_MSG_EQ (si->EndRx (), m_rxCorrectKnownOutcome, "no message");
129 }
130 
131 
132 
134 {
135 public:
137 };
138 
140  : TestSuite ("spectrum-interference", UNIT)
141 {
142 
143  NS_LOG_INFO ("creating SpectrumInterferenceTestSuite");
144 
146 
147  Bands bands;
148  struct BandInfo bi;
149 
150  bi.fl = 2.400e9;
151  bi.fc = 2.410e9;
152  bi.fh = 2.420e9;
153  bands.push_back (bi);
154 
155  bi.fl = 2.420e9;
156  bi.fc = 2.431e9;
157  bi.fh = 2.442e9;
158  bands.push_back (bi);
159 
160  m = Create<SpectrumModel> (bands);
161 
162 
163  double b; // max deliverable bytes
164 
165  const double e = 1e-5; // max tolerated relative error for
166  // deliverable bytes
167 
168  // Power Spectral Density of the signal of interest = [-46 -48] dBm;
169  Ptr<SpectrumValue> s1 = Create<SpectrumValue> (m);
170  (*s1)[0] = 1.255943215755e-15;
171  (*s1)[1] = 7.204059965732e-16;
172  b = 10067205.5632012;
173  AddTestCase (new SpectrumInterferenceTestCase (s1, 0, true, "sdBm = [-46 -48] tx bytes: 1"), TestCase::QUICK);
174  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * 0.5 + 0.5), true, "sdBm = [-46 -48] tx bytes: b*0.5"), TestCase::QUICK);
175  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * (1 - e) + 0.5), true, "sdBm = [-46 -48] tx bytes: b*(1-e)"), TestCase::QUICK);
176  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * (1 + e) + 0.5), false, "sdBm = [-46 -48] tx bytes: b*(1+e)"), TestCase::QUICK);
177  AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * 1.5 + 0.5), false, "sdBm = [-46 -48] tx bytes: b*1.5"), TestCase::QUICK);
178  AddTestCase (new SpectrumInterferenceTestCase (s1, 0xffffffff, false, "sdBm = [-46 -48] tx bytes: 2^32-1"), TestCase::QUICK);
179 
180  // Power Spectral Density of the signal of interest = [-63 -61] dBm;
181  Ptr<SpectrumValue> s2 = Create<SpectrumValue> (m);
182  (*s2)[0] = 2.505936168136e-17;
183  (*s2)[1] = 3.610582885110e-17;
184  b = 882401.591840728;
185  AddTestCase (new SpectrumInterferenceTestCase (s2, 1, true, "sdBm = [-63 -61] tx bytes: 1"), TestCase::QUICK);
186  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * 0.5 + 0.5), true, "sdBm = [-63 -61] tx bytes: b*0.5"), TestCase::QUICK);
187  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * (1 - e) + 0.5), true, "sdBm = [-63 -61] tx bytes: b*(1-e)"), TestCase::QUICK);
188  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * (1 + e) + 0.5), false, "sdBm = [-63 -61] tx bytes: b*(1+e)"), TestCase::QUICK);
189  AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * 1.5 + 0.5), false, "sdBm = [-63 -61] tx bytes: b*1.5"), TestCase::QUICK);
190  AddTestCase (new SpectrumInterferenceTestCase (s2, 0xffffffff, false, "sdBm = [-63 -61] tx bytes: 2^32-1"), TestCase::QUICK);
191 
192 }
193 
void RetrieveTestResult(SpectrumInterference *si)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
A suite of tests to run.
Definition: test.h:1333
void SetNoisePowerSpectralDensity(Ptr< const SpectrumValue > noisePsd)
Set the Noise Power Spectral Density.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
encapsulates test code
Definition: test.h:1147
SpectrumInterferenceTestCase(Ptr< SpectrumValue > s, uint32_t txBytes, bool rxCorrect, std::string name)
std::vector< BandInfo > Bands
Container of BandInfo.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:297
double fc
center frequency
#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:161
double fl
lower limit of subband
bool EndRx()
Notify that the RX attempt has ended.
This class implements a gaussian interference model, i.e., all incoming signals are added to the tota...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetErrorModel(Ptr< SpectrumErrorModel > e)
Set the SpectrumErrorModel to be used.
double fh
upper limit of subband
static SpectrumInterferenceTestSuite spectrumInterferenceTestSuite
The building block of a SpectrumModel.
Ptr< const SpectrumModel > m_mySpectrumModel