A Discrete-Event Network Simulator
API
example-as-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 Lawrence Livermore National Laboratory
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: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
21 #include "example-as-test.h"
22 #include "ascii-test.h"
23 #include "log.h"
24 #include "unused.h"
25 #include "assert.h"
26 
27 #include <string>
28 #include <sstream>
29 #include <cstdlib> // itoa(), system ()
30 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("ExampleAsTestCase");
40 
41 // Running tests as examples currently requires bash shell; uses Unix
42 // piping that does not work on Windows.
43 #if defined(NS3_ENABLE_EXAMPLES) && !defined (__win32__)
44 
45 ExampleAsTestCase::ExampleAsTestCase (const std::string name,
46  const std::string program,
47  const std::string dataDir,
48  const std::string args /* = "" */)
49  : TestCase (name),
50  m_program (program),
51  m_dataDir (dataDir),
52  m_args (args)
53 {
54  NS_LOG_FUNCTION (this << name << program << dataDir << args);
55 }
56 
58 {
60 }
61 
62 std::string
64 {
66  std::string command ("%s ");
67  command += m_args;
68  return command;
69 }
70 
71 std::string
73 {
75  std::string command ("");
76  return command;
77 }
78 
79 void
81 {
83  // Set up the output file names
85  std::string refFile = CreateDataDirFilename (GetName () + ".reflog");
86  std::string testFile = CreateTempDirFilename (GetName () + ".reflog");
87 
88  std::stringstream ss;
89 
90  // Use bash as shell to allow use of PIPESTATUS
91  ss << "bash -c './waf --run-no-build " << m_program
92  << " --command-template=\"" << GetCommandTemplate () << "\""
93 
94  // redirect std::clog, std::cerr to std::cout
95  << " 2>&1 "
96 
97  // Suppress the waf lines from output; waf output contains directory paths which will
98  // obviously differ during a test run
99  << " | grep -v 'Waf:' "
101  << " > " << testFile
102 
103  // Get the status of waf
104  << "; exit ${PIPESTATUS[0]}'";
105 
106  int status = std::system (ss.str ().c_str ());
107 
108  std::cout << "command: " << ss.str () << "\n"
109  << "status: " << status << "\n"
110  << "refFile: " << refFile << "\n"
111  << "testFile: " << testFile << "\n"
112  << std::endl;
113  std::cout << "testFile contents:" << std::endl;
114 
115  std::ifstream logF (testFile);
116  std::string line;
117  while (getline (logF, line))
118  {
119  std::cout << line << "\n";
120  }
121  logF.close ();
122 
123  // Make sure the example didn't outright crash
124  NS_TEST_ASSERT_MSG_EQ (status, 0, "example " + m_program + " failed");
125 
126  // Compare the testFile to the reference file
127  NS_ASCII_TEST_EXPECT_EQ (testFile, refFile);
128 }
129 
130 ExampleAsTestSuite::ExampleAsTestSuite (const std::string name,
131  const std::string program,
132  const std::string dataDir,
133  const std::string args /* = "" */,
134  const TestDuration duration /* =QUICK */)
135  : TestSuite (name, EXAMPLE)
136 {
137  NS_LOG_FUNCTION (this << name << program << dataDir << args << duration);
138  AddTestCase (new ExampleAsTestCase (name, program, dataDir, args), duration);
139 }
140 
141 #endif // NS3_ENABLE_EXAMPLES && !defined (__win32__)
142 
143 } // namespace ns3
virtual void DoRun(void)
Implementation to actually run this TestCase.
ExampleAsTestSuite(const std::string name, const std::string program, const std::string dataDir, const std::string args="", const TestDuration duration=QUICK)
Constructor.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual std::string GetPostProcessingCommand(void) const
Customization point for tests requiring post-processing of stdout.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
std::string m_args
Any additional arguments to the program.
Enable examples to be run as meaningful tests.
ExampleAsTestCase(const std::string name, const std::string program, const std::string dataDir, const std::string args="")
Constructor.
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:166
#define NS_ASCII_TEST_EXPECT_EQ(gotFilename, expectedFilename)
Test that a pair of new/reference ascii files are equal.
Definition: ascii-test.h:38
std::string m_dataDir
The source directory for the test.
virtual ~ExampleAsTestCase(void)
Destructor.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string CreateDataDirFilename(std::string filename)
Construct the full path to a file in the data directory.
Definition: test.cc:412
std::string GetName(void) const
Definition: test.cc:370
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition: test.cc:430
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition: test.cc:465
std::string m_program
The program to run.
virtual std::string GetCommandTemplate(void) const
Customization point for more complicated patterns to invoke the example program.
Debug message logging.
NS_UNUSED and NS_UNUSED_GLOBAL macro definitions.