A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
pair-value-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Caliola Engineering, LLC.
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: Jared Dulmage <jared.dulmage@caliola.com>
18 */
19
20#include <ns3/double.h>
21#include <ns3/integer.h>
22#include <ns3/log.h>
23#include <ns3/object.h>
24#include <ns3/pair.h>
25#include <ns3/ptr.h>
26#include <ns3/string.h>
27#include <ns3/test.h>
28#include <ns3/type-id.h>
29
30#include <algorithm>
31#include <iterator>
32#include <sstream>
33#include <utility>
34
35using namespace ns3;
36
37NS_LOG_COMPONENT_DEFINE("PairTestSuite");
38
55class PairObject : public Object
56{
57 public:
58 PairObject();
59 ~PairObject() override;
60
65 static TypeId GetTypeId();
66
67 friend std::ostream& operator<<(std::ostream& os, const PairObject& obj);
68
69 private:
70 std::pair<std::string, std::string> m_stringPair;
71 std::pair<double, int> m_doubleIntPair;
72};
73
75{
76}
77
79{
80}
81
84{
85 static TypeId tid =
86 TypeId("ns3::PairObject")
88 .SetGroupName("Test")
89 .AddConstructor<PairObject>()
90 .AddAttribute(
91 "StringPair",
92 "Pair: string, string",
94 MakePairAccessor<StringValue, StringValue>(&PairObject::m_stringPair),
95 MakePairChecker<StringValue, StringValue>(MakeStringChecker(), MakeStringChecker()))
96 .AddAttribute("DoubleIntPair",
97 "Pair: double int",
98 // the container value container differs from the underlying object
100 MakePairAccessor<DoubleValue, IntegerValue>(&PairObject::m_doubleIntPair),
101 MakePairChecker<DoubleValue, IntegerValue>(MakeDoubleChecker<double>(),
102 MakeIntegerChecker<int>()));
103 return tid;
104}
105
113std::ostream&
114operator<<(std::ostream& os, const PairObject& obj)
115{
116 os << "StringPair = { " << obj.m_stringPair << " } ";
117 os << "DoubleIntPair = { " << obj.m_doubleIntPair << " }";
118 return os;
119}
120
127{
128 public:
130
132 {
133 }
134
135 private:
136 void DoRun() override;
137};
138
140 : TestCase("test instantiation, initialization, access")
141{
142}
143
144void
146{
147 {
148 std::pair<const int, double> ref = {1, 2.4};
149
151
152 std::pair<const int, double> rv = ac.Get();
153 NS_TEST_ASSERT_MSG_EQ(rv, ref, "Attribute value does not equal original");
154 }
155
156 {
157 std::pair<const std::string, double> ref = {"hello", 3.14};
158
160 ac.Set(ref);
161 std::pair<const std::string, double> rv = ac.Get();
162 NS_TEST_ASSERT_MSG_EQ(rv, ref, "Attribute value does not equal original");
163 }
164}
165
172{
173 public:
175
176 void DoRun() override;
177};
178
180 : TestCase("test setting through attribute interface")
181{
182}
183
184void
186{
187 auto p = CreateObject<PairObject>();
188 p->SetAttribute("StringPair",
189 PairValue<StringValue, StringValue>(std::make_pair("hello", "world")));
190 p->SetAttribute("DoubleIntPair",
191 PairValue<DoubleValue, IntegerValue>(std::make_pair(3.14, 31)));
192
193 std::ostringstream oss;
194 oss << *p;
195
196 std::ostringstream ref;
197 ref << "StringPair = { (hello,world) } DoubleIntPair = { (3.14,31) }";
198
199 NS_TEST_ASSERT_MSG_EQ((oss.str()), (ref.str()), "Pairs not correctly set");
200}
201
208{
209 public:
211};
212
214 : TestSuite("pair-value-test-suite", Type::UNIT)
215{
216 AddTestCase(new PairValueTestCase(), TestCase::Duration::QUICK);
217 AddTestCase(new PairValueSettingsTestCase(), TestCase::Duration::QUICK);
218}
219
Object holding pairs of values.
static TypeId GetTypeId()
Get the type ID.
~PairObject() override
std::pair< double, int > m_doubleIntPair
A pair of double, int.
std::pair< std::string, std::string > m_stringPair
A string pair.
friend std::ostream & operator<<(std::ostream &os, const PairObject &obj)
Stream insertion operator.
Pair test - test setting through attribute interface.
void DoRun() override
Implementation to actually run this TestCase.
Pair test - Test instantiation, initialization, access.
void DoRun() override
Implementation to actually run this TestCase.
The pair-value Test Suite.
A base class which provides memory management and object aggregation.
Definition: object.h:89
AttributeValue implementation for Pair.
Definition: pair.h:65
result_type Get() const
Get the stored value as a std::pair.
Definition: pair.h:389
void Set(const result_type &value)
Set the value.
Definition: pair.h:396
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
Type
Type of test.
Definition: test.h:1275
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Ptr< const AttributeChecker > MakeStringChecker()
Definition: string.cc:30
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:145
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static PairValueTestSuite g_pairValueTestSuite
Static variable for test initialization.
std::ostream & operator<<(std::ostream &os, const PairObject &obj)
Stream insertion operator.