allow attribute setters to fail.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
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;
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.
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
18 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
28 #include "random-variable.h"
30 #include "object-vector.h"
31 #include "traced-value.h"
32 #include "trace-source-accessor.h"
44 bool operator != (const ValueClassTest &a, const ValueClassTest &b)
48 std::ostream & operator << (std::ostream &os, ValueClassTest v)
52 std::istream & operator >> (std::istream &is, ValueClassTest &v)
56 ATTRIBUTE_HELPER_HEADER (ValueClassTest);
57 ATTRIBUTE_HELPER_CPP (ValueClassTest);
59 class AttributeTest : public Test
63 virtual bool RunTests (void);
65 void NotifySource1 (int8_t old, int8_t n) {
68 void NotifySource2 (double a, int b, float c) {
71 void NotifySourceValue (ValueClassTest old, ValueClassTest n) {
76 ValueClassTest m_gotValue;
79 class Derived : public Object
82 static TypeId GetTypeId (void) {
83 static TypeId tid = TypeId ("Derived")
90 class AttributeObjectTest : public Object
98 static TypeId GetTypeId (void) {
99 static TypeId tid = TypeId ("ns3::AttributeObjectTest")
100 .SetParent<Object> ()
101 .HideFromDocumentation ()
102 .AddAttribute ("TestBoolName", "help text",
103 BooleanValue (false),
104 MakeBooleanAccessor (&AttributeObjectTest::m_boolTest),
105 MakeBooleanChecker ())
106 .AddAttribute ("TestBoolA", "help text",
107 BooleanValue (false),
108 MakeBooleanAccessor (&AttributeObjectTest::DoSetTestB,
109 &AttributeObjectTest::DoGetTestB),
110 MakeBooleanChecker ())
111 .AddAttribute ("TestInt16", "help text",
113 MakeIntegerAccessor (&AttributeObjectTest::m_int16),
114 MakeIntegerChecker<int16_t> ())
115 .AddAttribute ("TestInt16WithBounds", "help text",
117 MakeIntegerAccessor (&AttributeObjectTest::m_int16WithBounds),
118 MakeIntegerChecker<int16_t> (-5, 10))
119 .AddAttribute ("TestInt16SetGet", "help text",
121 MakeIntegerAccessor (&AttributeObjectTest::DoSetInt16,
122 &AttributeObjectTest::DoGetInt16),
123 MakeIntegerChecker<int16_t> ())
124 .AddAttribute ("TestUint8", "help text",
126 MakeUintegerAccessor (&AttributeObjectTest::m_uint8),
127 MakeUintegerChecker<uint8_t> ())
128 .AddAttribute ("TestEnum", "help text",
130 MakeEnumAccessor (&AttributeObjectTest::m_enum),
131 MakeEnumChecker (TEST_A, "TestA",
134 .AddAttribute ("TestRandom", "help text",
135 RandomVariableValue (ConstantVariable (1.0)),
136 MakeRandomVariableAccessor (&AttributeObjectTest::m_random),
137 MakeRandomVariableChecker ())
138 .AddAttribute ("TestFloat", "help text",
140 MakeDoubleAccessor (&AttributeObjectTest::m_float),
141 MakeDoubleChecker<float> ())
142 .AddAttribute ("TestVector1", "help text",
143 ObjectVectorValue (),
144 MakeObjectVectorAccessor (&AttributeObjectTest::m_vector1),
145 MakeObjectVectorChecker<Derived> ())
146 .AddAttribute ("TestVector2", "help text",
147 ObjectVectorValue (),
148 MakeObjectVectorAccessor (&AttributeObjectTest::DoGetVectorN,
149 &AttributeObjectTest::DoGetVector),
150 MakeObjectVectorChecker<Derived> ())
151 .AddAttribute ("IntegerTraceSource1", "help text",
153 MakeIntegerAccessor (&AttributeObjectTest::m_intSrc1),
154 MakeIntegerChecker<int8_t> ())
155 .AddAttribute ("IntegerTraceSource2", "help text",
157 MakeIntegerAccessor (&AttributeObjectTest::DoSetIntSrc,
158 &AttributeObjectTest::DoGetIntSrc),
159 MakeIntegerChecker<int8_t> ())
160 .AddAttribute ("ValueClassSource", "help text",
161 ValueClassTestValue (ValueClassTest ()),
162 MakeValueClassTestAccessor (&AttributeObjectTest::m_valueSrc),
163 MakeValueClassTestChecker ())
164 .AddTraceSource ("Source1", "help test",
165 MakeTraceSourceAccessor (&AttributeObjectTest::m_intSrc1))
166 .AddTraceSource ("Source2", "help text",
167 MakeTraceSourceAccessor (&AttributeObjectTest::m_cb))
168 .AddTraceSource ("ValueSource", "help text",
169 MakeTraceSourceAccessor (&AttributeObjectTest::m_valueSrc))
170 .AddAttribute ("Pointer", "help text",
172 MakePointerAccessor (&AttributeObjectTest::m_ptr),
173 MakePointerChecker<Derived> ())
179 void AddToVector1 (void) {
180 m_vector1.push_back (CreateObject<Derived> ());
182 void AddToVector2 (void) {
183 m_vector2.push_back (CreateObject<Derived> ());
186 void InvokeCb (double a, int b, float c) {
191 void DoSetTestB (bool v) {
194 bool DoGetTestB (void) const {
197 int16_t DoGetInt16 (void) const {
198 return m_int16SetGet;
200 void DoSetInt16 (int16_t v) {
203 uint32_t DoGetVectorN (void) const {
204 return m_vector2.size ();
206 Ptr<Derived> DoGetVector (uint32_t i) const {
209 bool DoSetIntSrc (int8_t v) {
213 int8_t DoGetIntSrc (void) const {
219 int16_t m_int16WithBounds;
220 int16_t m_int16SetGet;
224 RandomVariable m_random;
225 std::vector<Ptr<Derived> > m_vector1;
226 std::vector<Ptr<Derived> > m_vector2;
227 TracedValue<int8_t> m_intSrc1;
228 TracedValue<int8_t> m_intSrc2;
229 TracedCallback<double, int, float> m_cb;
230 TracedValue<ValueClassTest> m_valueSrc;
235 #define CHECK_GET_STR(p,name,value) \
237 std::string expected = value; \
239 bool ok = p->GetAttributeFailSafe (name, got); \
240 NS_TEST_ASSERT (ok); \
241 NS_TEST_ASSERT_EQUAL (got.Get (), expected); \
243 #define CHECK_GET_PARAM(p,name,type,value) \
245 const type expected = value; \
247 bool ok = p->GetAttributeFailSafe (name, got); \
248 NS_TEST_ASSERT (ok); \
249 NS_TEST_ASSERT_EQUAL (got.Get (), expected.Get ()); \
252 NS_OBJECT_ENSURE_REGISTERED (AttributeObjectTest);
254 AttributeTest::AttributeTest ()
258 AttributeTest::RunTests (void)
262 AttributeList params;
263 Ptr<AttributeObjectTest> p;
264 NS_TEST_ASSERT (params.SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
265 p = CreateObject<AttributeObjectTest> (params);
266 CHECK_GET_STR (p, "TestBoolName", "false");
267 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
269 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", StringValue ("true")));
270 CHECK_GET_STR (p, "TestBoolName", "true");
271 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
273 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", BooleanValue (false)));
274 CHECK_GET_STR (p, "TestBoolName", "false");
275 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
277 p = CreateObject<AttributeObjectTest> ("TestBoolName", StringValue ("true"));
278 CHECK_GET_STR (p, "TestBoolName", "true");
279 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
281 p = CreateObject<AttributeObjectTest> ("TestBoolName", BooleanValue (true));
282 CHECK_GET_STR (p, "TestBoolName", "true");
283 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
285 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("false")));
286 CHECK_GET_STR (p, "TestBoolA", "false");
287 CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, false);
289 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("true")));
290 CHECK_GET_STR (p, "TestBoolA", "true");
291 CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, true);
294 CHECK_GET_STR (p, "TestInt16", "-2");
295 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -2);
297 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", StringValue ("-5")));
298 CHECK_GET_STR (p, "TestInt16", "-5");
299 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -5);
301 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (+2)));
302 CHECK_GET_STR (p, "TestInt16", "2");
303 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, +2);
305 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (-32768)));
306 CHECK_GET_STR (p, "TestInt16", "-32768");
307 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
309 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (-32769)));
310 CHECK_GET_STR (p, "TestInt16", "-32768");
311 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
313 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (32767)));
314 CHECK_GET_STR (p, "TestInt16", "32767");
315 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
317 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (32768)));
318 CHECK_GET_STR (p, "TestInt16", "32767");
319 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
321 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (10)));
322 CHECK_GET_STR (p, "TestInt16WithBounds", "10");
323 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
324 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (11)));
325 CHECK_GET_STR (p, "TestInt16WithBounds", "10");
326 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
328 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-5)));
329 CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
330 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
331 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-6)));
332 CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
333 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
335 CHECK_GET_STR (p, "TestInt16SetGet", "6");
336 CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 6);
337 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16SetGet", IntegerValue (0)));
338 CHECK_GET_STR (p, "TestInt16SetGet", "0");
339 CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 0);
341 CHECK_GET_STR (p, "TestUint8", "1");
342 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 1);
343 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (0)));
344 CHECK_GET_STR (p, "TestUint8", "0");
345 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 0);
346 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (255)));
347 CHECK_GET_STR (p, "TestUint8", "255");
348 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
349 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", StringValue ("255")));
350 CHECK_GET_STR (p, "TestUint8", "255");
351 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
352 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("256")));
353 CHECK_GET_STR (p, "TestUint8", "255");
354 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
355 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("-1")));
356 CHECK_GET_STR (p, "TestUint8", "255");
357 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
358 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", UintegerValue ((uint64_t)-1)));
359 CHECK_GET_STR (p, "TestUint8", "255");
360 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
362 CHECK_GET_STR (p, "TestFloat", "-1.1");
363 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestFloat", DoubleValue ((float)+2.3)));
364 CHECK_GET_PARAM (p, "TestFloat", DoubleValue, (float)+2.3);
366 CHECK_GET_STR (p, "TestEnum", "TestA");
367 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_A);
368 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", EnumValue (AttributeObjectTest::TEST_C)));
369 CHECK_GET_STR (p, "TestEnum", "TestC");
370 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_C);
371 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", StringValue ("TestB")));
372 CHECK_GET_STR (p, "TestEnum", "TestB");
373 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
374 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", StringValue ("TestD")));
375 CHECK_GET_STR (p, "TestEnum", "TestB");
376 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
377 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", EnumValue (5)));
378 CHECK_GET_STR (p, "TestEnum", "TestB");
379 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
381 RandomVariableValue ran;
382 p->GetAttribute ("TestRandom", ran);
383 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (UniformVariable (0.0, 1.0))));
384 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (ConstantVariable (10.0))));
387 ObjectVectorValue vector;
388 p->GetAttribute ("TestVector1", vector);
389 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
391 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
392 p->GetAttribute ("TestVector1", vector);
393 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
394 Ptr<Object> a = vector.Get (0);
395 NS_TEST_ASSERT_UNEQUAL (a, 0);
397 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
398 p->GetAttribute ("TestVector1", vector);
399 NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
403 ObjectVectorValue vector;
404 p->GetAttribute ("TestVector2", vector);
405 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
407 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
408 p->GetAttribute ("TestVector2", vector);
409 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
410 Ptr<Object> a = vector.Get (0);
411 NS_TEST_ASSERT_UNEQUAL (a, 0);
413 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
414 p->GetAttribute ("TestVector2", vector);
415 NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
418 NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("true")));
419 p = CreateObject<AttributeObjectTest> ();
421 p->GetAttribute ("TestBoolName", boolV);
422 NS_TEST_ASSERT_EQUAL (boolV.Get (), true);
424 NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
425 p = CreateObject<AttributeObjectTest> ();
426 p->GetAttribute ("TestBoolName", boolV);
427 NS_TEST_ASSERT_EQUAL (boolV.Get (), false);
430 p->GetAttribute ("IntegerTraceSource1", i);
431 NS_TEST_ASSERT_EQUAL (i.Get (), -2);
432 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (+5)));
433 p->GetAttribute ("IntegerTraceSource1", i);
434 NS_TEST_ASSERT_EQUAL (i.Get (), +5);
435 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (127)));
436 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (128)));
437 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-128)));
438 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-129)));
440 p->GetAttribute ("IntegerTraceSource2", i);
441 NS_TEST_ASSERT_EQUAL (i.Get (), -2);
442 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (+5)));
443 p->GetAttribute ("IntegerTraceSource2", i);
444 NS_TEST_ASSERT_EQUAL (i.Get (), +5);
445 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (127)));
446 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (128)));
447 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-128)));
448 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-129)));
451 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-1)));
452 NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
453 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (0)));
454 NS_TEST_ASSERT_EQUAL (m_got1, 0);
455 NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
456 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (1)));
457 NS_TEST_ASSERT_EQUAL (m_got1, 0);
460 p->InvokeCb (1.0, -5, 0.0);
461 NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
462 NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
463 NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
464 p->InvokeCb (1.0, -5, 0.0);
465 NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
466 NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
467 p->InvokeCb (-1.0, -5, 0.0);
468 NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
470 NS_TEST_ASSERT (p->TraceConnectWithoutContext ("ValueSource", MakeCallback (&AttributeTest::NotifySourceValue, this)));
473 p->GetAttribute ("Pointer", ptr);
474 Ptr<Derived> derived = ptr.Get<Derived> ();
475 NS_TEST_ASSERT (derived == 0);
476 derived = Create<Derived> ();
477 NS_TEST_ASSERT (p->SetAttributeFailSafe("Pointer", PointerValue (derived)));
478 p->GetAttribute ("Pointer", ptr);
479 Ptr<Derived> stored = ptr.Get<Derived> ();
480 NS_TEST_ASSERT (stored == derived);
481 p->GetAttribute ("Pointer", ptr);
482 Ptr<Object> storedBase = ptr.Get<Object> ();
483 NS_TEST_ASSERT (stored == storedBase);
484 p->GetAttribute ("Pointer", ptr);
485 Ptr<AttributeObjectTest> x = ptr.Get<AttributeObjectTest> ();
486 NS_TEST_ASSERT (x == 0);
488 p = CreateObject<AttributeObjectTest> ("Pointer", PointerValue (Create<Derived> ()));
489 NS_TEST_ASSERT (p != 0);
491 p->GetAttribute ("Pointer", ptr);
492 derived = ptr.Get<Derived> ();
493 NS_TEST_ASSERT (derived != 0);
500 static AttributeTest g_parameterTest;
505 #endif /* RUN_SELF_TESTS */