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 <sstream>
20#include <utility>
21
22using namespace ns3;
23
24NS_LOG_COMPONENT_DEFINE("PairTestSuite");
25
26/**
27 * @file
28 * @ingroup pair-tests
29 * Pairs tests.
30 */
31
32/**
33 * @ingroup core-tests
34 * @defgroup pair-tests Pairs tests
35 */
36
37/**
38 * @ingroup pair-tests
39 *
40 * Object holding pairs of values.
41 */
42class PairObject : public Object
43{
44 public:
45 PairObject();
46 ~PairObject() override;
47
48 /**
49 * @brief Get the type ID.
50 * @return The object TypeId.
51 */
52 static TypeId GetTypeId();
53
54 friend std::ostream& operator<<(std::ostream& os, const PairObject& obj);
55
56 private:
57 std::pair<std::string, std::string> m_stringPair; //!< A string pair.
58 std::pair<double, int> m_doubleIntPair; //!< A pair of double, int.
59};
60
64
68
71{
72 static TypeId tid =
73 TypeId("ns3::PairObject")
75 .SetGroupName("Test")
76 .AddConstructor<PairObject>()
77 .AddAttribute(
78 "StringPair",
79 "Pair: string, string",
83 .AddAttribute("DoubleIntPair",
84 "Pair: double int",
85 // the container value container differs from the underlying object
90 return tid;
91}
92
93/**
94 * @brief Stream insertion operator.
95 *
96 * @param [in] os The reference to the output stream.
97 * @param [in] obj The PairObject.
98 * @returns The reference to the output stream.
99 */
100std::ostream&
101operator<<(std::ostream& os, const PairObject& obj)
102{
103 os << "StringPair = { " << obj.m_stringPair << " } ";
104 os << "DoubleIntPair = { " << obj.m_doubleIntPair << " }";
105 return os;
106}
107
108/**
109 * @ingroup pair-tests
110 *
111 * Pair test - Test instantiation, initialization, access.
112 */
114{
115 public:
117
119 {
120 }
121
122 private:
123 void DoRun() override;
124};
125
127 : TestCase("test instantiation, initialization, access")
128{
129}
130
131void
133{
134 {
135 std::pair<const int, double> ref = {1, 2.4};
136
138
139 std::pair<const int, double> rv = ac.Get();
140 NS_TEST_ASSERT_MSG_EQ(rv, ref, "Attribute value does not equal original");
141 }
142
143 {
144 std::pair<const std::string, double> ref = {"hello", 3.14};
145
147 ac.Set(ref);
148 std::pair<const std::string, double> rv = ac.Get();
149 NS_TEST_ASSERT_MSG_EQ(rv, ref, "Attribute value does not equal original");
150 }
151}
152
153/**
154 * @ingroup pair-tests
155 *
156 * Pair test - test setting through attribute interface.
157 */
159{
160 public:
162
163 void DoRun() override;
164};
165
167 : TestCase("test setting through attribute interface")
168{
169}
170
171void
173{
174 auto p = CreateObject<PairObject>();
175 p->SetAttribute("StringPair",
176 PairValue<StringValue, StringValue>(std::make_pair("hello", "world")));
177 p->SetAttribute("DoubleIntPair",
178 PairValue<DoubleValue, IntegerValue>(std::make_pair(3.14, 31)));
179
180 std::ostringstream oss;
181 oss << *p;
182
183 std::ostringstream ref;
184 ref << "StringPair = { (hello,world) } DoubleIntPair = { (3.14,31) }";
185
186 NS_TEST_ASSERT_MSG_EQ((oss.str()), (ref.str()), "Pairs not correctly set");
187}
188
189/**
190 * @ingroup pair-tests
191 *
192 * @brief The pair-value Test Suite.
193 */
195{
196 public:
198};
199
206
207static PairValueTestSuite g_pairValueTestSuite; //!< Static variable for test initialization
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.
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
AttributeValue implementation for Pair.
Definition pair.h:54
result_type Get() const
Get the stored value as a std::pair.
Definition pair.h:380
void Set(const result_type &value)
Set the value.
Definition pair.h:387
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeChecker > MakeIntegerChecker()
Definition integer.h:99
Ptr< const AttributeAccessor > MakePairAccessor(T1 a1)
Create an AttributeAccessor for std::pair<>.
Definition pair.h:403
Ptr< AttributeChecker > MakePairChecker(const PairValue< A, B > &value)
Make a PairChecker from a PairValue.
Definition pair.h:272
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#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:133
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
static PairValueTestSuite g_pairValueTestSuite
Static variable for test initialization.