src/core/attribute-test.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3763 e46e361a4262
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2581
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2581
     2
/*
mathieu@2581
     3
 * Copyright (c) 2008 INRIA
mathieu@2581
     4
 *
mathieu@2581
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2581
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2581
     7
 * published by the Free Software Foundation;
mathieu@2581
     8
 *
mathieu@2581
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2581
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2581
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2581
    12
 * GNU General Public License for more details.
mathieu@2581
    13
 *
mathieu@2581
    14
 * You should have received a copy of the GNU General Public License
mathieu@2581
    15
 * along with this program; if not, write to the Free Software
mathieu@2581
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2581
    17
 *
mathieu@2581
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2581
    19
 */
mathieu@2374
    20
#ifdef RUN_SELF_TESTS
mathieu@2374
    21
#include "test.h"
mathieu@2374
    22
#include "object.h"
mathieu@2452
    23
#include "boolean.h"
mathieu@2454
    24
#include "integer.h"
mathieu@2455
    25
#include "uinteger.h"
mathieu@2457
    26
#include "enum.h"
mathieu@2502
    27
#include "string.h"
mathieu@2384
    28
#include "random-variable.h"
mathieu@2456
    29
#include "double.h"
mathieu@2405
    30
#include "object-vector.h"
mathieu@2482
    31
#include "traced-value.h"
mathieu@3763
    32
#include "callback.h"
mathieu@2463
    33
#include "trace-source-accessor.h"
mathieu@2926
    34
#include "pointer.h"
mathieu@2405
    35
mathieu@2374
    36
