test/unit_tests/prophet-list-test.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 #include <dtn-config.h>
       
     2 #include <oasys/util/UnitTest.h>
       
     3 #include <netinet/in.h>
       
     4 #include <sys/types.h>
       
     5 #include <oasys/debug/Log.h>
       
     6 #include <oasys/util/StringBuffer.h>
       
     7 
       
     8 #include "prophet/Ack.h"
       
     9 #include "prophet/Node.h"
       
    10 #include "prophet/Table.h"
       
    11 #include "prophet/AckList.h"
       
    12 #include "prophet/Dictionary.h"
       
    13 #include "prophet/BundleCore.h"
       
    14 #include "prophet/BundleTLVEntry.h"
       
    15 #include "prophet/BundleTLVEntryList.h"
       
    16 
       
    17 #include "prophet/PointerList.h"
       
    18 
       
    19 using namespace oasys;
       
    20 
       
    21 size_t test_iterations = 10;
       
    22 size_t table_iterations = 100;
       
    23 
       
    24 DECLARE_TEST(NodeList) {
       
    25     prophet::NodeList list;
       
    26     prophet::Node* node;
       
    27 
       
    28     for (size_t i=0; i < test_iterations; i++) {
       
    29         oasys::StringBuffer str(128,"dtn://node-");
       
    30         str.appendf("%zu",i);
       
    31         node = new prophet::Node(str.c_str());
       
    32         CHECK_EQUAL(i,list.size());
       
    33         list.push_back(node);
       
    34         CHECK(i != list.size());
       
    35     }
       
    36 
       
    37     return UNIT_TEST_PASSED;
       
    38 }
       
    39 
       
    40 DECLARE_TEST(Dictionary) {
       
    41     prophet::Dictionary pd("dtn://sender","dtn://receiver");
       
    42     size_t testsize = test_iterations;
       
    43     u_int16_t sid = 0xffff;
       
    44 
       
    45     // By definition, sender is string_id 0
       
    46     DO(sid = pd.find("dtn://sender"));
       
    47     CHECK_EQUAL(sid,0);
       
    48     CHECK_EQUALSTR("dtn://sender",pd.find(0).c_str());
       
    49 
       
    50     // By definition, receiver is string_id 1
       
    51     DO(sid = pd.find("dtn://receiver"));
       
    52     CHECK_EQUAL(sid,1);
       
    53     CHECK_EQUALSTR("dtn://receiver",pd.find(1).c_str());
       
    54 
       
    55     std::string tester;
       
    56     for (size_t i=0; i<testsize; i++) {
       
    57         oasys::StringBuffer str(128,"dtn://node-");
       
    58         str.appendf("%zu/test",i);
       
    59         sid = pd.insert(str.c_str());
       
    60         CHECK(sid != 0);
       
    61         tester.assign(pd.find(sid));
       
    62         CHECK_EQUALSTR(tester.c_str(),str.c_str());
       
    63     }
       
    64 
       
    65     CHECK(pd.size() == testsize);
       
    66 
       
    67     prophet::Dictionary pd2(pd);
       
    68 
       
    69     CHECK_EQUAL(pd.size(),pd2.size());
       
    70 
       
    71     std::string iter;
       
    72     for(size_t i = 0; i<pd2.size(); i++) {
       
    73         tester.assign(pd.find(i));
       
    74         iter.assign(pd2.find(i));
       
    75         CHECK_EQUALSTR(tester.c_str(),iter.c_str());
       
    76     }
       
    77 
       
    78     return UNIT_TEST_PASSED;
       
    79 }
       
    80 
       
    81 DECLARE_TEST(Table) {
       
    82     prophet::BundleCoreTestImpl core;
       
    83     prophet::Table pt(&core,"pt");
       
    84 
       
    85     std::string a("dtn://test-a"), b("dtn://test-b");
       
    86 
       
    87     CHECK(pt.find(a) == NULL);
       
    88     CHECK(pt.p_value(a) == 0.0);
       
    89     DO(pt.update_route(a,true,true,false));
       
    90     CHECK(pt.p_value(a) == 0.75);
       
    91 
       
    92     CHECK(pt.find(b) == NULL);
       
    93     CHECK(pt.p_value(b) == 0.0);
       
    94     DO(pt.update_transitive(b,a,0.75,true,true,false));
       
    95     CHECK(pt.p_value(b) > 0.0);
       
    96     printf("pt.p_value(b) == %.02f\n",pt.p_value(b));
       
    97 
       
    98     prophet::Table *p2 = NULL;
       
    99     {
       
   100         prophet::Table p3(&core,"p2");
       
   101         p3.update_route(a);
       
   102         p3.update_route(b);
       
   103         p3.update_route(b);
       
   104         p2 = new prophet::Table(p3);
       
   105     }
       
   106     CHECK(p2->p_value(a) == 0.75);
       
   107     CHECK(p2->p_value(b) >= 0.75);
       
   108     delete p2;
       
   109 
       
   110     // default is 0, quota disabled
       
   111     prophet::Table p3(&core,"p3");
       
   112 
       
   113     for (size_t i=0; i<table_iterations; i++) {
       
   114         oasys::StringBuffer str(128,"dtn://node-");
       
   115         str.appendf("%zu",i);
       
   116         p3.update_route(str.c_str());
       
   117         int j = i % 10;
       
   118         while (j-- > 0) {
       
   119             p3.update_route(str.c_str());
       
   120         }
       
   121     }
       
   122 
       
   123     p3.set_max_route(table_iterations/2);
       
   124     CHECK_EQUAL(p3.size(), table_iterations/2);
       
   125 
       
   126     prophet::Table::heap_iterator hi = p3.heap_begin();
       
   127     std::vector<prophet::Node*> list(p3.heap_begin(),p3.heap_end());
       
   128     struct prophet::heap_compare c;
       
   129     CHECK( (prophet::Heap<prophet::Node*,
       
   130                           std::vector<prophet::Node*>,
       
   131                           struct prophet::heap_compare>::is_heap(list,c)) ); 
       
   132     return UNIT_TEST_PASSED;
       
   133 }
       
   134 
       
   135 DECLARE_TEST(BundleTLVList) {
       
   136     prophet::BundleOfferList bol;
       
   137     for(size_t i = test_iterations; i>0; --i) {
       
   138         CHECK(bol.add_offer(0xffff+i,i,i,false,false));
       
   139         CHECK(!bol.add_offer(0xffff+i,i,i,true,true));
       
   140         CHECK_LTU(bol.size(),test_iterations - i + 1);
       
   141     }
       
   142 
       
   143     prophet::BundleResponseList brl;
       
   144     for(size_t i = test_iterations; i>0; --i) {
       
   145         CHECK(brl.add_response(0xffff+i,i,i,false,true));
       
   146         CHECK(!brl.add_response(0xffff+i,i,i,true,false));
       
   147         CHECK_LTU(brl.size(),test_iterations - i + 1);
       
   148     }
       
   149 
       
   150     CHECK(bol.size() == test_iterations);
       
   151     for(prophet::BundleOfferList::iterator i = bol.begin();
       
   152         i != bol.end();
       
   153         i++)
       
   154     {
       
   155         prophet::BundleOfferEntry* bo = *i;
       
   156         CHECK(bol.find(bo->creation_ts(),bo->seqno(),bo->sid()) != NULL);
       
   157     }
       
   158     bol.clear();
       
   159     CHECK(bol.size() == 0);
       
   160     return UNIT_TEST_PASSED;
       
   161 }
       
   162 
       
   163 DECLARE_TEST(AckList) {
       
   164     prophet::PointerList<prophet::Ack> clone;
       
   165     prophet::AckList pl;
       
   166     for(size_t i = 0; i<test_iterations; i++) {
       
   167         oasys::StringBuffer str(128,"dtn://node-");
       
   168         str.appendf("%zu",i);
       
   169         CHECK(pl.insert(str.c_str(),0xff00+i,i,test_iterations-i));
       
   170         prophet::Ack* pa = new prophet::Ack(str.c_str(),0xff00+i,i,test_iterations-i);
       
   171         CHECK(!pl.insert(pa));
       
   172         delete pa;
       
   173 
       
   174         CHECK(pl.size() == i+1);
       
   175 
       
   176         CHECK(pl.is_ackd(str.c_str(),0xff00+i,i));
       
   177 
       
   178         CHECK(pl.fetch(str.c_str(),&clone) == 1);
       
   179         pa = clone.front();
       
   180         CHECK_EQUALSTR(str.c_str(), pa->dest_id().c_str());
       
   181         clone.clear();
       
   182     }
       
   183     CHECK_EQUAL(pl.size(),test_iterations);
       
   184     CHECK_EQUAL(pl.expire(),test_iterations);
       
   185     CHECK_EQUAL(0,pl.size());
       
   186     CHECK(pl.empty());
       
   187     return UNIT_TEST_PASSED;
       
   188 }
       
   189 
       
   190 DECLARE_TESTER(ProphetListTest) {
       
   191     ADD_TEST(NodeList);
       
   192     ADD_TEST(Dictionary);
       
   193     ADD_TEST(Table);
       
   194     ADD_TEST(BundleTLVList);
       
   195     ADD_TEST(AckList);
       
   196 }
       
   197 
       
   198 DECLARE_TEST_FILE(ProphetListTest, "prophet list test");