src/vfer_test_btree.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 2005, 2006, Internet2
00003  * Legal conditions are in file LICENSE
00004  * (MD5 = c434f2e53b8089d8b4d0172c7ce07360).
00005  */
00006 /**
00007  * @file   vfer_test_btree.c
00008  * @author Ivan Beschastnikh
00009  * @brief  Tests the btree_delay implementation for storing moving delay history
00010  *
00011  * This file tests the moving(in time) delay hist implementation as an
00012  * AVL tree.
00013  *
00014  * -     04/30/06        ivan            Created
00015  */
00016 
00017 #include "vfer_btree_delay.h"
00018 #include "vfer_tests.h"
00019 
00020 /**
00021  * Program entrance, arguments aren't used
00022  */
00023 int main() {
00024         tree_t t;
00025         struct timeval tv;
00026         int r, j, min;
00027 
00028         TEST_INIT("TEST_BTREE", "test_btree.c", stdout, stdout);
00029         srand(time(NULL));
00030 
00031         /* initialize with window of 2 seconds */
00032         tv.tv_usec = 0;
00033         tv.tv_sec = 2;
00034         Tree_Init(&t, tv);
00035 
00036         /* insert a number in the middle of the window & check that min is correct */
00037         tv.tv_sec = 1;
00038         Tree_Insert(&t, 5, tv);
00039         TEST_CHECK((Tree_Min(&t, tv) == 5), "Tree_Min[1] incorrect return value");
00040 
00041         /* insert a number with a stamp expiring the first number & check that min is correct */
00042         tv.tv_sec = 4;
00043         Tree_Insert(&t, 6, tv);
00044         TEST_CHECK((Tree_Min(&t, tv) == 6), "Tree_Min[2] incorrect return value");
00045 
00046         /* check corner case where a val is on the edge of the window (check by using min) */
00047         tv.tv_sec = 6;
00048         Tree_Insert(&t, 7, tv);
00049         TEST_CHECK((Tree_Min(&t, tv) == 6), "Tree_Min[3] incorrect return value");
00050 
00051         /* expire the value in the tree and make sure min returns the last value inserted */
00052         tv.tv_sec = 9;
00053         TEST_CHECK((Tree_Min(&t, tv) == 7), "Tree_Min[4] incorrect return value");
00054 
00055         /* set the window to a high value, and make sure that min is
00056          * preserved for a long time & is consistent */
00057         tv.tv_sec = 256;
00058         Tree_Set_Window(&t, tv);
00059         min = 128;
00060         for (j = 0; j < 256; j++) {
00061                 tv.tv_sec = j;
00062                 r = rand() % 128;
00063                 if (r < min) min = r;
00064                 Tree_Insert(&t, r, tv);
00065                 TEST_CHECK((Tree_Min(&t, tv) == min), "Tree_Min[loop] incorrect return value");
00066         }
00067         
00068         /* enqueue another set of values but this time in increasing
00069          * order & then check that they expire in a sliding window
00070          * fashion */
00071         for (j = 0; j < 256; j++) {
00072                 tv.tv_sec = 256+j;
00073                 Tree_Insert(&t, j, tv);
00074         }
00075         for (j = 0; j < 256; j++) {
00076                 tv.tv_sec = 512+j;
00077                 TEST_CHECK((Tree_Min(&t, tv) == j), "Tree_Min[loop2] incorrect return value");
00078         }
00079 
00080         TEST_SUMMARY();
00081         return 0;
00082 } /* main() */

Generated on Tue Aug 8 16:07:19 2006 for VFER by  doxygen 1.4.7