namespace ns3 {
mathieu@2374
    37
mathieu@2469
    38
class ValueClassTest 
mathieu@2469
    39
{
mathieu@2469
    40
public:
mathieu@2469
    41
  ValueClassTest () {}
mathieu@2469
    42
private:
mathieu@2469
    43
  int m_v;
mathieu@2469
    44
};
mathieu@2469
    45
bool operator != (const ValueClassTest &a, const ValueClassTest &b)
mathieu@2469
    46
{
mathieu@2469
    47
  return true;
mathieu@2469
    48
}
mathieu@2469
    49
std::ostream & operator << (std::ostream &os, ValueClassTest v)
mathieu@2469
    50
{
mathieu@2469
    51
  return os;
mathieu@2469
    52
}
mathieu@2469
    53
std::istream & operator >> (std::istream &is, ValueClassTest &v)
mathieu@2469
    54
{
mathieu@2469
    55
  return is;
mathieu@2469
    56
}
mathieu@3094
    57
ATTRIBUTE_HELPER_HEADER (ValueClassTest);
mathieu@2582
    58
ATTRIBUTE_HELPER_CPP (ValueClassTest);
mathieu@2469
    59
mathieu@2450
    60
class AttributeTest : public Test
mathieu@2374
    61
{
mathieu@2374
    62
public:
mathieu@2450
    63
  AttributeTest ();
mathieu@2374
    64
  virtual bool RunTests (void);
mathieu@2374
    65
private:
mathieu@2468
    66
  void NotifySource1 (int8_t old, int8_t n) {
mathieu@2466
    67
    m_got1 = n;
mathieu@2463
    68
  }
mathieu@2466
    69
  void NotifySource2 (double a, int b, float c) {
mathieu@2466
    70
    m_got2 = a;
mathieu@2466
    71
  }
mathieu@2469
    72
  void NotifySourceValue (ValueClassTest old, ValueClassTest n) {
mathieu@2469
    73
    m_gotValue = n;
mathieu@2469
    74
  }
mathieu@3763
    75
  void NotifyCallbackValue (int8_t a) {
mathieu@3763
    76
    m_gotCbValue = a;
mathieu@3763
    77
  }
mathieu@2466
    78
  int64_t m_got1;
mathieu@2466
    79
  double m_got2;
mathieu@2469
    80
  ValueClassTest m_gotValue;
mathieu@3763
    81
  int16_t m_gotCbValue;
mathieu@2374
    82
};
mathieu@2374
    83
mathieu@2374
    84
class Derived : public Object
mathieu@2374
    85
{
mathieu@2374
    86
public:
mathieu@2374
    87
  static TypeId GetTypeId (void) {
mathieu@2374
    88
    static TypeId tid = TypeId ("Derived")
mathieu@2374
    89
      .SetParent<Object> ()
mathieu@2374
    90
      ;
mathieu@2374
    91
    return tid;
mathieu@2374
    92
  }
mathieu@2374
    93
};
mathieu@2374
    94
mathieu@2450
    95
class AttributeObjectTest : public Object
mathieu@2374
    96
{
mathieu@2374
    97
public:
mathieu@2965
    98
  enum Test_e {
mathieu@2374
    99
    TEST_A,
mathieu@2374
   100
    TEST_B,
mathieu@2374
   101
    TEST_C
mathieu@2374
   102
  };
mathieu@2374
   103
  static TypeId GetTypeId (void) {
mathieu@2613
   104
    static TypeId tid = TypeId ("ns3::AttributeObjectTest")
mathieu@2374
   105
      .SetParent<Object> ()
mathieu@2602
   106
      .HideFromDocumentation ()
mathieu@2458
   107
      .AddAttribute ("TestBoolName", "help text",
mathieu@2965
   108
		     BooleanValue (false),
mathieu@2450
   109
		     MakeBooleanAccessor (&AttributeObjectTest::m_boolTest),
mathieu@2427
   110
		     MakeBooleanChecker ())
mathieu@2458
   111
      .AddAttribute ("TestBoolA", "help text",
mathieu@2965
   112
		     BooleanValue (false),
mathieu@2450
   113
		     MakeBooleanAccessor (&AttributeObjectTest::DoSetTestB,
mathieu@2450
   114
					   &AttributeObjectTest::DoGetTestB),
mathieu@2427
   115
		     MakeBooleanChecker ())
mathieu@2458
   116
      .AddAttribute ("TestInt16", "help text",
mathieu@2965
   117
		     IntegerValue (-2),
mathieu@2450
   118
		     MakeIntegerAccessor (&AttributeObjectTest::m_int16),
mathieu@2439
   119
		     MakeIntegerChecker<int16_t> ())
mathieu@2458
   120
      .AddAttribute ("TestInt16WithBounds", "help text",
mathieu@2965
   121
		     IntegerValue (-2),
mathieu@2450
   122
		     MakeIntegerAccessor (&AttributeObjectTest::m_int16WithBounds),
mathieu@2599
   123
		     MakeIntegerChecker<int16_t> (-5, 10))
mathieu@2458
   124
      .AddAttribute ("TestInt16SetGet", "help text",
mathieu@2965
   125
		     IntegerValue (6),
mathieu@2450
   126
		     MakeIntegerAccessor (&AttributeObjectTest::DoSetInt16,
mathieu@2450
   127
				       &AttributeObjectTest::DoGetInt16),
mathieu@2439
   128
		     MakeIntegerChecker<int16_t> ())
mathieu@2458
   129
      .AddAttribute ("TestUint8", "help text",
mathieu@2965
   130
		     UintegerValue (1),
mathieu@2450
   131
		     MakeUintegerAccessor (&AttributeObjectTest::m_uint8),
mathieu@2439
   132
		     MakeUintegerChecker<uint8_t> ())
mathieu@2458
   133
      .AddAttribute ("TestEnum", "help text",
mathieu@2965
   134
		     EnumValue (TEST_A),
mathieu@2450
   135
		     MakeEnumAccessor (&AttributeObjectTest::m_enum),
mathieu@2427
   136
		     MakeEnumChecker (TEST_A, "TestA",
mathieu@2427
   137
				      TEST_B, "TestB",
mathieu@2427
   138
				      TEST_C, "TestC"))
mathieu@2458
   139
      .AddAttribute ("TestRandom", "help text",
mathieu@2965
   140
		     RandomVariableValue (ConstantVariable (1.0)),
mathieu@2450
   141
		     MakeRandomVariableAccessor (&AttributeObjectTest::m_random),
mathieu@2427
   142
		     MakeRandomVariableChecker ())
mathieu@2458
   143
      .AddAttribute ("TestFloat", "help text",
mathieu@2965
   144
		     DoubleValue (-1.1),
mathieu@2450
   145
		     MakeDoubleAccessor (&AttributeObjectTest::m_float),
mathieu@2444
   146
		     MakeDoubleChecker<float> ())
mathieu@2458
   147
      .AddAttribute ("TestVector1", "help text",
mathieu@2965
   148
		     ObjectVectorValue (),
mathieu@2450
   149
		     MakeObjectVectorAccessor (&AttributeObjectTest::m_vector1),
mathieu@2933
   150
		     MakeObjectVectorChecker<Derived> ())
mathieu@2458
   151
      .AddAttribute ("TestVector2", "help text",
mathieu@2965
   152
		     ObjectVectorValue (),
mathieu@2450
   153
		     MakeObjectVectorAccessor (&AttributeObjectTest::DoGetVectorN,
mathieu@2450
   154
						&AttributeObjectTest::DoGetVector),
mathieu@2933
   155
		     MakeObjectVectorChecker<Derived> ())
mathieu@2462
   156
      .AddAttribute ("IntegerTraceSource1", "help text",
mathieu@2965
   157
		     IntegerValue (-2),
mathieu@2464
   158
		     MakeIntegerAccessor (&AttributeObjectTest::m_intSrc1),
mathieu@2462
   159
		     MakeIntegerChecker<int8_t> ())
mathieu@2462
   160
      .AddAttribute ("IntegerTraceSource2", "help text",
mathieu@2965
   161
		     IntegerValue (-2),
mathieu@2464
   162
		     MakeIntegerAccessor (&AttributeObjectTest::DoSetIntSrc,
mathieu@2464
   163
					  &AttributeObjectTest::DoGetIntSrc),
mathieu@2462
   164
		     MakeIntegerChecker<int8_t> ())
mathieu@2469
   165
      .AddAttribute ("ValueClassSource", "help text",
mathieu@2965
   166
		     ValueClassTestValue (ValueClassTest ()),
mathieu@2469
   167
		     MakeValueClassTestAccessor (&AttributeObjectTest::m_valueSrc),
mathieu@2469
   168
		     MakeValueClassTestChecker ())
mathieu@2463
   169
      .AddTraceSource ("Source1", "help test",
mathieu@2463
   170
		       MakeTraceSourceAccessor (&AttributeObjectTest::m_intSrc1))
mathieu@2466
   171
      .AddTraceSource ("Source2", "help text",
mathieu@2466
   172
		       MakeTraceSourceAccessor (&AttributeObjectTest::m_cb))
mathieu@2469
   173
      .AddTraceSource ("ValueSource", "help text",
mathieu@2469
   174
		       MakeTraceSourceAccessor (&AttributeObjectTest::m_valueSrc))
mathieu@3190
   175
      .AddAttribute ("Pointer", "help text",
mathieu@2965
   176
                     PointerValue (),
mathieu@2926
   177
                     MakePointerAccessor (&AttributeObjectTest::m_ptr),
mathieu@2926
   178
                     MakePointerChecker<Derived> ())
mathieu@3763
   179
      .AddAttribute ("Callback", "help text",
mathieu@3763
   180
                     CallbackValue (),
mathieu@3763
   181
                     MakeCallbackAccessor (&AttributeObjectTest::m_cbValue),
mathieu@3763
   182
                     MakeCallbackChecker ())
mathieu@2374
   183
      ;
mathieu@2374
   184
        
mathieu@2374
   185
    return tid;
mathieu@2374
   186
  }
mathieu@2374
   187
mathieu@2405
   188
  void AddToVector1 (void) {
mathieu@2405
   189
    m_vector1.push_back (CreateObject<Derived> ());
mathieu@2405
   190
  }
mathieu@2405
   191
  void AddToVector2 (void) {
mathieu@2405
   192
    m_vector2.push_back (CreateObject<Derived> ());
mathieu@2405
   193
  }
mathieu@2405
   194
mathieu@2466
   195
  void InvokeCb (double a, int b, float c) {
mathieu@2466
   196
    m_cb (a,b,c);
mathieu@2466
   197
  }
mathieu@2466
   198
mathieu@3763
   199
  void InvokeCbValue (int8_t a) {
mathieu@3763
   200
    if (!m_cbValue.IsNull ())
mathieu@3763
   201
      {
mathieu@3763
   202
        m_cbValue (a);
mathieu@3763
   203
      }
mathieu@3763
   204
  }
mathieu@3763
   205
mathieu@2374
   206
private:
mathieu@2374
   207
  void DoSetTestB (bool v) {
mathieu@2374
   208
    m_boolTestA = v;
mathieu@2374
   209
  }
mathieu@2374
   210
  bool DoGetTestB (void) const {
mathieu@2374
   211
    return m_boolTestA;
mathieu@2374
   212
  }
mathieu@2374
   213
  int16_t DoGetInt16 (void) const {
mathieu@2374
   214
    return m_int16SetGet;
mathieu@2374
   215
  }
mathieu@2374
   216
  void DoSetInt16 (int16_t v) {
mathieu@2374
   217
    m_int16SetGet = v;
mathieu@2374
   218
  }
mathieu@2405
   219
  uint32_t DoGetVectorN (void) const {
mathieu@2405
   220
    return m_vector2.size ();
mathieu@2405
   221
  }
mathieu@2405
   222
  Ptr<Derived> DoGetVector (uint32_t i) const {
mathieu@2405
   223
    return m_vector2[i];
mathieu@2405
   224
  }
mathieu@3527
   225
  bool DoSetIntSrc (int8_t v) {
mathieu@2462
   226
    m_intSrc2 = v;
mathieu@3527
   227
    return true;
mathieu@2462
   228
  }
mathieu@2462
   229
  int8_t DoGetIntSrc (void) const {
mathieu@2462
   230
    return m_intSrc2;
mathieu@2462
   231
  }
mathieu@2374
   232
  bool m_boolTestA;
mathieu@2374
   233
  bool m_boolTest;
mathieu@2374
   234
  int16_t m_int16;
mathieu@2374
   235
  int16_t m_int16WithBounds;
mathieu@2374
   236
  int16_t m_int16SetGet;
mathieu@2374
   237
  uint8_t m_uint8;
mathieu@2374
   238
  float m_float;
mathieu@2965
   239
  enum Test_e m_enum;
mathieu@2384
   240
  RandomVariable m_random;
mathieu@2405
   241
  std::vector<Ptr<Derived> > m_vector1;
mathieu@2405
   242
  std::vector<Ptr<Derived> > m_vector2;
mathieu@3763
   243
  Callback<void,int8_t> m_cbValue;
mathieu@2482
   244
  TracedValue<int8_t> m_intSrc1;
mathieu@2482
   245
  TracedValue<int8_t> m_intSrc2;
mathieu@2482
   246
  TracedCallback<double, int, float> m_cb;
mathieu@2482
   247
  TracedValue<ValueClassTest> m_valueSrc;
mathieu@2926
   248
  Ptr<Derived> m_ptr;
mathieu@2374
   249
};
mathieu@2374
   250
mathieu@2374
   251
mathieu@2965
   252
#define CHECK_GET_STR(p,name,value)                               \
mathieu@2965
   253
  {                                                               \
mathieu@2965
   254
    std::string expected = value;                                 \
mathieu@2965
   255
    StringValue got;                                              \
mathieu@2965
   256
    bool ok = p->GetAttributeFailSafe (name, got);                \
mathieu@2965
   257
    NS_TEST_ASSERT (ok);                                          \
mathieu@2965
   258
    NS_TEST_ASSERT_EQUAL (got.Get (), expected);                  \
mathieu@2374
   259
  }
mathieu@2374
   260
#define CHECK_GET_PARAM(p,name,type,value)		\
mathieu@2374
   261
  {							\
mathieu@2374
   262
    const type expected = value;			\
mathieu@2965
   263
    type got;                                           \
mathieu@2965
   264
    bool ok = p->GetAttributeFailSafe (name, got);      \
mathieu@2716
   265
    NS_TEST_ASSERT (ok);                                \
mathieu@2374
   266
    NS_TEST_ASSERT_EQUAL (got.Get (), expected.Get ());	\
mathieu@2374
   267
  }
mathieu@2374
   268
mathieu@2450
   269
NS_OBJECT_ENSURE_REGISTERED (AttributeObjectTest);
mathieu@2374
   270
mathieu@2450
   271
AttributeTest::AttributeTest ()
mathieu@2450
   272
  : Test ("Attribute")
mathieu@2374
   273
{}
mathieu@2374
   274
bool 
mathieu@2450
   275
AttributeTest::RunTests (void)
mathieu@2374
   276
{
mathieu@2374
   277
  bool result = true;
mathieu@2374
   278
mathieu@2459
   279
  AttributeList params;
mathieu@2450
   280
  Ptr<AttributeObjectTest> p;
mathieu@2965
   281
  NS_TEST_ASSERT (params.SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
mathieu@4554
   282
  p = CreateObjectWithAttributes<AttributeObjectTest> (params);
mathieu@2374
   283
  CHECK_GET_STR (p, "TestBoolName", "false");
mathieu@2965
   284
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
mathieu@2374
   285
mathieu@2965
   286
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", StringValue ("true")));
mathieu@2374
   287
  CHECK_GET_STR (p, "TestBoolName", "true");
mathieu@2965
   288
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
mathieu@2374
   289
mathieu@2965
   290
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", BooleanValue (false)));
mathieu@2374
   291
  CHECK_GET_STR (p, "TestBoolName", "false");
mathieu@2965
   292
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
mathieu@2374
   293
mathieu@4554
   294
  p = CreateObjectWithAttributes<AttributeObjectTest> ("TestBoolName", StringValue ("true"));
mathieu@2374
   295
  CHECK_GET_STR (p, "TestBoolName", "true");
mathieu@2965
   296
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
mathieu@2374
   297
mathieu@4554
   298
  p = CreateObjectWithAttributes<AttributeObjectTest> ("TestBoolName", BooleanValue (true));
mathieu@2374
   299
  CHECK_GET_STR (p, "TestBoolName", "true");
mathieu@2965
   300
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
mathieu@2374
   301
mathieu@2965
   302
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("false")));
mathieu@2374
   303
  CHECK_GET_STR (p, "TestBoolA", "false");
mathieu@2965
   304
  CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, false);
