author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Tue, 02 Sep 2008 17:20:32 -0700 | |
changeset 3588 | ad0a36bfdb62 |
parent 3546 | cecda7126440 |
child 3731 | 317f9dbccc2b |
permissions | -rw-r--r-- |
3408 | 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 |
if 0: # these tests are known to fail for now (pybindgen limitation) |
|
46 |
def testTime_EQ(self): |
|
47 |
self.assert_(ns3.Seconds(123) == ns3.Seconds(123)) |
|
48 |
def testTime_GE(self): |
|
49 |
self.assert_(ns3.Seconds(123) >= ns3.Seconds(123)) |
|
50 |
def testTime_LE(self): |
|
51 |
self.assert_(ns3.Seconds(123) <= ns3.Seconds(123)) |
|
52 |
def testTime_GT(self): |
|
53 |
self.assert_(ns3.Seconds(124) > ns3.Seconds(123)) |
|
54 |
def testTime_LT(self): |
|
55 |
self.assert_(ns3.Seconds(123) < ns3.Seconds(124)) |
|
56 |
||
57 |
def testConfig(self): |
|
58 |
ns3.Config.Set("ns3::OnOffApplication::PacketSize", ns3.UintegerValue(123)) |
|
59 |
ns3.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns3.UintegerValue(123)) |
|
60 |
# hm.. no Config.Get? |
|
61 |
||
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
|
62 |
def testSocket(self): |
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
|
63 |
node = ns3.Node() |
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
|
64 |
ns3.AddInternetStack(node) |
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
|
65 |
self._received_packet = None |
3408 | 66 |
|
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
|
67 |
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
|
68 |
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
|
69 |
self._received_packet = socket.Recv() |
3408 | 70 |
|
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
|
71 |
sink = ns3.Socket.CreateSocket(node, ns3.TypeId.LookupByName("ns3::UdpSocketFactory")) |
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
|
72 |
sink.Bind(ns3.InetSocketAddress(ns3.Ipv4Address.GetAny(), 80)) |
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
|
73 |
sink.SetRecvCallback(rx_callback) |
3408 | 74 |
|
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
|
75 |
source = ns3.Socket.CreateSocket(node, ns3.TypeId.LookupByName("ns3::UdpSocketFactory")) |
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
|
76 |
source.SendTo(ns3.Packet(19), 0, ns3.InetSocketAddress(ns3.Ipv4Address("127.0.0.1"), 80)) |
3408 | 77 |
|
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
|
78 |
ns3.Simulator.Run() |
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
|
79 |
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
|
80 |
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
|
81 |
|
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
82 |
|
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
83 |
def testAttributes(self): |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
84 |
## |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
85 |
## 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
|
86 |
## 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
|
87 |
## |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
88 |
queue = ns3.DropTailQueue() |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
89 |
|
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
90 |
queue.SetAttribute("MaxPackets", ns3.UintegerValue(123456)) |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
91 |
|
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
92 |
limit = ns3.UintegerValue() |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
93 |
queue.GetAttribute("MaxPackets", limit) |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
94 |
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
|
95 |
|
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
96 |
## -- object pointer values |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
97 |
mobility = ns3.RandomWaypointMobilityModel() |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
98 |
ptr = ns3.PointerValue() |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
99 |
mobility.GetAttribute("Position", ptr) |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
100 |
self.assertEqual(ptr.GetObject(), None) |
3408 | 101 |
|
3431
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
102 |
pos = ns3.ListPositionAllocator() |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
103 |
mobility.SetAttribute("Position", ns3.PointerValue(pos)) |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
104 |
|
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
105 |
ptr = ns3.PointerValue() |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
106 |
mobility.GetAttribute("Position", ptr) |
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
107 |
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
|
108 |
|
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
|
109 |
def testIdentity(self): |
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
|
110 |
csma = ns3.CsmaNetDevice() |
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
|
111 |
channel = ns3.CsmaChannel() |
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
|
112 |
csma.Attach(channel) |
3431
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
113 |
|
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
|
114 |
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
|
115 |
c2 = csma.GetChannel() |
3431
ccf8108ce6d7
A couple of attribute python unit tests
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3414
diff
changeset
|
116 |
|
3546
cecda7126440
New PyBindGen, fixes python wrapper identity issue.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3501
diff
changeset
|
117 |
self.assert_(c1 is c2) |
3408 | 118 |
|
119 |
if __name__ == '__main__': |
|
120 |
unittest.main() |