A Discrete-Event Network Simulator
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 * 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
23namespace ns3
24{
25
26namespace tests
27{
28
29/**
30 * \file
31 * \ingroup core-tests
32 * SplitString test suite implementation.
33 */
34
35/**
36 * \ingroup core-tests
37 * \defgroup environ-var-tests Environment variable caching tests
38 */
39
40/**
41 * \ingroup core-tests
42 *
43 * SplitString tests.
44 */
46{
47 public:
48 /** Constructor */
50
51 /** Destructor */
52 ~SplitStringTestCase() override = default;
53
54 private:
55 /** Run the tests */
56 void DoRun() override;
57
58 /**
59 * Read \p str and check that it contains only the key,value pairs
60 * from \p expect.
61 * \param where The test condition being checked.
62 * \param str The environment variable to parse and check.
63 * \param expect The set of key,values expected.
64 */
65 void Check(const std::string& where, const std::string& str, const StringVector& expect);
66
67 /** Test suite delimiter. */
68 const std::string m_delimiter{":|:"};
69
70}; // class SplitStringTestCase
71
73 : TestCase("split-string")
74{
75}
76
77void
78SplitStringTestCase::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
103void
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
133/**
134 * \ingroup typeid-tests
135 *
136 * TypeId test suites.
137 */
139{
140 public:
142};
143
145 : TestSuite("split-string")
146{
148}
149
150/**
151 * \ingroup environ-var-tests
152 * Static variable for test initialization.
153 */
155
156} // namespace tests
157
158} // namespace ns3
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
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.
const std::string m_delimiter
Test suite delimiter.
~SplitStringTestCase() override=default
Destructor.
void DoRun() override
Run the tests.
static SplitStringTestSuite g_SplitStringTestSuite
Static variable for test initialization.
#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:252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::vector< std::string > StringVector
Return type of SplitString.
Definition: string.h:37
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
Definition: string.cc:34