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 * 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
12namespace ns3
13{
14
15namespace 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 */
35{
36 public:
37 /** Constructor */
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 // end of class SplitStringTestCase
60};
61
66
67void
68SplitStringTestCase::Check(const std::string& where,
69 const std::string& str,
70 const StringVector& expect)
71{
72 const StringVector res = SplitString(str, m_delimiter);
73
74 // Print the res and expect
75 std::cout << where << ": '" << str << "'\nindex\texpect[" << expect.size() << "]\t\tresult["
76 << res.size() << "]" << (expect.size() != res.size() ? "\tFAIL SIZE" : "")
77 << "\n ";
78 NS_TEST_EXPECT_MSG_EQ(expect.size(),
79 res.size(),
80 "res and expect have different number of entries");
81 for (std::size_t i = 0; i < std::max(res.size(), expect.size()); ++i)
82 {
83 const std::string r = (i < res.size() ? res[i] : "''" + std::to_string(i));
84 const std::string e = (i < expect.size() ? expect[i] : "''" + std::to_string(i));
85 const bool ok = (r == e);
86 std::cout << i << "\t'" << e << (ok ? "'\t== '" : "'\t!= '") << r
87 << (!ok ? "'\tFAIL MATCH" : "'") << "\n ";
88 NS_TEST_EXPECT_MSG_EQ(e, r, "res[i] does not match expect[i]");
89 }
90 std::cout << std::endl;
91}
92
93void
95{
96 // Test points
97
98 // Empty string
99 Check("empty", "", {""});
100
101 // No delimiter
102 Check("no-delim", "token", {"token"});
103
104 // Extra leading, trailing delimiter: ":string", "string:"
105 Check("front-:|:", ":|:token", {"", "token"});
106 Check("back-:|:", "token:|:", {"token", ""});
107
108 // Double delimiter: ":|::|:token", "token:|::|:"
109 Check("front-:|::|:", ":|::|:token", {"", "", "token"});
110 Check("back-:|::|:", "token:|::|:", {"token", "", ""});
111
112 // Two tokens: "token1:|:token2"
113 Check("two", "token1:|:token2", {"token1", "token2"});
114
115 // Extra/double delimiters:|: ":|::|:token1:|:token2", ":|:token1:|:token2",
116 // "token1:|::|:token2", "token1:|:token2:|:",
117 Check(":|::|:two", ":|::|:token1:|:token2", {"", "", "token1", "token2"});
118 Check(":|:one:|:two", ":|:token1:|:token2", {"", "token1", "token2"});
119 Check("double:|:", "token1:|::|:token2", {"token1", "", "token2"});
120 Check("two:|:", "token1:|:token2:|::|:", {"token1", "token2", "", ""});
121}
122
123/**
124 * @ingroup typeid-tests
125 *
126 * TypeId test suites.
127 */
129{
130 public:
132};
133
139
140/**
141 * @ingroup environ-var-tests
142 * Static variable for test initialization.
143 */
145
146} // namespace tests
147
148} // namespace ns3
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
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:241
Every class exported by the ns3 library is enclosed in the ns3 namespace.
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
Definition string.cc:23
std::vector< std::string > StringVector
Return type of SplitString.
Definition string.h:26