A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
splitstring-test-suite.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2022 Lawrence Livermore National Laboratory
3
*
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation;
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*
17
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18
*/
19
20
#include "ns3/string.h"
21
#include "ns3/test.h"
22
23
namespace
ns3
24
{
25
26
namespace
tests
27
{
28
45
class
SplitStringTestCase
:
public
TestCase
46
{
47
public
:
49
SplitStringTestCase
();
50
52
~SplitStringTestCase
()
override
=
default
;
53
54
private
:
56
void
DoRun
()
override
;
57
65
void
Check
(
const
std::string& where,
const
std::string& str,
const
StringVector
& expect);
66
68
const
std::string
m_delimiter
{
":|:"
};
69
70
};
// class SplitStringTestCase
71
72
SplitStringTestCase::SplitStringTestCase
()
73
:
TestCase
(
"split-string"
)
74
{
75
}
76
77
void
78
SplitStringTestCase::Check
(
const
std::string& where,
79
const
std::string& str,
80
const
StringVector
& expect)
81
{
82
const
StringVector
res
=
SplitString
(str,
m_delimiter
);
83
84
// Print the res and expect
85
std::cout << where <<
": '"
<< str <<
"'\nindex\texpect["
<< expect.size() <<
"]\t\tresult["
86
<<
res
.size() <<
"]"
<< (expect.size() !=
res
.size() ?
"\tFAIL SIZE"
:
""
)
87
<<
"\n "
;
88
NS_TEST_EXPECT_MSG_EQ
(expect.size(),
89
res
.size(),
90
"res and expect have different number of entries"
);
91
for
(std::size_t i = 0; i <
std::max
(
res
.size(), expect.size()); ++i)
92
{
93
const
std::string r = (i <
res
.size() ?
res
[i] :
"''"
+
std::to_string
(i));
94
const
std::string e = (i < expect.size() ? expect[i] :
"''"
+
std::to_string
(i));
95
const
bool
ok = (r == e);
96
std::cout << i <<
"\t'"
<< e << (ok ?
"'\t== '"
:
"'\t!= '"
) << r
97
<< (!ok ?
"'\tFAIL MATCH"
:
"'"
) <<
"\n "
;
98
NS_TEST_EXPECT_MSG_EQ
(e, r,
"res[i] does not match expect[i]"
);
99
}
100
std::cout << std::endl;
101
}
102
103
void
104
SplitStringTestCase::DoRun
()
105
{
106
// Test points
107
108
// Empty string
109
Check
(
"empty"
,
""
, {
""
});
110
111
// No delimiter
112
Check
(
"no-delim"
,
"token"
, {
"token"
});
113
114
// Extra leading, trailing delimiter: ":string", "string:"
115
Check
(
"front-:|:"
,
":|:token"
, {
""
,
"token"
});
116
Check
(
"back-:|:"
,
"token:|:"
, {
"token"
,
""
});
117
118
// Double delimiter: ":|::|:token", "token:|::|:"
119
Check
(
"front-:|::|:"
,
":|::|:token"
, {
""
,
""
,
"token"
});
120
Check
(
"back-:|::|:"
,
"token:|::|:"
, {
"token"
,
""
,
""
});
121
122
// Two tokens: "token1:|:token2"
123
Check
(
"two"
,
"token1:|:token2"
, {
"token1"
,
"token2"
});
124
125
// Extra/double delimiters:|: ":|::|:token1:|:token2", ":|:token1:|:token2",
126
// "token1:|::|:token2", "token1:|:token2:|:",
127
Check
(
":|::|:two"
,
":|::|:token1:|:token2"
, {
""
,
""
,
"token1"
,
"token2"
});
128
Check
(
":|:one:|:two"
,
":|:token1:|:token2"
, {
""
,
"token1"
,
"token2"
});
129
Check
(
"double:|:"
,
"token1:|::|:token2"
, {
"token1"
,
""
,
"token2"
});
130
Check
(
"two:|:"
,
"token1:|:token2:|::|:"
, {
"token1"
,
"token2"
,
""
,
""
});
131
}
132
138
class
SplitStringTestSuite
:
public
TestSuite
139
{
140
public
:
141
SplitStringTestSuite
();
142
};
143
144
SplitStringTestSuite::SplitStringTestSuite
()
145
:
TestSuite
(
"split-string"
)
146
{
147
AddTestCase
(
new
SplitStringTestCase
);
148
}
149
154
static
SplitStringTestSuite
g_SplitStringTestSuite
;
155
156
}
// namespace tests
157
158
}
// namespace ns3
max
#define max(a, b)
Definition:
80211b.c:42
ns3::TestCase
encapsulates test code
Definition:
test.h:1060
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:301
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1256
ns3::tests::SplitStringTestCase
SplitString tests.
Definition:
splitstring-test-suite.cc:46
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:78
ns3::tests::SplitStringTestCase::m_delimiter
const std::string m_delimiter
Test suite delimiter.
Definition:
splitstring-test-suite.cc:68
ns3::tests::SplitStringTestCase::~SplitStringTestCase
~SplitStringTestCase() override=default
Destructor.
ns3::tests::SplitStringTestCase::DoRun
void DoRun() override
Run the tests.
Definition:
splitstring-test-suite.cc:104
ns3::tests::SplitStringTestCase::SplitStringTestCase
SplitStringTestCase()
Constructor.
Definition:
splitstring-test-suite.cc:72
ns3::tests::SplitStringTestSuite
TypeId test suites.
Definition:
splitstring-test-suite.cc:139
ns3::tests::SplitStringTestSuite::SplitStringTestSuite
SplitStringTestSuite()
Definition:
splitstring-test-suite.cc:144
ns3::tests::g_SplitStringTestSuite
static SplitStringTestSuite g_SplitStringTestSuite
Static variable for test initialization.
Definition:
splitstring-test-suite.cc:154
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:251
nlohmann::to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition:
json.h:25255
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::StringVector
std::vector< std::string > StringVector
Return type of SplitString.
Definition:
string.h:37
ns3::SplitString
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
Definition:
string.cc:34
two-ray-to-three-gpp-ch-calibration.res
res
Definition:
two-ray-to-three-gpp-ch-calibration.py:549
src
core
test
splitstring-test-suite.cc
Generated on Sun Mar 3 2024 17:10:56 for ns-3 by
1.9.1