File: iflist_tests.c

package info (click to toggle)
vnstat 2.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,148 kB
  • sloc: ansic: 25,669; sh: 4,495; perl: 472; makefile: 179; php: 12
file content (126 lines) | stat: -rw-r--r-- 3,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "common.h"
#include "vnstat_tests.h"
#include "iflist_tests.h"
#include "iflist.h"

START_TEST(iflistfree_can_free_null)
{
	iflist *ifl = NULL;
    iflistfree(&ifl);
}
END_TEST

START_TEST(iflistadd_can_add)
{
    int ret;
    iflist *ifl = NULL;
    ck_assert_ptr_eq(ifl, NULL);

    ret = iflistadd(&ifl, "eth0", 1, 0);
    ck_assert_int_eq(ret, 1);
    ck_assert_str_eq(ifl->interface, "eth0");
    ck_assert_int_eq(ifl->id, 1);
    ck_assert_int_eq(ifl->bandwidth, 0);
    ck_assert_ptr_eq(ifl->next, NULL);

    ret = iflistadd(&ifl, "eth1", 2, 1);
    ck_assert_int_eq(ret, 1);
    ck_assert_str_eq(ifl->interface, "eth0");
    ck_assert_int_eq(ifl->id, 1);
    ck_assert_int_eq(ifl->bandwidth, 0);
    ck_assert_ptr_ne(ifl->next, NULL);
    ck_assert_str_eq(ifl->next->interface, "eth1");
    ck_assert_int_eq(ifl->next->id, 2);
    ck_assert_int_eq(ifl->next->bandwidth, 1);

    ret = iflistadd(&ifl, "eth0", 3, 2);
    ck_assert_int_eq(ret, 1);
    ck_assert_str_eq(ifl->interface, "eth0");
    ck_assert_int_eq(ifl->id, 1);
    ck_assert_int_eq(ifl->bandwidth, 0);
    ck_assert_ptr_ne(ifl->next, NULL);
    ck_assert_str_eq(ifl->next->interface, "eth1");
    ck_assert_int_eq(ifl->next->id, 2);
    ck_assert_int_eq(ifl->next->bandwidth, 1);
    ck_assert_ptr_ne(ifl->next->next, NULL);
    ck_assert_str_eq(ifl->next->next->interface, "eth0");
    ck_assert_int_eq(ifl->next->next->id, 3);
    ck_assert_int_eq(ifl->next->next->bandwidth, 2);

    iflistfree(&ifl);
    ck_assert_ptr_eq(ifl, NULL);
}
END_TEST

START_TEST(iflistsearch_can_search)
{
    int ret;
    iflist *ifl = NULL;
    ck_assert_ptr_eq(ifl, NULL);

    ret = iflistsearch(&ifl, "eth0");
    ck_assert_int_eq(ret, 0);

    ret = iflistadd(&ifl, "eth0", 0, 0);
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth0");
    ck_assert_int_eq(ret, 1);

    ret = iflistadd(&ifl, "eth1", 0, 1);
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth0");
    ck_assert_int_eq(ret, 1);

    ret = iflistadd(&ifl, "eth0", 0, 2);
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth0");
    ck_assert_int_eq(ret, 1);

    ret = iflistadd(&ifl, "eth2", 0, 10);
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth0");
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth2");
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth0");
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth1");
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth3");
    ck_assert_int_eq(ret, 0);

    ret = iflistadd(&ifl, "eth3", 0, 0);
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth3");
    ck_assert_int_eq(ret, 1);

    ret = iflistadd(&ifl, "eth4", 0, 0);
    ck_assert_int_eq(ret, 1);

    ret = iflistsearch(&ifl, "eth3");
    ck_assert_int_eq(ret, 1);

    iflistfree(&ifl);
    ck_assert_ptr_eq(ifl, NULL);
}
END_TEST

void add_iflist_tests(Suite *s)
{
	TCase *tc_iflist = tcase_create("Iflist");
	tcase_add_checked_fixture(tc_iflist, setup, teardown);
	tcase_add_unchecked_fixture(tc_iflist, setup, teardown);
	tcase_add_test(tc_iflist, iflistfree_can_free_null);
    tcase_add_test(tc_iflist, iflistadd_can_add);
    tcase_add_test(tc_iflist, iflistsearch_can_search);
	suite_add_tcase(s, tc_iflist);
}