A Discrete-Event Network Simulator
API
python-unit-tests.py
Go to the documentation of this file.
1 # Copyright (C) 2008-2011 INESC Porto
2 
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
18 
19 import unittest
20 from ns.core import Simulator, Seconds, Config, int64x64_t
21 import ns.core
22 import ns.network
23 import ns.internet
24 import ns.mobility
25 import ns.csma
26 import ns.applications
27 
28 
29 
30 class TestSimulator(unittest.TestCase):
31 
39 
40  def testScheduleNow(self):
41  """! Test schedule now
42  @param self this object
43  @return none
44  """
45  def callback(args):
46  """! Callback function
47  @param args arguments
48  return none
49  """
50  self._args_received = args
51  self._cb_time = Simulator.Now()
52  Simulator.Destroy()
53  self._args_received = None
54  self._cb_time = None
55  Simulator.ScheduleNow(callback, "args")
56  Simulator.Run()
57  self.assertEqual(self._args_received, "args")
58  self.assertEqual(self._cb_time.GetSeconds(), 0.0)
59 
60  def testSchedule(self):
61  """! Test schedule
62  @param self this object
63  @return none
64  """
65  def callback(args):
66  """! Callback function
67  @param args arguments
68  @return none
69  """
70  self._args_received = args
71  self._cb_time = Simulator.Now()
72  Simulator.Destroy()
73  self._args_received = None
74  self._cb_time = None
75  Simulator.Schedule(Seconds(123), callback, "args")
76  Simulator.Run()
77  self.assertEqual(self._args_received, "args")
78  self.assertEqual(self._cb_time.GetSeconds(), 123.0)
79 
81  """! Test schedule destroy
82  @param self this object
83  @return none
84  """
85  def callback(args):
86  """! Callback function
87  @param args
88  @return none
89  """
90  self._args_received = args
91  self._cb_time = Simulator.Now()
92  Simulator.Destroy()
93  self._args_received = None
94  self._cb_time = None
95  def null(): pass
96  Simulator.Schedule(Seconds(123), null)
97  Simulator.ScheduleDestroy(callback, "args")
98  Simulator.Run()
99  Simulator.Destroy()
100  self.assertEqual(self._args_received, "args")
101  self.assertEqual(self._cb_time.GetSeconds(), 123.0)
102 
104  """! Test schedule with context
105  @param self this object
106  @return none
107  """
108  def callback(context, args):
109  """! Callback
110  @param context the cntet
111  @param args the arguments
112  @return none
113  """
114  self._context_received = context
115  self._args_received = args
116  self._cb_time = Simulator.Now()
117  Simulator.Destroy()
118  self._args_received = None
119  self._cb_time = None
120  self._context_received = None
121  Simulator.ScheduleWithContext(54321, Seconds(123), callback, "args")
122  Simulator.Run()
123  self.assertEqual(self._context_received, 54321)
124  self.assertEqual(self._args_received, "args")
125  self.assertEqual(self._cb_time.GetSeconds(), 123.0)
126 
128  """! Test time comparison
129  @param self this object
130  @return none
131  """
132  self.assertTrue(Seconds(123) == Seconds(123))
133  self.assertTrue(Seconds(123) >= Seconds(123))
134  self.assertTrue(Seconds(123) <= Seconds(123))
135  self.assertTrue(Seconds(124) > Seconds(123))
136  self.assertTrue(Seconds(123) < Seconds(124))
137 
139  """! Test numeric operations
140  @param self this object
141  @return none
142  """
143  self.assertEqual(Seconds(10) + Seconds(5), Seconds(15))
144  self.assertEqual(Seconds(10) - Seconds(5), Seconds(5))
145 
146  v1 = int64x64_t(5.0)*int64x64_t(10)
147  self.assertEqual(v1, int64x64_t(50))
148 
149  def testConfig(self):
150  """! Test configuration
151  @param self this object
152  @return none
153  """
154  Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
155  # hm.. no Config.Get?
156 
157  def testSocket(self):
158  """! Test socket
159  @param self
160  @return none
161  """
162  node = ns.network.Node()
163  internet = ns.internet.InternetStackHelper()
164  internet.Install(node)
165  self._received_packet = None
166 
167  def rx_callback(socket):
168  """! Receive Callback
169  @param socket the socket to receive
170  @return none
171  """
172  assert self._received_packet is None
173  self._received_packet = socket.Recv()
174 
175  sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
176  sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
177  sink.SetRecvCallback(rx_callback)
178 
179  source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
180  source.SendTo(ns.network.Packet(19), 0, ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80))
181 
182  Simulator.Run()
183  self.assertTrue(self._received_packet is not None)
184  self.assertEqual(self._received_packet.GetSize(), 19)
185 
186 
187  def testAttributes(self):
188  """! Test attributes function
189  @param self this object
190  @return none
191  """
192  # Templated class DropTailQueue<Packet> in C++
193  queue = ns.network.DropTailQueue__Ns3Packet()
194  queueSizeValue = ns.network.QueueSizeValue (ns.network.QueueSize ("500p"))
195  queue.SetAttribute("MaxSize", queueSizeValue)
196 
197  limit = ns.network.QueueSizeValue()
198  queue.GetAttribute("MaxSize", limit)
199  self.assertEqual(limit.Get(), ns.network.QueueSize ("500p"))
200 
201 
202  mobility = ns.mobility.RandomWaypointMobilityModel()
203  ptr = ns.core.PointerValue()
204  mobility.GetAttribute("PositionAllocator", ptr)
205  self.assertEqual(ptr.GetObject(), None)
206 
207  pos = ns.mobility.ListPositionAllocator()
208  mobility.SetAttribute("PositionAllocator", ns.core.PointerValue(pos))
209 
210  ptr = ns.core.PointerValue()
211  mobility.GetAttribute("PositionAllocator", ptr)
212  self.assertTrue(ptr.GetObject() is not None)
213 
214  def testIdentity(self):
215  """! Test identify
216  @param self this object
217  @return none
218  """
219  csma = ns.csma.CsmaNetDevice()
220  channel = ns.csma.CsmaChannel()
221  csma.Attach(channel)
222 
223  c1 = csma.GetChannel()
224  c2 = csma.GetChannel()
225 
226  self.assertTrue(c1 is c2)
227 
228  def testTypeId(self):
229  """! Test type ID
230  @param self this object
231  @return none
232  """
233  typeId1 = ns.core.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
234  self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
235 
236  self.assertRaises(KeyError, ns.core.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
237 
238  def testCommandLine(self):
239  """! Test command line
240  @param self this object
241  @return none
242  """
243  cmd = ns.core.CommandLine()
244  cmd.AddValue("Test1", "this is a test option")
245  cmd.AddValue("Test2", "this is a test option")
246  cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
247  cmd.Test1 = None
248  cmd.Test2 = None
249  cmd.test_xxx = None
250  class Foo:
251  pass
252  foo = Foo()
253  foo.test_foo = None
254  cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
255 
256  cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
257 
258  self.assertEqual(cmd.Test1, "value1")
259  self.assertEqual(cmd.Test2, "value2")
260  self.assertEqual(cmd.test_xxx, "123")
261  self.assertEqual(foo.test_foo, "xpto")
262 
263  def testSubclass(self):
264  """! Test subclass
265  @param self this object
266  @return none
267  """
268 
269  class MyNode(ns.network.Node):
270  def __init__(self):
271  """! Initializer
272  @param self this object
273  @return none
274  """
275  super(MyNode, self).__init__()
276 
277  node = MyNode()
278 
279 
280 if __name__ == '__main__':
281  unittest.main()
python-unit-tests.TestSimulator._cb_time
_cb_time
current time
Definition: python-unit-tests.py:51
python-unit-tests.TestSimulator.testTimeComparison
def testTimeComparison(self)
Test time comparison.
Definition: python-unit-tests.py:127
ns3::GetSize
uint32_t GetSize(Ptr< const Packet > packet, const WifiMacHeader *hdr, bool isAmpdu)
Return the total size of the packet after WifiMacHeader and FCS trailer have been added.
Definition: wifi-utils.cc:129
python-unit-tests.TestSimulator._received_packet
_received_packet
received packet
Definition: python-unit-tests.py:165
python-unit-tests.TestSimulator.testSocket
def testSocket(self)
Test socket.
Definition: python-unit-tests.py:157
python-unit-tests.TestSimulator.testAttributes
def testAttributes(self)
Test attributes function.
Definition: python-unit-tests.py:187
python-unit-tests.TestSimulator.testIdentity
def testIdentity(self)
Test identify.
Definition: python-unit-tests.py:214
python-unit-tests.TestSimulator.testScheduleWithContext
def testScheduleWithContext(self)
Test schedule with context.
Definition: python-unit-tests.py:103
python-unit-tests.TestSimulator.testCommandLine
def testCommandLine(self)
Test command line.
Definition: python-unit-tests.py:238
python-unit-tests.TestSimulator.testTypeId
def testTypeId(self)
Test type ID.
Definition: python-unit-tests.py:228
python-unit-tests.TestSimulator.testTimeNumericOperations
def testTimeNumericOperations(self)
Test numeric operations.
Definition: python-unit-tests.py:138
python-unit-tests.TestSimulator
TestSimulator class.
Definition: python-unit-tests.py:30
python-unit-tests.TestSimulator.testScheduleDestroy
def testScheduleDestroy(self)
Test schedule destroy.
Definition: python-unit-tests.py:80
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
python-unit-tests.TestSimulator.testSubclass
def testSubclass(self)
Test subclass.
Definition: python-unit-tests.py:263
python-unit-tests.TestSimulator._context_received
_context_received
context
Definition: python-unit-tests.py:114
python-unit-tests.TestSimulator._args_received
_args_received
args
Definition: python-unit-tests.py:50
python-unit-tests.TestSimulator.testScheduleNow
def testScheduleNow(self)
Test schedule now.
Definition: python-unit-tests.py:40
python-unit-tests.TestSimulator.testSchedule
def testSchedule(self)
Test schedule.
Definition: python-unit-tests.py:60
python-unit-tests.TestSimulator.testConfig
def testConfig(self)
Test configuration.
Definition: python-unit-tests.py:149