mathieu@2374
   305
mathieu@2965
   306
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("true")));
mathieu@2374
   307
  CHECK_GET_STR (p, "TestBoolA", "true");
mathieu@2965
   308
  CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, true);
mathieu@2374
   309
mathieu@2374
   310
mathieu@2374
   311
  CHECK_GET_STR (p, "TestInt16", "-2");
mathieu@2965
   312
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -2);
mathieu@2374
   313
mathieu@2965
   314
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", StringValue ("-5")));
mathieu@2374
   315
  CHECK_GET_STR (p, "TestInt16", "-5");
mathieu@2965
   316
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -5);
mathieu@2374
   317
mathieu@2965
   318
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (+2)));
mathieu@2374
   319
  CHECK_GET_STR (p, "TestInt16", "2");
mathieu@2965
   320
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, +2);
mathieu@2374
   321
mathieu@2965
   322
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (-32768)));
mathieu@2374
   323
  CHECK_GET_STR (p, "TestInt16", "-32768");
mathieu@2965
   324
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
mathieu@2374
   325
mathieu@2965
   326
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (-32769)));
mathieu@2374
   327
  CHECK_GET_STR (p, "TestInt16", "-32768");
mathieu@2965
   328
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
mathieu@2374
   329
mathieu@2965
   330
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (32767)));
mathieu@2374
   331
  CHECK_GET_STR (p, "TestInt16", "32767");
