A Discrete-Event Network Simulator
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
26namespace ns3
27{
28
29namespace tests
30{
31
32/**
33 * @ingroup logging-tests
34 * Verify that ParameterLogger limits the number of logged container elements.
35 */
37{
38 public:
39 /** Constructor. */
41
42 private:
43 void DoRun() override;
44};
45
47 : TestCase("Check ParameterLogger container output")
48{
49}
50
51void
53{
54 std::ostringstream stream;
55 ParameterLogger logger(stream);
56 const auto originalMaximum = logger.GetMaxLoggedContainerElements();
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
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 */
93class LogTestSuite : public TestSuite
94{
95 public:
97};
98
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
123}
124
125/**
126 * @ingroup logging-tests
127 * LogTestSuite instance variable.
128 */
130
131} // namespace tests
132
133} // namespace ns3
Execute an example program as a test, by comparing the output to a reference file.
Insert , when streaming function arguments.
Definition log.h:497
void SetMaxLoggedContainerElements(std::size_t max)
Set the maximum number of elements to print for a container.
Definition log.cc:720
std::size_t GetMaxLoggedContainerElements() const
Definition log.cc:726
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
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
Run log-line-test-example as a test, checking the assembled log lines: the time, node,...
Verify that ParameterLogger limits the number of logged container elements.
void DoRun() override
Implementation to actually run this TestCase.
static LogTestSuite g_logTestSuite
LogTestSuite instance variable.
#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
Namespace for test files, TestCases and TestSuites.
Every class exported by the ns3 library is enclosed in the ns3 namespace.