A Discrete-Event Network Simulator
API
tuple-value-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2021 Universita' degli Studi di Napoli Federico II
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Stefano Avallone <stavallo@unina.it>
19  */
20 
21 #include <ns3/test.h>
22 #include <ns3/log.h>
23 #include <ns3/tuple.h>
24 #include <ns3/double.h>
25 #include <ns3/uinteger.h>
26 #include <ns3/string.h>
27 #include <ns3/ptr.h>
28 #include <ns3/object.h>
29 #include <ns3/enum.h>
30 
31 #include <algorithm>
32 #include <iterator>
33 #include <sstream>
34 #include <utility>
35 #include <tuple>
36 
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE ("TupleTestSuite");
41 
43 class TupleObject : public Object
44 {
45 public:
50  {
53  VALUE3
54  };
55 
56  TupleObject ();
57  virtual ~TupleObject ();
58 
63  static TypeId GetTypeId ();
64 
65  // NOTE EnumValue::Get() return an int, so the tuple element type must be an int
66  // in place of the enum type
70 
71  using Tuple2 = std::tuple <double, uint16_t, std::string>;
72 
77  void SetTuple1 (const Tuple1& tuple);
82  Tuple1 GetTuple1 (void) const;
87  void SetTuple2 (const Tuple2& tuple);
92  Tuple2 GetTuple2 (void) const;
93 
94 private:
97 };
98 
99 TypeId
101 {
102  static TypeId tid = TypeId ("ns3::TupleObject")
103  .SetParent<Object> ()
104  .SetGroupName("Test")
105  .AddConstructor<TupleObject> ()
106  .AddAttribute ("StringStringEnumTuple", "Tuple1: string, string, enum",
107  MakeTupleValue<Tuple1Pack> (Tuple1 {"Hey", "Jude", TupleObject::VALUE1}),
108  MakeTupleAccessor<Tuple1Pack> (&TupleObject::m_tuple1),
109  MakeTupleChecker<Tuple1Pack> (MakeStringChecker (), MakeStringChecker (),
111  TupleObject::VALUE2, "VALUE2")))
112  .AddAttribute ("DoubleUintStringTuple", "Tuple2: double, uint16_t, string",
114  MakeTupleAccessor <DoubleValue, UintegerValue, StringValue> (&TupleObject::SetTuple2,
116  MakeTupleChecker<DoubleValue, UintegerValue, StringValue> (MakeDoubleChecker<double> (1.0, 10.0),
117  MakeUintegerChecker<int> (1, 30),
118  MakeStringChecker ()))
119  ;
120  return tid;
121 }
122 
124 {
125 }
126 
128 {
129 }
130 
131 void
133 {
134  m_tuple1 = tuple;
135 }
136 
139 {
140  return m_tuple1;
141 }
142 
143 void
145 {
146  m_tuple2 = tuple;
147 }
148 
151 {
152  return m_tuple2;
153 }
154 
155 
158 {
159 public:
161  virtual ~TupleValueTestCase () {}
162 
163 private:
164  virtual void DoRun ();
165 };
166 
168  : TestCase ("test TupleValue attribute value")
169 {
170 }
171 
172 void
174 {
175  auto tupleObject = CreateObject <TupleObject> ();
176 
177  // Test that default values have been assigned to tuple 1
178  auto t1 = tupleObject->GetTuple1 ();
179  NS_TEST_ASSERT_MSG_EQ ((std::get<0> (t1) == "Hey"), true, "First element of tuple 1 not correctly set");
180  NS_TEST_ASSERT_MSG_EQ ((std::get<1> (t1) == "Jude"), true, "Second element of tuple 1 not correctly set");
181  NS_TEST_ASSERT_MSG_EQ (std::get<2> (t1), (int)(TupleObject::VALUE1), "Third element of tuple 1 not correctly set");
182 
183  // Test that default values have been assigned to tuple 2
184  auto t2 = tupleObject->GetTuple2 ();
185  NS_TEST_ASSERT_MSG_EQ (std::get<0> (t2), 6.022, "First element of tuple 2 not correctly set");
186  NS_TEST_ASSERT_MSG_EQ (std::get<1> (t2), 23, "Second element of tuple 2 not correctly set");
187  NS_TEST_ASSERT_MSG_EQ ((std::get<2> (t2) == "Avogadro"), true, "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 ("StringStringEnumTuple",
191  MakeTupleValue<TupleObject::Tuple1Pack> (TupleObject::Tuple1 {"Norwegian", "Wood", TupleObject::VALUE2}));
192  NS_TEST_ASSERT_MSG_EQ (ret1, true, "Setting valid values to tuple 1 failed");
193 
195  ret1 = tupleObject->GetAttributeFailSafe ("StringStringEnumTuple", tupleValue1);
196  NS_TEST_ASSERT_MSG_EQ (ret1, true, "Getting values for tuple 1 failed");
197 
198  t1 = tupleValue1.Get ();
199  NS_TEST_ASSERT_MSG_EQ ((std::get<0> (t1) == "Norwegian"), true, "First element of tuple 1 not correctly set");
200  NS_TEST_ASSERT_MSG_EQ ((std::get<1> (t1) == "Wood"), true, "Second element of tuple 1 not correctly set");
201  NS_TEST_ASSERT_MSG_EQ (std::get<2> (t1), (int)(TupleObject::VALUE2), "Third element of tuple 1 not correctly set");
202 
203  // Test that we can correctly set and get new values for tuple 2
204  bool ret2 = tupleObject->SetAttributeFailSafe ("DoubleUintStringTuple",
206  NS_TEST_ASSERT_MSG_EQ (ret2, true, "Setting valid values to tuple 2 failed");
207 
209  ret2 = tupleObject->GetAttributeFailSafe ("DoubleUintStringTuple", tupleValue2);
210  NS_TEST_ASSERT_MSG_EQ (ret2, true, "Getting values for tuple 2 failed");
211 
212  t2 = tupleValue2.Get ();
213  NS_TEST_ASSERT_MSG_EQ (std::get<0> (t2), 8.987, "First element of tuple 2 not correctly set");
214  NS_TEST_ASSERT_MSG_EQ (std::get<1> (t2), 9, "Second element of tuple 2 not correctly set");
215  NS_TEST_ASSERT_MSG_EQ ((std::get<2> (t2) == "Coulomb"), true, "Third element of tuple 2 not correctly set");
216 
217  // Test that we can set tuple 1 from string
218  ret1 = tupleObject->SetAttributeFailSafe ("StringStringEnumTuple", StringValue ("{Come, Together, VALUE1}"));
219  NS_TEST_ASSERT_MSG_EQ (ret1, true, "Setting valid values to tuple 1 failed");
220 
221  t1 = tupleObject->GetTuple1 ();
222  NS_TEST_ASSERT_MSG_EQ ((std::get<0> (t1) == "Come"), true, "First element of tuple 1 not correctly set");
223  NS_TEST_ASSERT_MSG_EQ ((std::get<1> (t1) == "Together"), true, "Second element of tuple 1 not correctly set");
224  NS_TEST_ASSERT_MSG_EQ (std::get<2> (t1), (int)(TupleObject::VALUE1), "Third element of tuple 1 not correctly set");
225 
226  // Test that we can set tuple 2 from string
227  ret2 = tupleObject->SetAttributeFailSafe ("DoubleUintStringTuple", StringValue ("{2.99, 8, LightSpeed}"));
228  NS_TEST_ASSERT_MSG_EQ (ret2, true, "Setting valid values to tuple 2 failed");
229 
230  t2 = tupleObject->GetTuple2 ();
231  NS_TEST_ASSERT_MSG_EQ (std::get<0> (t2), 2.99, "First element of tuple 2 not correctly set");
232  NS_TEST_ASSERT_MSG_EQ (std::get<1> (t2), 8, "Second element of tuple 2 not correctly set");
233  NS_TEST_ASSERT_MSG_EQ ((std::get<2> (t2) == "LightSpeed"), true, "Third element of tuple 2 not correctly set");
234 
235  // Test that setting invalid values fails
236  ret1 = tupleObject->SetAttributeFailSafe ("StringStringEnumTuple",
237  TupleValue<StringValue, StringValue> ({"Get", "Back"}));
238  NS_TEST_ASSERT_MSG_EQ (ret1, false, "Too few values");
239  NS_TEST_ASSERT_MSG_EQ ((tupleObject->GetTuple1 () == std::make_tuple ("Come", "Together", (int)(TupleObject::VALUE1))), true,
240  "Tuple modified after failed assignment");
241 
242  ret1 = tupleObject->SetAttributeFailSafe ("StringStringEnumTuple",
243  MakeTupleValue<TupleObject::Tuple1Pack> (TupleObject::Tuple1 {"Get", "Back", TupleObject::VALUE3}));
244  NS_TEST_ASSERT_MSG_EQ (ret1, false, "Invalid enum value");
245  NS_TEST_ASSERT_MSG_EQ ((tupleObject->GetTuple1 () == std::make_tuple ("Come", "Together", (int)(TupleObject::VALUE1))), true,
246  "Tuple modified after failed assignment");
247 
248  ret2 = tupleObject->SetAttributeFailSafe ("DoubleUintStringTuple",
249  TupleValue <DoubleValue, UintegerValue, StringValue, StringValue> ({4.83, 14, "Josephson", "constant"}));
250  NS_TEST_ASSERT_MSG_EQ (ret2, false, "Too many values");
251  NS_TEST_ASSERT_MSG_EQ ((tupleObject->GetTuple2 () == std::make_tuple (2.99, 8, "LightSpeed")), true,
252  "Tuple modified after failed assignment");
253 
254  ret2 = tupleObject->SetAttributeFailSafe ("DoubleUintStringTuple",
255  TupleValue <DoubleValue, UintegerValue, StringValue> ({48.3, 13, "Josephson"}));
256  NS_TEST_ASSERT_MSG_EQ (ret2, false, "Double value out of range");
257  NS_TEST_ASSERT_MSG_EQ ((tupleObject->GetTuple2 () == std::make_tuple (2.99, 8, "LightSpeed")), true,
258  "Tuple modified after failed assignment");
259 
260  ret2 = tupleObject->SetAttributeFailSafe ("DoubleUintStringTuple",
261  TupleValue <DoubleValue, UintegerValue, StringValue> ({4.83, 130, "Josephson"}));
262  NS_TEST_ASSERT_MSG_EQ (ret2, false, "Uinteger value out of range");
263  NS_TEST_ASSERT_MSG_EQ ((tupleObject->GetTuple2 () == std::make_tuple (2.99, 8, "LightSpeed")), true,
264  "Tuple modified after failed assignment");
265 
266 
267 }
268 
269 
272 {
273  public:
275 };
276 
278  : TestSuite ("tuple-value-test-suite", UNIT)
279 {
280  AddTestCase (new TupleValueTestCase (), TestCase::QUICK);
281 }
282 
Object with attribute values storing tuples.
Tuple2 GetTuple2(void) const
Get tuple2.
void SetTuple1(const Tuple1 &tuple)
Set tuple1.
Tuple1Value::value_type Tuple1Pack
tuple of attribute values
Tuple2 m_tuple2
second tuple
Tuple1 GetTuple1(void) const
Get tuple1.
static TypeId GetTypeId()
Get the type ID.
Tuple1 m_tuple1
first tuple
TupleTestEnum
Test enum type.
void SetTuple2(const Tuple2 &tuple)
Set tuple2.
std::tuple< double, uint16_t, std::string > Tuple2
Tuple2 typedef.
Tuple1Value::result_type Tuple1
tuple of values
Test instantiation, initialization, access.
virtual void DoRun()
Implementation to actually run this TestCase.
A base class which provides memory management and object aggregation.
Definition: object.h:88
Hold variables of type string.
Definition: string.h:41
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Hold objects of type std::tuple<Args...>.
Definition: tuple.h:70
result_type Get(void) const
Get the stored values as a std::tuple.
Definition: tuple.h:314
std::tuple< Args... > value_type
Type of value stored in the TupleValue.
Definition: tuple.h:73
std::tuple< std::invoke_result_t< decltype(&Args::Get), Args >... > result_type
Type returned by Get or passed in Set.
Definition: tuple.h:75
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:141
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:162
static TupleValueTestSuite g_tupleValueTestSuite
Static variable for test initialization.