A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
des-metrics-test-suite.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 * Author: Srajan Gupta <srajangupta1818@gmail.com>
4 */
5
6#include "ns3/des-metrics.h"
7#include "ns3/system-path.h"
8#include "ns3/test.h"
9
10#include <fstream>
11#include <list>
12#include <sstream>
13#include <string>
14
15/**
16 * @file
17 * @ingroup core-tests
18 * Tests for the DesMetrics trace file header.
19 */
20
21namespace ns3
22{
23
24namespace tests
25{
26
27/**
28 * @ingroup core-tests
29 * Verify that DesMetrics writes the command line arguments into the JSON
30 * header, and that it emits a placeholder when no arguments are available.
31 */
33{
34 public:
37
38 private:
39 void DoRun() override;
40};
41
43 : TestCase("Check DesMetrics command line arguments header")
44{
45}
46
47void
49{
50 std::string tempDir = CreateTempDirFilename("des-metrics-test.json");
51 std::list<std::string> components = SystemPath::Split(tempDir);
52 components.pop_back();
53 tempDir = SystemPath::Join(components.begin(), components.end());
54
55 // The first file, named after args[0], must record the arguments.
56 // The second Initialize call closes the previous file. The second file,
57 // opened with no arguments, must record the placeholder instead.
58 DesMetrics::Get()->Initialize({"des-metrics-test", "--foo", "bar"}, tempDir);
59 DesMetrics::Get()->Initialize({}, tempDir);
60 DesMetrics::Get()->Initialize({"des-metrics-test-close"}, tempDir);
61
62 auto readFile = [](const std::string& path) {
63 std::ifstream file(path);
64 std::ostringstream contents;
65 contents << file.rdbuf();
66 return contents.str();
67 };
68
69 const std::string withArgs = readFile(SystemPath::Append(tempDir, "des-metrics-test.json"));
70 const std::string withoutArgs = readFile(SystemPath::Append(tempDir, "desTraceFile.json"));
71
72 NS_TEST_ASSERT_MSG_NE(withArgs.find("\"command_line_arguments\" : "
73 "\"des-metrics-test --foo bar\","),
74 std::string::npos,
75 "Command line arguments were not written into the header");
76 NS_TEST_ASSERT_MSG_NE(withoutArgs.find("\"command_line_arguments\" : "
77 "\"[argv empty or not available]\","),
78 std::string::npos,
79 "Placeholder was not written for empty arguments");
80}
81
82/**
83 * @ingroup core-tests
84 * DesMetrics test suite.
85 */
87{
88 public:
90};
91
97
98/**
99 * @ingroup core-tests
100 * DesMetricsTestSuite instance variable.
101 */
103
104} // namespace tests
105
106} // namespace ns3
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition test.cc:436
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Verify that DesMetrics writes the command line arguments into the JSON header, and that it emits a pl...
void DoRun() override
Implementation to actually run this TestCase.
static DesMetricsTestSuite g_desMetricsTestSuite
DesMetricsTestSuite instance variable.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
std::string Append(std::string left, std::string right)
Join two file system path elements.
std::string Join(std::list< std::string >::const_iterator begin, std::list< std::string >::const_iterator end)
Join a list of file system path directories into a single file system path.
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition test.h:553
Namespace for test files, TestCases and TestSuites.
Every class exported by the ns3 library is enclosed in the ns3 namespace.