Update python unit tests to new API

This commit is contained in:
Gustavo J. A. M. Carneiro
2011-07-20 16:02:19 +01:00
parent 5fbd0a133d
commit 913d2d9767

View File

@@ -1,68 +1,74 @@
import unittest
import ns3
from ns.core import Simulator, Seconds, Config, int64x64_t
import ns.core
import ns.network
import ns.internet
import ns.mobility
import ns.csma
class TestSimulator(unittest.TestCase):
def testScheduleNow(self):
def callback(args):
self._args_received = args
self._cb_time = ns3.Simulator.Now()
ns3.Simulator.Destroy()
self._cb_time = Simulator.Now()
Simulator.Destroy()
self._args_received = None
self._cb_time = None
ns3.Simulator.ScheduleNow(callback, "args")
ns3.Simulator.Run()
Simulator.ScheduleNow(callback, "args")
Simulator.Run()
self.assertEqual(self._args_received, "args")
self.assertEqual(self._cb_time.GetSeconds(), 0.0)
def testSchedule(self):
def callback(args):
self._args_received = args
self._cb_time = ns3.Simulator.Now()
ns3.Simulator.Destroy()
self._cb_time = Simulator.Now()
Simulator.Destroy()
self._args_received = None
self._cb_time = None
ns3.Simulator.Schedule(ns3.Seconds(123), callback, "args")
ns3.Simulator.Run()
Simulator.Schedule(Seconds(123), callback, "args")
Simulator.Run()
self.assertEqual(self._args_received, "args")
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
def testScheduleDestroy(self):
def callback(args):
self._args_received = args
self._cb_time = ns3.Simulator.Now()
ns3.Simulator.Destroy()
self._cb_time = Simulator.Now()
Simulator.Destroy()
self._args_received = None
self._cb_time = None
def null(): pass
ns3.Simulator.Schedule(ns3.Seconds(123), null)
ns3.Simulator.ScheduleDestroy(callback, "args")
ns3.Simulator.Run()
ns3.Simulator.Destroy()
Simulator.Schedule(Seconds(123), null)
Simulator.ScheduleDestroy(callback, "args")
Simulator.Run()
Simulator.Destroy()
self.assertEqual(self._args_received, "args")
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
def testTimeComparison(self):
self.assert_(ns3.Seconds(123) == ns3.Seconds(123))
self.assert_(ns3.Seconds(123) >= ns3.Seconds(123))
self.assert_(ns3.Seconds(123) <= ns3.Seconds(123))
self.assert_(ns3.Seconds(124) > ns3.Seconds(123))
self.assert_(ns3.Seconds(123) < ns3.Seconds(124))
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))
def testTimeNumericOperations(self):
self.assertEqual(ns3.Seconds(10) + ns3.Seconds(5), ns3.Seconds(15))
self.assertEqual(ns3.Seconds(10) - ns3.Seconds(5), ns3.Seconds(5))
v1 = ns3.Scalar(5)*ns3.Seconds(10)
self.assertEqual(v1, ns3.Seconds(50))
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))
def testConfig(self):
ns3.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns3.UintegerValue(123))
Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
# hm.. no Config.Get?
def testSocket(self):
node = ns3.Node()
internet = ns3.InternetStackHelper()
node = ns.network.Node()
internet = ns.internet.InternetStackHelper()
internet.Install(node)
self._received_packet = None
@@ -70,14 +76,14 @@ class TestSimulator(unittest.TestCase):
assert self._received_packet is None
self._received_packet = socket.Recv()
sink = ns3.Socket.CreateSocket(node, ns3.TypeId.LookupByName("ns3::UdpSocketFactory"))
sink.Bind(ns3.InetSocketAddress(ns3.Ipv4Address.GetAny(), 80))
sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
sink.SetRecvCallback(rx_callback)
source = ns3.Socket.CreateSocket(node, ns3.TypeId.LookupByName("ns3::UdpSocketFactory"))
source.SendTo(ns3.Packet(19), 0, ns3.InetSocketAddress(ns3.Ipv4Address("127.0.0.1"), 80))
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))
ns3.Simulator.Run()
Simulator.Run()
self.assert_(self._received_packet is not None)
self.assertEqual(self._received_packet.GetSize(), 19)
@@ -87,30 +93,30 @@ class TestSimulator(unittest.TestCase):
## Yes, I know, the GetAttribute interface for Python is
## horrible, we should fix this soon, I hope.
##
queue = ns3.DropTailQueue()
queue = ns.network.DropTailQueue()
queue.SetAttribute("MaxPackets", ns3.UintegerValue(123456))
queue.SetAttribute("MaxPackets", ns.core.UintegerValue(123456))
limit = ns3.UintegerValue()
limit = ns.core.UintegerValue()
queue.GetAttribute("MaxPackets", limit)
self.assertEqual(limit.Get(), 123456)
## -- object pointer values
mobility = ns3.RandomWaypointMobilityModel()
ptr = ns3.PointerValue()
mobility = ns.mobility.RandomWaypointMobilityModel()
ptr = ns.core.PointerValue()
mobility.GetAttribute("PositionAllocator", ptr)
self.assertEqual(ptr.GetObject(), None)
pos = ns3.ListPositionAllocator()
mobility.SetAttribute("PositionAllocator", ns3.PointerValue(pos))
pos = ns.mobility.ListPositionAllocator()
mobility.SetAttribute("PositionAllocator", ns.core.PointerValue(pos))
ptr = ns3.PointerValue()
ptr = ns.core.PointerValue()
mobility.GetAttribute("PositionAllocator", ptr)
self.assert_(ptr.GetObject() is not None)
def testIdentity(self):
csma = ns3.CsmaNetDevice()
channel = ns3.CsmaChannel()
csma = ns.csma.CsmaNetDevice()
channel = ns.csma.CsmaChannel()
csma.Attach(channel)
c1 = csma.GetChannel()
@@ -119,13 +125,13 @@ class TestSimulator(unittest.TestCase):
self.assert_(c1 is c2)
def testTypeId(self):
typeId1 = ns3.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
typeId1 = ns.core.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
self.assertRaises(KeyError, ns3.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
self.assertRaises(KeyError, ns.core.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
def testCommandLine(self):
cmd = ns3.CommandLine()
cmd = ns.core.CommandLine()
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")
@@ -146,7 +152,7 @@ class TestSimulator(unittest.TestCase):
self.assertEqual(foo.test_foo, "xpto")
def testSubclass(self):
class MyNode(ns3.Node):
class MyNode(ns.network.Node):
def __init__(self):
super(MyNode, self).__init__()