mathieu@2965
   332
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
mathieu@2374
   333
mathieu@2965
   334
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (32768)));
mathieu@2374
   335
  CHECK_GET_STR (p, "TestInt16", "32767");
mathieu@2965
   336
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
mathieu@2374
   337
mathieu@2965
   338
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (10)));
mathieu@2374
   339
  CHECK_GET_STR (p, "TestInt16WithBounds", "10");
mathieu@2965
   340
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
mathieu@2965
   341
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (11)));
mathieu@2374
   342
  CHECK_GET_STR (p, "TestInt16WithBounds", "10");
mathieu@2965
   343
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
mathieu@2374
   344
mathieu@2965
   345
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-5)));
mathieu@2374
   346
  CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
mathieu@2965
   347
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
mathieu@2965
   348
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-6)));
mathieu@2374
   349
  CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
mathieu@2965
   350
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
mathieu@2374
   351
mathieu@2374
   352
  CHECK_GET_STR (p, "TestInt16SetGet", "6");
mathieu@2965
   353
  CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 6);
mathieu@2965
   354
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16SetGet", IntegerValue (0)));
mathieu@2374
   355
  CHECK_GET_STR (p, "TestInt16SetGet", "0");
mathieu@2965
   356
  CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 0);
mathieu@2375
   357
