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 <algorithm>
20#include <iterator>
21#include <sstream>
22#include <tuple>
23#include <utility>
24
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE("TupleTestSuite");
28
29/** Object with attribute values storing tuples */
30class TupleObject : public Object
31{
32 public:
33 /**
34 * Test enum type
35 */
42
44 ~TupleObject() override;
45
46 /**
47 * \brief Get the type ID.
48 * \return the object TypeId
49 */
50 static TypeId GetTypeId();
51
52 // NOTE EnumValue::Get() return an int, so the tuple element type must be an int
53 // in place of the enum type
56 using Tuple1 = Tuple1Value::result_type; //!< tuple of values
57 using Tuple1Pack = Tuple1Value::value_type; //!< tuple of attribute values
58
59 using Tuple2 = std::tuple<double, uint16_t, std::string>; //!< Tuple2 typedef
60
61 /**
62 * Set tuple1
63 * \param tuple tuple value
64 */
65 void SetTuple1(const Tuple1& tuple);
66 /**
67 * Get tuple1
68 * \return tuple1
69 */
70 Tuple1 GetTuple1() const;
71 /**
72 * Set tuple2
73 * \param tuple tuple value
74 */
75 void SetTuple2(const Tuple2& tuple);
76 /**
77 * Get tuple2
78 * \return tuple2
79 */
80 Tuple2 GetTuple2() const;
81
82 private:
83 Tuple1 m_tuple1; //!< first tuple
84 Tuple2 m_tuple2; //!< second tuple
85};
86
89{
90 static TypeId tid =
91 TypeId("ns3::TupleObject")
93 .SetGroupName("Test")
94 .AddConstructor<TupleObject>()
95 .AddAttribute(
96 "StringStringEnumTuple",
97 "Tuple1: string, string, enum",
104 .AddAttribute(
105 "DoubleUintStringTuple",
106 "Tuple2: double, uint16_t, string",
111 MakeDoubleChecker<double>(1.0, 10.0),
114 return tid;
115}
116
120
124
125void
127{
128 m_tuple1 = tuple;
129}
130
133{
134 return m_tuple1;
135}
136
137void
139{
140 m_tuple2 = tuple;
141}
142
145{
146 return m_tuple2;
147}
148
149/** Test instantiation, initialization, access */
151{
152 public:
154
156 {
157 }
158
159 private:
160 void DoRun() override;
161};
162
164 : TestCase("test TupleValue attribute value")
165{
166}
167
168void
170{
171 auto tupleObject = CreateObject<TupleObject>();
172
173 // Test that default values have been assigned to tuple 1
174 auto t1 = tupleObject->GetTuple1();
175 NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Hey"),
176 true,
177 "First element of tuple 1 not correctly set");
178 NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Jude"),
179 true,
180 "Second element of tuple 1 not correctly set");
181 NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
182 (int)(TupleObject::VALUE1),
183 "Third element of tuple 1 not correctly set");
184
185 // Test that default values have been assigned to tuple 2
186 auto t2 = tupleObject->GetTuple2();
187 NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 6.022, "First element of tuple 2 not correctly set");
188 NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 23, "Second element of tuple 2 not correctly set");
189 NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "Avogadro"),
190 true,
191 "Third element of tuple 2 not correctly set");
192
193 // Test that we can correctly set and get new values for tuple 1
194 bool ret1 = tupleObject->SetAttributeFailSafe(
195 "StringStringEnumTuple",
197 TupleObject::Tuple1{"Norwegian", "Wood", TupleObject::VALUE2}));
198 NS_TEST_ASSERT_MSG_EQ(ret1, true, "Setting valid values to tuple 1 failed");
199
201 ret1 = tupleObject->GetAttributeFailSafe("StringStringEnumTuple", tupleValue1);
202 NS_TEST_ASSERT_MSG_EQ(ret1, true, "Getting values for tuple 1 failed");
203
204 t1 = tupleValue1.Get();
205 NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Norwegian"),
206 true,
207 "First element of tuple 1 not correctly set");
208 NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Wood"),
209 true,
210 "Second element of tuple 1 not correctly set");
211 NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
212 (int)(TupleObject::VALUE2),
213 "Third element of tuple 1 not correctly set");
214
215 // Test that we can correctly set and get new values for tuple 2
216 bool ret2 = tupleObject->SetAttributeFailSafe(
217 "DoubleUintStringTuple",
219 NS_TEST_ASSERT_MSG_EQ(ret2, true, "Setting valid values to tuple 2 failed");
220
222 ret2 = tupleObject->GetAttributeFailSafe("DoubleUintStringTuple", tupleValue2);
223 NS_TEST_ASSERT_MSG_EQ(ret2, true, "Getting values for tuple 2 failed");
224
225 t2 = tupleValue2.Get();
226 NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 8.987, "First element of tuple 2 not correctly set");
227 NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 9, "Second element of tuple 2 not correctly set");
228 NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "Coulomb"),
229 true,
230 "Third element of tuple 2 not correctly set");
231
232 // Test that we can set tuple 1 from string
233 ret1 = tupleObject->SetAttributeFailSafe("StringStringEnumTuple",
234 StringValue("{Come, Together, VALUE1}"));
235 NS_TEST_ASSERT_MSG_EQ(ret1, true, "Setting valid values to tuple 1 failed");
236
237 t1 = tupleObject->GetTuple1();
238 NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Come"),
239 true,
240 "First element of tuple 1 not correctly set");
241 NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Together"),
242 true,
243 "Second element of tuple 1 not correctly set");
244 NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
245 (int)(TupleObject::VALUE1),
246 "Third element of tuple 1 not correctly set");
247
248 // Test that we can set tuple 2 from string
249 ret2 = tupleObject->SetAttributeFailSafe("DoubleUintStringTuple",
250 StringValue("{2.99, 8, LightSpeed}"));
251 NS_TEST_ASSERT_MSG_EQ(ret2, true, "Setting valid values to tuple 2 failed");
252
253 t2 = tupleObject->GetTuple2();
254 NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 2.99, "First element of tuple 2 not correctly set");
255 NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 8, "Second element of tuple 2 not correctly set");
256 NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "LightSpeed"),
257 true,
258 "Third element of tuple 2 not correctly set");
259
260 // Test that setting invalid values fails
261 ret1 = tupleObject->SetAttributeFailSafe("StringStringEnumTuple",
262 TupleValue<StringValue, StringValue>({"Get", "Back"}));
263 NS_TEST_ASSERT_MSG_EQ(ret1, false, "Too few values");
264 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple1() ==
265 std::make_tuple("Come", "Together", (int)(TupleObject::VALUE1))),
266 true,
267 "Tuple modified after failed assignment");
268
269 ret1 = tupleObject->SetAttributeFailSafe(
270 "StringStringEnumTuple",
273 NS_TEST_ASSERT_MSG_EQ(ret1, false, "Invalid enum value");
274 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple1() ==
275 std::make_tuple("Come", "Together", (int)(TupleObject::VALUE1))),
276 true,
277 "Tuple modified after failed assignment");
278
279 ret2 = tupleObject->SetAttributeFailSafe(
280 "DoubleUintStringTuple",
282 {4.83, 14, "Josephson", "constant"}));
283 NS_TEST_ASSERT_MSG_EQ(ret2, false, "Too many values");
284 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
285 true,
286 "Tuple modified after failed assignment");
287
288 ret2 = tupleObject->SetAttributeFailSafe(
289 "DoubleUintStringTuple",
291 NS_TEST_ASSERT_MSG_EQ(ret2, false, "Double value out of range");
292 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
293 true,
294 "Tuple modified after failed assignment");
295
296 ret2 = tupleObject->SetAttributeFailSafe(
297 "DoubleUintStringTuple",
299 NS_TEST_ASSERT_MSG_EQ(ret2, false, "Uinteger value out of range");
300 NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
301 true,
302 "Tuple modified after failed assignment");
303}
304
305/** Test suite */
307{
308 public:
310};
311
313 : TestSuite("tuple-value-test-suite", Type::UNIT)
314{
315 AddTestCase(new TupleValueTestCase(), TestCase::Duration::QUICK);
316}
317
318static TupleValueTestSuite g_tupleValueTestSuite; //!< Static variable for test initialization
Object with attribute values storing tuples.
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.
A base class which provides memory management and object aggregation.
Definition object.h:78
Hold variables of type string.
Definition string.h:45
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
AttributeValue implementation for Tuple.
Definition tuple.h:67
std::tuple< Args... > value_type
Type of value stored in the TupleValue.
Definition tuple.h:70
result_type Get() const
Get the stored values as a std::tuple.
Definition tuple.h:322
std::tuple< std::invoke_result_t< decltype(&Args::Get), Args >... > result_type
Type returned by Get or passed in Set.
Definition tuple.h:72
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 AttributeChecker > MakeTupleChecker(Ts... checkers)
Create a TupleChecker from AttributeCheckers associated with TupleValue elements.
Definition tuple.h:529
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:536
auto MakeTupleValue(T2 t)
Create a TupleValue object.
Definition tuple.h:522
#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 > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
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:179
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
static TupleValueTestSuite g_tupleValueTestSuite
Static variable for test initialization.