A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
uan-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 University of Washington
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: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #include "ns3/uan-net-device.h"
22 #include "ns3/uan-channel.h"
23 #include "ns3/uan-mac-aloha.h"
24 #include "ns3/uan-phy-gen.h"
25 #include "ns3/uan-transducer-hd.h"
26 #include "ns3/uan-prop-model-ideal.h"
27 #include "ns3/constant-position-mobility-model.h"
28 #include "ns3/simulator.h"
29 #include "ns3/test.h"
30 #include "ns3/node.h"
31 #include "ns3/object-factory.h"
32 #include "ns3/pointer.h"
33 #include "ns3/callback.h"
34 
35 using namespace ns3;
36 
37 class UanTest : public TestCase
38 {
39 public:
40  UanTest ();
41 
42  virtual void DoRun (void);
43 private:
44  Ptr<UanNetDevice> CreateNode (Vector pos, Ptr<UanChannel> chan);
45  bool DoPhyTests ();
46  uint32_t DoOnePhyTest (Time t1, Time t2, uint32_t r1, uint32_t r2, Ptr<UanPropModel> prop, uint32_t mode1 = 0, uint32_t mode2 = 0);
47  bool RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender);
48  void SendOnePacket (Ptr<UanNetDevice> dev, uint32_t mode);
50  uint32_t m_bytesRx;
51 
52 };
53 
54 
56 {
57 
58 }
59 
60 bool
61 UanTest::RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender)
62 {
63  m_bytesRx += pkt->GetSize ();
64  return true;
65 }
66 void
68 {
69  Ptr<Packet> pkt = Create<Packet> (17);
70  dev->Send (pkt, dev->GetBroadcast (), mode);
71 
72 }
75 {
76 
77 
79  Ptr<Node> node = CreateObject<Node> ();
80  Ptr<UanNetDevice> dev = CreateObject<UanNetDevice> ();
81  Ptr<UanMacAloha> mac = CreateObject<UanMacAloha> ();
82  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
83 
84  Ptr<UanTransducerHd> trans = CreateObject<UanTransducerHd> ();
85 
86  mobility->SetPosition (pos);
87  node->AggregateObject (mobility);
88  mac->SetAddress (UanAddress::Allocate ());
89 
90  dev->SetPhy (phy);
91  dev->SetMac (mac);
92  dev->SetChannel (chan);
93  dev->SetTransducer (trans);
94  node->AddDevice (dev);
95 
96  return dev;
97 }
98 
99 
100 uint32_t
102  Time txTime2,
103  uint32_t r1,
104  uint32_t r2,
105  Ptr<UanPropModel> prop,
106  uint32_t mode1,
107  uint32_t mode2)
108 {
109 
110  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
111  channel->SetAttribute ("PropagationModel", PointerValue (prop));
112 
113  Ptr<UanNetDevice> dev0 = CreateNode (Vector (r1,50,50), channel);
114  Ptr<UanNetDevice> dev1 = CreateNode (Vector (0,50,50), channel);
115  Ptr<UanNetDevice> dev2 = CreateNode (Vector (r1 + r2, 50, 50), channel);
116 
118 
119  Simulator::Schedule (txTime1, &UanTest::SendOnePacket, this, dev1, mode1);
120  Simulator::Schedule (txTime2, &UanTest::SendOnePacket, this, dev2, mode2);
121 
122  m_bytesRx = 0;
123  Simulator::Stop (Seconds (20.0));
124  Simulator::Run ();
125  Simulator::Destroy ();
126 
127  return m_bytesRx;
128 }
129 
130 
131 bool
133 {
134  // Phy Gen / Default PER / Default SINR
135  UanModesList mList;
136  UanTxMode mode = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode");
137  mList.AppendMode (UanTxMode (mode));
138  Ptr<UanPhyPerGenDefault> perDef = CreateObject<UanPhyPerGenDefault> ();
139  Ptr<UanPhyCalcSinrDefault> sinrDef = CreateObject<UanPhyCalcSinrDefault> ();
140  m_phyFac.SetTypeId ("ns3::UanPhyGen");
141  m_phyFac.Set ("PerModel", PointerValue (perDef));
142  m_phyFac.Set ("SinrModel", PointerValue (sinrDef));
143  m_phyFac.Set ("SupportedModes", UanModesListValue (mList));
144 
145  Ptr<UanPropModelIdeal> prop = CreateObject<UanPropModelIdeal> ();
146 
147  // No collision (Get 2 packets)
148  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.001), 50, 50, prop),
149  34, "Should have received 34 bytes from 2 disjoint packets");
150 
151  // Collision (Lose both packets)
152  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop),
153  0, "Expected collision resulting in loss of both packets");
154 
155 
156  // Phy Gen / FH-FSK SINR check
157 
158  Ptr<UanPhyCalcSinrFhFsk> sinrFhfsk = CreateObject <UanPhyCalcSinrFhFsk> ();
159  m_phyFac.Set ("PerModel", PointerValue (perDef));
160  m_phyFac.Set ("SinrModel", PointerValue (sinrFhfsk));
161  m_phyFac.Set ("SupportedModes", UanModesListValue (mList));
162 
163 #ifdef UAN_PROP_BH_INSTALLED
164  Ptr<UanPropModelBh> prop = CreateObject<UanPropModelBh> ();
165 #endif // UAN_PROP_BH_INSTALLED
166 
167  // No collision (Get 2 packets)
168  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.001), 50, 50, prop),
169  34, "Should have received 34 bytes from 2 disjoint packets");
170 
171  // Should correctly receive first arriving packet
172  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (1.0126), 50, 50, prop),
173  17, "Should have recieved 17 bytes from first arriving packet");
174 
175  // Packets should collide and both be lost
176  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (1.0 + 7.01 * (13.0 / 80.0)), 50, 50, prop),
177  0, "Packets should collide, but received data");
178 
179  // Phy Dual
180  UanTxMode mode00 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode00");
181  UanTxMode mode10 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode10");
182  UanTxMode mode20 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode20");
183  UanTxMode mode01 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode01");
184  UanTxMode mode11 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode11");
185  UanTxMode mode21 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode21");
186 
187  UanModesList m0;
188  m0.AppendMode (mode00);
189  m0.AppendMode (mode10);
190  m0.AppendMode (mode20);
192  m1.AppendMode (mode01);
193  m1.AppendMode (mode11);
194  m1.AppendMode (mode21);
195 
196  m_phyFac = ObjectFactory ();
197  m_phyFac.SetTypeId ("ns3::UanPhyDual");
198  m_phyFac.Set ("SupportedModesPhy1", UanModesListValue (m0));
199  m_phyFac.Set ("SupportedModesPhy2", UanModesListValue (m1));
200 
201  // No collision (Get 2 packets)
202  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.01), 50, 50, prop),
203  34, "Expected no collision");
204 
205  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 0),
206  0, "Expected collision with both packets lost");
207 
208  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 2),
209  17, "Expected collision with only one packets lost");
210 
211  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 5),
212  34, "Expected no collision");
213 
214  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 2, 3),
215  34, "Expected no collision");
216 
217  return false;
218 }
219 
220 void
222 {
223 
224  Ptr<UanPhyPerUmodem> per = CreateObject<UanPhyPerUmodem> ();
225  Ptr<Packet> pkt = Create<Packet> (1000);
226  double error = per->CalcPer (pkt, 9, UanPhyGen::GetDefaultModes ()[0]);
227  NS_TEST_ASSERT_MSG_EQ_TOL (error, 0.539, 0.001, "Got PER outside of tolerance");
228 
229 #ifdef UAN_PROP_BH_INSTALLED
230  // Correct DB lookup for BH prop.
231  Ptr<UanPropModelBh> propBh = CreateObject<UanPropModelBh> ();
232  BellhopResp resp = propBh->GetResp (10000, 50, 50, 1000);
233 
234  NS_TEST_ASSERT_MSG_EQ_TOL (resp.GetPathLossDb (), -44.1753, 0.001, "Got BH Pathloss outside of tolerance");
235 
236  NS_TEST_ASSERT_MSG_EQ_TOL (resp.GetPdp ().GetTap (4).GetAmp (), 0.14159, 0.001, "Got BH arrival outside of tolerance");
237 
238 #endif // UAN_PROP_BH_INSTALLED
239 
240  DoPhyTests ();
241 }
242 
243 
244 class UanTestSuite : public TestSuite
245 {
246 public:
247  UanTestSuite ();
248 };
249 
251  : TestSuite ("devices-uan", UNIT)
252 {
253  AddTestCase (new UanTest, TestCase::QUICK);
254 }
255 
257 
258 
259 
260 
uint32_t m_bytesRx
Definition: uan-test.cc:50
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
A suite of tests to run.
Definition: test.h:1105
void SetTypeId(TypeId tid)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
Container for UanTxModes.
Definition: uan-tx-mode.h:255
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
encapsulates test code
Definition: test.h:929
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: uan-test.cc:221
virtual Address GetBroadcast(void) const
a 3d vector
Definition: vector.h:31
a polymophic address class
Definition: address.h:86
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:207
Ptr< Object > Create(void) const
void SendOnePacket(Ptr< UanNetDevice > dev, uint32_t mode)
Definition: uan-test.cc:67
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:41
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
Ptr< UanNetDevice > CreateNode(Vector pos, Ptr< UanChannel > chan)
Definition: uan-test.cc:74
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:355
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Definition: uan-test.cc:61
ObjectFactory m_phyFac
Definition: uan-test.cc:49
uint32_t DoOnePhyTest(Time t1, Time t2, uint32_t r1, uint32_t r2, Ptr< UanPropModel > prop, uint32_t mode1=0, uint32_t mode2=0)
Definition: uan-test.cc:101
hold objects of type Ptr
Definition: pointer.h:33
static UanTestSuite g_uanTestSuite
Definition: uan-test.cc:256
virtual double CalcPer(Ptr< Packet > pkt, double sinrDb, UanTxMode mode)
Calculate the packet error probability based on SINR at the receiver and a tx mode.
Definition: uan-phy-gen.cc:282
void Set(std::string name, const AttributeValue &value)
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
Base class for UAN Phy models.
Definition: uan-phy.h:175
instantiate subclasses of ns3::Object.
bool DoPhyTests()
Definition: uan-test.cc:132
void AppendMode(UanTxMode mode)
Add mode to this list.
Definition: uan-tx-mode.cc:232
UanTest()
Definition: uan-test.cc:55
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176
Attribute Value class for UanTxModes.