A Discrete-Event Network Simulator
API
python-unit-tests.py
Go to the documentation of this file.
1 import unittest
2 from ns.core import Simulator, Seconds, Config, int64x64_t
3 import ns.core
4 import ns.network
5 import ns.internet
6 import ns.mobility
7 import ns.csma
8 import ns.applications
9 
10 
11 class TestSimulator(unittest.TestCase):
12 
13  def testScheduleNow(self):
14  def callback(args):
15  self._args_received = args
16  self._cb_time = Simulator.Now()
17  Simulator.Destroy()
18  self._args_received = None
19  self._cb_time = None
20  Simulator.ScheduleNow(callback, "args")
21  Simulator.Run()
22  self.assertEqual(self._args_received, "args")
23  self.assertEqual(self._cb_time.GetSeconds(), 0.0)
24 
25  def testSchedule(self):
26  def callback(args):
27  self._args_received = args
28  self._cb_time = Simulator.Now()
29  Simulator.Destroy()
30  self._args_received = None
31  self._cb_time = None
32  Simulator.Schedule(Seconds(123), callback, "args")
33  Simulator.Run()
34  self.assertEqual(self._args_received, "args")
35  self.assertEqual(self._cb_time.GetSeconds(), 123.0)
36 
38  def callback(args):
39  self._args_received = args
40  self._cb_time = Simulator.Now()
41  Simulator.Destroy()
42  self._args_received = None
43  self._cb_time = None
44  def null(): pass
45  Simulator.Schedule(Seconds(123), null)
46  Simulator.ScheduleDestroy(callback, "args")
47  Simulator.Run()
48  Simulator.Destroy()
49  self.assertEqual(self._args_received, "args")
50  self.assertEqual(self._cb_time.GetSeconds(), 123.0)
51 
53  def callback(context, args):
54  self._context_received = context
55  self._args_received = args
56  self._cb_time = Simulator.Now()
57  Simulator.Destroy()
58  self._args_received = None
59  self._cb_time = None
60  self._context_received = None
61  Simulator.ScheduleWithContext(54321, Seconds(123), callback, "args")
62  Simulator.Run()
63  self.assertEqual(self._context_received, 54321)
64  self.assertEqual(self._args_received, "args")
65  self.assertEqual(self._cb_time.GetSeconds(), 123.0)
66 
67  def testTimeComparison(self):
68  self.assert_(Seconds(123) == Seconds(123))
69  self.assert_(Seconds(123) >= Seconds(123))
70  self.assert_(Seconds(123) <= Seconds(123))
71  self.assert_(Seconds(124) > Seconds(123))
72  self.assert_(Seconds(123) < Seconds(124))
73 
75  self.assertEqual(Seconds(10) + Seconds(5), Seconds(15))
76  self.assertEqual(Seconds(10) - Seconds(5), Seconds(5))
77 
78  v1 = int64x64_t(5.0)*int64x64_t(10)
79  self.assertEqual(v1, int64x64_t(50))
80 
81  def testConfig(self):
82  Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
83  # hm.. no Config.Get?
84 
85  def testSocket(self):
86  node = ns.network.Node()
87  internet = ns.internet.InternetStackHelper()
88  internet.Install(node)
89  self._received_packet = None
90 
91  def rx_callback(socket):
92  assert self._received_packet is None
93  self._received_packet = socket.Recv()
94 
95  sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
96  sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
97  sink.SetRecvCallback(rx_callback)
98 
99  source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
100  source.SendTo(ns.network.Packet(19), 0, ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80))
101 
102  Simulator.Run()
103  self.assert_(self._received_packet is not None)
104  self.assertEqual(self._received_packet.GetSize(), 19)
105 
106 
107  def testAttributes(self):
108  ##
109  ## Yes, I know, the GetAttribute interface for Python is
110  ## horrible, we should fix this soon, I hope.
111  ##
112  queue = ns.network.DropTailQueue()
113 
114  queue.SetAttribute("MaxPackets", ns.core.UintegerValue(123456))
115 
116  limit = ns.core.UintegerValue()
117  queue.GetAttribute("MaxPackets", limit)
118  self.assertEqual(limit.Get(), 123456)
119 
120  ## -- object pointer values
121  mobility = ns.mobility.RandomWaypointMobilityModel()
122  ptr = ns.core.PointerValue()
123  mobility.GetAttribute("PositionAllocator", ptr)
124  self.assertEqual(ptr.GetObject(), None)
125 
126  pos = ns.mobility.ListPositionAllocator()
127  mobility.SetAttribute("PositionAllocator", ns.core.PointerValue(pos))
128 
129  ptr = ns.core.PointerValue()
130  mobility.GetAttribute("PositionAllocator", ptr)
131  self.assert_(ptr.GetObject() is not None)
132 
133  def testIdentity(self):
134  csma = ns.csma.CsmaNetDevice()
135  channel = ns.csma.CsmaChannel()
136  csma.Attach(channel)
137 
138  c1 = csma.GetChannel()
139  c2 = csma.GetChannel()
140 
141  self.assert_(c1 is c2)
142 
143  def testTypeId(self):
144  typeId1 = ns.core.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
145  self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
146 
147  self.assertRaises(KeyError, ns.core.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
148 
149  def testCommandLine(self):
150  cmd = ns.core.CommandLine()
151  cmd.AddValue("Test1", "this is a test option")
152  cmd.AddValue("Test2", "this is a test option")
153  cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
154  cmd.Test1 = None
155  cmd.Test2 = None
156  cmd.test_xxx = None
157  class Foo:
158  pass
159  foo = Foo()
160  foo.test_foo = None
161  cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
162 
163  cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
164 
165  self.assertEqual(cmd.Test1, "value1")
166  self.assertEqual(cmd.Test2, "value2")
167  self.assertEqual(cmd.test_xxx, "123")
168  self.assertEqual(foo.test_foo, "xpto")
169 
170  def testSubclass(self):
171  class MyNode(ns.network.Node):
172  def __init__(self):
173  super(MyNode, self).__init__()
174 
175  node = MyNode()
176 
177 
178 if __name__ == '__main__':
179  unittest.main()
def testAttributes(self)
Yes, I know, the GetAttribute interface for Python is horrible, we should fix this soon...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895