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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Jared Dulmage <jared.dulmage@caliola.com>
7 */
8
9#include <ns3/double.h>
10#include <ns3/integer.h>
11#include <ns3/log.h>
12#include <ns3/object.h>
13#include <ns3/pair.h>
14#include <ns3/ptr.h>
15#include <ns3/string.h>
16#include <ns3/test.h>
17#include <ns3/type-id.h>
18
19#include <algorithm>
20#include <iterator>
21#include <sstream>
22#include <utility>
23
24using namespace ns3;
25
26NS_LOG_COMPONENT_DEFINE("PairTestSuite");
27
28/**
29 * \file
30 * \ingroup pair-tests
31 * Pairs tests.
32 */
33
34/**
35 * \ingroup core-tests
36 * \defgroup pair-tests Pairs tests
37 */
38
39/**
40 * \ingroup pair-tests
41 *
42 * Object holding pairs of values.
43 */
44class PairObject : public Object
45{
46 public:
47 PairObject();
48 ~PairObject() override;
49
50 /**
51 * \brief Get the type ID.
52 * \return The object TypeId.
53 */
54 static TypeId GetTypeId();
55
56 friend std::ostream& operator<<(std::ostream& os, const PairObject& obj);
57
58 private:
59 std::pair<std::string, std::string> m_stringPair; //!< A string pair.
60 std::pair<double, int> m_doubleIntPair; //!< A pair of double, int.
61};
62
66
70
73{
74 static TypeId tid =
75 TypeId("ns3::PairObject")
77 .SetGroupName("Test")
78 .AddConstructor<PairObject>()
79 .AddAttribute(
80 "StringPair",
81 "Pair: string, string",
85 .AddAttribute("DoubleIntPair",
86 "Pair: double int",
87 // the container value container differs from the underlying object
92 return tid;
93}
94
95/**
96 * \brief Stream insertion operator.
97 *
98 * \param [in] os The reference to the output stream.
99 * \param [in] obj The PairObject.
100 * \returns The reference to the output stream.
101 */
102std::ostream&
103operator<<(std::ostream& os, const PairObject& obj)
104{
105 os << "StringPair = { " << obj.m_stringPair << " } ";
106 os << "DoubleIntPair = { " << obj.m_doubleIntPair << " }";
107 return os;
108}
109
110/**
111 * \ingroup pair-tests
112 *
113 * Pair test - Test instantiation, initialization, access.
114 */
116{
117 public:
119
121 {
122 }
123
124 private:
125 void DoRun() override;
126};
127
129 : TestCase("test instantiation, initialization, access")
130{
131}
132
133void
135{
136 {
137 std::pair<const int, double> ref = {1, 2.4};
138
140
141 std::pair<const int, double> rv = ac.Get();
142 NS_TEST_ASSERT_MSG_EQ(rv, ref, "Attribute value does not equal original");
143 }
144
145 {
146 std::pair<const std::string, double> ref = {"hello", 3.14};
147
149 ac.Set(ref);
150 std::pair<const std::string, double> rv = ac.Get();
151 NS_TEST_ASSERT_MSG_EQ(rv, ref, "Attribute value does not equal original");
152 }
153}
154
155/**
156 * \ingroup pair-tests
157 *
158 * Pair test - test setting through attribute interface.
159 */
161{
162 public:
164
165 void DoRun() override;
166};
167
169 : TestCase("test setting through attribute interface")
170{
171}
172
173void
175{
176 auto p = CreateObject<PairObject>();
177 p->SetAttribute("StringPair",
178 PairValue<StringValue, StringValue>(std::make_pair("hello", "world")));
179 p->SetAttribute("DoubleIntPair",
180 PairValue<DoubleValue, IntegerValue>(std::make_pair(3.14, 31)));
181
182 std::ostringstream oss;
183 oss << *p;
184
185 std::ostringstream ref;
186 ref << "StringPair = { (hello,world) } DoubleIntPair = { (3.14,31) }";
187
188 NS_TEST_ASSERT_MSG_EQ((oss.str()), (ref.str()), "Pairs not correctly set");
189}
190
191/**
192 * \ingroup pair-tests
193 *
194 * \brief The pair-value Test Suite.
195 */
197{
198 public:
200};
201
203 : TestSuite("pair-value-test-suite", Type::UNIT)
204{
205 AddTestCase(new PairValueTestCase(), TestCase::Duration::QUICK);
206 AddTestCase(new PairValueSettingsTestCase(), TestCase::Duration::QUICK);
207}
208
209static PairValueTestSuite g_pairValueTestSuite; //!< Static variable for test initialization
Object holding pairs of values.
static TypeId GetTypeId()
Get the type ID.
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:78
AttributeValue implementation for Pair.
Definition pair.h:54
result_type Get() const
Get the stored value as a std::pair.
Definition pair.h:378
void Set(const result_type &value)
Set the value.
Definition pair.h:385
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
Type
Type of test.
Definition test.h:1274
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Ptr< const AttributeAccessor > MakePairAccessor(T1 a1)
Create an AttributeAccessor for std::pair<>.
Definition pair.h:401
Ptr< AttributeChecker > MakePairChecker()
Make a PairChecker without abscissa and ordinate AttributeCheckers.
Definition pair.h:287
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeIntegerChecker()
Definition integer.h:99
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
static PairValueTestSuite g_pairValueTestSuite
Static variable for test initialization.