A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
pair-value-test-suite.cc
Go to the documentation of this file.
1
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2018 Caliola Engineering, LLC.
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Jared Dulmage <jared.dulmage@caliola.com>
19
*/
20
21
#include <ns3/test.h>
22
#include <ns3/log.h>
23
#include <ns3/pair.h>
24
#include <ns3/double.h>
25
#include <ns3/integer.h>
26
#include <ns3/string.h>
27
#include <ns3/ptr.h>
28
#include <ns3/object.h>
29
#include <ns3/type-id.h>
30
31
#include <algorithm>
32
#include <iterator>
33
#include <sstream>
34
#include <utility>
35
36
37
using namespace
ns3
;
38
39
NS_LOG_COMPONENT_DEFINE
(
"PairTestSuite"
);
40
41
class
PairObject
:
public
Object
42
{
43
public
:
44
PairObject
();
45
virtual
~
PairObject
();
46
47
static
48
TypeId
GetTypeId ();
49
50
friend
std::ostream &
operator <<
(std::ostream &os,
const
PairObject
&obj);
51
52
private
:
53
std::pair <std::string, std::string>
m_stringPair
;
54
std::pair <double, int>
m_doubleIntPair
;
55
};
56
57
PairObject::PairObject
()
58
{
59
60
}
61
62
PairObject::~PairObject
()
63
{
64
65
}
66
67
TypeId
68
PairObject::GetTypeId
()
69
{
70
static
TypeId
tid =
TypeId
(
"ns3::PairObject"
)
71
.
SetParent
<
Object
> ()
72
.SetGroupName(
"Test"
)
73
.AddConstructor<
PairObject
> ()
74
.AddAttribute (
"StringPair"
,
"Pair: string, string"
,
75
PairValue <StringValue, StringValue>
(),
76
MakePairAccessor <StringValue, StringValue> (&
PairObject::m_stringPair
),
77
MakePairChecker<StringValue, StringValue> (
MakeStringChecker
(),
MakeStringChecker
()))
78
.AddAttribute (
"DoubleIntPair"
,
"Pair: double int"
,
79
// the container value container differs from the underlying object
80
PairValue <DoubleValue, IntegerValue>
(),
81
MakePairAccessor <DoubleValue, IntegerValue> (&
PairObject::m_doubleIntPair
),
82
MakePairChecker<DoubleValue, IntegerValue> (MakeDoubleChecker<double> (), MakeIntegerChecker<int> ()))
83
;
84
return
tid;
85
}
86
87
std::ostream &
88
operator <<
(std::ostream &os,
const
PairObject
&obj)
89
{
90
os <<
"StringPair = { "
<< obj.
m_stringPair
<<
" } "
;
91
os <<
"DoubleIntPair = { "
<< obj.
m_doubleIntPair
<<
" }"
;
92
return
os;
93
}
94
95
/* Test instantiation, initialization, access */
96
class
PairValueTestCase
:
public
TestCase
97
{
98
public
:
99
PairValueTestCase
();
100
virtual
~PairValueTestCase
() {}
101
102
private
:
103
virtual
void
DoRun ();
104
};
105
106
PairValueTestCase::PairValueTestCase
()
107
:
TestCase
(
"test instantiation, initialization, access"
)
108
{
109
110
}
111
112
void
113
PairValueTestCase::DoRun
()
114
{
115
{
116
std::pair<const int, double> ref = {1, 2.4};
117
118
PairValue<IntegerValue, DoubleValue>
ac (ref);
119
120
std::pair<const int, double> rv = ac.
Get
();
121
NS_TEST_ASSERT_MSG_EQ
(rv, ref,
"Attribute value does not equal original"
);
122
}
123
124
{
125
std::pair<const std::string, double> ref = {
"hello"
, 3.14};
126
127
PairValue<StringValue, DoubleValue>
ac;
128
ac.
Set
(ref);
129
std::pair<const std::string, double> rv = ac.
Get
();
130
NS_TEST_ASSERT_MSG_EQ
(rv, ref,
"Attribute value does not equal original"
);
131
}
132
133
}
134
135
class
PairValueSettingsTestCase
:
public
TestCase
136
{
137
public
:
138
PairValueSettingsTestCase
();
139
140
void
DoRun
();
141
};
142
143
PairValueSettingsTestCase::PairValueSettingsTestCase
()
144
:
TestCase
(
"test setting through attribute interface"
)
145
{}
146
147
void
148
PairValueSettingsTestCase::DoRun
()
149
{
150
auto
p = CreateObject <PairObject> ();
151
p->SetAttribute (
"StringPair"
,
PairValue<StringValue, StringValue>
(std::make_pair (
"hello"
,
"world"
)));
152
p->SetAttribute (
"DoubleIntPair"
,
PairValue<DoubleValue, IntegerValue>
(std::make_pair (3.14, 31)));
153
154
std::ostringstream oss;
155
oss << *p;
156
157
std::ostringstream ref;
158
ref <<
"StringPair = { (hello,world) } DoubleIntPair = { (3.14,31) }"
;
159
160
NS_TEST_ASSERT_MSG_EQ
((oss.str ()), (ref.str ()),
"Pairs not correctly set"
);
161
}
162
163
class
PairValueTestSuite
:
public
TestSuite
164
{
165
public
:
166
PairValueTestSuite
();
167
};
168
169
PairValueTestSuite::PairValueTestSuite
()
170
:
TestSuite
(
"pair-value-test-suite"
, UNIT)
171
{
172
AddTestCase
(
new
PairValueTestCase
(), TestCase::QUICK);
173
AddTestCase
(
new
PairValueSettingsTestCase
(), TestCase::QUICK);
174
}
175
176
static
PairValueTestSuite
pairValueTestSuite
;
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
ns3::PairValue::Set
void Set(const result_type &value)
Set the stored value.
Definition:
pair.h:323
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
PairValueTestCase::PairValueTestCase
PairValueTestCase()
Definition:
pair-value-test-suite.cc:106
PairValueSettingsTestCase
Definition:
pair-value-test-suite.cc:136
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:923
ns3::TestCase
encapsulates test code
Definition:
test.h:1154
pairValueTestSuite
static PairValueTestSuite pairValueTestSuite
Static variable for test initialization.
Definition:
pair-value-test-suite.cc:176
PairObject::GetTypeId
static TypeId GetTypeId()
Definition:
pair-value-test-suite.cc:68
PairObject::~PairObject
virtual ~PairObject()
Definition:
pair-value-test-suite.cc:62
ns3::Object
A base class which provides memory management and object aggregation.
Definition:
object.h:88
PairValueTestCase::DoRun
virtual void DoRun()
Implementation to actually run this TestCase.
Definition:
pair-value-test-suite.cc:113
PairObject
Definition:
pair-value-test-suite.cc:42
PairValueTestSuite::PairValueTestSuite
PairValueTestSuite()
Definition:
pair-value-test-suite.cc:169
PairValueSettingsTestCase::PairValueSettingsTestCase
PairValueSettingsTestCase()
Definition:
pair-value-test-suite.cc:143
PairObject::m_doubleIntPair
std::pair< double, int > m_doubleIntPair
Definition:
pair-value-test-suite.cc:54
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1344
NS_TEST_ASSERT_MSG_EQ
#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:166
ns3::PairValue::Get
result_type Get(void) const
Get the stored value as a std::pair.
Definition:
pair.h:316
PairObject::m_stringPair
std::pair< std::string, std::string > m_stringPair
Definition:
pair-value-test-suite.cc:53
PairValueTestCase::~PairValueTestCase
virtual ~PairValueTestCase()
Definition:
pair-value-test-suite.cc:100
PairValueSettingsTestCase::DoRun
void DoRun()
Implementation to actually run this TestCase.
Definition:
pair-value-test-suite.cc:148
PairObject::PairObject
PairObject()
Definition:
pair-value-test-suite.cc:57
ns3::MakeStringChecker
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition:
string.cc:30
PairValueTestCase
Definition:
pair-value-test-suite.cc:97
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition:
angles.cc:137
PairValueTestSuite
Definition:
pair-value-test-suite.cc:164
ns3::PairValue
Hold objects of type std::pair<A, B>.
Definition:
pair.h:48
src
core
test
pair-value-test-suite.cc
Generated on Fri Oct 1 2021 17:03:00 for ns-3 by
1.8.20