25UINT32_MAX = 0xFFFFFFFF
40 """! Test schedule now
41 @param self this object
44 def callback(args: ns.cppyy.gbl.std.vector) ->
None:
45 """! Callback function
52 ns.Simulator.Destroy()
56 EventImpl* pythonMakeEvent(void (*f)(std::vector<std::string>), std::vector<std::string> l)
61 event = ns.cppyy.gbl.pythonMakeEvent(callback, sys.argv)
62 ns.Simulator.ScheduleNow(event)
65 self.assertEqual(self._cb_time.GetSeconds(), 0.0)
69 @param self this object
72 def callback(args: ns.cppyy.gbl.std.vector):
73 """! Callback function
79 ns.Simulator.Destroy()
83 EventImpl* pythonMakeEvent2(void (*f)(std::vector<std::string>), std::vector<std::string> l)
88 event = ns.cppyy.gbl.pythonMakeEvent2(callback, sys.argv)
89 ns.Simulator.Schedule(ns.Seconds(123), event)
92 self.assertEqual(self._cb_time.GetSeconds(), 123.0)
95 """! Test schedule destroy
96 @param self this object
99 def callback(args: ns.cppyy.gbl.std.vector):
100 """! Callback function
106 ns.Simulator.Destroy()
109 ns.cppyy.cppdef(
"void null(){ return; }")
110 ns.Simulator.Schedule(ns.Seconds(123), ns.cppyy.gbl.null)
112 EventImpl* pythonMakeEvent3(void (*f)(std::vector<std::string>), std::vector<std::string> l)
117 event = ns.cppyy.gbl.pythonMakeEvent3(callback, sys.argv)
118 ns.Simulator.ScheduleDestroy(event)
120 ns.Simulator.Destroy()
122 self.assertEqual(self._cb_time.GetSeconds(), 123.0)
125 """! Test schedule with context
126 @param self this object
129 def callback(context, args: ns.cppyy.gbl.std.vector):
131 @param context the cntet
132 @param args the arguments
138 ns.Simulator.Destroy()
143 EventImpl* pythonMakeEvent4(void (*f)(uint32_t, std::vector<std::string>), uint32_t context, std::vector<std::string> l)
148 event = ns.cppyy.gbl.pythonMakeEvent4(callback, 54321, sys.argv)
149 ns.Simulator.ScheduleWithContext(54321, ns.Seconds(123), event)
153 self.assertEqual(self._cb_time.GetSeconds(), 123.0)
156 """! Test time comparison
157 @param self this object
160 self.assertTrue(ns.Seconds(123) == ns.Seconds(123))
161 self.assertTrue(ns.Seconds(123) >= ns.Seconds(123))
162 self.assertTrue(ns.Seconds(123) <= ns.Seconds(123))
163 self.assertTrue(ns.Seconds(124) > ns.Seconds(123))
164 self.assertTrue(ns.Seconds(123) < ns.Seconds(124))
167 """! Test numeric operations
168 @param self this object
171 self.assertEqual(ns.Seconds(10) + ns.Seconds(5), ns.Seconds(15))
172 self.assertEqual(ns.Seconds(10) - ns.Seconds(5), ns.Seconds(5))
174 v1 = ns.int64x64_t(5.0)*ns.int64x64_t(10)
175 self.assertEqual(v1, ns.int64x64_t(50))
178 """! Test configuration
179 @param self this object
182 ns.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
190 nc = ns.NodeContainer(1)
192 internet = ns.CreateObject("InternetStackHelper")
193 internet.Install(node)
196 def python_rx_callback(socket) -> None:
200 Callback<void,ns3::Ptr<ns3::Socket> > make_rx_callback(void(*func)(Ptr<Socket>))
206 sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
207 sink.Bind(ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80)))
208 sink.SetRecvCallback(ns.cppyy.gbl.make_rx_callback(python_rx_callback))
210 source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName(
"ns3::UdpSocketFactory"))
211 source.SendTo(ns.network.Packet(19), 0, ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address(
"127.0.0.1"), 80)))
221 """! Test attributes function
222 @param self this object
226 queue = ns.CreateObject(
"DropTailQueue<Packet>")
227 queueSizeValue = ns.network.QueueSizeValue (ns.network.QueueSize (
"500p"))
228 queue.SetAttribute(
"MaxSize", queueSizeValue)
230 limit = ns.network.QueueSizeValue()
231 queue.GetAttribute(
"MaxSize", limit)
232 self.assertEqual(limit.Get(), ns.network.QueueSize (
"500p"))
235 mobility = ns.CreateObject(
"RandomWaypointMobilityModel")
236 ptr = ns.CreateObject(
"PointerValue")
237 mobility.GetAttribute(
"PositionAllocator", ptr)
238 self.assertEqual(ptr.GetObject(), ns.core.Ptr[
"Object"](ns.cppyy.nullptr))
240 pos = ns.mobility.ListPositionAllocator()
242 mobility.SetAttribute(
"PositionAllocator", ptr)
244 ptr2 = ns.CreateObject(
"PointerValue")
245 mobility.GetAttribute(
"PositionAllocator", ptr2)
246 self.assertNotEqual(ptr.GetObject(), ns.core.Ptr[
"Object"](ns.cppyy.nullptr))
249 del queue, mobility, ptr, ptr2
253 @param self this object
256 csma = ns.CreateObject("CsmaNetDevice")
257 channel = ns.CreateObject(
"CsmaChannel")
260 c1 = csma.GetChannel()
261 c2 = csma.GetChannel()
263 self.assertEqual(c1, c2)
270 @param self this object
273 ok, typeId1 = ns.LookupByNameFailSafe("ns3::UdpSocketFactory")
275 self.assertEqual(typeId1.GetName (),
"ns3::UdpSocketFactory")
277 ok, typeId1 = ns.LookupByNameFailSafe(
"ns3::__InvalidTypeName__")
281 """! Test command line
282 @param self this object
285 from ctypes
import c_bool, c_int, c_double, c_char_p, create_string_buffer
289 test3 = c_double(3.1415)
291 test4Buffer = create_string_buffer(b
"this is a test option", BUFFLEN)
292 test4 = c_char_p(test4Buffer.raw)
294 cmd = ns.core.CommandLine(__file__)
295 cmd.AddValue(
"Test1",
"this is a test option", test1)
296 cmd.AddValue(
"Test2",
"this is a test option", test2)
297 cmd.AddValue(
"Test3",
"this is a test option", test3)
298 cmd.AddValue(
"Test4",
"this is a test option", test4, BUFFLEN)
300 cmd.Parse([
"python"])
301 self.assertEqual(test1.value,
True)
302 self.assertEqual(test2.value, 42)
303 self.assertEqual(test3.value, 3.1415)
304 self.assertEqual(test4.value, b
"this is a test option")
306 cmd.Parse([
"python",
"--Test1=false",
"--Test2=0",
"--Test3=0.0"])
307 self.assertEqual(test1.value,
False)
308 self.assertEqual(test2.value, 0)
309 self.assertEqual(test3.value, 0.0)
311 cmd.Parse([
"python",
"--Test4=new_string"])
312 self.assertEqual(test4.value, b
"new_string")
316 @param self this object
320 class MyNode(ns.network.Node):
321 def GetLocalTime(self) -> ns.Time:
322 return ns.Seconds(10)
325 forced_local_time = node.GetLocalTime()
326 self.assertEqual(forced_local_time, ns.Seconds(10))
330if __name__ ==
'__main__':
331 unittest.main(verbosity=1, failfast=
True)
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.
_received_packet
received packet
def testIdentity(self)
Test identify.
EventImpl * MakeEvent(void(*f)())
Make an EventImpl from a function pointer taking varying numbers of arguments.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
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.