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
splitstring-test-suite.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2022 Lawrence Livermore National Laboratory
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7
*/
8
9
#include "ns3/string.h"
10
#include "ns3/test.h"
11
12
namespace
ns3
13
{
14
15
namespace
tests
16
{
17
18
/**
19
* \file
20
* \ingroup core-tests
21
* SplitString test suite implementation.
22
*/
23
24
/**
25
* \ingroup core-tests
26
* \defgroup environ-var-tests Environment variable caching tests
27
*/
28
29
/**
30
* \ingroup core-tests
31
*
32
* SplitString tests.
33
*/
34
class
SplitStringTestCase
:
public
TestCase
35
{
36
public
:
37
/** Constructor */
38
SplitStringTestCase
();
39
40
/** Destructor */
41
~SplitStringTestCase
()
override
=
default
;
42
43
private
:
44
/** Run the tests */
45
void
DoRun
()
override
;
46
47
/**
48
* Read \p str and check that it contains only the key,value pairs
49
* from \p expect.
50
* \param where The test condition being checked.
51
* \param str The environment variable to parse and check.
52
* \param expect The set of key,values expected.
53
*/
54
void
Check
(
const
std::string& where,
const
std::string& str,
const
StringVector
& expect);
55
56
/** Test suite delimiter. */
57
const
std::string
m_delimiter
{
":|:"
};
58
59
};
// class SplitStringTestCase
60
61
SplitStringTestCase::SplitStringTestCase
()
62
:
TestCase
(
"split-string"
)
63
{
64
}
65
66
void
67
SplitStringTestCase::Check
(
const
std::string& where,
68
const
std::string& str,
69
const
StringVector
& expect)
70
{
71
const
StringVector
res =
SplitString
(str,
m_delimiter
);
72
73
// Print the res and expect
74
std::cout << where <<
": '"
<< str <<
"'\nindex\texpect["
<< expect.size() <<
"]\t\tresult["
75
<< res.size() <<
"]"
<< (expect.size() != res.size() ?
"\tFAIL SIZE"
:
""
)
76
<<
"\n "
;
77
NS_TEST_EXPECT_MSG_EQ
(expect.size(),
78
res.size(),
79
"res and expect have different number of entries"
);
80
for
(std::size_t i = 0; i < std::max(res.size(), expect.size()); ++i)
81
{
82
const
std::string r = (i < res.size() ? res[i] :
"''"
+ std::to_string(i));
83
const
std::string e = (i < expect.size() ? expect[i] :
"''"
+ std::to_string(i));
84
const
bool
ok = (r == e);
85
std::cout << i <<
"\t'"
<< e << (ok ?
"'\t== '"
:
"'\t!= '"
) << r
86
<< (!ok ?
"'\tFAIL MATCH"
:
"'"
) <<
"\n "
;
87
NS_TEST_EXPECT_MSG_EQ
(e, r,
"res[i] does not match expect[i]"
);
88
}
89
std::cout << std::endl;
90
}
91
92
void
93
SplitStringTestCase::DoRun
()
94
{
95
// Test points
96
97
// Empty string
98
Check
(
"empty"
,
""
, {
""
});
99
100
// No delimiter
101
Check
(
"no-delim"
,
"token"
, {
"token"
});
102
103
// Extra leading, trailing delimiter: ":string", "string:"
104
Check
(
"front-:|:"
,
":|:token"
, {
""
,
"token"
});
105
Check
(
"back-:|:"
,
"token:|:"
, {
"token"
,
""
});
106
107
// Double delimiter: ":|::|:token", "token:|::|:"
108
Check
(
"front-:|::|:"
,
":|::|:token"
, {
""
,
""
,
"token"
});
109
Check
(
"back-:|::|:"
,
"token:|::|:"
, {
"token"
,
""
,
""
});
110
111
// Two tokens: "token1:|:token2"
112
Check
(
"two"
,
"token1:|:token2"
, {
"token1"
,
"token2"
});
113
114
// Extra/double delimiters:|: ":|::|:token1:|:token2", ":|:token1:|:token2",
115
// "token1:|::|:token2", "token1:|:token2:|:",
116
Check
(
":|::|:two"
,
":|::|:token1:|:token2"
, {
""
,
""
,
"token1"
,
"token2"
});
117
Check
(
":|:one:|:two"
,
":|:token1:|:token2"
, {
""
,
"token1"
,
"token2"
});
118
Check
(
"double:|:"
,
"token1:|::|:token2"
, {
"token1"
,
""
,
"token2"
});
119
Check
(
"two:|:"
,
"token1:|:token2:|::|:"
, {
"token1"
,
"token2"
,
""
,
""
});
120
}
121
122
/**
123
* \ingroup typeid-tests
124
*
125
* TypeId test suites.
126
*/
127
class
SplitStringTestSuite
:
public
TestSuite
128
{
129
public
:
130
SplitStringTestSuite
();
131
};
132
133
SplitStringTestSuite::SplitStringTestSuite
()
134
:
TestSuite
(
"split-string"
)
135
{
136
AddTestCase
(
new
SplitStringTestCase
);
137
}
138
139
/**
140
* \ingroup environ-var-tests
141
* Static variable for test initialization.
142
*/
143
static
SplitStringTestSuite
g_SplitStringTestSuite
;
144
145
}
// namespace tests
146
147
}
// namespace ns3
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::tests::SplitStringTestCase
SplitString tests.
Definition
splitstring-test-suite.cc:35
ns3::tests::SplitStringTestCase::Check
void Check(const std::string &where, const std::string &str, const StringVector &expect)
Read str and check that it contains only the key,value pairs from expect.
Definition
splitstring-test-suite.cc:67
ns3::tests::SplitStringTestCase::m_delimiter
const std::string m_delimiter
Test suite delimiter.
Definition
splitstring-test-suite.cc:57
ns3::tests::SplitStringTestCase::~SplitStringTestCase
~SplitStringTestCase() override=default
Destructor.
ns3::tests::SplitStringTestCase::DoRun
void DoRun() override
Run the tests.
Definition
splitstring-test-suite.cc:93
ns3::tests::SplitStringTestCase::SplitStringTestCase
SplitStringTestCase()
Constructor.
Definition
splitstring-test-suite.cc:61
ns3::tests::SplitStringTestSuite
TypeId test suites.
Definition
splitstring-test-suite.cc:128
ns3::tests::SplitStringTestSuite::SplitStringTestSuite
SplitStringTestSuite()
Definition
splitstring-test-suite.cc:133
ns3::tests::g_SplitStringTestSuite
static SplitStringTestSuite g_SplitStringTestSuite
Static variable for test initialization.
Definition
splitstring-test-suite.cc:143
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:241
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SplitString
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
Definition
string.cc:23
ns3::StringVector
std::vector< std::string > StringVector
Return type of SplitString.
Definition
string.h:26
src
core
test
splitstring-test-suite.cc
Generated on Fri Nov 8 2024 13:59:00 for ns-3 by
1.11.0