A Discrete-Event Network Simulator
API
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  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Stefano Avallone <stavallo@unina.it>
18  */
19 
20 #include <ns3/double.h>
21 #include <ns3/enum.h>
22 #include <ns3/log.h>
23 #include <ns3/object.h>
24 #include <ns3/ptr.h>
25 #include <ns3/string.h>
26 #include <ns3/test.h>
27 #include <ns3/tuple.h>
28 #include <ns3/uinteger.h>
29 
30 #include <algorithm>
31 #include <iterator>
32 #include <sstream>
33 #include <tuple>
34 #include <utility>
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE("TupleTestSuite");
39 
41 class TupleObject : public Object
42 {
43  public:
48  {
51  VALUE3
52  };
53 
54  TupleObject();
55  ~TupleObject() override;
56 
61  static TypeId GetTypeId();
62 
63  // NOTE EnumValue::Get() return an int, so the tuple element type must be an int
64  // in place of the enum type
68 
69  using Tuple2 = std::tuple<double, uint16_t, std::string>;
70 
75  void SetTuple1(const Tuple1& tuple);
80  Tuple1 GetTuple1() const;
85  void SetTuple2(const Tuple2& tuple);
90  Tuple2 GetTuple2() const;
91 
92  private:
95 };
96 
97 TypeId
99 {
100  static TypeId tid =
101  TypeId("ns3::TupleObject")
102  .SetParent<Object>()
103  .SetGroupName("Test")
104  .AddConstructor<TupleObject>()
105  .AddAttribute(
106  "StringStringEnumTuple",
107  "Tuple1: string, string, enum",
108  MakeTupleValue<Tuple1Pack>(Tuple1{"Hey", "Jude", TupleObject::VALUE1}),
109  MakeTupleAccessor<Tuple1Pack>(&TupleObject::m_tuple1),
110  MakeTupleChecker<Tuple1Pack>(
114  .AddAttribute(
115  "DoubleUintStringTuple",
116  "Tuple2: double, uint16_t, string",
118  MakeTupleAccessor<DoubleValue, UintegerValue, StringValue>(&TupleObject::SetTuple2,
120  MakeTupleChecker<DoubleValue, UintegerValue, StringValue>(
121  MakeDoubleChecker<double>(1.0, 10.0),
122  MakeUintegerChecker<int>(1, 30),
123  MakeStringChecker()));
124  return tid;
125 }
126 
128 {
129 }
130 
132 {
133 }
134 
135 void
137 {
138  m_tuple1 = tuple;
139 }
140 
143 {
144  return m_tuple1;
145 }
146 
147 void
149 {
150  m_tuple2 = tuple;
151 }
152 
155 {
156  return m_tuple2;
157 }
158 
161 {
162  public:
164 
166  {
167  }
168 
169  private:
170  void DoRun() override;
171 };
172 
174  : TestCase("test TupleValue attribute value")
175 {
176 }
177 
178 void
180 {
181  auto tupleObject = CreateObject<TupleObject>();
182 
183  // Test that default values have been assigned to tuple 1
184  auto t1 = tupleObject->GetTuple1();
185  NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Hey"),
186  true,
187  "First element of tuple 1 not correctly set");
188  NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Jude"),
189  true,
190  "Second element of tuple 1 not correctly set");
191  NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
192  (int)(TupleObject::VALUE1),
193  "Third element of tuple 1 not correctly set");
194 
195  // Test that default values have been assigned to tuple 2
196  auto t2 = tupleObject->GetTuple2();
197  NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 6.022, "First element of tuple 2 not correctly set");
198  NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 23, "Second element of tuple 2 not correctly set");
199  NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "Avogadro"),
200  true,
201  "Third element of tuple 2 not correctly set");
202 
203  // Test that we can correctly set and get new values for tuple 1
204  bool ret1 = tupleObject->SetAttributeFailSafe(
205  "StringStringEnumTuple",
206  MakeTupleValue<TupleObject::Tuple1Pack>(
207  TupleObject::Tuple1{"Norwegian", "Wood", TupleObject::VALUE2}));
208  NS_TEST_ASSERT_MSG_EQ(ret1, true, "Setting valid values to tuple 1 failed");
209 
211  ret1 = tupleObject->GetAttributeFailSafe("StringStringEnumTuple", tupleValue1);
212  NS_TEST_ASSERT_MSG_EQ(ret1, true, "Getting values for tuple 1 failed");
213 
214  t1 = tupleValue1.Get();
215  NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Norwegian"),
216  true,
217  "First element of tuple 1 not correctly set");
218  NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Wood"),
219  true,
220  "Second element of tuple 1 not correctly set");
221  NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
222  (int)(TupleObject::VALUE2),
223  "Third element of tuple 1 not correctly set");
224 
225  // Test that we can correctly set and get new values for tuple 2
226  bool ret2 = tupleObject->SetAttributeFailSafe(
227  "DoubleUintStringTuple",
229  NS_TEST_ASSERT_MSG_EQ(ret2, true, "Setting valid values to tuple 2 failed");
230 
232  ret2 = tupleObject->GetAttributeFailSafe("DoubleUintStringTuple", tupleValue2);
233  NS_TEST_ASSERT_MSG_EQ(ret2, true, "Getting values for tuple 2 failed");
234 
235  t2 = tupleValue2.Get();
236  NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 8.987, "First element of tuple 2 not correctly set");
237  NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 9, "Second element of tuple 2 not correctly set");
238  NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "Coulomb"),
239  true,
240  "Third element of tuple 2 not correctly set");
241 
242  // Test that we can set tuple 1 from string
243  ret1 = tupleObject->SetAttributeFailSafe("StringStringEnumTuple",
244  StringValue("{Come, Together, VALUE1}"));
245  NS_TEST_ASSERT_MSG_EQ(ret1, true, "Setting valid values to tuple 1 failed");
246 
247  t1 = tupleObject->GetTuple1();
248  NS_TEST_ASSERT_MSG_EQ((std::get<0>(t1) == "Come"),
249  true,
250  "First element of tuple 1 not correctly set");
251  NS_TEST_ASSERT_MSG_EQ((std::get<1>(t1) == "Together"),
252  true,
253  "Second element of tuple 1 not correctly set");
254  NS_TEST_ASSERT_MSG_EQ(std::get<2>(t1),
255  (int)(TupleObject::VALUE1),
256  "Third element of tuple 1 not correctly set");
257 
258  // Test that we can set tuple 2 from string
259  ret2 = tupleObject->SetAttributeFailSafe("DoubleUintStringTuple",
260  StringValue("{2.99, 8, LightSpeed}"));
261  NS_TEST_ASSERT_MSG_EQ(ret2, true, "Setting valid values to tuple 2 failed");
262 
263  t2 = tupleObject->GetTuple2();
264  NS_TEST_ASSERT_MSG_EQ(std::get<0>(t2), 2.99, "First element of tuple 2 not correctly set");
265  NS_TEST_ASSERT_MSG_EQ(std::get<1>(t2), 8, "Second element of tuple 2 not correctly set");
266  NS_TEST_ASSERT_MSG_EQ((std::get<2>(t2) == "LightSpeed"),
267  true,
268  "Third element of tuple 2 not correctly set");
269 
270  // Test that setting invalid values fails
271  ret1 = tupleObject->SetAttributeFailSafe("StringStringEnumTuple",
272  TupleValue<StringValue, StringValue>({"Get", "Back"}));
273  NS_TEST_ASSERT_MSG_EQ(ret1, false, "Too few values");
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  ret1 = tupleObject->SetAttributeFailSafe(
280  "StringStringEnumTuple",
281  MakeTupleValue<TupleObject::Tuple1Pack>(
282  TupleObject::Tuple1{"Get", "Back", TupleObject::VALUE3}));
283  NS_TEST_ASSERT_MSG_EQ(ret1, false, "Invalid enum value");
284  NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple1() ==
285  std::make_tuple("Come", "Together", (int)(TupleObject::VALUE1))),
286  true,
287  "Tuple modified after failed assignment");
288 
289  ret2 = tupleObject->SetAttributeFailSafe(
290  "DoubleUintStringTuple",
292  {4.83, 14, "Josephson", "constant"}));
293  NS_TEST_ASSERT_MSG_EQ(ret2, false, "Too many values");
294  NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
295  true,
296  "Tuple modified after failed assignment");
297 
298  ret2 = tupleObject->SetAttributeFailSafe(
299  "DoubleUintStringTuple",
300  TupleValue<DoubleValue, UintegerValue, StringValue>({48.3, 13, "Josephson"}));
301  NS_TEST_ASSERT_MSG_EQ(ret2, false, "Double value out of range");
302  NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
303  true,
304  "Tuple modified after failed assignment");
305 
306  ret2 = tupleObject->SetAttributeFailSafe(
307  "DoubleUintStringTuple",
308  TupleValue<DoubleValue, UintegerValue, StringValue>({4.83, 130, "Josephson"}));
309  NS_TEST_ASSERT_MSG_EQ(ret2, false, "Uinteger value out of range");
310  NS_TEST_ASSERT_MSG_EQ((tupleObject->GetTuple2() == std::make_tuple(2.99, 8, "LightSpeed")),
311  true,
312  "Tuple modified after failed assignment");
313 }
314 
317 {
318  public:
320 };
321 
323  : TestSuite("tuple-value-test-suite", UNIT)
324 {
325  AddTestCase(new TupleValueTestCase(), TestCase::QUICK);
326 }
327 
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:89
Hold variables of type string.
Definition: string.h:56
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
Hold objects of type std::tuple<Args...>.
Definition: tuple.h:69
result_type Get() const
Get the stored values as a std::tuple.
Definition: tuple.h:311
std::tuple< Args... > value_type
Type of value stored in the TupleValue.
Definition: tuple.h:72
std::tuple< std::invoke_result_t< decltype(&Args::Get), Args >... > result_type
Type returned by Get or passed in Set.
Definition: tuple.h:74
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Ptr< const AttributeChecker > MakeStringChecker()
Definition: string.cc:30
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:144
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:163
static TupleValueTestSuite g_tupleValueTestSuite
Static variable for test initialization.