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