src/core/attribute-test.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 02 Jun 2008 10:30:24 -0700
changeset 3190 51fe9001a679
parent 3094 6e787ec9c8aa
child 3527 1548d7e3604f
permissions -rw-r--r--
add some doxygen, remove a couple of XXX
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@2463
    32
#include "trace-source-accessor.h"
mathieu@2926
    33
#include "pointer.h"
mathieu@2405
    34
mathieu@2374
    35
namespace ns3 {
mathieu@2374
    36
mathieu@2469
    37
class ValueClassTest 
mathieu@2469
    38
{
mathieu@2469
    39
public:
mathieu@2469
    40
  ValueClassTest () {}
mathieu@2469
    41
private:
mathieu@2469
    42
  int m_v;
mathieu@2469
    43
};
mathieu@2469
    44
bool operator != (const ValueClassTest &a, const ValueClassTest &b)
mathieu@2469
    45
{
mathieu@2469
    46
  return true;
mathieu@2469
    47
}
mathieu@2469
    48
std::ostream & operator << (std::ostream &os, ValueClassTest v)
mathieu@2469
    49
{
mathieu@2469
    50
  return os;
mathieu@2469
    51
}
mathieu@2469
    52
std::istream & operator >> (std::istream &is, ValueClassTest &v)
mathieu@2469
    53
{
mathieu@2469
    54
  return is;
mathieu@2469
    55
}
mathieu@3094
    56
ATTRIBUTE_HELPER_HEADER (ValueClassTest);
mathieu@2582
    57
ATTRIBUTE_HELPER_CPP (ValueClassTest);
mathieu@2469
    58
mathieu@2450
    59
class AttributeTest : public Test
mathieu@2374
    60
{
mathieu@2374
    61
public:
mathieu@2450
    62
  AttributeTest ();
mathieu@2374
    63
  virtual bool RunTests (void);
mathieu@2374
    64
private:
mathieu@2468
    65
  void NotifySource1 (int8_t old, int8_t n) {
mathieu@2466
    66
    m_got1 = n;
mathieu@2463
    67
  }
mathieu@2466
    68
  void NotifySource2 (double a, int b, float c) {
mathieu@2466
    69
    m_got2 = a;
mathieu@2466
    70
  }
mathieu@2469
    71
  void NotifySourceValue (ValueClassTest old, ValueClassTest n) {
mathieu@2469
    72
    m_gotValue = n;
mathieu@2469
    73
  }
mathieu@2466
    74
  int64_t m_got1;
mathieu@2466
    75
  double m_got2;
mathieu@2469
    76
  ValueClassTest m_gotValue;
mathieu@2374
    77
};
mathieu@2374
    78
mathieu@2374
    79
class Derived : public Object
mathieu@2374
    80
{
mathieu@2374
    81
public:
mathieu@2374
    82
  static TypeId GetTypeId (void) {
mathieu@2374
    83
    static TypeId tid = TypeId ("Derived")
mathieu@2374
    84
      .SetParent<Object> ()
mathieu@2374
    85
      ;
mathieu@2374
    86
    return tid;
mathieu@2374
    87
  }
mathieu@2374
    88
};
mathieu@2374
    89
mathieu@2450
    90
class AttributeObjectTest : public Object
mathieu@2374
    91
{
mathieu@2374
    92
public:
mathieu@2965
    93
  enum Test_e {
mathieu@2374
    94
    TEST_A,
mathieu@2374
    95
    TEST_B,
mathieu@2374
    96
    TEST_C
mathieu@2374
    97
  };
mathieu@2374
    98
  static TypeId GetTypeId (void) {
mathieu@2613
    99
    static TypeId tid = TypeId ("ns3::AttributeObjectTest")
mathieu@2374
   100
      .SetParent<Object> ()
mathieu@2602
   101
      .HideFromDocumentation ()
mathieu@2458
   102
      .AddAttribute ("TestBoolName", "help text",
mathieu@2965
   103
		     BooleanValue (false),
mathieu@2450
   104
		     MakeBooleanAccessor (&AttributeObjectTest::m_boolTest),
mathieu@2427
   105
		     MakeBooleanChecker ())
mathieu@2458
   106
      .AddAttribute ("TestBoolA", "help text",
mathieu@2965
   107
		     BooleanValue (false),
mathieu@2450
   108
		     MakeBooleanAccessor (&AttributeObjectTest::DoSetTestB,
mathieu@2450
   109
					   &AttributeObjectTest::DoGetTestB),
mathieu@2427
   110
		     MakeBooleanChecker ())
mathieu@2458
   111
      .AddAttribute ("TestInt16", "help text",
mathieu@2965
   112
		     IntegerValue (-2),
mathieu@2450
   113
		     MakeIntegerAccessor (&AttributeObjectTest::m_int16),
mathieu@2439
   114
		     MakeIntegerChecker<int16_t> ())
mathieu@2458
   115
      .AddAttribute ("TestInt16WithBounds", "help text",
mathieu@2965
   116
		     IntegerValue (-2),
mathieu@2450
   117
		     MakeIntegerAccessor (&AttributeObjectTest::m_int16WithBounds),
mathieu@2599
   118
		     MakeIntegerChecker<int16_t> (-5, 10))
mathieu@2458
   119
      .AddAttribute ("TestInt16SetGet", "help text",
mathieu@2965
   120
		     IntegerValue (6),
mathieu@2450
   121
		     MakeIntegerAccessor (&AttributeObjectTest::DoSetInt16,
mathieu@2450
   122
				       &AttributeObjectTest::DoGetInt16),
mathieu@2439
   123
		     MakeIntegerChecker<int16_t> ())
mathieu@2458
   124
      .AddAttribute ("TestUint8", "help text",
mathieu@2965
   125
		     UintegerValue (1),
mathieu@2450
   126
		     MakeUintegerAccessor (&AttributeObjectTest::m_uint8),
mathieu@2439
   127
		     MakeUintegerChecker<uint8_t> ())
mathieu@2458
   128
      .AddAttribute ("TestEnum", "help text",
mathieu@2965
   129
		     EnumValue (TEST_A),
mathieu@2450
   130
		     MakeEnumAccessor (&AttributeObjectTest::m_enum),
mathieu@2427
   131
		     MakeEnumChecker (TEST_A, "TestA",
mathieu@2427
   132
				      TEST_B, "TestB",
mathieu@2427
   133
				      TEST_C, "TestC"))
mathieu@2458
   134
      .AddAttribute ("TestRandom", "help text",
mathieu@2965
   135
		     RandomVariableValue (ConstantVariable (1.0)),
mathieu@2450
   136
		     MakeRandomVariableAccessor (&AttributeObjectTest::m_random),
mathieu@2427
   137
		     MakeRandomVariableChecker ())
mathieu@2458
   138
      .AddAttribute ("TestFloat", "help text",
mathieu@2965
   139
		     DoubleValue (-1.1),
mathieu@2450
   140
		     MakeDoubleAccessor (&AttributeObjectTest::m_float),
mathieu@2444
   141
		     MakeDoubleChecker<float> ())
mathieu@2458
   142
      .AddAttribute ("TestVector1", "help text",
mathieu@2965
   143
		     ObjectVectorValue (),
mathieu@2450
   144
		     MakeObjectVectorAccessor (&AttributeObjectTest::m_vector1),
mathieu@2933
   145
		     MakeObjectVectorChecker<Derived> ())
mathieu@2458
   146
      .AddAttribute ("TestVector2", "help text",
mathieu@2965
   147
		     ObjectVectorValue (),
mathieu@2450
   148
		     MakeObjectVectorAccessor (&AttributeObjectTest::DoGetVectorN,
mathieu@2450
   149
						&AttributeObjectTest::DoGetVector),
mathieu@2933
   150
		     MakeObjectVectorChecker<Derived> ())
mathieu@2462
   151
      .AddAttribute ("IntegerTraceSource1", "help text",
mathieu@2965
   152
		     IntegerValue (-2),
mathieu@2464
   153
		     MakeIntegerAccessor (&AttributeObjectTest::m_intSrc1),
mathieu@2462
   154
		     MakeIntegerChecker<int8_t> ())
mathieu@2462
   155
      .AddAttribute ("IntegerTraceSource2", "help text",
mathieu@2965
   156
		     IntegerValue (-2),
mathieu@2464
   157
		     MakeIntegerAccessor (&AttributeObjectTest::DoSetIntSrc,
mathieu@2464
   158
					  &AttributeObjectTest::DoGetIntSrc),
mathieu@2462
   159
		     MakeIntegerChecker<int8_t> ())
mathieu@2469
   160
      .AddAttribute ("ValueClassSource", "help text",
mathieu@2965
   161
		     ValueClassTestValue (ValueClassTest ()),
mathieu@2469
   162
		     MakeValueClassTestAccessor (&AttributeObjectTest::m_valueSrc),
mathieu@2469
   163
		     MakeValueClassTestChecker ())
mathieu@2463
   164
      .AddTraceSource ("Source1", "help test",
mathieu@2463
   165
		       MakeTraceSourceAccessor (&AttributeObjectTest::m_intSrc1))
mathieu@2466
   166
      .AddTraceSource ("Source2", "help text",
mathieu@2466
   167
		       MakeTraceSourceAccessor (&AttributeObjectTest::m_cb))
mathieu@2469
   168
      .AddTraceSource ("ValueSource", "help text",
mathieu@2469
   169
		       MakeTraceSourceAccessor (&AttributeObjectTest::m_valueSrc))
mathieu@3190
   170
      .AddAttribute ("Pointer", "help text",
mathieu@2965
   171
                     PointerValue (),
mathieu@2926
   172
                     MakePointerAccessor (&AttributeObjectTest::m_ptr),
mathieu@2926
   173
                     MakePointerChecker<Derived> ())
mathieu@2374
   174
      ;
mathieu@2374
   175
        
mathieu@2374
   176
    return tid;
mathieu@2374
   177
  }
mathieu@2374
   178
mathieu@2405
   179
  void AddToVector1 (void) {
mathieu@2405
   180
    m_vector1.push_back (CreateObject<Derived> ());
mathieu@2405
   181
  }
mathieu@2405
   182
  void AddToVector2 (void) {
mathieu@2405
   183
    m_vector2.push_back (CreateObject<Derived> ());
mathieu@2405
   184
  }
mathieu@2405
   185
mathieu@2466
   186
  void InvokeCb (double a, int b, float c) {
mathieu@2466
   187
    m_cb (a,b,c);
mathieu@2466
   188
  }
mathieu@2466
   189
mathieu@2374
   190
private:
mathieu@2374
   191
  void DoSetTestB (bool v) {
mathieu@2374
   192
    m_boolTestA = v;
mathieu@2374
   193
  }
mathieu@2374
   194
  bool DoGetTestB (void) const {
mathieu@2374
   195
    return m_boolTestA;
mathieu@2374
   196
  }
mathieu@2374
   197
  int16_t DoGetInt16 (void) const {
mathieu@2374
   198
    return m_int16SetGet;
mathieu@2374
   199
  }
mathieu@2374
   200
  void DoSetInt16 (int16_t v) {
mathieu@2374
   201
    m_int16SetGet = v;
mathieu@2374
   202
  }
mathieu@2405
   203
  uint32_t DoGetVectorN (void) const {
mathieu@2405
   204
    return m_vector2.size ();
mathieu@2405
   205
  }
mathieu@2405
   206
  Ptr<Derived> DoGetVector (uint32_t i) const {
mathieu@2405
   207
    return m_vector2[i];
mathieu@2405
   208
  }
mathieu@2462
   209
  void DoSetIntSrc (int8_t v) {
mathieu@2462
   210
    m_intSrc2 = v;
mathieu@2462
   211
  }
mathieu@2462
   212
  int8_t DoGetIntSrc (void) const {
mathieu@2462
   213
    return m_intSrc2;
mathieu@2462
   214
  }
mathieu@2374
   215
  bool m_boolTestA;
mathieu@2374
   216
  bool m_boolTest;
mathieu@2374
   217
  int16_t m_int16;
mathieu@2374
   218
  int16_t m_int16WithBounds;
mathieu@2374
   219
  int16_t m_int16SetGet;
mathieu@2374
   220
  uint8_t m_uint8;
mathieu@2374
   221
  float m_float;
mathieu@2965
   222
  enum Test_e m_enum;
mathieu@2384
   223
  RandomVariable m_random;
mathieu@2405
   224
  std::vector<Ptr<Derived> > m_vector1;
mathieu@2405
   225
  std::vector<Ptr<Derived> > m_vector2;
mathieu@2482
   226
  TracedValue<int8_t> m_intSrc1;
mathieu@2482
   227
  TracedValue<int8_t> m_intSrc2;
mathieu@2482
   228
  TracedCallback<double, int, float> m_cb;
mathieu@2482
   229
  TracedValue<ValueClassTest> m_valueSrc;
mathieu@2926
   230
  Ptr<Derived> m_ptr;
mathieu@2374
   231
};
mathieu@2374
   232
mathieu@2374
   233
mathieu@2965
   234
#define CHECK_GET_STR(p,name,value)                               \
mathieu@2965
   235
  {                                                               \
mathieu@2965
   236
    std::string expected = value;                                 \
mathieu@2965
   237
    StringValue got;                                              \
mathieu@2965
   238
    bool ok = p->GetAttributeFailSafe (name, got);                \
mathieu@2965
   239
    NS_TEST_ASSERT (ok);                                          \
mathieu@2965
   240
    NS_TEST_ASSERT_EQUAL (got.Get (), expected);                  \
mathieu@2374
   241
  }
mathieu@2374
   242
#define CHECK_GET_PARAM(p,name,type,value)		\
mathieu@2374
   243
  {							\
mathieu@2374
   244
    const type expected = value;			\
mathieu@2965
   245
    type got;                                           \
mathieu@2965
   246
    bool ok = p->GetAttributeFailSafe (name, got);      \
mathieu@2716
   247
    NS_TEST_ASSERT (ok);                                \
mathieu@2374
   248
    NS_TEST_ASSERT_EQUAL (got.Get (), expected.Get ());	\
mathieu@2374
   249
  }
mathieu@2374
   250
mathieu@2450
   251
NS_OBJECT_ENSURE_REGISTERED (AttributeObjectTest);
mathieu@2374
   252
mathieu@2450
   253
AttributeTest::AttributeTest ()
mathieu@2450
   254
  : Test ("Attribute")
mathieu@2374
   255
{}
mathieu@2374
   256
bool 
mathieu@2450
   257
AttributeTest::RunTests (void)
mathieu@2374
   258
{
mathieu@2374
   259
  bool result = true;
mathieu@2374
   260
mathieu@2459
   261
  AttributeList params;
mathieu@2450
   262
  Ptr<AttributeObjectTest> p;
mathieu@2965
   263
  NS_TEST_ASSERT (params.SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
mathieu@2450
   264
  p = CreateObject<AttributeObjectTest> (params);
mathieu@2374
   265
  CHECK_GET_STR (p, "TestBoolName", "false");
mathieu@2965
   266
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
mathieu@2374
   267
mathieu@2965
   268
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", StringValue ("true")));
mathieu@2374
   269
  CHECK_GET_STR (p, "TestBoolName", "true");
mathieu@2965
   270
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
mathieu@2374
   271
mathieu@2965
   272
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolName", BooleanValue (false)));
mathieu@2374
   273
  CHECK_GET_STR (p, "TestBoolName", "false");
mathieu@2965
   274
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
mathieu@2374
   275
mathieu@2965
   276
  p = CreateObject<AttributeObjectTest> ("TestBoolName", StringValue ("true"));
mathieu@2374
   277
  CHECK_GET_STR (p, "TestBoolName", "true");
mathieu@2965
   278
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
mathieu@2374
   279
mathieu@2965
   280
  p = CreateObject<AttributeObjectTest> ("TestBoolName", BooleanValue (true));
mathieu@2374
   281
  CHECK_GET_STR (p, "TestBoolName", "true");
mathieu@2965
   282
  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
mathieu@2374
   283
mathieu@2965
   284
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("false")));
mathieu@2374
   285
  CHECK_GET_STR (p, "TestBoolA", "false");
mathieu@2965
   286
  CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, false);