mathieu@2374
   358
  CHECK_GET_STR (p, "TestUint8", "1");
mathieu@2965
   359
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 1);
mathieu@2965
   360
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (0)));
mathieu@2374
   361
  CHECK_GET_STR (p, "TestUint8", "0");
mathieu@2965
   362
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 0);
mathieu@2965
   363
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (255)));
mathieu@2374
   364
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   365
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   366
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", StringValue ("255")));
mathieu@2374
   367
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   368
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   369
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("256")));
mathieu@2374
   370
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   371
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   372
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("-1")));
mathieu@2374
   373
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   374
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   375
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", UintegerValue ((uint64_t)-1)));
mathieu@2374
   376
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   377
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2385
   378
mathieu@2374
   379
  CHECK_GET_STR (p, "TestFloat", "-1.1");
mathieu@2965
   380
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestFloat", DoubleValue ((float)+2.3)));
mathieu@2965
   381
  CHECK_GET_PARAM (p, "TestFloat", DoubleValue, (float)+2.3);
mathieu@2385
   382
mathieu@2374
   383
  CHECK_GET_STR (p, "TestEnum", "TestA");
mathieu@2965
   384
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_A);
mathieu@2965
   385
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", EnumValue (AttributeObjectTest::TEST_C)));
mathieu@2374
   386
  CHECK_GET_STR (p, "TestEnum", "TestC");
