A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
struct-value-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 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/attribute-container.h"
10#include "ns3/double.h"
11#include "ns3/object.h"
12#include "ns3/string.h"
13#include "ns3/struct.h"
14#include "ns3/test.h"
15#include "ns3/uinteger.h"
16
17#include <cstdint>
18#include <set>
19#include <string>
20#include <utility>
21
22using namespace ns3;
23
24/** Structure used by the StructValue tests. */
26{
27 std::string name; //!< Name field
28 uint16_t count; //!< Count field
29 double value; //!< Value field
30
31 /**
32 * Compare two structures.
33 *
34 * @return true if all the fields are equal
35 */
36 bool operator==(const StructTestData&) const = default;
37};
38
39/** StructValue type used by the tests. */
44
45/** Structure with private fields used by the StructValue tests. */
47{
48 public:
49 /** Default constructor. */
51
52 /**
53 * Construct an instance with the given field values.
54 *
55 * @param name The name field.
56 * @param count The count field.
57 * @param value The value field.
58 */
59 PrivateStructTestData(std::string name, uint16_t count, double value)
60 : m_name(std::move(name)),
61 m_count(count),
62 m_value(value)
63 {
64 }
65
66 /**
67 * Construct an instance from its field values.
68 *
69 * @param name The name field.
70 * @param count The count field.
71 * @param value The value field.
72 * @return the constructed instance
73 */
74 static PrivateStructTestData Create(std::string name, uint16_t count, double value)
75 {
76 return {std::move(name), count, value};
77 }
78
79 /** @return the count field */
80 uint16_t GetCount() const
81 {
82 return m_count;
83 }
84
85 /**
86 * Set the count field.
87 *
88 * @param count The count to set.
89 */
90 void SetCount(uint16_t count)
91 {
92 m_count = count;
93 }
94
95 /** @return the value field */
96 double GetValue() const
97 {
98 return m_value;
99 }
100
101 /**
102 * Set the value field.
103 *
104 * @param value The value to set.
105 */
106 void SetValue(double value)
107 {
108 m_value = value;
109 }
110
111 /**
112 * Compare two structures.
113 *
114 * @return true if all fields are equal
115 */
116 bool operator==(const PrivateStructTestData&) const = default;
117
118 std::string m_name; //!< Name field (publicly accessible)
119
120 private:
121 uint16_t m_count{}; //!< Count field
122 double m_value{}; //!< Value field
123};
124
125/** StructValue using getters and setters to access private fields. */
131
132/** StructValue using a constructor function and getters for private fields. */
139
140/** Object containing attributes represented by structures. */
141class StructObject : public Object
142{
143 public:
144 /**
145 * Get the type ID.
146 *
147 * @return the object TypeId
148 */
149 static TypeId GetTypeId();
150
151 /**
152 * Set the structure accessed through methods.
153 *
154 * @param value the structure to set
155 */
156 void SetMethodStruct(const StructTestData& value);
157
158 /**
159 * Get the structure accessed through methods.
160 *
161 * @return the stored structure
162 */
164
165 StructTestData m_memberStruct; //!< Structure accessed as a data member
166 PrivateStructTestData m_privateStruct; //!< Structure whose fields are accessed through methods
167
168 private:
169 StructTestData m_methodStruct; //!< Structure accessed through methods
170};
171
172TypeId
174{
175 static TypeId tid =
176 TypeId("ns3::StructObject")
177 .SetParent<Object>()
178 .SetGroupName("Test")
179 .AddConstructor<StructObject>()
180 .AddAttribute("MemberStruct",
181 "Structure accessed as a data member",
182 StructTestValue(StructTestData{"member", 2, 3.5}),
186 MakeDoubleChecker<double>(0.0, 10.0)))
187 .AddAttribute(
188 "PrivateStruct",
189 "Structure whose fields are accessed through getters and setters",
194 MakeDoubleChecker<double>(0.0, 10.0)))
195 .AddAttribute("MethodStruct",
196 "Structure accessed through a getter and a setter",
197 StructTestValue(StructTestData{"method", 4, 5.5}),
202 MakeDoubleChecker<double>(0.0, 10.0)));
203 return tid;
204}
205
206void
211
214{
215 return m_methodStruct;
216}
217
218/** Test StructValue construction, checking, serialization, and access. */
220{
221 public:
222 /** Constructor. */
224
225 private:
226 void DoRun() override;
227};
228
230 : TestCase("test StructValue attribute value")
231{
232}
233
234void
236{
237 auto object = CreateObject<StructObject>();
238
239 NS_TEST_ASSERT_MSG_EQ(object->m_memberStruct == StructTestData("member", 2, 3.5),
240 true,
241 "The data member did not receive its default value");
242 NS_TEST_ASSERT_MSG_EQ(object->GetMethodStruct() == StructTestData("method", 4, 5.5),
243 true,
244 "The setter did not receive its default value");
245 NS_TEST_ASSERT_MSG_EQ(object->m_privateStruct == PrivateStructTestData("private", 3, 4.5),
246 true,
247 "The private structure did not receive its default value");
248
249 auto success = object->SetAttributeFailSafe("MemberStruct",
250 StructTestValue(StructTestData{"updated", 6, 7.5}));
251 NS_TEST_ASSERT_MSG_EQ(success, true, "Setting a valid StructValue failed");
252 NS_TEST_ASSERT_MSG_EQ(object->m_memberStruct == StructTestData("updated", 6, 7.5),
253 true,
254 "The data member was not updated");
255
256 StructTestValue value;
257 success = object->GetAttributeFailSafe("MemberStruct", value);
258 NS_TEST_ASSERT_MSG_EQ(success, true, "Getting a StructValue failed");
259 NS_TEST_ASSERT_MSG_EQ(value.Get() == StructTestData("updated", 6, 7.5),
260 true,
261 "The StructValue does not contain the data member fields");
262
263 success = object->SetAttributeFailSafe("PrivateStruct", StringValue("{accessors, 7, 8.5}"));
264 NS_TEST_ASSERT_MSG_EQ(success, true, "Deserializing private structure fields failed");
265 NS_TEST_ASSERT_MSG_EQ(object->m_privateStruct == PrivateStructTestData("accessors", 7, 8.5),
266 true,
267 "The private fields were not updated through their setters");
268
269 PrivateStructTestValue privateValue;
270 success = object->GetAttributeFailSafe("PrivateStruct", privateValue);
271 NS_TEST_ASSERT_MSG_EQ(success, true, "Getting private structure fields failed");
272 NS_TEST_ASSERT_MSG_EQ(privateValue.Get() == PrivateStructTestData("accessors", 7, 8.5),
273 true,
274 "The private fields were not read through their getters");
275
276 auto constructorChecker =
279 MakeDoubleChecker<double>(0.0, 10.0));
280 ConstructedStructTestValue constructedValue;
281 success = constructedValue.DeserializeFromString("{constructed, 9, 1.5}", constructorChecker);
282 NS_TEST_ASSERT_MSG_EQ(success, true, "Deserializing constructor-based fields failed");
283 NS_TEST_ASSERT_MSG_EQ(constructedValue.Get() == PrivateStructTestData("constructed", 9, 1.5),
284 true,
285 "The private fields were not passed to the constructor function");
286
287 success = object->SetAttributeFailSafe("MethodStruct", StringValue("{parsed, 8, 9.5}"));
288 NS_TEST_ASSERT_MSG_EQ(success, true, "Deserializing a valid structure failed");
289 NS_TEST_ASSERT_MSG_EQ(object->GetMethodStruct() == StructTestData("parsed", 8, 9.5),
290 true,
291 "The deserialized structure was not passed to the setter");
292
293 success = object->SetAttributeFailSafe("MethodStruct",
294 StructTestValue(StructTestData{"invalid", 11, 2.5}));
295 NS_TEST_ASSERT_MSG_EQ(success, false, "A field outside its valid range was accepted");
296 NS_TEST_ASSERT_MSG_EQ(object->GetMethodStruct() == StructTestData("parsed", 8, 9.5),
297 true,
298 "The structure changed after a failed assignment");
299
300 success = object->SetAttributeFailSafe("MethodStruct", StringValue("{incomplete}"));
301 NS_TEST_ASSERT_MSG_EQ(success, false, "A structure with missing fields was accepted");
302 NS_TEST_ASSERT_MSG_EQ(object->GetMethodStruct() == StructTestData("parsed", 8, 9.5),
303 true,
304 "The structure changed after failed deserialization");
305
308 MakeDoubleChecker<double>(0.0, 10.0));
309 NS_TEST_ASSERT_MSG_EQ(value.SerializeToString(checker),
310 "{updated, 6, 7.5}",
311 "The structure was not serialized as a tuple");
312
313 auto copy = DynamicCast<StructTestValue>(value.Copy());
314 NS_TEST_ASSERT_MSG_NE(copy, nullptr, "Copy returned the wrong AttributeValue type");
315 NS_TEST_ASSERT_MSG_EQ(copy->Get() == value.Get(),
316 true,
317 "Copy did not preserve the structure fields");
318
319 struct ContainerData
320 {
321 std::set<uint8_t> ids;
322
323 bool operator==(const ContainerData&) const = default;
324 };
325
326 using ContainerValue =
327 StructValue<ContainerData,
328 StructField<AttributeContainerValue<UintegerValue, ';'>, &ContainerData::ids>>;
329 auto containerChecker = MakeStructChecker<ContainerValue>(
331 ContainerValue containerValue(ContainerData{{1, 3, 5}});
332 NS_TEST_ASSERT_MSG_EQ((containerValue.Get() == ContainerData{{1, 3, 5}}),
333 true,
334 "Container field was not stored from the data member");
335 NS_TEST_ASSERT_MSG_EQ(containerValue.SerializeToString(containerChecker),
336 "{1;3;5}",
337 "Container field was not serialized with the item separator");
338 NS_TEST_ASSERT_MSG_EQ(containerValue.DeserializeFromString("{2;4}", containerChecker),
339 true,
340 "Deserializing a container field failed");
341 NS_TEST_ASSERT_MSG_EQ((containerValue.Get() == ContainerData{{2, 4}}),
342 true,
343 "Container field was not written back to the data member");
344}
345
346/** StructValue test suite. */
348{
349 public:
350 /** Constructor. */
352};
353
359
360static StructValueTestSuite g_structValueTestSuite; //!< Static test suite registration
Object containing attributes represented by structures.
StructTestData GetMethodStruct() const
Get the structure accessed through methods.
StructTestData m_memberStruct
Structure accessed as a data member.
PrivateStructTestData m_privateStruct
Structure whose fields are accessed through methods.
void SetMethodStruct(const StructTestData &value)
Set the structure accessed through methods.
static TypeId GetTypeId()
Get the type ID.
StructTestData m_methodStruct
Structure accessed through methods.
Test StructValue construction, checking, serialization, and access.
void DoRun() override
Implementation to actually run this TestCase.
StructValue test suite.
A container for one type of attribute.
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Hold variables of type string.
Definition string.h:45
AttributeValue implementation for structures.
Definition struct.h:144
result_type Get() const
Get the stored fields as a structure.
Definition struct.h:636
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:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
Hold an unsigned integer type.
Definition uinteger.h:34
Ptr< AttributeChecker > MakeAttributeContainerChecker(const AttributeContainerValue< A, Sep, C > &value)
Make AttributeContainerChecker from AttributeContainerValue.
Ptr< const AttributeChecker > MakeStructChecker(Ts... checkers)
Create a StructChecker from the AttributeCheckers associated with the fields.
Definition struct.h:758
Ptr< const AttributeAccessor > MakeStructAccessor(T1 a1)
Create an AttributeAccessor for a structure data member, or for a lone class get functor or set metho...
Definition struct.h:765
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
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition test.h:553
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:167
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:643
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
STL namespace.
static StructValueTestSuite g_structValueTestSuite
Static test suite registration.
StructValue< PrivateStructTestData, StructConstructor<&PrivateStructTestData::Create >, StructField< StringValue, &PrivateStructTestData::m_name >, StructField< UintegerValue, &PrivateStructTestData::GetCount >, StructField< DoubleValue, &PrivateStructTestData::GetValue > > ConstructedStructTestValue
StructValue using a constructor function and getters for private fields.
StructValue< StructTestData, StructField< StringValue, &StructTestData::name >, StructField< UintegerValue, &StructTestData::count >, StructField< DoubleValue, &StructTestData::value > > StructTestValue
StructValue type used by the tests.
StructValue< PrivateStructTestData, StructField< StringValue, &PrivateStructTestData::m_name >, StructField< UintegerValue, &PrivateStructTestData::GetCount, &PrivateStructTestData::SetCount >, StructField< DoubleValue, &PrivateStructTestData::GetValue, &PrivateStructTestData::SetValue > > PrivateStructTestValue
StructValue using getters and setters to access private fields.
Structure with private fields used by the StructValue tests.
PrivateStructTestData(std::string name, uint16_t count, double value)
Construct an instance with the given field values.
std::string m_name
Name field (publicly accessible).
PrivateStructTestData()=default
Default constructor.
bool operator==(const PrivateStructTestData &) const =default
Compare two structures.
void SetValue(double value)
Set the value field.
static PrivateStructTestData Create(std::string name, uint16_t count, double value)
Construct an instance from its field values.
void SetCount(uint16_t count)
Set the count field.
Structure used by the StructValue tests.
double value
Value field.
uint16_t count
Count field.
std::string name
Name field.
bool operator==(const StructTestData &) const =default
Compare two structures.
Describe a function that constructs a structure from its field values.
Definition struct.h:262
Describe a structure field and the AttributeValue used to represent it.
Definition struct.h:227