mathieu@2374
   287
mathieu@2965
   288
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestBoolA", StringValue ("true")));
mathieu@2374
   289
  CHECK_GET_STR (p, "TestBoolA", "true");
mathieu@2965
   290
  CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, true);
mathieu@2374
   291
mathieu@2374
   292
mathieu@2374
   293
  CHECK_GET_STR (p, "TestInt16", "-2");
mathieu@2965
   294
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -2);
mathieu@2374
   295
mathieu@2965
   296
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", StringValue ("-5")));
mathieu@2374
   297
  CHECK_GET_STR (p, "TestInt16", "-5");
mathieu@2965
   298
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -5);
mathieu@2374
   299
mathieu@2965
   300
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (+2)));
mathieu@2374
   301
  CHECK_GET_STR (p, "TestInt16", "2");
mathieu@2965
   302
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, +2);
mathieu@2374
   303
mathieu@2965
   304
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (-32768)));
mathieu@2374
   305
  CHECK_GET_STR (p, "TestInt16", "-32768");
mathieu@2965
   306
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
mathieu@2374
   307
mathieu@2965
   308
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (-32769)));
mathieu@2374
   309
  CHECK_GET_STR (p, "TestInt16", "-32768");
mathieu@2965
   310
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, -32768);
mathieu@2374
   311
