LEFT | RIGHT |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2013 Mohammed J.F. Alenazi |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Author: Mohammed J.F. Alenazi <malenazi@ittc.ku.edu> |
| 19 * |
| 20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director |
| 21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets |
| 22 * Information and Telecommunication Technology Center (ITTC) |
| 23 * and Department of Electrical Engineering and Computer Science |
| 24 * The University of Kansas Lawrence, KS USA. |
| 25 * |
| 26 * Work supported by King Saud University and |
| 27 * the ITTC at The University of Kansas. |
| 28 */ |
2 | 29 |
3 // Include a header file from your module to test. | 30 #include "ns3/epidemic-routing-protocol.h" |
4 #include "ns3/epidemic.h" | 31 #include <vector> |
| 32 #include "ns3/ptr.h" |
| 33 #include "ns3/boolean.h" |
| 34 #include "ns3/test.h" |
| 35 #include "ns3/ipv4-route.h" |
| 36 #include "ns3/simulator.h" |
| 37 #include "ns3/double.h" |
| 38 #include "ns3/uinteger.h" |
| 39 #include "ns3/string.h" |
| 40 #include "ns3/ipv4-address-helper.h" |
| 41 #include "ns3/network-module.h" |
| 42 #include "ns3/applications-module.h" |
| 43 #include "ns3/mobility-module.h" |
| 44 #include "ns3/wifi-module.h" |
| 45 #include "ns3/internet-module.h" |
| 46 #include "ns3/epidemic-helper.h" |
| 47 #include "ns3/socket.h" |
| 48 #include "ns3/log.h" |
5 | 49 |
6 // An essential include is test.h | |
7 #include "ns3/test.h" | |
8 | 50 |
9 // Do not put your test classes in namespace ns3. You may find it useful | |
10 // to use the using directive to access the ns3 namespace directly | |
11 using namespace ns3; | 51 using namespace ns3; |
| 52 using namespace Epidemic; |
12 | 53 |
13 // This is an example TestCase. | 54 |
14 class EpidemicTestCase1 : public TestCase | 55 class EpidemicHeaderTestCase : public TestCase |
15 { | 56 { |
16 public: | 57 public: |
17 EpidemicTestCase1 (); | 58 EpidemicHeaderTestCase (); |
18 virtual ~EpidemicTestCase1 (); | 59 virtual ~EpidemicHeaderTestCase (); |
19 | 60 |
20 private: | 61 private: |
21 virtual void DoRun (void); | 62 virtual void DoRun (void); |
22 }; | 63 }; |
23 | 64 |
24 // Add some help text to this case to describe what it is intended to test | 65 |
25 EpidemicTestCase1::EpidemicTestCase1 () | 66 EpidemicHeaderTestCase::EpidemicHeaderTestCase () |
26 : TestCase ("Epidemic test case (does nothing)") | 67 : TestCase ("Verifying the Epidemic header") |
27 { | 68 { |
28 } | 69 } |
29 | 70 |
30 // This destructor does nothing but we include it as a reminder that | 71 |
31 // the test case should clean up after itself | 72 EpidemicHeaderTestCase::~EpidemicHeaderTestCase () |
32 EpidemicTestCase1::~EpidemicTestCase1 () | |
33 { | 73 { |
34 } | 74 } |
35 | 75 |
36 // | |
37 // This method is the pure virtual method from class TestCase that every | |
38 // TestCase must implement | |
39 // | |
40 void | 76 void |
41 EpidemicTestCase1::DoRun (void) | 77 EpidemicHeaderTestCase::DoRun (void) |
42 { | 78 { |
43 // A wide variety of test macros are available in src/core/test.h | 79 Ptr<Packet> packet = Create<Packet> (); |
44 NS_TEST_ASSERT_MSG_EQ (true, true, "true doesn't equal true for some reason"); | 80 Time ts1 = Simulator::Now (); |
45 // Use this one for floating point comparisons | 81 |
46 NS_TEST_ASSERT_MSG_EQ_TOL (0.01, 0.01, 0.001, "Numbers are not equal within to
lerance"); | 82 EpidemicHeader header1; |
| 83 header1.SetPacketID (12345); |
| 84 header1.SetTimeStamp (ts1); |
| 85 header1.SetHopCount (10); |
| 86 packet->AddHeader (header1); |
| 87 |
| 88 EpidemicHeader header2; |
| 89 packet->RemoveHeader (header2); |
| 90 |
| 91 NS_TEST_ASSERT_MSG_EQ (header2.GetSerializedSize (),16, |
| 92 "Checking Header Size"); |
| 93 NS_TEST_ASSERT_MSG_EQ (header2.GetPacketID (),12345, |
| 94 "Checking Packet ID"); |
| 95 NS_TEST_ASSERT_MSG_EQ (header2.GetTimeStamp (),ts1, |
| 96 "Checking TimeStamp"); |
| 97 NS_TEST_ASSERT_MSG_EQ (header2.GetHopCount (),10, |
| 98 "Checking hop count"); |
| 99 |
47 } | 100 } |
48 | 101 |
49 // The TestSuite class names the TestSuite, identifies what type of TestSuite, | 102 |
50 // and enables the TestCases to be run. Typically, only the constructor for | 103 |
51 // this class must be defined | 104 |
52 // | 105 |
| 106 |
| 107 struct EpidemicRqueueTest : public TestCase |
| 108 { |
| 109 EpidemicRqueueTest () : TestCase ("EpidemicQueue"), |
| 110 q (64) |
| 111 { |
| 112 } |
| 113 virtual void DoRun (); |
| 114 void Unicast (Ptr<Ipv4Route> route, |
| 115 Ptr<const Packet> packet, const Ipv4Header & header) |
| 116 { |
| 117 } |
| 118 void Error (Ptr<const Packet>, const Ipv4Header &, Socket::SocketErrno) |
| 119 { |
| 120 } |
| 121 void CheckSizeLimit (); |
| 122 void CheckTimeout (); |
| 123 PacketQueue q; |
| 124 }; |
| 125 |
| 126 void |
| 127 EpidemicRqueueTest::DoRun () |
| 128 { |
| 129 NS_TEST_EXPECT_MSG_EQ (q.GetMaxQueueLen (), 64, |
| 130 "Checking the default size for queue length"); |
| 131 q.SetMaxQueueLen (32); |
| 132 NS_TEST_EXPECT_MSG_EQ (q.GetMaxQueueLen (), 32, |
| 133 "Checking the set function for queue length"); |
| 134 |
| 135 |
| 136 Ptr<const Packet> packet = Create<Packet> (); |
| 137 Ipv4Header h; |
| 138 h.SetDestination (Ipv4Address ("1.2.3.4")); |
| 139 h.SetSource (Ipv4Address ("4.3.2.1")); |
| 140 Ipv4RoutingProtocol::UnicastForwardCallback ucb = |
| 141 MakeCallback (&EpidemicRqueueTest::Unicast, this); |
| 142 Ipv4RoutingProtocol::ErrorCallback ecb = |
| 143 MakeCallback (&EpidemicRqueueTest::Error, this); |
| 144 |
| 145 QueueEntry e1 (packet, h, ucb, ecb, Seconds (1),1111); |
| 146 q.Enqueue (e1); |
| 147 q.Enqueue (e1); |
| 148 q.Enqueue (e1); |
| 149 NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 1, |
| 150 "Checking if enqueue removes duplicates"); |
| 151 |
| 152 h.SetDestination (Ipv4Address ("2.2.2.2")); |
| 153 QueueEntry e2 (packet, h, ucb, ecb, Seconds (1),2222); |
| 154 q.Enqueue (e1); |
| 155 q.Enqueue (e2); |
| 156 Ptr<Packet> packet2 = Create<Packet> (); |
| 157 QueueEntry e3 (packet2, h, ucb, ecb, Seconds (1),3333); |
| 158 q.Enqueue (e2); |
| 159 q.Enqueue (e3); |
| 160 NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 3, |
| 161 "Checking the queue size function after multiple enqueu
e"); |
| 162 QueueEntry e4; |
| 163 q.Dequeue (e4); |
| 164 NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 2, |
| 165 "Checking the queue size function after dequeue functio
n is called"); |
| 166 NS_TEST_EXPECT_MSG_EQ (e4.GetPacketID (), e1.GetPacketID (), "trivial"); |
| 167 q.Dequeue (e4); |
| 168 NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 1, |
| 169 "Checking the queue size function after dequeue functio
n is called"); |
| 170 NS_TEST_EXPECT_MSG_EQ (e4.GetPacketID (), e2.GetPacketID (), |
| 171 "Checking the packetID for two entries"); |
| 172 q.Dequeue (e4); |
| 173 NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 0, |
| 174 "Checking if the queue is empty"); |
| 175 NS_TEST_EXPECT_MSG_EQ (e4.GetPacketID (), e3.GetPacketID (), |
| 176 "Checking the packetID for two entries"); |
| 177 NS_TEST_EXPECT_MSG_EQ (q.Dequeue (e4), false, |
| 178 "Checking the dequeue function with empty queue"); |
| 179 } |
| 180 |
| 181 |
| 182 |
53 class EpidemicTestSuite : public TestSuite | 183 class EpidemicTestSuite : public TestSuite |
54 { | 184 { |
55 public: | 185 public: |
56 EpidemicTestSuite (); | 186 EpidemicTestSuite (); |
57 }; | 187 }; |
58 | 188 |
59 EpidemicTestSuite::EpidemicTestSuite () | 189 EpidemicTestSuite::EpidemicTestSuite () |
60 : TestSuite ("epidemic", UNIT) | 190 : TestSuite ("epidemic", UNIT) |
61 { | 191 { |
62 AddTestCase (new EpidemicTestCase1); | 192 AddTestCase (new EpidemicHeaderTestCase, TestCase::QUICK); |
| 193 AddTestCase (new EpidemicRqueueTest, TestCase::QUICK); |
63 } | 194 } |
64 | 195 |
65 // Do not forget to allocate an instance of this TestSuite | 196 |
66 static EpidemicTestSuite epidemicTestSuite; | 197 static EpidemicTestSuite epidemicTestSuite; |
67 | 198 |
LEFT | RIGHT |