mathieu@2965
   387
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_C);
mathieu@2965
   388
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", StringValue ("TestB")));
mathieu@2374
   389
  CHECK_GET_STR (p, "TestEnum", "TestB");
mathieu@2965
   390
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
mathieu@2965
   391
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", StringValue ("TestD")));
mathieu@2374
   392
  CHECK_GET_STR (p, "TestEnum", "TestB");
mathieu@2965
   393
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
mathieu@2965
   394
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", EnumValue (5)));
mathieu@2374
   395
  CHECK_GET_STR (p, "TestEnum", "TestB");
mathieu@2965
   396
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
mathieu@2374
   397
mathieu@2965
   398
  RandomVariableValue ran;
mathieu@2965
   399
  p->GetAttribute ("TestRandom", ran);
mathieu@2965
   400
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (UniformVariable (0.0, 1.0))));
mathieu@2965
   401
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (ConstantVariable (10.0))));
mathieu@2405
   402
mathieu@2405
   403
  {
mathieu@2965
   404
    ObjectVectorValue vector;
mathieu@2965
   405
    p->GetAttribute ("TestVector1", vector);
mathieu@2405
   406
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2405
   407
    p->AddToVector1 ();
mathieu@2405
   408
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2965
   409
    p->GetAttribute ("TestVector1", vector);
mathieu@2405
   410
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2405
   411
    Ptr<Object> a = vector.Get (0);
mathieu@2405
   412
    NS_TEST_ASSERT_UNEQUAL (a, 0);
mathieu@2405
   413
    p->AddToVector1 ();
mathieu@2405
   414
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2965
   415
    p->GetAttribute ("TestVector1", vector);
mathieu@2405
   416
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
mathieu@2405
   417
  }