mathieu@2965
   312
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16", IntegerValue (32767)));
mathieu@2374
   313
  CHECK_GET_STR (p, "TestInt16", "32767");
mathieu@2965
   314
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
mathieu@2374
   315
mathieu@2965
   316
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16", IntegerValue (32768)));
mathieu@2374
   317
  CHECK_GET_STR (p, "TestInt16", "32767");
mathieu@2965
   318
  CHECK_GET_PARAM (p, "TestInt16", IntegerValue, 32767);
mathieu@2374
   319
mathieu@2965
   320
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (10)));
mathieu@2374
   321
  CHECK_GET_STR (p, "TestInt16WithBounds", "10");
mathieu@2965
   322
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
mathieu@2965
   323
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (11)));
mathieu@2374
   324
  CHECK_GET_STR (p, "TestInt16WithBounds", "10");
mathieu@2965
   325
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, 10);
mathieu@2374
   326
mathieu@2965
   327
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-5)));
mathieu@2374
   328
  CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
mathieu@2965
   329
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
mathieu@2965
   330
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestInt16WithBounds", IntegerValue (-6)));
mathieu@2374
   331
  CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
mathieu@2965
   332
  CHECK_GET_PARAM (p, "TestInt16WithBounds", IntegerValue, -5);
mathieu@2374
   333
