A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tuple-value-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Universita' degli Studi di Napoli Federico II
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Stefano Avallone <stavallo@unina.it>
7 */
8
9#include "ns3/double.h"
10#include "ns3/enum.h"
11#include "ns3/log.h"
12#include "ns3/object.h"
13#include "ns3/ptr.h"
14#include "ns3/string.h"
15#include "ns3/test.h"
16#include "ns3/tuple.h"
17#include "ns3/uinteger.h"
18
19#include <tuple>
20
21using namespace ns3;
22
23NS_LOG_COMPONENT_DEFINE("TupleTestSuite");
24
25/** Object with attribute values storing tuples */
26class TupleObject : public Object
27{
28 public:
29 /**
30 * Test enum type
31 */
38
40 ~TupleObject() override;
41
42 /**
43 * @brief Get the type ID.
44 * @return the object TypeId
45 */
46 static TypeId GetTypeId();
47
48 // NOTE EnumValue::Get() return an int, so the tuple element type must be an int
49 // in place of the enum type
52 using Tuple1 = Tuple1Value::result_type; //!< tuple of values
53 using Tuple1Pack = Tuple1Value::value_type; //!< tuple of attribute values
54
55 using Tuple2 = std::tuple<double, uint16_t, std::string>; //!< Tuple2 typedef
56
57 /**
58 * Set tuple1
59 * @param tuple tuple value
60 */
61 void SetTuple1(const Tuple1& tuple);
62 /**
63 * Get tuple1
64 * @return tuple1
65 */
66 Tuple1 GetTuple1() const;
67 /**
68 * Set tuple2
69 * @param tuple tuple value
70 */
71 void SetTuple2(const Tuple2& tuple);
72 /**
73 * Get tuple2
74 * @return tuple2
75 */
76 Tuple2 GetTuple2() const;
77
78 private:
79 Tuple1 m_tuple1; //!< first tuple
80 Tuple2 m_tuple2; //!< second tuple
81};
82
85{
86 static TypeId tid =
87 TypeId("ns3::TupleObject")
89 .SetGroupName("Test")
90 .AddConstructor<TupleObject>()
91 .AddAttribute(
92 "StringStringEnumTuple",
93 "Tuple1: string, string, enum",
100 .AddAttribute(
101 "DoubleUintStringTuple",
102 "Tuple2: double, uint16_t, string",
107 MakeDoubleChecker<double>(1.0, 10.0),
110 return tid;
111}
112
116
120
121void
123{
124 m_tuple1 = tuple;
125}
126
129{
130 return m_tuple1;
131}
132
133void
135{
136 m_tuple2 = tuple;
137}
138
141{
142 return m_tuple2;
143}
144
145/** Test instantiation, initialization, access */
147{
148 public:
150
152 {
153 }
154
155 private:
156 void DoRun() override;
157};
158
160 : TestCase("test TupleValue attribute value")
161{
162}
163
164void
166{
167 auto tupleObject = CreateObject<TupleObject>();
168
169 // Test that default values have been assigned to tuple 1
170 auto t1 = tupleObject->GetTuple1();
171 NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Hey"),
172 true,
173 "First element of tuple 1 not correctly set");
174 NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Jude"),
175 true,
176 "Second element of tuple 1 not correctly set");
177 NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
178 (int)(TupleObject::VALUE1),
179 "Third element of tuple 1 not correctly set");
180
181 // Test that default values have been assigned to tuple 2
182 auto t2 = tupleObject->GetTuple2();
183 NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 6.022, "First element of tuple 2 not correctly set");
184 NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 23, "Second element of tuple 2 not correctly set");
185 NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "Avogadro"),
186 true,
187 "Third element of tuple 2 not correctly set");
188
189 // Test that we can correctly set and get new values for tuple 1
190 bool ret1 = tupleObject->SetAttributeFailSafe(
191 "StringStringEnumTuple",
193 TupleObject::Tuple1{"Norwegian", "Wood", TupleObject::VALUE2}));
194 NS_TEST_ASSERT_MSG_EQ(ret1, true, "Setting valid values to tuple 1 failed");
195
197 ret1 = tupleObject->GetAttributeFailSafe("StringStringEnumTuple", tupleValue1);
198 NS_TEST_ASSERT_MSG_EQ(ret1, true, "Getting values for tuple 1 failed");
199
200 t1 = tupleValue1.Get();
201 NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Norwegian"),
202 true,
203 "First element of tuple 1 not correctly set");
204 NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Wood"),
205 true,
206 "Second element of tuple 1 not correctly set");
207 NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
208 (int)(TupleObject::VALUE2),
209 "Third element of tuple 1 not correctly set");
210
211 // Test that we can correctly set and get new values for tuple 2
212 bool ret2 = tupleObject->SetAttributeFailSafe(
213 "DoubleUintStringTuple",
215 NS_TEST_ASSERT_MSG_EQ(ret2, true, "Setting valid values to tuple 2 failed");
216
218 ret2 = tupleObject->GetAttributeFailSafe("DoubleUintStringTuple", tupleValue2);
219 NS_TEST_ASSERT_MSG_EQ(ret2, true, "Getting values for tuple 2 failed");
220
221 t2 = tupleValue2.Get();
222 NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 8.987, "First element of tuple 2 not correctly set");
223 NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 9, "Second element of tuple 2 not correctly set");
224 NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "Coulomb"),
225 true,
226 "Third element of tuple 2 not correctly set");
227
228 // Test that we can set tuple 1 from string
229 ret1 = tupleObject->SetAttributeFailSafe("StringStringEnumTuple",
230 StringValue("{Come, Together, VALUE1}"));
231 NS_TEST_ASSERT_MSG_EQ(ret1, true, "Setting valid values to tuple 1 failed");
232
233 t1 = tupleObject->GetTuple1();
234 NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Come"),
235 true,
236 "First element of tuple 1 not correctly set");
237 NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Together"),
238 true,
239 "Second element of tuple 1 not correctly set");
240 NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
241 (int)(TupleObject::VALUE1),
242 "Third element of tuple 1 not correctly set");
243
244 // Test that we can set tuple 2 from string
245 ret2 = tupleObject->SetAttributeFailSafe("DoubleUintStringTuple",
246 StringValue("{2.99, 8, LightSpeed}"));
247 NS_TEST_ASSERT_MSG_EQ(ret2, true, "Setting valid values to tuple 2 failed");
248
249 t2 = tupleObject->GetTuple2();
250 NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 2.99, "First element of tuple 2 not correctly set");
251 NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 8, "Second element of tuple 2 not correctly set");
252 NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "LightSpeed"),
253 true,
254 "Third element of tuple 2 not correctly set");
255
256 // Test that setting invalid values fails
257 ret1 = tupleObject->SetAttributeFailSafe("StringStringEnumTuple",
258 TupleValue<StringValue, StringValue>({"Get", "Back"}));
259 NS_TEST_ASSERT_MSG_EQ(ret1, false, "Too few values");
260 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple1() ==
261 std::make_tuple("Come", "Together", (int)(TupleObject::VALUE1))),
262 true,
263 "Tuple modified after failed assignment");
264
265 ret1 = tupleObject->SetAttributeFailSafe(
266 "StringStringEnumTuple",
269 NS_TEST_ASSERT_MSG_EQ(ret1, false, "Invalid enum value");
270 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple1() ==
271 std::make_tuple("Come", "Together", (int)(TupleObject::VALUE1))),
272 true,
273 "Tuple modified after failed assignment");
274
275 ret2 = tupleObject->SetAttributeFailSafe(
276 "DoubleUintStringTuple",
278 {4.83, 14, "Josephson", "constant"}));
279 NS_TEST_ASSERT_MSG_EQ(ret2, false, "Too many values");
280 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
281 true,
282 "Tuple modified after failed assignment");
283
284 ret2 = tupleObject->SetAttributeFailSafe(
285 "DoubleUintStringTuple",
287 NS_TEST_ASSERT_MSG_EQ(ret2, false, "Double value out of range");
288 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
289 true,
290 "Tuple modified after failed assignment");
291
292 ret2 = tupleObject->SetAttributeFailSafe(
293 "DoubleUintStringTuple",
295 NS_TEST_ASSERT_MSG_EQ(ret2, false, "Uinteger value out of range");
296 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
297 true,
298 "Tuple modified after failed assignment");
299}
300
301/** Test suite */
303{
304 public:
306};
307
313
314static TupleValueTestSuite g_tupleValueTestSuite; //!< Static variable for test initialization
TupleValue< StringValue, StringValue, EnumValue< TupleTestEnum > > Tuple1Value
Tuple1 attribute value.
Tuple1 GetTuple1() const
Get tuple1.
void SetTuple1(const Tuple1 &tuple)
Set tuple1.
Tuple1Value::value_type Tuple1Pack
tuple of attribute values
Tuple2 m_tuple2
second tuple
std::tuple< double, uint16_t, std::string > Tuple2
Tuple2 typedef.
static TypeId GetTypeId()
Get the type ID.
Tuple2 GetTuple2() const
Get tuple2.
Tuple1 m_tuple1
first tuple
TupleTestEnum
Test enum type.
void SetTuple2(const Tuple2 &tuple)
Set tuple2.
Tuple1Value::result_type Tuple1
tuple of values
Test instantiation, initialization, access.
void DoRun() override
Implementation to actually run this TestCase.
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Hold variables of type string.
Definition string.h:45
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
AttributeValue implementation for Tuple.
Definition tuple.h:67
result_type Get() const
Get the stored values as a std::tuple.
Definition tuple.h:325
std::tuple< std::invoke_result_t< decltype(&Args::Get), Args >... > result_type
Definition tuple.h:72
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 > MakeStringChecker()
Definition string.cc:19
Ptr< const AttributeChecker > MakeTupleChecker(Ts... checkers)
Create a TupleChecker from AttributeCheckers associated with TupleValue elements.
Definition tuple.h:532
Ptr< const AttributeAccessor > MakeTupleAccessor(T1 a1)
Create an AttributeAccessor for a class data member of type tuple, or a lone class get functor or set...
Definition tuple.h:539
auto MakeTupleValue(T2 t)
Create a TupleValue object.
Definition tuple.h:525
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
#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.
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition enum.h:181
static TupleValueTestSuite g_tupleValueTestSuite
Static variable for test initialization.