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