mathieu@2374
   334
  CHECK_GET_STR (p, "TestInt16SetGet", "6");
mathieu@2965
   335
  CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 6);
mathieu@2965
   336
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestInt16SetGet", IntegerValue (0)));
mathieu@2374
   337
  CHECK_GET_STR (p, "TestInt16SetGet", "0");
mathieu@2965
   338
  CHECK_GET_PARAM (p, "TestInt16SetGet", IntegerValue, 0);
mathieu@2375
   339
mathieu@2374
   340
  CHECK_GET_STR (p, "TestUint8", "1");
mathieu@2965
   341
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 1);
mathieu@2965
   342
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (0)));
mathieu@2374
   343
  CHECK_GET_STR (p, "TestUint8", "0");
mathieu@2965
   344
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 0);
mathieu@2965
   345
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", UintegerValue (255)));
mathieu@2374
   346
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   347
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   348
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestUint8", StringValue ("255")));
mathieu@2374
   349
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   350
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   351
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("256")));
mathieu@2374
   352
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   353
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   354
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", StringValue ("-1")));
mathieu@2374
   355
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   356
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2965
   357
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestUint8", UintegerValue ((uint64_t)-1)));
mathieu@2374
   358
  CHECK_GET_STR (p, "TestUint8", "255");
mathieu@2965
   359
  CHECK_GET_PARAM (p, "TestUint8", UintegerValue, 255);
