utils/python-unit-tests.py
author Josh Pelkey <jpelkey@gatech.edu>
Wed, 11 Aug 2010 11:37:37 -0400
changeset 6553 fb5ad9c7755a
parent 4472 e20a31541404
permissions -rw-r--r--
update release notes and fix doxygen warnings
     1 import unittest
     2 import ns3
     3 
     4 class TestSimulator(unittest.TestCase):
     5 
     6     def testScheduleNow(self):
     7         def callback(args):
     8             self._args_received = args
     9             self._cb_time = ns3.Simulator.Now()
    10         ns3.Simulator.Destroy()
    11         self._args_received = None
    12         self._cb_time = None
    13         ns3.Simulator.ScheduleNow(callback, "args")
    14         ns3.Simulator.Run()
    15         self.assertEqual(self._args_received, "args")
    16         self.assertEqual(self._cb_time.GetSeconds(), 0.0)
    17 
    18     def testSchedule(self):
    19         def callback(args):
    20             self._args_received = args
    21             self._cb_time = ns3.Simulator.Now()
    22         ns3.Simulator.Destroy()
    23         self._args_received = None
    24         self._cb_time = None
    25         ns3.Simulator.Schedule(ns3.Seconds(123), callback, "args")
    26         ns3.Simulator.Run()
    27         self.assertEqual(self._args_received, "args")
    28         self.assertEqual(self._cb_time.GetSeconds(), 123.0)
    29 
    30     def testScheduleDestroy(self):
    31         def callback(args):
    32             self._args_received = args
    33             self._cb_time = ns3.Simulator.Now()
    34         ns3.Simulator.Destroy()
    35         self._args_received = None
    36         self._cb_time = None
    37         def null(): pass
    38         ns3.Simulator.Schedule(ns3.Seconds(123), null)
    39         ns3.Simulator.ScheduleDestroy(callback, "args")
    40         ns3.Simulator.Run()
    41         ns3.Simulator.Destroy()
    42         self.assertEqual(self._args_received, "args")
    43         self.assertEqual(self._cb_time.GetSeconds(), 123.0)
    44 
    45     def testTimeComparison(self):
    46         self.assert_(ns3.Seconds(123) == ns3.Seconds(123))
    47         self.assert_(ns3.Seconds(123) >= ns3.Seconds(123))
    48         self.assert_(ns3.Seconds(123) <= ns3.Seconds(123))
    49         self.assert_(ns3.Seconds(124) > ns3.Seconds(123))
    50         self.assert_(ns3.Seconds(123) < ns3.Seconds(124))
    51 
    52     def testTimeNumericOperations(self):
    53         self.assertEqual(ns3.Seconds(10) + ns3.Seconds(5), ns3.Seconds(15))
    54         self.assertEqual(ns3.Seconds(10) - ns3.Seconds(5), ns3.Seconds(5))
    55 
    56         v1 = ns3.Scalar(5)*ns3.Seconds(10)
    57         self.assertEqual(v1, ns3.Seconds(50))
    58 
    59     def testConfig(self):
    60         ns3.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns3.UintegerValue(123))
    61         # hm.. no Config.Get?
    62 
    63     def testSocket(self):
    64         node = ns3.Node()
    65         internet = ns3.InternetStackHelper()
    66         internet.Install(node)
    67         self._received_packet = None
    68 
    69         def rx_callback(socket):
    70             assert self._received_packet is None
    71             self._received_packet = socket.Recv()
    72 
    73         sink = ns3.Socket.CreateSocket(node, ns3.TypeId.LookupByName("ns3::UdpSocketFactory"))
    74         sink.Bind(ns3.InetSocketAddress(ns3.Ipv4Address.GetAny(), 80))
    75         sink.SetRecvCallback(rx_callback)
    76 
    77         source = ns3.Socket.CreateSocket(node, ns3.TypeId.LookupByName("ns3::UdpSocketFactory"))
    78         source.SendTo(ns3.Packet(19), 0, ns3.InetSocketAddress(ns3.Ipv4Address("127.0.0.1"), 80))
    79 
    80         ns3.Simulator.Run()
    81         self.assert_(self._received_packet is not None)
    82         self.assertEqual(self._received_packet.GetSize(), 19)
    83 
    84 
    85     def testAttributes(self):
    86         ##
    87         ## Yes, I know, the GetAttribute interface for Python is
    88         ## horrible, we should fix this soon, I hope.
    89         ##
    90         queue = ns3.DropTailQueue()
    91 
    92         queue.SetAttribute("MaxPackets", ns3.UintegerValue(123456))
    93 
    94         limit = ns3.UintegerValue()
    95         queue.GetAttribute("MaxPackets", limit)
    96         self.assertEqual(limit.Get(), 123456)
    97 
    98         ## -- object pointer values
    99         mobility = ns3.RandomWaypointMobilityModel()
   100         ptr = ns3.PointerValue()
   101         mobility.GetAttribute("PositionAllocator", ptr)
   102         self.assertEqual(ptr.GetObject(), None)
   103         
   104         pos = ns3.ListPositionAllocator()
   105         mobility.SetAttribute("PositionAllocator", ns3.PointerValue(pos))
   106 
   107         ptr = ns3.PointerValue()
   108         mobility.GetAttribute("PositionAllocator", ptr)
   109         self.assert_(ptr.GetObject() is not None)
   110 
   111     def testIdentity(self):
   112         csma = ns3.CsmaNetDevice()
   113         channel = ns3.CsmaChannel()
   114         csma.Attach(channel)
   115         
   116         c1 = csma.GetChannel()
   117         c2 = csma.GetChannel()
   118 
   119         self.assert_(c1 is c2)
   120 
   121     def testTypeId(self):
   122         typeId1 = ns3.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
   123         self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
   124         
   125         self.assertRaises(KeyError, ns3.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
   126 
   127     def testCommandLine(self):
   128         cmd = ns3.CommandLine()
   129         cmd.AddValue("Test1", "this is a test option")
   130         cmd.AddValue("Test2", "this is a test option")
   131         cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
   132         cmd.Test1 = None
   133         cmd.Test2 = None
   134         cmd.test_xxx = None
   135         class Foo:
   136             pass
   137         foo = Foo()
   138         foo.test_foo = None
   139         cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
   140         
   141         cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
   142 
   143         self.assertEqual(cmd.Test1, "value1")
   144         self.assertEqual(cmd.Test2, "value2")
   145         self.assertEqual(cmd.test_xxx, "123")
   146         self.assertEqual(foo.test_foo, "xpto")
   147 
   148     def testSubclass(self):
   149         class MyNode(ns3.Node):
   150             def __init__(self):
   151                 super(MyNode, self).__init__()
   152 
   153         node = MyNode()
   154 
   155 
   156 if __name__ == '__main__':
   157     unittest.main()