From 751daa724be07a1dd4f7e86dba4802366ee3c18c Mon Sep 17 00:00:00 2001 From: Tim Schubert Date: Fri, 3 Jul 2020 17:21:19 +0200 Subject: [PATCH] Make examples into tests --- examples/leo-example.cc | 76 ------------------- examples/wscript | 7 +- .../isl-example.cc => test/isl-test-suite.cc | 50 ++++++++---- test/leo-test-suite.cc | 74 +++++++++++++----- wscript | 1 + 5 files changed, 93 insertions(+), 115 deletions(-) delete mode 100644 examples/leo-example.cc rename examples/isl-example.cc => test/isl-test-suite.cc (72%) diff --git a/examples/leo-example.cc b/examples/leo-example.cc deleted file mode 100644 index d28ad4e..0000000 --- a/examples/leo-example.cc +++ /dev/null @@ -1,76 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ - -#include "ns3/core-module.h" -#include "ns3/network-module.h" -#include "ns3/internet-module.h" -#include "ns3/applications-module.h" -#include "ns3/node-container.h" -#include "ns3/core-module.h" - -#include "ns3/leo-module.h" - -using namespace ns3; - -NS_LOG_COMPONENT_DEFINE ("LeoExample"); - -int -main (int argc, char *argv[]) -{ - bool verbose = true; - - CommandLine cmd; - cmd.AddValue ("verbose", "Tell application to log if true", verbose); - - cmd.Parse (argc,argv); - - Time::SetResolution (Time::NS); - LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); - LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); - LogComponentEnable ("MockNetDevice", LOG_LEVEL_DEBUG); - - NodeContainer satellites; - satellites.Create (100); - NodeContainer gateways; - gateways.Create (10); - NodeContainer terminals; - terminals.Create (20); - - LeoHelper leo; - leo.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); - leo.SetChannelAttribute ("PropagationDelay", StringValue ("ns3::ConstantSpeedPropagationDelayModel")); - leo.SetChannelAttribute ("PropagationLoss", StringValue ("ns3::IslPropagationLossModel")); - leo.SetDeviceAttribute ("MobilityModel", StringValue ("ns3::LeoMobilityModel")); - - leo.Install (satellites, gateways, terminals); - - // we want to ping terminals - UdpEchoServerHelper echoServer (9); - ApplicationContainer serverApps = echoServer.Install (terminals); - - // TODO routing - - // install a client on each of the terminals - ApplicationContainer clientApps; - for (uint32_t i = 1; i < terminals.GetN (); i++) - { - Address remote = terminals.Get (i)->GetObject ()->GetAddress (1, 0).GetAddress (); - NS_LOG_DEBUG ("REMOTE " << i << " " << remote); - UdpEchoClientHelper echoClient (remote, 9); - echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); - echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); - echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); - - clientApps.Add (echoClient.Install (terminals.Get (i-1))); - } - - clientApps.Start (Seconds (2.0)); - clientApps.Stop (Seconds (10.0)); - - serverApps.Start (Seconds (1.0)); - serverApps.Stop (Seconds (10.0)); - - Simulator::Run (); - Simulator::Destroy (); - - return 0; -} diff --git a/examples/wscript b/examples/wscript index ed4a77c..5903ffe 100644 --- a/examples/wscript +++ b/examples/wscript @@ -1,9 +1,4 @@ # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- def build(bld): - obj = bld.create_ns3_program('leo-example', ['leo']) - obj.source = 'leo-example.cc' - - obj = bld.create_ns3_program('isl-example', ['leo']) - obj.source = 'isl-example.cc' - + pass diff --git a/examples/isl-example.cc b/test/isl-test-suite.cc similarity index 72% rename from examples/isl-example.cc rename to test/isl-test-suite.cc index 93a75e4..f6355f9 100644 --- a/examples/isl-example.cc +++ b/test/isl-test-suite.cc @@ -7,22 +7,33 @@ #include "ns3/node-container.h" #include "ns3/leo-module.h" +#include "ns3/test.h" using namespace ns3; -NS_LOG_COMPONENT_DEFINE ("IslExample"); - -int -main (int argc, char *argv[]) +class IslIcmpTestCase : public TestCase { - CommandLine cmd; - cmd.Parse (argc, argv); +public: + IslIcmpTestCase (); + virtual ~IslIcmpTestCase (); - Time::SetResolution (Time::NS); - LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); - LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); - LogComponentEnable ("IslExample", LOG_LEVEL_DEBUG); +private: + virtual void DoRun (void); +}; +IslIcmpTestCase::IslIcmpTestCase () + : TestCase ("Test connectivity using ICMP") +{ +} + +IslIcmpTestCase::~IslIcmpTestCase () +{ + Simulator::Destroy (); +} + +void +IslIcmpTestCase::DoRun (void) +{ NodeContainer nodes; nodes.Create (3); @@ -69,7 +80,20 @@ main (int argc, char *argv[]) serverApps.Stop (Seconds (10.0)); Simulator::Run (); - Simulator::Destroy (); - - return 0; } + +class IslTestSuite : public TestSuite +{ +public: + IslTestSuite (); +}; + +IslTestSuite::IslTestSuite () + : TestSuite ("isl", EXAMPLE) +{ + // TestDuration for TestCase can be QUICK, EXTENSIVE or TAKES_FOREVER + AddTestCase (new IslIcmpTestCase, TestCase::EXTENSIVE); +} + +// Do not forget to allocate an instance of this TestSuite +static IslTestSuite leoTestSuite; diff --git a/test/leo-test-suite.cc b/test/leo-test-suite.cc index 595270b..4cfb105 100644 --- a/test/leo-test-suite.cc +++ b/test/leo-test-suite.cc @@ -1,16 +1,18 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -// Include a header file from your module to test. -#include "ns3/leo.h" +#include "ns3/core-module.h" +#include "ns3/network-module.h" +#include "ns3/internet-module.h" +#include "ns3/applications-module.h" +#include "ns3/node-container.h" +#include "ns3/core-module.h" + +#include "ns3/leo-module.h" -// An essential include is test.h #include "ns3/test.h" -// Do not put your test classes in namespace ns3. You may find it useful -// to use the using directive to access the ns3 namespace directly using namespace ns3; -// This is an example TestCase. class LeoTestCase1 : public TestCase { public: @@ -21,29 +23,62 @@ private: virtual void DoRun (void); }; -// Add some help text to this case to describe what it is intended to test LeoTestCase1::LeoTestCase1 () : TestCase ("Leo test case (does nothing)") { } -// This destructor does nothing but we include it as a reminder that -// the test case should clean up after itself LeoTestCase1::~LeoTestCase1 () { + Simulator::Destroy (); } -// -// This method is the pure virtual method from class TestCase that every -// TestCase must implement -// void LeoTestCase1::DoRun (void) { - // A wide variety of test macros are available in src/core/test.h - NS_TEST_ASSERT_MSG_EQ (true, true, "true doesn't equal true for some reason"); - // Use this one for floating point comparisons - NS_TEST_ASSERT_MSG_EQ_TOL (0.01, 0.01, 0.001, "Numbers are not equal within tolerance"); + Time::SetResolution (Time::NS); + + NodeContainer satellites; + satellites.Create (100); + NodeContainer gateways; + gateways.Create (10); + NodeContainer terminals; + terminals.Create (20); + + LeoHelper leo; + leo.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); + leo.SetChannelAttribute ("PropagationDelay", StringValue ("ns3::ConstantSpeedPropagationDelayModel")); + leo.SetChannelAttribute ("PropagationLoss", StringValue ("ns3::IslPropagationLossModel")); + leo.SetDeviceAttribute ("MobilityModel", StringValue ("ns3::LeoMobilityModel")); + + NetDeviceContainer allDevices = leo.Install (satellites, gateways, terminals); + + // we want to ping terminals + UdpEchoServerHelper echoServer (9); + ApplicationContainer serverApps = echoServer.Install (terminals); + + // TODO routing + + // install a client on each of the terminals + ApplicationContainer clientApps; + for (uint32_t i = 1; i < terminals.GetN (); i++) + { + Address remote = terminals.Get (i)->GetObject ()->GetAddress (1, 0).GetAddress (); + UdpEchoClientHelper echoClient (remote, 9); + echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); + echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); + echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); + + clientApps.Add (echoClient.Install (terminals.Get (i-1))); + } + + clientApps.Start (Seconds (2.0)); + clientApps.Stop (Seconds (10.0)); + + serverApps.Start (Seconds (1.0)); + serverApps.Stop (Seconds (10.0)); + + Simulator::Run (); } // The TestSuite class names the TestSuite, identifies what type of TestSuite, @@ -57,12 +92,11 @@ public: }; LeoTestSuite::LeoTestSuite () - : TestSuite ("leo", UNIT) + : TestSuite ("leo", EXAMPLE) { // TestDuration for TestCase can be QUICK, EXTENSIVE or TAKES_FOREVER - AddTestCase (new LeoTestCase1, TestCase::QUICK); + AddTestCase (new LeoTestCase1, TestCase::EXTENSIVE); } // Do not forget to allocate an instance of this TestSuite static LeoTestSuite leoTestSuite; - diff --git a/wscript b/wscript index 2ab04d5..d0f6353 100644 --- a/wscript +++ b/wscript @@ -26,6 +26,7 @@ def build(bld): module_test = bld.create_ns3_module_test_library('leo') module_test.source = [ 'test/leo-test-suite.cc', + 'test/isl-test-suite.cc', ] headers = bld(features='ns3header')