mathieu@2385
   360
mathieu@2374
   361
  CHECK_GET_STR (p, "TestFloat", "-1.1");
mathieu@2965
   362
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestFloat", DoubleValue ((float)+2.3)));
mathieu@2965
   363
  CHECK_GET_PARAM (p, "TestFloat", DoubleValue, (float)+2.3);
mathieu@2385
   364
mathieu@2374
   365
  CHECK_GET_STR (p, "TestEnum", "TestA");
mathieu@2965
   366
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_A);
mathieu@2965
   367
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", EnumValue (AttributeObjectTest::TEST_C)));
mathieu@2374
   368
  CHECK_GET_STR (p, "TestEnum", "TestC");
mathieu@2965
   369
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_C);
mathieu@2965
   370
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestEnum", StringValue ("TestB")));
mathieu@2374
   371
  CHECK_GET_STR (p, "TestEnum", "TestB");
mathieu@2965
   372
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
mathieu@2965
   373
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", StringValue ("TestD")));
mathieu@2374
   374
  CHECK_GET_STR (p, "TestEnum", "TestB");
mathieu@2965
   375
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
mathieu@2965
   376
  NS_TEST_ASSERT (!p->SetAttributeFailSafe("TestEnum", EnumValue (5)));
mathieu@2374
   377
  CHECK_GET_STR (p, "TestEnum", "TestB");
