src/core/value-test.cc
changeset 2374 570f3c6c6142
child 2375 6f8733409b3b
equal deleted inserted replaced
2373:73983ba47afc 2374:570f3c6c6142
       
     1 #ifdef RUN_SELF_TESTS
       
     2 #include "test.h"
       
     3 #include "object.h"
       
     4 #include "boolean-value.h"
       
     5 #if 0
       
     6 #include "int-value.h"
       
     7 #include "uint-value.h"
       
     8 #include "fp-value.h"
       
     9 #include "enum-value.h"
       
    10 #endif
       
    11 namespace ns3 {
       
    12 
       
    13 class ParamSpecTest : public Test
       
    14 {
       
    15 public:
       
    16   ParamSpecTest ();
       
    17   virtual bool RunTests (void);
       
    18 private:
       
    19 };
       
    20 
       
    21 class Derived : public Object
       
    22 {
       
    23 public:
       
    24   static TypeId GetTypeId (void) {
       
    25     static TypeId tid = TypeId ("Derived")
       
    26       .SetParent<Object> ()
       
    27       ;
       
    28     return tid;
       
    29   }
       
    30 };
       
    31 
       
    32 class ParamSpecObjectTest : public Object
       
    33 {
       
    34 public:
       
    35   enum TestEnum {
       
    36     TEST_A,
       
    37     TEST_B,
       
    38     TEST_C
       
    39   };
       
    40   static TypeId GetTypeId (void) {
       
    41     static TypeId tid = TypeId ("ParamSpecObjectTest")
       
    42       .SetParent<Object> ()
       
    43       .AddParameter ("TestBoolName", "help text",
       
    44 		     MakeBooleanParamSpec (&ParamSpecObjectTest::m_boolTest, false))
       
    45       .AddParameter ("TestBoolA", "help text",
       
    46 		     MakeBooleanParamSpec (&ParamSpecObjectTest::DoSetTestB,
       
    47 					   &ParamSpecObjectTest::DoGetTestB, 
       
    48 					   false))
       
    49       .AddParameter ("TestPtr", "help text", 
       
    50 		     MakePtrParamSpec (&ParamSpecObjectTest::m_derived))
       
    51 #if 0
       
    52       .AddParameter ("TestInt16", "help text",
       
    53 		     MakeIntParamSpec (-2, &ParamSpecObjectTest::m_int16))
       
    54       .AddParameter ("TestInt16WithBounds", "help text",
       
    55 		     MakeIntParamSpec (-2, -5, 10, 
       
    56 				       &ParamSpecObjectTest::m_int16WithBounds))
       
    57       .AddParameter ("TestInt16SetGet", "help text",
       
    58 		     MakeIntParamSpec (6, &ParamSpecObjectTest::DoSetInt16,
       
    59 				       &ParamSpecObjectTest::DoGetInt16))
       
    60       .AddParameter ("TestUint8", "help text",
       
    61 		     MakeUintParamSpec (1, &ParamSpecObjectTest::m_uint8))
       
    62       .AddParameter ("TestFloat", "help text",
       
    63 		     MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float))
       
    64       .AddParameter ("TestEnum", "help text",
       
    65 		     MakeEnumParamSpec (&ParamSpecObjectTest::m_enum,
       
    66 					TEST_A, "TestA",
       
    67 					TEST_B, "TestB",
       
    68 					TEST_C, "TestC"))
       
    69 #endif
       
    70       ;
       
    71         
       
    72     return tid;
       
    73   }
       
    74 
       
    75 private:
       
    76   void DoSetTestB (bool v) {
       
    77     m_boolTestA = v;
       
    78   }
       
    79   bool DoGetTestB (void) const {
       
    80     return m_boolTestA;
       
    81   }
       
    82   int16_t DoGetInt16 (void) const {
       
    83     return m_int16SetGet;
       
    84   }
       
    85   void DoSetInt16 (int16_t v) {
       
    86     m_int16SetGet = v;
       
    87   }
       
    88   bool m_boolTestA;
       
    89   bool m_boolTest;
       
    90   Ptr<Derived> m_derived;
       
    91   int16_t m_int16;
       
    92   int16_t m_int16WithBounds;
       
    93   int16_t m_int16SetGet;
       
    94   uint8_t m_uint8;
       
    95   float m_float;
       
    96   enum TestEnum m_enum;
       
    97 };
       
    98 
       
    99 
       
   100 #define CHECK_GET_STR(p,name,value)		\
       
   101   {						\
       
   102     std::string expected = value;		\
       
   103     std::string got;				\
       
   104     bool ok = p->Get (name, got);		\
       
   105     NS_TEST_ASSERT (ok);			\
       
   106     NS_TEST_ASSERT_EQUAL (got, expected);	\
       
   107   }
       
   108 #define CHECK_GET_PARAM(p,name,type,value)		\
       
   109   {							\
       
   110     const type expected = value;			\
       
   111     type got = value;					\
       
   112     PValue v = p->Get (name);				\
       
   113     NS_TEST_ASSERT (v.DynCast<type *> () != 0);		\
       
   114     got = v;						\
       
   115     NS_TEST_ASSERT_EQUAL (got.Get (), expected.Get ());	\
       
   116   }
       
   117 
       
   118 
       
   119 NS_OBJECT_ENSURE_REGISTERED (ParamSpecObjectTest);
       
   120 
       
   121 ParamSpecTest::ParamSpecTest ()
       
   122   : Test ("ParamSpec")
       
   123 {}
       
   124 bool 
       
   125 ParamSpecTest::RunTests (void)
       
   126 {
       
   127   bool result = true;
       
   128 
       
   129   Parameters params;
       
   130   Ptr<ParamSpecObjectTest> p;
       
   131   NS_TEST_ASSERT (params.Set ("ParamSpecObjectTest::TestBoolName", "false"));
       
   132   p = CreateObject<ParamSpecObjectTest> (params);
       
   133   CHECK_GET_STR (p, "TestBoolName", "false");
       
   134   CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
       
   135 
       
   136   NS_TEST_ASSERT (p->Set ("TestBoolName", "true"));
       
   137   CHECK_GET_STR (p, "TestBoolName", "true");
       
   138   CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
       
   139 
       
   140   NS_TEST_ASSERT (p->Set ("TestBoolName", BooleanValue (false)));
       
   141   CHECK_GET_STR (p, "TestBoolName", "false");
       
   142   CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
       
   143 
       
   144   p = CreateObjectWith<ParamSpecObjectTest> ("TestBoolName", "true");
       
   145   CHECK_GET_STR (p, "TestBoolName", "true");
       
   146   CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
       
   147 
       
   148   p = CreateObjectWith<ParamSpecObjectTest> ("TestBoolName", BooleanValue (true));
       
   149   CHECK_GET_STR (p, "TestBoolName", "true");
       
   150   CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
       
   151 
       
   152   NS_TEST_ASSERT (p->Set ("TestBoolA", "false"));
       
   153   CHECK_GET_STR (p, "TestBoolA", "false");
       
   154   CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, false);
       
   155 
       
   156   NS_TEST_ASSERT (p->Set ("TestBoolA", "true"));
       
   157   CHECK_GET_STR (p, "TestBoolA", "true");
       
   158   CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, true);
       
   159 
       
   160 
       
   161   Ptr<Derived> derived = p->Get ("TestPtr");
       
   162   NS_TEST_ASSERT (derived == 0);
       
   163   derived = Create<Derived> ();
       
   164   NS_TEST_ASSERT (p->Set ("TestPtr", derived));
       
   165   Ptr<Derived> stored = p->Get ("TestPtr");
       
   166   NS_TEST_ASSERT (stored == derived);
       
   167   Ptr<Object> storedBase = p->Get ("TestPtr");
       
   168   NS_TEST_ASSERT (stored == storedBase);
       
   169   Ptr<ParamSpecObjectTest> x = p->Get ("TestPtr");
       
   170   NS_TEST_ASSERT (x == 0);
       
   171 
       
   172   p = CreateObjectWith<ParamSpecObjectTest> ("TestPtr", Create<Derived> ());
       
   173   NS_TEST_ASSERT (p != 0);
       
   174   derived = 0;
       
   175   derived = p->Get ("TestPtr");
       
   176   NS_TEST_ASSERT (derived != 0);
       
   177 
       
   178 #if 0
       
   179   CHECK_GET_STR (p, "TestInt16", "-2");
       
   180   CHECK_GET_PARAM (p, "TestInt16", IntValue, -2);
       
   181 
       
   182   NS_TEST_ASSERT (p->Set ("TestInt16", "-5"));
       
   183   CHECK_GET_STR (p, "TestInt16", "-5");
       
   184   CHECK_GET_PARAM (p, "TestInt16", IntValue, -5);
       
   185 
       
   186   NS_TEST_ASSERT (p->Set ("TestInt16", IntValue (+2)));
       
   187   CHECK_GET_STR (p, "TestInt16", "2");
       
   188   CHECK_GET_PARAM (p, "TestInt16", IntValue, +2);
       
   189 
       
   190   NS_TEST_ASSERT (p->Set ("TestInt16", IntValue (-32768)));
       
   191   CHECK_GET_STR (p, "TestInt16", "-32768");
       
   192   CHECK_GET_PARAM (p, "TestInt16", IntValue, -32768);
       
   193 
       
   194   NS_TEST_ASSERT (!p->Set ("TestInt16", IntValue (-32769)));
       
   195   CHECK_GET_STR (p, "TestInt16", "-32768");
       
   196   CHECK_GET_PARAM (p, "TestInt16", IntValue, -32768);
       
   197 
       
   198   NS_TEST_ASSERT (p->Set ("TestInt16", IntValue (32767)));
       
   199   CHECK_GET_STR (p, "TestInt16", "32767");
       
   200   CHECK_GET_PARAM (p, "TestInt16", IntValue, 32767);
       
   201 
       
   202   NS_TEST_ASSERT (!p->Set ("TestInt16", IntValue (32768)));
       
   203   CHECK_GET_STR (p, "TestInt16", "32767");
       
   204   CHECK_GET_PARAM (p, "TestInt16", IntValue, 32767);
       
   205 
       
   206   NS_TEST_ASSERT (p->Set ("TestInt16WithBounds", IntValue (10)));
       
   207   CHECK_GET_STR (p, "TestInt16WithBounds", "10");
       
   208   CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, 10);
       
   209   NS_TEST_ASSERT (!p->Set ("TestInt16WithBounds", IntValue (11)));
       
   210   CHECK_GET_STR (p, "TestInt16WithBounds", "10");
       
   211   CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, 10);
       
   212 
       
   213   NS_TEST_ASSERT (p->Set ("TestInt16WithBounds", IntValue (-5)));
       
   214   CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
       
   215   CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, -5);
       
   216   NS_TEST_ASSERT (!p->Set ("TestInt16WithBounds", IntValue (-6)));
       
   217   CHECK_GET_STR (p, "TestInt16WithBounds", "-5");
       
   218   CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, -5);
       
   219 
       
   220   CHECK_GET_STR (p, "TestInt16SetGet", "6");
       
   221   CHECK_GET_PARAM (p, "TestInt16SetGet", IntValue, 6);
       
   222   NS_TEST_ASSERT (p->Set ("TestInt16SetGet", IntValue (0)));
       
   223   CHECK_GET_STR (p, "TestInt16SetGet", "0");
       
   224   CHECK_GET_PARAM (p, "TestInt16SetGet", IntValue, 0);
       
   225   
       
   226   CHECK_GET_STR (p, "TestUint8", "1");
       
   227   CHECK_GET_PARAM (p, "TestUint8", UintValue, 1);
       
   228   NS_TEST_ASSERT (p->Set ("TestUint8", UintValue (0)));
       
   229   CHECK_GET_STR (p, "TestUint8", "0");
       
   230   CHECK_GET_PARAM (p, "TestUint8", UintValue, 0);
       
   231   NS_TEST_ASSERT (p->Set ("TestUint8", UintValue (255)));
       
   232   CHECK_GET_STR (p, "TestUint8", "255");
       
   233   CHECK_GET_PARAM (p, "TestUint8", UintValue, 255);
       
   234   NS_TEST_ASSERT (p->Set ("TestUint8", "255"));
       
   235   CHECK_GET_STR (p, "TestUint8", "255");
       
   236   CHECK_GET_PARAM (p, "TestUint8", UintValue, 255);
       
   237   NS_TEST_ASSERT (!p->Set ("TestUint8", "256"));
       
   238   CHECK_GET_STR (p, "TestUint8", "255");
       
   239   CHECK_GET_PARAM (p, "TestUint8", UintValue, 255);
       
   240   NS_TEST_ASSERT (!p->Set ("TestUint8", "-1"));
       
   241   CHECK_GET_STR (p, "TestUint8", "255");
       
   242   CHECK_GET_PARAM (p, "TestUint8", UintValue, 255);
       
   243   NS_TEST_ASSERT (!p->Set ("TestUint8", UintValue (-1)));
       
   244   CHECK_GET_STR (p, "TestUint8", "255");
       
   245   CHECK_GET_PARAM (p, "TestUint8", UintValue, 255);
       
   246 
       
   247   CHECK_GET_STR (p, "TestFloat", "-1.1");
       
   248   NS_TEST_ASSERT (p->Set ("TestFloat", FpValue ((float)+2.3)));
       
   249   CHECK_GET_PARAM (p, "TestFloat", FpValue, (float)+2.3);
       
   250 
       
   251   CHECK_GET_STR (p, "TestEnum", "TestA");
       
   252   CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_A);
       
   253   NS_TEST_ASSERT (p->Set ("TestEnum", EnumValue (ParamSpecObjectTest::TEST_C)));
       
   254   CHECK_GET_STR (p, "TestEnum", "TestC");
       
   255   CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_C);
       
   256   NS_TEST_ASSERT (p->Set ("TestEnum", "TestB"));
       
   257   CHECK_GET_STR (p, "TestEnum", "TestB");
       
   258   CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B);
       
   259   NS_TEST_ASSERT (!p->Set ("TestEnum", "TestD"));
       
   260   CHECK_GET_STR (p, "TestEnum", "TestB");
       
   261   CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B);
       
   262   NS_TEST_ASSERT (!p->Set ("TestEnum", EnumValue (5)));
       
   263   CHECK_GET_STR (p, "TestEnum", "TestB");
       
   264   CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B);
       
   265 #endif
       
   266 
       
   267 #if 0
       
   268   p->Set ("TestBoolName", "true");
       
   269   NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "true");
       
   270   p->Set ("TestBoolName", "false");
       
   271   NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "false");
       
   272 
       
   273   Parameters::GetGlobal ()->Set ("TestBoolName", "true");
       
   274   p = CreateObjectWith<ParamSpecObjectTest> ();
       
   275   NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "true");
       
   276 
       
   277   Parameters::GetGlobal ()->Set ("TestBoolName", "false");
       
   278   p = CreateObjectWith<ParamSpecObjectTest> ();
       
   279   NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "false");
       
   280 #endif
       
   281   return result;
       
   282 }
       
   283 
       
   284 
       
   285 
       
   286 static ParamSpecTest g_parameterTest;
       
   287 
       
   288 } // namespace ns3
       
   289 
       
   290 
       
   291 #endif /* RUN_SELF_TESTS */