utils/python-unit-tests.py
author Sébastien Deronne <sebastien.deronne@gmail.com>
Fri, 04 Sep 2015 22:17:57 +0200
changeset 11636 d56935c679c6
parent 7710 a1eb376aaa6a
permissions -rw-r--r--
Update RELEASE_NOTES
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     1
import unittest
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
     2
from ns.core import Simulator, Seconds, Config, int64x64_t
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
     3
import ns.core
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
     4
import ns.network
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
     5
import ns.internet
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
     6
import ns.mobility
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
     7
import ns.csma
7709
1655e5f183ac Fix the python unit tests: now we need to import the applications module
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 7374
diff changeset
     8
import ns.applications
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
     9
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    10
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    11
class TestSimulator(unittest.TestCase):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    12
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    13
    def testScheduleNow(self):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    14
        def callback(args):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
            self._args_received = args
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    16
            self._cb_time = Simulator.Now()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    17
        Simulator.Destroy()
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
        self._args_received = None
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    19
        self._cb_time = None
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    20
        Simulator.ScheduleNow(callback, "args")
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    21
        Simulator.Run()
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    22
        self.assertEqual(self._args_received, "args")
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    23
        self.assertEqual(self._cb_time.GetSeconds(), 0.0)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    24
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    25
    def testSchedule(self):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    26
        def callback(args):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    27
            self._args_received = args
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    28
            self._cb_time = Simulator.Now()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    29
        Simulator.Destroy()
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    30
        self._args_received = None
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    31
        self._cb_time = None
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    32
        Simulator.Schedule(Seconds(123), callback, "args")
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    33
        Simulator.Run()
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    34
        self.assertEqual(self._args_received, "args")
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    35
        self.assertEqual(self._cb_time.GetSeconds(), 123.0)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    36
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    37
    def testScheduleDestroy(self):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    38
        def callback(args):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    39
            self._args_received = args
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    40
            self._cb_time = Simulator.Now()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    41
        Simulator.Destroy()
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    42
        self._args_received = None
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    43
        self._cb_time = None
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    44
        def null(): pass
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    45
        Simulator.Schedule(Seconds(123), null)
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    46
        Simulator.ScheduleDestroy(callback, "args")
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    47
        Simulator.Run()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    48
        Simulator.Destroy()
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    49
        self.assertEqual(self._args_received, "args")
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    50
        self.assertEqual(self._cb_time.GetSeconds(), 123.0)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    51
7710
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    52
    def testScheduleWithContext(self):
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    53
        def callback(context, args):
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    54
            self._context_received = context
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    55
            self._args_received = args
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    56
            self._cb_time = Simulator.Now()
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    57
        Simulator.Destroy()
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    58
        self._args_received = None
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    59
        self._cb_time = None
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    60
        self._context_received = None
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    61
        Simulator.ScheduleWithContext(54321, Seconds(123), callback, "args")
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    62
        Simulator.Run()
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    63
        self.assertEqual(self._context_received, 54321)
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    64
        self.assertEqual(self._args_received, "args")
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    65
        self.assertEqual(self._cb_time.GetSeconds(), 123.0)
a1eb376aaa6a Bug 1350 - Simulator.ScheduleWithContext Python binding missing
alina & Gustavo Carneiro
parents: 7709
diff changeset
    66
