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}; // class SplitStringTestCase
60
65
66void
67SplitStringTestCase::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
92void
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 */
128{
129 public:
131};
132
138
139/**
140 * \ingroup environ-var-tests
141 * Static variable for test initialization.
142 */
144
145} // namespace tests
146
147} // 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