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
log-test-suite.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7
*/
8
9
#include "ns3/example-as-test.h"
10
#include "ns3/log.h"
11
#include "ns3/test.h"
12
13
#include <list>
14
#include <map>
15
#include <set>
16
#include <sstream>
17
#include <vector>
18
19
/**
20
* @file
21
* @ingroup core-tests
22
* @defgroup logging-tests Logging test suite
23
* Logging test suite, checking the log lines emitted by log-line-test-example.
24
*/
25
26
namespace
ns3
27
{
28
29
namespace
tests
30
{
31
32
/**
33
* @ingroup logging-tests
34
* Verify that ParameterLogger limits the number of logged container elements.
35
*/
36
class
ParameterLoggerTestCase
:
public
TestCase
37
{
38
public
:
39
/** Constructor. */
40
ParameterLoggerTestCase
();
41
42
private
:
43
void
DoRun
()
override
;
44
};
45
46
ParameterLoggerTestCase::ParameterLoggerTestCase
()
47
:
TestCase
(
"Check ParameterLogger container output"
)
48
{
49
}
50
51
void
52
ParameterLoggerTestCase::DoRun
()
53
{
54
std::ostringstream stream;
55
ParameterLogger
logger(stream);
56
const
auto
originalMaximum = logger.
GetMaxLoggedContainerElements
();
57
logger.
SetMaxLoggedContainerElements
(2);
58
59
logger << std::vector<int>{1, 2, 3} << 4;
60
NS_TEST_EXPECT_MSG_EQ
(stream.str(),
"vector:[1, 2, ...], 4"
,
"Unexpected vector output"
);
61
62
stream.str(
""
);
63
ParameterLogger
(stream) << std::list<int>{1, 2, 3};
64
NS_TEST_EXPECT_MSG_EQ
(stream.str(),
"list:[1, 2, ...]"
,
"Unexpected list output"
);
65
66
stream.str(
""
);
67
ParameterLogger
(stream) << std::set<int>{1, 2, 3};
68
NS_TEST_EXPECT_MSG_EQ
(stream.str(),
"set:[1, 2, ...]"
,
"Unexpected set output"
);
69
70
stream.str(
""
);
71
ParameterLogger
(stream) << std::map<int, int>{{1, 10}, {2, 20}, {3, 30}};
72
NS_TEST_EXPECT_MSG_EQ
(stream.str(),
"map:[1: 10, 2: 20, ...]"
,
"Unexpected map output"
);
73
74
stream.str(
""
);
75
ParameterLogger
(stream) << std::vector<int>{1, 2};
76
NS_TEST_EXPECT_MSG_EQ
(stream.str(),
"vector:[1, 2]"
,
"Unexpected exact-limit output"
);
77
78
logger.
SetMaxLoggedContainerElements
(0);
79
stream.str(
""
);
80
ParameterLogger
(stream) << std::vector<int>{1, 2, 3};
81
NS_TEST_EXPECT_MSG_EQ
(stream.str(),
"vector:[...]"
,
"Unexpected zero-limit output"
);
82
83
logger.
SetMaxLoggedContainerElements
(originalMaximum);
84
}
85
86
/**
87
* @ingroup logging-tests
88
* Run log-line-test-example as a test, checking the assembled log lines: the
89
* time, node, function and context prefixes, nested logging from a
90
* user-defined operator<<, and the line in progress emitted when the
91
* program dies while assembling it.
92
*/
93
class
LogTestSuite
:
public
TestSuite
94
{
95
public
:
96
LogTestSuite
();
97
};
98
99
LogTestSuite::LogTestSuite
()
100
:
TestSuite
(
"log"
,
Type
::UNIT)
101
{
102
#ifdef NS3_LOG_ENABLE
103
// Log lines are only assembled when logging is compiled in.
104
AddTestCase
(
new
ExampleAsTestCase
(
"core-example-log-line-test"
,
105
"log-line-test-example"
,
106
NS_TEST_SOURCEDIR,
107
"--time=9e18 --context=7 --label=lbl --nested"
));
108
AddTestCase
(
new
ExampleAsTestCase
(
"core-example-log-line-test-fatal"
,
109
"log-line-test-example"
,
110
NS_TEST_SOURCEDIR,
111
"--time=9e18 --context=7 --label=lbl --nested --mode=fatal"
,
112
false
));
113
#ifdef NS3_ASSERT_ENABLE
114
AddTestCase
(
new
ExampleAsTestCase
(
"core-example-log-line-test-assert"
,
115
"log-line-test-example"
,
116
NS_TEST_SOURCEDIR,
117
"--time=9e18 --context=7 --label=lbl --mode=assert"
,
118
false
));
119
#endif
120
#endif
121
122
AddTestCase
(
new
ParameterLoggerTestCase
,
TestCase::Duration::QUICK
);
123
}
124
125
/**
126
* @ingroup logging-tests
127
* LogTestSuite instance variable.
128
*/
129
static
LogTestSuite
g_logTestSuite
;
130
131
}
// namespace tests
132
133
}
// namespace ns3
ns3::ExampleAsTestCase
Execute an example program as a test, by comparing the output to a reference file.
Definition
example-as-test.h:38
ns3::ParameterLogger
Insert , when streaming function arguments.
Definition
log.h:497
ns3::ParameterLogger::SetMaxLoggedContainerElements
void SetMaxLoggedContainerElements(std::size_t max)
Set the maximum number of elements to print for a container.
Definition
log.cc:720
ns3::ParameterLogger::GetMaxLoggedContainerElements
std::size_t GetMaxLoggedContainerElements() const
Definition
log.cc:726
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::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::LogTestSuite
Run log-line-test-example as a test, checking the assembled log lines: the time, node,...
Definition
log-test-suite.cc:94
ns3::tests::LogTestSuite::LogTestSuite
LogTestSuite()
Definition
log-test-suite.cc:99
ns3::tests::ParameterLoggerTestCase
Verify that ParameterLogger limits the number of logged container elements.
Definition
log-test-suite.cc:37
ns3::tests::ParameterLoggerTestCase::ParameterLoggerTestCase
ParameterLoggerTestCase()
Constructor.
Definition
log-test-suite.cc:46
ns3::tests::ParameterLoggerTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
log-test-suite.cc:52
ns3::tests::g_logTestSuite
static LogTestSuite g_logTestSuite
LogTestSuite instance variable.
Definition
log-test-suite.cc:129
NS_TEST_EXPECT_MSG_EQ
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition
test.h:240
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
log-test-suite.cc
Generated on
for ns-3 by
1.16.1