3731
317f9dbccc2b New pybindgen and new API scanning, brings support for comparison operators and + - * / numeric operators.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3546
diff changeset
    67
    def testTimeComparison(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    68
        self.assert_(Seconds(123) == Seconds(123))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    69
        self.assert_(Seconds(123) >= Seconds(123))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    70
        self.assert_(Seconds(123) <= Seconds(123))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    71
        self.assert_(Seconds(124) > Seconds(123))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    72
        self.assert_(Seconds(123) < Seconds(124))
3731
317f9dbccc2b New pybindgen and new API scanning, brings support for comparison operators and + - * / numeric operators.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3546
diff changeset
    73
317f9dbccc2b New pybindgen and new API scanning, brings support for comparison operators and + - * / numeric operators.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3546
diff changeset
    74
    def testTimeNumericOperations(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    75
        self.assertEqual(Seconds(10) + Seconds(5), Seconds(15))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    76
        self.assertEqual(Seconds(10) - Seconds(5), Seconds(5))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    77
        
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    78
        v1 = int64x64_t(5.0)*int64x64_t(10)
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    79
        self.assertEqual(v1, int64x64_t(50))
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    80
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    81
    def testConfig(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    82
        Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    83
        # hm.. no Config.Get?
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    84
3414
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
    85
    def testSocket(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    86
        node = ns.network.Node()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    87
        internet = ns.internet.InternetStackHelper()
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4319
diff changeset
    88
        internet.Install(node)
3414
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
    89
        self._received_packet = None
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    90
3414
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
    91
        def rx_callback(socket):
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
    92
            assert self._received_packet is None
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
    93
            self._received_packet = socket.Recv()
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    94
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    95
        sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    96
        sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
3414
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
    97
        sink.SetRecvCallback(rx_callback)
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    98
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
    99
        source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   100
        source.SendTo(ns.network.Packet(19), 0, ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80))
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   101
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   102
        Simulator.Run()
3414
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
   103
        self.assert_(self._received_packet is not None)
48d69d8eac38 Rescan API; fix and enable the socket unit test, as it now works.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3408
diff changeset
   104
        self.assertEqual(self._received_packet.GetSize(), 19)
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   105
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   106
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   107
    def testAttributes(self):
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   108
        ##
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   109
        ## Yes, I know, the GetAttribute interface for Python is
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   110
        ## horrible, we should fix this soon, I hope.
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   111
        ##
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   112
        queue = ns.network.DropTailQueue()
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   113
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   114
        queue.SetAttribute("MaxPackets", ns.core.UintegerValue(123456))
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   115
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   116
        limit = ns.core.UintegerValue()
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   117
        queue.GetAttribute("MaxPackets", limit)
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   118
        self.assertEqual(limit.Get(), 123456)
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   119
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   120
        ## -- object pointer values
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   121
        mobility = ns.mobility.RandomWaypointMobilityModel()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   122
        ptr = ns.core.PointerValue()
4319
f4a73242f151 rename attribute from Position to PositionAllocator to avoid conflict with Position attribute defined in ns3::MobilityModel
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3929
diff changeset
   123
        mobility.GetAttribute("PositionAllocator", ptr)
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   124
        self.assertEqual(ptr.GetObject(), None)
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   125
        
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   126
        pos = ns.mobility.ListPositionAllocator()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   127
        mobility.SetAttribute("PositionAllocator", ns.core.PointerValue(pos))
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   128
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   129
        ptr = ns.core.PointerValue()
4319
f4a73242f151 rename attribute from Position to PositionAllocator to avoid conflict with Position attribute defined in ns3::MobilityModel
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3929
diff changeset
   130
        mobility.GetAttribute("PositionAllocator", ptr)
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   131
        self.assert_(ptr.GetObject() is not None)
3501
f725f0e0d6b7 Python: use hash() as temporary workaround to check if two objects with different wrappers are underneath the same.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3431
diff changeset
   132
f725f0e0d6b7 Python: use hash() as temporary workaround to check if two objects with different wrappers are underneath the same.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3431
diff changeset
   133
    def testIdentity(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   134
        csma = ns.csma.CsmaNetDevice()
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   135
        channel = ns.csma.CsmaChannel()
3501
f725f0e0d6b7 Python: use hash() as temporary workaround to check if two objects with different wrappers are underneath the same.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3431
diff changeset
   136
        csma.Attach(channel)
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   137
        
3501
f725f0e0d6b7 Python: use hash() as temporary workaround to check if two objects with different wrappers are underneath the same.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3431
diff changeset
   138
        c1 = csma.GetChannel()
f725f0e0d6b7 Python: use hash() as temporary workaround to check if two objects with different wrappers are underneath the same.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3431
diff changeset
   139
        c2 = csma.GetChannel()
3431
ccf8108ce6d7 A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3414
diff changeset
   140
3546
cecda7126440 New PyBindGen, fixes python wrapper identity issue.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3501
diff changeset
   141
        self.assert_(c1 is c2)
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   142
3753
a84a48233eb3 A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3731
diff changeset
   143
    def testTypeId(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   144
        typeId1 = ns.core.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
3753
a84a48233eb3 A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3731
diff changeset
   145
        self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
a84a48233eb3 A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3731
diff changeset
   146
        
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   147
        self.assertRaises(KeyError, ns.core.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
3753
a84a48233eb3 A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3731
diff changeset
   148
3929
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   149
    def testCommandLine(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   150
        cmd = ns.core.CommandLine()
3929
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   151
        cmd.AddValue("Test1", "this is a test option")
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   152
        cmd.AddValue("Test2", "this is a test option")
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   153
        cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   154
        cmd.Test1 = None
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   155
        cmd.Test2 = None
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   156
        cmd.test_xxx = None
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   157
        class Foo:
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   158
            pass
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   159
        foo = Foo()
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   160
        foo.test_foo = None
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   161
        cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   162
        
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   163
        cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   164
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   165
        self.assertEqual(cmd.Test1, "value1")
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   166
        self.assertEqual(cmd.Test2, "value2")
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   167
        self.assertEqual(cmd.test_xxx, "123")
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   168
        self.assertEqual(foo.test_foo, "xpto")
909b0a724ed3 Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3768
diff changeset
   169
6290
e01b68222e60 Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4472
diff changeset
   170
    def testSubclass(self):
7374
a5437cb9144d Update python unit tests to new API
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6290
diff changeset
   171
        class MyNode(ns.network.Node):
6290
e01b68222e60 Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4472
diff changeset
   172
            def __init__(self):
e01b68222e60 Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4472
diff changeset
   173
                super(MyNode, self).__init__()
e01b68222e60 Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4472
diff changeset
   174
e01b68222e60 Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4472
diff changeset
   175
        node = MyNode()
e01b68222e60 Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4472
diff changeset
   176
e01b68222e60 Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4472
diff changeset
   177
3408
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   178
if __name__ == '__main__':
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   179
    unittest.main()