mathieu@2965
   378
  CHECK_GET_PARAM (p, "TestEnum", EnumValue, AttributeObjectTest::TEST_B);
mathieu@2374
   379
mathieu@2965
   380
  RandomVariableValue ran;
mathieu@2965
   381
  p->GetAttribute ("TestRandom", ran);
mathieu@2965
   382
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (UniformVariable (0.0, 1.0))));
mathieu@2965
   383
  NS_TEST_ASSERT (p->SetAttributeFailSafe("TestRandom", RandomVariableValue (ConstantVariable (10.0))));
mathieu@2405
   384
mathieu@2405
   385
  {
mathieu@2965
   386
    ObjectVectorValue vector;
mathieu@2965
   387
    p->GetAttribute ("TestVector1", vector);
mathieu@2405
   388
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2405
   389
    p->AddToVector1 ();
mathieu@2405
   390
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2965
   391
    p->GetAttribute ("TestVector1", vector);
mathieu@2405
   392
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2405
   393
    Ptr<Object> a = vector.Get (0);
mathieu@2405
   394
    NS_TEST_ASSERT_UNEQUAL (a, 0);
mathieu@2405
   395
    p->AddToVector1 ();
mathieu@2405
   396
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2965
   397
    p->GetAttribute ("TestVector1", vector);
mathieu@2405
   398
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
mathieu@2405
   399
  }
mathieu@2405
   400
mathieu@2405
   401
  {
mathieu@2965
   402
    ObjectVectorValue vector;
mathieu@2965
   403
    p->GetAttribute ("TestVector2", vector);
mathieu@2405
   404
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2405
   405
    p->AddToVector2 ();
mathieu@2405
   406
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 0);
mathieu@2965
   407
    p->GetAttribute ("TestVector2", vector);
mathieu@2405
   408
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2405
   409
    Ptr<Object> a = vector.Get (0);
mathieu@2405
   410
    NS_TEST_ASSERT_UNEQUAL (a, 0);
mathieu@2405
   411
    p->AddToVector2 ();
mathieu@2405
   412
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 1);
mathieu@2965
   413
    p->GetAttribute ("TestVector2", vector);
mathieu@2405
   414
    NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
mathieu@2405
   415
  }
mathieu@2384
   416
mathieu@2965
   417
  NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("true")));
mathieu@2592
   418
  p = CreateObject<AttributeObjectTest> ();
mathieu@2965
   419
  BooleanValue boolV;
mathieu@2965
   420
  p->GetAttribute ("TestBoolName", boolV);
mathieu@2965
   421
  NS_TEST_ASSERT_EQUAL (boolV.Get (), true);
mathieu@2374
   422
mathieu@2965
   423
  NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
mathieu@2592
   424
  p = CreateObject<AttributeObjectTest> ();
mathieu@2965
   425
  p->GetAttribute ("TestBoolName", boolV);
mathieu@2965
   426
  NS_TEST_ASSERT_EQUAL (boolV.Get (), false);
mathieu@2374
   427
mathieu@2965
   428
  IntegerValue i;
mathieu@2965
   429
  p->GetAttribute ("IntegerTraceSource1", i);
mathieu@2462
   430
  NS_TEST_ASSERT_EQUAL (i.Get (), -2);
mathieu@2965
   431
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (+5)));
mathieu@2965
   432
  p->GetAttribute ("IntegerTraceSource1", i);
mathieu@2462
   433
  NS_TEST_ASSERT_EQUAL (i.Get (), +5);
mathieu@2965
   434
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (127)));
mathieu@2965
   435
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (128)));
mathieu@2965
   436
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-128)));
mathieu@2965
   437
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-129)));
mathieu@2462
   438
mathieu@2965
   439
  p->GetAttribute ("IntegerTraceSource2", i);
mathieu@2462
   440
  NS_TEST_ASSERT_EQUAL (i.Get (), -2);