mathieu@2405
   418
mathieu@2405
   419
  {
mathieu@2965
   420
    ObjectVectorValue vector;
mathieu@2965
   421
    p->GetAttribute ("TestVector2", vector);
mathieu@2405
   422
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2405
   423
    p->AddToVector2 ();
mathieu@2405
   424
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2965
   425
    p->GetAttribute ("TestVector2", vector);
mathieu@2405
   426
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2405
   427
    Ptr<Object> a = vector.Get (0);
mathieu@2405
   428
    NS_TEST_ASSERT_UNEQUAL (a, 0);
mathieu@2405
   429
    p->AddToVector2 ();
mathieu@2405
   430
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2965
   431
    p->GetAttribute ("TestVector2", vector);
mathieu@2405
   432
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
mathieu@2405
   433
  }
mathieu@2384
   434
mathieu@2965
   435
  NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("true")));
mathieu@2592
   436
  p = CreateObject<AttributeObjectTest> ();
mathieu@2965
   437
  BooleanValue boolV;
mathieu@2965
   438
  p->GetAttribute ("TestBoolName", boolV);
mathieu@2965
   439
  NS_TEST_ASSERT_EQUAL (boolV.Get (), true);
mathieu@2374
   440
mathieu@2965
   441
  NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
mathieu@2592
   442
  p = CreateObject<AttributeObjectTest> ();
mathieu@2965
   443
  p->GetAttribute ("TestBoolName", boolV);
mathieu@2965
   444
  NS_TEST_ASSERT_EQUAL (boolV.Get (), false);
mathieu@2374
   445
mathieu@2965
   446
  IntegerValue i;
mathieu@2965
   447
  p->GetAttribute ("IntegerTraceSource1", i);
mathieu@2462
   448
  NS_TEST_ASSERT_EQUAL (i.Get (), -2);
mathieu@2965
   449
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (+5)));
mathieu@2965
   450
  p->GetAttribute ("IntegerTraceSource1", i);
mathieu@2462
   451
  NS_TEST_ASSERT_EQUAL (i.Get (), +5);
mathieu@2965
   452
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (127)));
mathieu@2965
   453
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (128)));
mathieu@2965
   454
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-128)));
mathieu@2965
   455
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-129)));
mathieu@2462
   456
mathieu@2965
   457
  p->GetAttribute ("IntegerTraceSource2", i);
mathieu@2462
   458
  NS_TEST_ASSERT_EQUAL (i.Get (), -2);
mathieu@2965
   459
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (+5)));
mathieu@2965
   460
  p->GetAttribute ("IntegerTraceSource2", i);
mathieu@2462
   461
  NS_TEST_ASSERT_EQUAL (i.Get (), +5);
mathieu@2965
   462
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (127)));
mathieu@2965
   463
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (128)));
mathieu@2965
   464
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-128)));
mathieu@2965
   465
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-129)));
mathieu@2462
   466
mathieu@2466
   467
  m_got1 = -2;
mathieu@2965
   468
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-1)));
mathieu@2594
   469
  NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
mathieu@2965
   470
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (0)));
mathieu@2466
   471
  NS_TEST_ASSERT_EQUAL (m_got1, 0);
mathieu@2594
   472
  NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
mathieu@2965
   473
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (1)));
mathieu@2466
   474
  NS_TEST_ASSERT_EQUAL (m_got1, 0);
