"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/datacache_tests.c" (7 Jul 2019, 7045 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "datacache_tests.h"
    4 #include "datacache.h"
    5 
    6 START_TEST(datacache_can_clear_empty_cache)
    7 {
    8     datacache *dc = NULL;
    9 
   10     datacache_clear(&dc);
   11     ck_assert_ptr_eq(dc, NULL);
   12 }
   13 END_TEST
   14 
   15 START_TEST(datacache_can_add_to_cache)
   16 {
   17     int ret;
   18     datacache *dc = NULL;
   19 
   20     ret = datacache_add(&dc, "eth0", 0);
   21     ck_assert_int_eq(ret, 1);
   22     ck_assert_str_eq(dc->interface, "eth0");
   23     ck_assert_int_eq(dc->active, 1);
   24     ck_assert_int_eq(dc->filled, 0);
   25     ck_assert_int_eq(dc->syncneeded, 0);
   26     ck_assert_int_eq(dc->currx, 0);
   27     ck_assert_int_eq(dc->curtx, 0);
   28     ck_assert_ptr_eq(dc->log, NULL);
   29     ck_assert_ptr_eq(dc->next, NULL);
   30 
   31     datacache_clear(&dc);
   32     ck_assert_ptr_eq(dc, NULL);
   33 }
   34 END_TEST
   35 
   36 START_TEST(datacache_can_add_to_cache_consistently)
   37 {
   38     int ret;
   39     datacache *dc = NULL;
   40     datacache *bookmark = NULL;
   41 
   42     ret = datacache_add(&dc, "eth0", 0);
   43     ck_assert_int_eq(ret, 1);
   44     ck_assert_str_eq(dc->interface, "eth0");
   45     ck_assert_int_eq(dc->active, 1);
   46     ck_assert_int_eq(dc->filled, 0);
   47     ck_assert_int_eq(dc->syncneeded, 0);
   48     ck_assert_int_eq(dc->currx, 0);
   49     ck_assert_int_eq(dc->curtx, 0);
   50     ck_assert_ptr_eq(dc->log, NULL);
   51     ck_assert_ptr_eq(dc->next, NULL);
   52 
   53     bookmark = dc;
   54 
   55     ret = datacache_add(&dc, "eth1", 0);
   56     ck_assert_int_eq(ret, 1);
   57 
   58     ck_assert_str_eq(dc->interface, "eth1");
   59     ck_assert_int_eq(dc->active, 1);
   60     ck_assert_int_eq(dc->filled, 0);
   61     ck_assert_int_eq(dc->syncneeded, 0);
   62     ck_assert_int_eq(dc->currx, 0);
   63     ck_assert_int_eq(dc->curtx, 0);
   64     ck_assert_ptr_eq(dc->log, NULL);
   65     ck_assert_ptr_ne(dc->next, NULL);
   66 
   67     ck_assert_str_eq(bookmark->interface, "eth0");
   68 
   69     bookmark = dc;
   70     ck_assert_str_eq(bookmark->interface, "eth1");
   71 
   72     bookmark = bookmark->next;
   73     ck_assert_str_eq(bookmark->interface, "eth0");
   74 
   75     datacache_clear(&dc);
   76     ck_assert_ptr_eq(dc, NULL);
   77 }
   78 END_TEST
   79 
   80 START_TEST(datacache_knows_how_to_count)
   81 {
   82     int ret;
   83     datacache *dc = NULL;
   84 
   85     ret = datacache_add(&dc, "eth0", 0);
   86     ck_assert_int_eq(ret, 1);
   87 
   88     ret = datacache_count(&dc);
   89     ck_assert_int_eq(ret, 1);
   90 
   91     ret = datacache_add(&dc, "eth1", 0);
   92     ck_assert_int_eq(ret, 1);
   93 
   94     ret = datacache_count(&dc);
   95     ck_assert_int_eq(ret, 2);
   96 
   97     ret = datacache_activecount(&dc);
   98     ck_assert_int_eq(ret, 2);
   99 
  100     dc->active = 0;
  101 
  102     ret = datacache_activecount(&dc);
  103     ck_assert_int_eq(ret, 1);
  104 
  105     datacache_clear(&dc);
  106     ck_assert_ptr_eq(dc, NULL);
  107 
  108     ret = datacache_count(&dc);
  109     ck_assert_int_eq(ret, 0);
  110 
  111     ret = datacache_activecount(&dc);
  112     ck_assert_int_eq(ret, 0);
  113 }
  114 END_TEST
  115 
  116 START_TEST(datacache_can_remove)
  117 {
  118     int ret;
  119     datacache *dc = NULL;
  120 
  121     ret = datacache_add(&dc, "eth0", 0);
  122     ck_assert_int_eq(ret, 1);
  123 
  124     ret = datacache_add(&dc, "eth1", 0);
  125     ck_assert_int_eq(ret, 1);
  126 
  127     ret = datacache_add(&dc, "eth2", 0);
  128     ck_assert_int_eq(ret, 1);
  129 
  130     ret = datacache_add(&dc, "eth3", 0);
  131     ck_assert_int_eq(ret, 1);
  132 
  133     ret = datacache_count(&dc);
  134     ck_assert_int_eq(ret, 4);
  135 
  136     /* invalid removal doesn't cause issues */
  137     ret = datacache_remove(&dc, "eth4");
  138     ck_assert_int_eq(ret, 0);
  139     ret = datacache_count(&dc);
  140     ck_assert_int_eq(ret, 4);
  141     ck_assert_str_eq(dc->interface, "eth3");
  142 
  143     /* head removal */
  144     ret = datacache_remove(&dc, "eth3");
  145     ck_assert_int_eq(ret, 1);
  146     ret = datacache_count(&dc);
  147     ck_assert_int_eq(ret, 3);
  148     ck_assert_str_eq(dc->interface, "eth2");
  149 
  150     /* middle removal */
  151     ret = datacache_remove(&dc, "eth1");
  152     ck_assert_int_eq(ret, 1);
  153     ret = datacache_count(&dc);
  154     ck_assert_int_eq(ret, 2);
  155     ck_assert_str_eq(dc->interface, "eth2");
  156 
  157     /* tail removal */
  158     ret = datacache_remove(&dc, "eth0");
  159     ck_assert_int_eq(ret, 1);
  160     ret = datacache_count(&dc);
  161     ck_assert_int_eq(ret, 1);
  162     ck_assert_str_eq(dc->interface, "eth2");
  163 
  164     datacache_clear(&dc);
  165     ck_assert_ptr_eq(dc, NULL);
  166 }
  167 END_TEST
  168 
  169 START_TEST(datacache_can_do_stuff)
  170 {
  171     int ret;
  172     datacache *dc = NULL;
  173 
  174     ret = datacache_add(&dc, "eth0", 0);
  175     ck_assert_int_eq(ret, 1);
  176 
  177     ret = datacache_add(&dc, "eth1", 0);
  178     ck_assert_int_eq(ret, 1);
  179 
  180     ret = xferlog_add(&dc->log, 2, 1, 2);
  181     ck_assert_int_eq(ret, 1);
  182 
  183     ret = xferlog_add(&dc->log, 2, 10, 15);
  184     ck_assert_int_eq(ret, 1);
  185 
  186     ret = datacache_add(&dc, "eth2", 0);
  187     ck_assert_int_eq(ret, 1);
  188 
  189     ret = datacache_add(&dc, "eth3", 0);
  190     ck_assert_int_eq(ret, 1);
  191 
  192     ret = xferlog_add(&dc->log, 2, 2, 2);
  193     ck_assert_int_eq(ret, 1);
  194 
  195     ret = datacache_count(&dc);
  196     ck_assert_int_eq(ret, 4);
  197 
  198     /* suppress output to validate that debug function doesn't cause a crash */
  199     suppress_output();
  200     datacache_debug(&dc);
  201     printf("\n");
  202 
  203     ret = datacache_remove(&dc, "eth1");
  204     ck_assert_int_eq(ret, 1);
  205     ret = datacache_count(&dc);
  206     ck_assert_int_eq(ret, 3);
  207 
  208     datacache_debug(&dc);
  209     printf("\n");
  210 
  211     datacache_clear(&dc);
  212     ck_assert_ptr_eq(dc, NULL);
  213     datacache_debug(&dc);
  214     ret = datacache_count(&dc);
  215     ck_assert_int_eq(ret, 0);
  216 }
  217 END_TEST
  218 
  219 START_TEST(xferlog_can_clear_empty_log)
  220 {
  221     xferlog *log = NULL;
  222 
  223     xferlog_clear(&log);
  224     ck_assert_ptr_eq(log, NULL);
  225 }
  226 END_TEST
  227 
  228 START_TEST(xferlog_can_log)
  229 {
  230     int ret;
  231     xferlog *log = NULL;
  232 
  233     ret = xferlog_add(&log, 1, 1, 1);
  234     ck_assert_int_eq(ret, 1);
  235 
  236     ck_assert_ptr_ne(log, NULL);
  237 
  238     xferlog_clear(&log);
  239     ck_assert_ptr_eq(log, NULL);
  240 }
  241 END_TEST
  242 
  243 START_TEST(xferlog_can_handle_multiple_entries)
  244 {
  245     int ret;
  246     xferlog *log = NULL;
  247     xferlog *bookmark = NULL;
  248 
  249     ret = xferlog_add(&log, 1, 1, 1);
  250     ck_assert_int_eq(ret, 1);
  251     ck_assert_int_eq(log->timestamp, 1);
  252     ck_assert_int_eq(log->rx, 1);
  253     ck_assert_int_eq(log->tx, 1);
  254 
  255     bookmark = log;
  256 
  257     ret = xferlog_add(&log, 1, 1, 2);
  258     ck_assert_int_eq(ret, 1);
  259     ck_assert_int_eq(log->timestamp, 1);
  260     ck_assert_int_eq(log->rx, 2);
  261     ck_assert_int_eq(log->tx, 3);
  262 
  263     ret = xferlog_add(&log, 4, 5, 5);
  264     ck_assert_int_eq(ret, 1);
  265     ck_assert_int_eq(log->timestamp, 4);
  266     ck_assert_int_eq(log->rx, 5);
  267     ck_assert_int_eq(log->tx, 5);
  268 
  269     ret = xferlog_add(&log, 7, 4, 4);
  270     ck_assert_int_eq(ret, 1);
  271     ck_assert_int_eq(log->timestamp, 7);
  272     ck_assert_int_eq(log->rx, 4);
  273     ck_assert_int_eq(log->tx, 4);
  274 
  275     ret = xferlog_add(&log, 1, 1, 1);
  276     ck_assert_int_eq(ret, 1);
  277     ck_assert_int_eq(log->timestamp, 1);
  278     ck_assert_int_eq(log->rx, 1);
  279     ck_assert_int_eq(log->tx, 1);
  280 
  281     /* check that new addition with same timestamp doesn't modify old record */
  282     ck_assert_int_eq(bookmark->timestamp, 1);
  283     ck_assert_int_eq(bookmark->rx, 2);
  284     ck_assert_int_eq(bookmark->tx, 3);
  285 
  286     xferlog_clear(&log);
  287     ck_assert_ptr_eq(log, NULL);
  288 }
  289 END_TEST
  290 
  291 void add_datacache_tests(Suite *s)
  292 {
  293     TCase *tc_datacache = tcase_create("Datacache");
  294     tcase_add_checked_fixture(tc_datacache, setup, teardown);
  295     tcase_add_unchecked_fixture(tc_datacache, setup, teardown);
  296     tcase_add_test(tc_datacache, datacache_can_clear_empty_cache);
  297     tcase_add_test(tc_datacache, datacache_can_add_to_cache);
  298     tcase_add_test(tc_datacache, datacache_can_add_to_cache_consistently);
  299     tcase_add_test(tc_datacache, datacache_knows_how_to_count);
  300     tcase_add_test(tc_datacache, datacache_can_remove);
  301     tcase_add_test(tc_datacache, datacache_can_do_stuff);
  302     tcase_add_test(tc_datacache, xferlog_can_clear_empty_log);
  303     tcase_add_test(tc_datacache, xferlog_can_log);
  304     tcase_add_test(tc_datacache, xferlog_can_handle_multiple_entries);
  305     suite_add_tcase(s, tc_datacache);
  306 }