A Discrete-Event Network Simulator
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 
46 {
47  public:
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 
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 {
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
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 
139 {
140  public:
142 };
143 
145  : TestSuite("split-string")
146 {
148 }
149 
155 
156 } // namespace tests
157 
158 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
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:251
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
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