2008-07-08 10:43:58 -07:00
|
|
|
import unittest
|
2011-07-20 16:02:19 +01:00
|
|
|
from ns.core import Simulator, Seconds, Config, int64x64_t
|
|
|
|
|
import ns.core
|
|
|
|
|
import ns.network
|
|
|
|
|
import ns.internet
|
|
|
|
|
import ns.mobility
|
|
|
|
|
import ns.csma
|
2012-02-14 16:03:50 +00:00
|
|
|
import ns.applications
|
2011-07-20 16:02:19 +01:00
|
|
|
|
2008-07-08 10:43:58 -07:00
|
|
|
|
|
|
|
|
class TestSimulator(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def testScheduleNow(self):
|
|
|
|
|
def callback(args):
|
|
|
|
|
self._args_received = args
|
2011-07-20 16:02:19 +01:00
|
|
|
self._cb_time = Simulator.Now()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = None
|
|
|
|
|
self._cb_time = None
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.ScheduleNow(callback, "args")
|
|
|
|
|
Simulator.Run()
|
2008-07-08 10:43:58 -07:00
|
|
|
self.assertEqual(self._args_received, "args")
|
|
|
|
|
self.assertEqual(self._cb_time.GetSeconds(), 0.0)
|
|
|
|
|
|
|
|
|
|
def testSchedule(self):
|
|
|
|
|
def callback(args):
|
|
|
|
|
self._args_received = args
|
2011-07-20 16:02:19 +01:00
|
|
|
self._cb_time = Simulator.Now()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = None
|
|
|
|
|
self._cb_time = None
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.Schedule(Seconds(123), callback, "args")
|
|
|
|
|
Simulator.Run()
|
2008-07-08 10:43:58 -07:00
|
|
|
self.assertEqual(self._args_received, "args")
|
|
|
|
|
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
|
|
|
|
|
|
|
|
|
|
def testScheduleDestroy(self):
|
|
|
|
|
def callback(args):
|
|
|
|
|
self._args_received = args
|
2011-07-20 16:02:19 +01:00
|
|
|
self._cb_time = Simulator.Now()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = None
|
|
|
|
|
self._cb_time = None
|
|
|
|
|
def null(): pass
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.Schedule(Seconds(123), null)
|
|
|
|
|
Simulator.ScheduleDestroy(callback, "args")
|
|
|
|
|
Simulator.Run()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self.assertEqual(self._args_received, "args")
|
|
|
|
|
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
|
|
|
|
|
|
2008-10-06 17:39:35 +01:00
|
|
|
def testTimeComparison(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
self.assert_(Seconds(123) == Seconds(123))
|
|
|
|
|
self.assert_(Seconds(123) >= Seconds(123))
|
|
|
|
|
self.assert_(Seconds(123) <= Seconds(123))
|
|
|
|
|
self.assert_(Seconds(124) > Seconds(123))
|
|
|
|
|
self.assert_(Seconds(123) < Seconds(124))
|
2008-10-06 17:39:35 +01:00
|
|
|
|
|
|
|
|
def testTimeNumericOperations(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
self.assertEqual(Seconds(10) + Seconds(5), Seconds(15))
|
|
|
|
|
self.assertEqual(Seconds(10) - Seconds(5), Seconds(5))
|
|
|
|
|
|
|
|
|
|
v1 = int64x64_t(5.0)*int64x64_t(10)
|
|
|
|
|
self.assertEqual(v1, int64x64_t(50))
|
2008-07-08 10:43:58 -07:00
|
|
|
|
|
|
|
|
def testConfig(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
|
2008-07-08 10:43:58 -07:00
|
|
|
# hm.. no Config.Get?
|
|
|
|
|
|
2008-07-09 18:50:28 +01:00
|
|
|
def testSocket(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
node = ns.network.Node()
|
|
|
|
|
internet = ns.internet.InternetStackHelper()
|
2009-05-28 21:37:25 -07:00
|
|
|
internet.Install(node)
|
2008-07-09 18:50:28 +01:00
|
|
|
self._received_packet = None
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2008-07-09 18:50:28 +01:00
|
|
|
def rx_callback(socket):
|
|
|
|
|
assert self._received_packet is None
|
|
|
|
|
self._received_packet = socket.Recv()
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
|
|
|
|
|
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
|
2008-07-09 18:50:28 +01:00
|
|
|
sink.SetRecvCallback(rx_callback)
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
|
|
|
|
|
source.SendTo(ns.network.Packet(19), 0, ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80))
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.Run()
|
2008-07-09 18:50:28 +01:00
|
|
|
self.assert_(self._received_packet is not None)
|
|
|
|
|
self.assertEqual(self._received_packet.GetSize(), 19)
|
2008-07-17 17:57:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def testAttributes(self):
|
|
|
|
|
##
|
|
|
|
|
## Yes, I know, the GetAttribute interface for Python is
|
|
|
|
|
## horrible, we should fix this soon, I hope.
|
|
|
|
|
##
|
2011-07-20 16:02:19 +01:00
|
|
|
queue = ns.network.DropTailQueue()
|
2008-07-17 17:57:18 +01:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
queue.SetAttribute("MaxPackets", ns.core.UintegerValue(123456))
|
2008-07-17 17:57:18 +01:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
limit = ns.core.UintegerValue()
|
2008-07-17 17:57:18 +01:00
|
|
|
queue.GetAttribute("MaxPackets", limit)
|
|
|
|
|
self.assertEqual(limit.Get(), 123456)
|
|
|
|
|
|
|
|
|
|
## -- object pointer values
|
2011-07-20 16:02:19 +01:00
|
|
|
mobility = ns.mobility.RandomWaypointMobilityModel()
|
|
|
|
|
ptr = ns.core.PointerValue()
|
2009-04-16 10:55:42 +02:00
|
|
|
mobility.GetAttribute("PositionAllocator", ptr)
|
2008-07-17 17:57:18 +01:00
|
|
|
self.assertEqual(ptr.GetObject(), None)
|
|
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
pos = ns.mobility.ListPositionAllocator()
|
|
|
|
|
mobility.SetAttribute("PositionAllocator", ns.core.PointerValue(pos))
|
2008-07-17 17:57:18 +01:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
ptr = ns.core.PointerValue()
|
2009-04-16 10:55:42 +02:00
|
|
|
mobility.GetAttribute("PositionAllocator", ptr)
|
2008-07-17 17:57:18 +01:00
|
|
|
self.assert_(ptr.GetObject() is not None)
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2008-08-01 23:11:42 +01:00
|
|
|
def testIdentity(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
csma = ns.csma.CsmaNetDevice()
|
|
|
|
|
channel = ns.csma.CsmaChannel()
|
2008-08-01 23:11:42 +01:00
|
|
|
csma.Attach(channel)
|
|
|
|
|
|
|
|
|
|
c1 = csma.GetChannel()
|
|
|
|
|
c2 = csma.GetChannel()
|
|
|
|
|
|
2008-08-23 22:35:10 +01:00
|
|
|
self.assert_(c1 is c2)
|
2008-07-17 17:57:18 +01:00
|
|
|
|
2008-10-15 15:55:09 +01:00
|
|
|
def testTypeId(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
typeId1 = ns.core.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
|
2008-10-15 15:55:09 +01:00
|
|
|
self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
|
|
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
self.assertRaises(KeyError, ns.core.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
|
2008-10-15 15:55:09 +01:00
|
|
|
|
2008-11-26 12:11:11 +00:00
|
|
|
def testCommandLine(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
cmd = ns.core.CommandLine()
|
2008-11-26 12:11:11 +00:00
|
|
|
cmd.AddValue("Test1", "this is a test option")
|
|
|
|
|
cmd.AddValue("Test2", "this is a test option")
|
|
|
|
|
cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
|
|
|
|
|
cmd.Test1 = None
|
|
|
|
|
cmd.Test2 = None
|
|
|
|
|
cmd.test_xxx = None
|
|
|
|
|
class Foo:
|
|
|
|
|
pass
|
|
|
|
|
foo = Foo()
|
|
|
|
|
foo.test_foo = None
|
|
|
|
|
cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
|
|
|
|
|
|
|
|
|
|
cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
|
|
|
|
|
|
|
|
|
|
self.assertEqual(cmd.Test1, "value1")
|
|
|
|
|
self.assertEqual(cmd.Test2, "value2")
|
|
|
|
|
self.assertEqual(cmd.test_xxx, "123")
|
|
|
|
|
self.assertEqual(foo.test_foo, "xpto")
|
|
|
|
|
|
2010-05-06 14:21:20 +01:00
|
|
|
def testSubclass(self):
|
2011-07-20 16:02:19 +01:00
|
|
|
class MyNode(ns.network.Node):
|
2010-05-06 14:21:20 +01:00
|
|
|
def __init__(self):
|
|
|
|
|
super(MyNode, self).__init__()
|
|
|
|
|
|
|
|
|
|
node = MyNode()
|
|
|
|
|
|
|
|
|
|
|
2008-07-08 10:43:58 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|