From dd98ef2ee9b17a1a107c8a70444c4ca80c17acd3 Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Thu, 24 Sep 2009 12:15:14 -0700 Subject: [PATCH] Remove and replace CommandLine BVT (bug 675) --- src/core/attribute-test-suite.cc | 2 +- src/core/command-line.cc | 227 ++++++++++++++++++++++++------- 2 files changed, 180 insertions(+), 49 deletions(-) diff --git a/src/core/attribute-test-suite.cc b/src/core/attribute-test-suite.cc index b53db40e6..e240f2753 100644 --- a/src/core/attribute-test-suite.cc +++ b/src/core/attribute-test-suite.cc @@ -1062,6 +1062,7 @@ PointerAttributeTestCase::DoRun (void) } // =========================================================================== +// Test the Attributes of type CallbackVale. // =========================================================================== class CallbackValueTestCase : public TestCase { @@ -1076,7 +1077,6 @@ public: } } - private: virtual bool DoRun (void); diff --git a/src/core/command-line.cc b/src/core/command-line.cc index 77849cb1f..97ddf3838 100644 --- a/src/core/command-line.cc +++ b/src/core/command-line.cc @@ -22,8 +22,10 @@ #include "config.h" #include "global-value.h" #include "type-id.h" +#include "test.h" #include "string.h" #include +#include NS_LOG_COMPONENT_DEFINE ("CommandLine"); @@ -277,30 +279,25 @@ CommandLine::AddValue (const std::string &name, m_items.push_back (item); } - -} // namespace ns3 - -#ifdef RUN_SELF_TESTS - -#include "test.h" -#include - -using namespace ns3; - -class CommandLineTest : public Test +// =========================================================================== +// A test base class that drives Command Line parsing +// =========================================================================== +class CommandLineTestCaseBase : public TestCase { public: - CommandLineTest (); - virtual bool RunTests (void); -private: + CommandLineTestCaseBase (std::string description); + virtual ~CommandLineTestCaseBase () {} + void Parse (const CommandLine &cmd, int n, ...); }; -CommandLineTest::CommandLineTest () - : Test ("CommandLine") -{} +CommandLineTestCaseBase::CommandLineTestCaseBase (std::string description) + : TestCase (description) +{ +} + void -CommandLineTest::Parse (const CommandLine &cmd, int n, ...) +CommandLineTestCaseBase::Parse (const CommandLine &cmd, int n, ...) { char **args = new char* [n+1]; args[0] = (char *) "Test"; @@ -317,41 +314,175 @@ CommandLineTest::Parse (const CommandLine &cmd, int n, ...) cmd.Parse (argc, args); delete [] args; } -bool -CommandLineTest::RunTests (void) + +// =========================================================================== +// Test boolean Command Line processing +// =========================================================================== +class CommandLineBooleanTestCase : public CommandLineTestCaseBase { - bool result = true; +public: + CommandLineBooleanTestCase (); + virtual ~CommandLineBooleanTestCase () {} - bool myBool = false; - uint32_t myUint32 = 10; - std::string myStr = "MyStr"; - int32_t myInt32 = -1; - CommandLine cmd; +private: + virtual bool DoRun (void); - cmd.AddValue ("my-bool", "help", myBool); - Parse (cmd, 1, "--my-bool=0"); - NS_TEST_ASSERT_EQUAL (myBool, false); - Parse (cmd, 1, "--my-bool=1"); - NS_TEST_ASSERT_EQUAL (myBool, true); +}; - cmd.AddValue ("my-uint32", "help", myUint32); - Parse (cmd, 2, "--my-bool=0", "--my-uint32=9"); - NS_TEST_ASSERT_EQUAL (myUint32, 9); - - cmd.AddValue ("my-str", "help", myStr); - Parse (cmd, 2, "--my-bool=0", "--my-str=XX"); - NS_TEST_ASSERT_EQUAL (myStr, "XX"); - - cmd.AddValue ("my-int32", "help", myInt32); - Parse (cmd, 2, "--my-bool=0", "--my-int32=-3"); - NS_TEST_ASSERT_EQUAL (myInt32, -3); - Parse (cmd, 2, "--my-bool=0", "--my-int32=+2"); - NS_TEST_ASSERT_EQUAL (myInt32, +2); - - return result; +CommandLineBooleanTestCase::CommandLineBooleanTestCase () + : CommandLineTestCaseBase ("Check boolean arguments") +{ } +bool +CommandLineBooleanTestCase::DoRun (void) +{ + CommandLine cmd; + bool myBool = true; -static CommandLineTest g_cmdLineTest; + cmd.AddValue ("my-bool", "help", myBool); -#endif /* RUN_SELF_TESTS */ + Parse (cmd, 1, "--my-bool=0"); + NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false"); + + Parse (cmd, 1, "--my-bool=1"); + NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true"); + + return GetErrorStatus (); +} + +// =========================================================================== +// Test int Command Line processing +// =========================================================================== +class CommandLineIntTestCase : public CommandLineTestCaseBase +{ +public: + CommandLineIntTestCase (); + virtual ~CommandLineIntTestCase () {} + +private: + virtual bool DoRun (void); + +}; + +CommandLineIntTestCase::CommandLineIntTestCase () + : CommandLineTestCaseBase ("Check int arguments") +{ +} + +bool +CommandLineIntTestCase::DoRun (void) +{ + CommandLine cmd; + bool myBool = true; + int32_t myInt32 = 10; + + cmd.AddValue ("my-bool", "help", myBool); + cmd.AddValue ("my-int32", "help", myInt32); + + Parse (cmd, 2, "--my-bool=0", "--my-int32=-3"); + NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false"); + NS_TEST_ASSERT_MSG_EQ (myInt32, -3, "Command parser did not correctly set an integer value to -3"); + + Parse (cmd, 2, "--my-bool=1", "--my-int32=+2"); + NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true"); + NS_TEST_ASSERT_MSG_EQ (myInt32, +2, "Command parser did not correctly set an integer value to +2"); + + return GetErrorStatus (); +} + +// =========================================================================== +// Test unsigned int Command Line processing +// =========================================================================== +class CommandLineUnsignedIntTestCase : public CommandLineTestCaseBase +{ +public: + CommandLineUnsignedIntTestCase (); + virtual ~CommandLineUnsignedIntTestCase () {} + +private: + virtual bool DoRun (void); + +}; + +CommandLineUnsignedIntTestCase::CommandLineUnsignedIntTestCase () + : CommandLineTestCaseBase ("Check unsigned int arguments") +{ +} + +bool +CommandLineUnsignedIntTestCase::DoRun (void) +{ + CommandLine cmd; + bool myBool = true; + uint32_t myUint32 = 10; + + cmd.AddValue ("my-bool", "help", myBool); + cmd.AddValue ("my-uint32", "help", myUint32); + + Parse (cmd, 2, "--my-bool=0", "--my-uint32=9"); + + NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to true"); + NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9"); + + return GetErrorStatus (); +} + +// =========================================================================== +// Test string Command Line processing +// =========================================================================== +class CommandLineStringTestCase : public CommandLineTestCaseBase +{ +public: + CommandLineStringTestCase (); + virtual ~CommandLineStringTestCase () {} + +private: + virtual bool DoRun (void); + +}; + +CommandLineStringTestCase::CommandLineStringTestCase () + : CommandLineTestCaseBase ("Check unsigned int arguments") +{ +} + +bool +CommandLineStringTestCase::DoRun (void) +{ + CommandLine cmd; + uint32_t myUint32 = 10; + std::string myStr = "MyStr"; + + cmd.AddValue ("my-uint32", "help", myUint32); + cmd.AddValue ("my-str", "help", myStr); + + Parse (cmd, 2, "--my-uint32=9", "--my-str=XX"); + + NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9"); + NS_TEST_ASSERT_MSG_EQ (myStr, "XX", "Command parser did not correctly set an string value to \"XX\""); + + return GetErrorStatus (); +} + +// =========================================================================== +// The Test Suite that glues all of the Test Cases together. +// =========================================================================== +class CommandLineTestSuite : public TestSuite +{ +public: + CommandLineTestSuite (); +}; + +CommandLineTestSuite::CommandLineTestSuite () + : TestSuite ("CommandLine", BVT) +{ + AddTestCase (new CommandLineBooleanTestCase); + AddTestCase (new CommandLineIntTestCase); + AddTestCase (new CommandLineUnsignedIntTestCase); + AddTestCase (new CommandLineStringTestCase); +} + +CommandLineTestSuite CommandLineTestSuite; + +} // namespace ns3