A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
21
namespace
ns3
22
{
23
24
namespace
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
*/
32
class
DesMetricsCommandLineArgsTestCase
:
public
TestCase
33
{
34
public
:
35
DesMetricsCommandLineArgsTestCase
();
36
~DesMetricsCommandLineArgsTestCase
()
override
=
default
;
37
38
private
:
39
void
DoRun
()
override
;
40
};
41
42
DesMetricsCommandLineArgsTestCase::DesMetricsCommandLineArgsTestCase
()
43
:
TestCase
(
"Check DesMetrics command line arguments header"
)
44
{
45
}
46
47
void
48
DesMetricsCommandLineArgsTestCase::DoRun
()
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
*/
86
class
DesMetricsTestSuite
:
public
TestSuite
87
{
88
public
:
89
DesMetricsTestSuite
();
90
};
91
92
DesMetricsTestSuite::DesMetricsTestSuite
()
93
:
TestSuite
(
"des-metrics"
,
Type
::UNIT)
94
{
95
AddTestCase
(
new
DesMetricsCommandLineArgsTestCase
,
TestCase::Duration::QUICK
);
96
}
97
98
/**
99
* @ingroup core-tests
100
* DesMetricsTestSuite instance variable.
101
*/
102
static
DesMetricsTestSuite
g_desMetricsTestSuite
;
103
104
}
// namespace tests
105
106
}
// namespace ns3
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:296
ns3::TestCase::Duration::QUICK
@ QUICK
Fast test.
Definition
test.h:1057
ns3::TestCase::CreateTempDirFilename
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition
test.cc:436
ns3::TestCase::TestCase
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1271
ns3::TestSuite::TestSuite
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition
test.cc:494
ns3::tests::DesMetricsCommandLineArgsTestCase
Verify that DesMetrics writes the command line arguments into the JSON header, and that it emits a pl...
Definition
des-metrics-test-suite.cc:33
ns3::tests::DesMetricsCommandLineArgsTestCase::DesMetricsCommandLineArgsTestCase
DesMetricsCommandLineArgsTestCase()
Definition
des-metrics-test-suite.cc:42
ns3::tests::DesMetricsCommandLineArgsTestCase::~DesMetricsCommandLineArgsTestCase
~DesMetricsCommandLineArgsTestCase() override=default
ns3::tests::DesMetricsCommandLineArgsTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
des-metrics-test-suite.cc:48
ns3::tests::DesMetricsTestSuite
DesMetrics test suite.
Definition
des-metrics-test-suite.cc:87
ns3::tests::DesMetricsTestSuite::DesMetricsTestSuite
DesMetricsTestSuite()
Definition
des-metrics-test-suite.cc:92
ns3::tests::g_desMetricsTestSuite
static DesMetricsTestSuite g_desMetricsTestSuite
DesMetricsTestSuite instance variable.
Definition
des-metrics-test-suite.cc:102
ns3::SystemPath::Split
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
Definition
system-path.cc:236
ns3::SystemPath::Append
std::string Append(std::string left, std::string right)
Join two file system path elements.
Definition
system-path.cc:218
ns3::SystemPath::Join
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.
Definition
system-path.cc:245
NS_TEST_ASSERT_MSG_NE
#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
ns3::tests
Namespace for test files, TestCases and TestSuites.
Definition
environment-variable.h:32
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
test
des-metrics-test-suite.cc
Generated on
for ns-3 by
1.16.1