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 void DoSetIntSrc (int8_t v) {
212 int8_t DoGetIntSrc (void) const {
218 int16_t m_int16WithBounds;
219 int16_t m_int16SetGet;
223 RandomVariable m_random;
224 std::vector<Ptr<Derived> > m_vector1;
225 std::vector<Ptr<Derived> > m_vector2;
226 TracedValue<int8_t> m_intSrc1;
227 TracedValue<int8_t> m_intSrc2;
228 TracedCallback<double, int, float> m_cb;
229 TracedValue<ValueClassTest> m_valueSrc;
234 #define CHECK_GET_STR(p,name,value) \
236 std::string expected = value; \
238 bool ok = p->GetAttributeFailSafe (name, got); \
239 NS_TEST_ASSERT (ok); \
240 NS_TEST_ASSERT_EQUAL (got.Get (), expected); \
242 #define CHECK_GET_PARAM(p,name,type,value) \
244 const type expected = value; \
246 bool ok = p->GetAttributeFailSafe (name, got); \
247 NS_TEST_ASSERT (ok); \
248 NS_TEST_ASSERT_EQUAL (got.Get (), expected.Get ()); \
251 NS_OBJECT_ENSURE_REGISTERED (AttributeObjectTest);
253 AttributeTest::AttributeTest ()
257 AttributeTest::RunTests (void)
261 AttributeList params;
262 Ptr<AttributeObjectTest> p;
263 NS_TEST_ASSERT (params.SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
264 p = CreateObject<AttributeObjectTest> (params);
265 CHECK_GET_STR (p, "TestBoolName", "false");
266 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
268 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", StringValue ("true")));
269 CHECK_GET_STR (p, "TestBoolName", "true");
270 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
272 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", BooleanValue (false)));
273 CHECK_GET_STR (p, "TestBoolName", "false");
274 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
276 p = CreateObject<AttributeObjectTest> ("TestBoolName", StringValue ("true"));
277 CHECK_GET_STR (p, "TestBoolName", "true");
278 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
280 p = CreateObject<AttributeObjectTest> ("TestBoolName", BooleanValue (true));
281 CHECK_GET_STR (p, "TestBoolName", "true");
282 CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
284 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("false")));
285 CHECK_GET_STR (p, "TestBoolA", "false");
286 CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, false);
288 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("true")));
289 CHECK_GET_STR (p, "TestBoolA", "true");
290 CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, true);
293 CHECK_GET_STR (p, "TestInt16", "-2");
294 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -2);
296 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", StringValue ("-5")));
297 CHECK_GET_STR (p, "TestInt16", "-5");
298 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -5);
300 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (+2)));
301 CHECK_GET_STR (p, "TestInt16", "2");
302 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, +2);
304 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (-32768)));
305 CHECK_GET_STR (p, "TestInt16", "-32768");
306 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
308 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (-32769)));
309 CHECK_GET_STR (p, "TestInt16", "-32768");
310 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
312 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (32767)));
313 CHECK_GET_STR (p, "TestInt16", "32767");
314 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
316 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (32768)));
317 CHECK_GET_STR (p, "TestInt16", "32767");
318 CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
320 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (10)));
321 CHECK_GET_STR (p, "TestInt16WithBounds", "10");
322 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
323 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (11)));
324 CHECK_GET_STR (p, "TestInt16WithBounds", "10");
325 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
327 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-5)));
328 CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
329 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
330 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-6)));
331 CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
332 CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
334 CHECK_GET_STR (p, "TestInt16SetGet", "6");
335 CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 6);
336 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16SetGet", IntegerValue (0)));
337 CHECK_GET_STR (p, "TestInt16SetGet", "0");
338 CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 0);
340 CHECK_GET_STR (p, "TestUint8", "1");
341 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 1);
342 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (0)));
343 CHECK_GET_STR (p, "TestUint8", "0");
344 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 0);
345 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (255)));
346 CHECK_GET_STR (p, "TestUint8", "255");
347 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
348 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", StringValue ("255")));
349 CHECK_GET_STR (p, "TestUint8", "255");
350 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
351 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("256")));
352 CHECK_GET_STR (p, "TestUint8", "255");
353 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
354 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("-1")));
355 CHECK_GET_STR (p, "TestUint8", "255");
356 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
357 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", UintegerValue ((uint64_t)-1)));
358 CHECK_GET_STR (p, "TestUint8", "255");
359 CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
361 CHECK_GET_STR (p, "TestFloat", "-1.1");
362 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestFloat", DoubleValue ((float)+2.3)));
363 CHECK_GET_PARAM (p, "TestFloat", DoubleValue, (float)+2.3);
365 CHECK_GET_STR (p, "TestEnum", "TestA");
366 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_A);
367 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", EnumValue (AttributeObjectTest::TEST_C)));
368 CHECK_GET_STR (p, "TestEnum", "TestC");
369 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_C);
370 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", StringValue ("TestB")));
371 CHECK_GET_STR (p, "TestEnum", "TestB");
372 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
373 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", StringValue ("TestD")));
374 CHECK_GET_STR (p, "TestEnum", "TestB");
375 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
376 NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", EnumValue (5)));
377 CHECK_GET_STR (p, "TestEnum", "TestB");
378 CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
380 RandomVariableValue ran;
381 p->GetAttribute ("TestRandom", ran);
382 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (UniformVariable (0.0, 1.0))));
383 NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (ConstantVariable (10.0))));
386 ObjectVectorValue vector;
387 p->GetAttribute ("TestVector1", vector);
388 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
390 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
391 p->GetAttribute ("TestVector1", vector);
392 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
393 Ptr<Object> a = vector.Get (0);
394 NS_TEST_ASSERT_UNEQUAL (a, 0);
396 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
397 p->GetAttribute ("TestVector1", vector);
398 NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
402 ObjectVectorValue vector;
403 p->GetAttribute ("TestVector2", vector);
404 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
406 NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
407 p->GetAttribute ("TestVector2", vector);
408 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
409 Ptr<Object> a = vector.Get (0);
410 NS_TEST_ASSERT_UNEQUAL (a, 0);
412 NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
413 p->GetAttribute ("TestVector2", vector);
414 NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
417 NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("true")));
418 p = CreateObject<AttributeObjectTest> ();
420 p->GetAttribute ("TestBoolName", boolV);
421 NS_TEST_ASSERT_EQUAL (boolV.Get (), true);
423 NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
424 p = CreateObject<AttributeObjectTest> ();
425 p->GetAttribute ("TestBoolName", boolV);
426 NS_TEST_ASSERT_EQUAL (boolV.Get (), false);
429 p->GetAttribute ("IntegerTraceSource1", i);
430 NS_TEST_ASSERT_EQUAL (i.Get (), -2);
431 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (+5)));
432 p->GetAttribute ("IntegerTraceSource1", i);
433 NS_TEST_ASSERT_EQUAL (i.Get (), +5);
434 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (127)));
435 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (128)));
436 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-128)));
437 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-129)));
439 p->GetAttribute ("IntegerTraceSource2", i);
440 NS_TEST_ASSERT_EQUAL (i.Get (), -2);
441 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (+5)));
442 p->GetAttribute ("IntegerTraceSource2", i);
443 NS_TEST_ASSERT_EQUAL (i.Get (), +5);
444 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (127)));
445 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (128)));
446 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-128)));
447 NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-129)));
450 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-1)));
451 NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
452 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (0)));
453 NS_TEST_ASSERT_EQUAL (m_got1, 0);
454 NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
455 NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (1)));
456 NS_TEST_ASSERT_EQUAL (m_got1, 0);
459 p->InvokeCb (1.0, -5, 0.0);
460 NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
461 NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
462 NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
463 p->InvokeCb (1.0, -5, 0.0);
464 NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
465 NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
466 p->InvokeCb (-1.0, -5, 0.0);
467 NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
469 NS_TEST_ASSERT (p->TraceConnectWithoutContext ("ValueSource", MakeCallback (&AttributeTest::NotifySourceValue, this)));
472 p->GetAttribute ("Pointer", ptr);
473 Ptr<Derived> derived = ptr.Get<Derived> ();
474 NS_TEST_ASSERT (derived == 0);
475 derived = Create<Derived> ();
476 NS_TEST_ASSERT (p->SetAttributeFailSafe("Pointer", PointerValue (derived)));
477 p->GetAttribute ("Pointer", ptr);
478 Ptr<Derived> stored = ptr.Get<Derived> ();
479 NS_TEST_ASSERT (stored == derived);
480 p->GetAttribute ("Pointer", ptr);
481 Ptr<Object> storedBase = ptr.Get<Object> ();
482 NS_TEST_ASSERT (stored == storedBase);
483 p->GetAttribute ("Pointer", ptr);
484 Ptr<AttributeObjectTest> x = ptr.Get<AttributeObjectTest> ();
485 NS_TEST_ASSERT (x == 0);
487 p = CreateObject<AttributeObjectTest> ("Pointer", PointerValue (Create<Derived> ()));
488 NS_TEST_ASSERT (p != 0);
490 p->GetAttribute ("Pointer", ptr);
491 derived = ptr.Get<Derived> ();
492 NS_TEST_ASSERT (derived != 0);
499 static AttributeTest g_parameterTest;
504 #endif /* RUN_SELF_TESTS */