mathieu@2965
   441
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (+5)));
mathieu@2965
   442
  p->GetAttribute ("IntegerTraceSource2", i);
mathieu@2462
   443
  NS_TEST_ASSERT_EQUAL (i.Get (), +5);
mathieu@2965
   444
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (127)));
mathieu@2965
   445
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (128)));
mathieu@2965
   446
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-128)));
mathieu@2965
   447
  NS_TEST_ASSERT (!p->SetAttributeFailSafe ("IntegerTraceSource2", IntegerValue (-129)));
mathieu@2462
   448
mathieu@2466
   449
  m_got1 = -2;
mathieu@2965
   450
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (-1)));
mathieu@2594
   451
  NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
mathieu@2965
   452
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (0)));
mathieu@2466
   453
  NS_TEST_ASSERT_EQUAL (m_got1, 0);
mathieu@2594
   454
  NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
mathieu@2965
   455
  NS_TEST_ASSERT (p->SetAttributeFailSafe ("IntegerTraceSource1", IntegerValue (1)));
mathieu@2466
   456
  NS_TEST_ASSERT_EQUAL (m_got1, 0);
mathieu@2466
   457
mathieu@2466
   458
  m_got2 = 4.3;
mathieu@2466
   459
  p->InvokeCb (1.0, -5, 0.0);
mathieu@2466
   460
  NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
mathieu@2594
   461
  NS_TEST_ASSERT (p->TraceConnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
mathieu@2466
   462
  NS_TEST_ASSERT_EQUAL (m_got2, 4.3);
mathieu@2466
   463
  p->InvokeCb (1.0, -5, 0.0);
mathieu@2466
   464
  NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
mathieu@2594
   465
  NS_TEST_ASSERT (p->TraceDisconnectWithoutContext ("Source2", MakeCallback (&AttributeTest::NotifySource2, this)));
mathieu@2466
   466
  p->InvokeCb (-1.0, -5, 0.0);
mathieu@2466
   467
  NS_TEST_ASSERT_EQUAL (m_got2, 1.0);
mathieu@2469
   468
mathieu@2594
   469
  NS_TEST_ASSERT (p->TraceConnectWithoutContext ("ValueSource", MakeCallback (&AttributeTest::NotifySourceValue, this)));
mathieu@2926
   470
mathieu@2965
   471
  PointerValue ptr;
mathieu@2965
   472
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   473
  Ptr<Derived> derived = ptr.Get<Derived> ();
mathieu@2926
   474
  NS_TEST_ASSERT (derived == 0);
mathieu@2926
   475
  derived = Create<Derived> ();
mathieu@2965
   476
  NS_TEST_ASSERT (p->SetAttributeFailSafe("Pointer", PointerValue (derived)));
mathieu@2965
   477
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   478
  Ptr<Derived> stored = ptr.Get<Derived> ();
mathieu@2926
   479
  NS_TEST_ASSERT (stored == derived);
mathieu@2965
   480
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   481
  Ptr<Object> storedBase = ptr.Get<Object> ();
mathieu@2926
   482
  NS_TEST_ASSERT (stored == storedBase);
mathieu@2965
   483
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   484
  Ptr<AttributeObjectTest> x = ptr.Get<AttributeObjectTest> ();
mathieu@2926
   485
  NS_TEST_ASSERT (x == 0);
mathieu@2926
   486
mathieu@2965
   487
  p = CreateObject<AttributeObjectTest> ("Pointer", PointerValue (Create<Derived> ()));
mathieu@2926
   488
  NS_TEST_ASSERT (p != 0);
mathieu@2926
   489
  derived = 0;
mathieu@2965
   490
  p->GetAttribute ("Pointer", ptr);
mathieu@2965
   491
  derived = ptr.Get<Derived> ();
mathieu@2926
   492
  NS_TEST_ASSERT (derived != 0);
mathieu@2463
   493
mathieu@2374
   494
  return result;
mathieu@2374
   495
}
mathieu@2374
   496
mathieu@2374
   497
mathieu@2374
   498
mathieu@2450
   499
static AttributeTest g_parameterTest;
mathieu@2374
   500
mathieu@2374
   501
} // namespace ns3
mathieu@2374
   502
mathieu@2374
   503
mathieu@2374
   504
#endif /* RUN_SELF_TESTS */