mathieu@2466
   475
mathieu@2466
   476
  m_got2 = 4.3;
mathieu@2466
   477
  p->InvokeCb (1.0, -5, 0.0);
mathieu@2466
   478
  NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
mathieu@2594
   479
  NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
mathieu@2466
   480
  NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
mathieu@2466
   481
  p->InvokeCb (1.0, -5, 0.0);
mathieu@2466
   482
  NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
mathieu@2594
   483
  NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
mathieu@2466
   484
  p->InvokeCb (-1.0, -5, 0.0);
mathieu@2466
   485
  NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
mathieu@2469
   486
mathieu@2594
   487
  NS_TEST_ASSERT (p->TraceConnectWithoutContext ("ValueSource", MakeCallback (&AttributeTest::NotifySourceValue, this)));
mathieu@2926
   488
mathieu@2965
   489
  PointerValue ptr;
mathieu@2965
   490
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   491
  Ptr<Derived> derived = ptr.Get<Derived> ();
mathieu@2926
   492
  NS_TEST_ASSERT (derived == 0);
mathieu@2926
   493
  derived = Create<Derived> ();
mathieu@2965
   494
  NS_TEST_ASSERT (p->SetAttributeFailSafe("Pointer", PointerValue (derived)));
mathieu@2965
   495
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   496
  Ptr<Derived> stored = ptr.Get<Derived> ();
mathieu@2926
   497
  NS_TEST_ASSERT (stored == derived);
mathieu@2965
   498
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   499
  Ptr<Object> storedBase = ptr.Get<Object> ();
mathieu@2926
   500
  NS_TEST_ASSERT (stored == storedBase);
mathieu@2965
   501
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   502
  Ptr<AttributeObjectTest> x = ptr.Get<AttributeObjectTest> ();
mathieu@2926
   503
  NS_TEST_ASSERT (x == 0);
mathieu@2926
   504
mathieu@4554
   505
  p = CreateObjectWithAttributes<AttributeObjectTest> ("Pointer", PointerValue (Create<Derived> ()));
mathieu@2926
   506
  NS_TEST_ASSERT (p != 0);
mathieu@2926
   507
  derived = 0;
mathieu@2965
   508
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   509
  derived = ptr.Get<Derived> ();
mathieu@2926
   510
  NS_TEST_ASSERT (derived != 0);
mathieu@2463
   511
mathieu@3763
   512
  p = CreateObject<AttributeObjectTest> ();
mathieu@3763
   513
  NS_TEST_ASSERT (p != 0);
mathieu@3763
   514
  m_gotCbValue = 1;
mathieu@3763
   515
  p->InvokeCbValue (2);
mathieu@3763
   516
  CallbackValue cbValue = MakeCallback (&AttributeTest::NotifyCallbackValue, this);
mathieu@3763
   517
  NS_TEST_ASSERT_EQUAL (m_gotCbValue, 1);
mathieu@3763
   518
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("Callback", 
mathieu@3763
   519
                                           cbValue));
mathieu@3763
   520
  p->InvokeCbValue (2);
mathieu@3763
   521
  NS_TEST_ASSERT_EQUAL (m_gotCbValue, 2);
mathieu@3763
   522
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("Callback", 
mathieu@3763
   523
                                           CallbackValue (MakeNullCallback<void,int8_t> ())));
mathieu@3763
   524
  p->InvokeCbValue (3);
mathieu@3763
   525
  NS_TEST_ASSERT_EQUAL (m_gotCbValue, 2);
mathieu@3763
   526
  
mathieu@2374
   527
  return result;
mathieu@2374
   528
}
mathieu@2374
   529
mathieu@2374
   530
mathieu@2374
   531
mathieu@2450
   532
static AttributeTest g_parameterTest;
mathieu@2374
   533
mathieu@2374
   534
} // namespace ns3
mathieu@2374
   535
mathieu@2374
   536
mathieu@2374
   537
#endif /* RUN_SELF_TESTS */