Statistics
| Branch: | Revision:

peerstreamer-src / Test / int_bucket_test.c @ 9eb656e7

History | View | Annotate | Download (2.87 KB)

1
#include<malloc.h>
2
#include<assert.h>
3

    
4
#include<int_bucket.h>
5

    
6
void int_bucket_init_test()
7
{
8

    
9
        struct int_bucket * ib;
10

    
11
        ib = int_bucket_new(0);
12
        int_bucket_destroy(&ib);
13
        assert(ib==NULL);
14

    
15
        ib = int_bucket_new(3);
16
        int_bucket_destroy(&ib);
17
        assert(ib==NULL);
18

    
19
        fprintf(stderr,"%s successfully passed!\n",__func__);
20
}
21

    
22
void int_bucket_insert_test()
23
{
24
        struct int_bucket * ib;
25

    
26
        assert(int_bucket_insert(NULL,1,1) < 0);
27

    
28
        ib = int_bucket_new(0);
29

    
30
        assert(0 == int_bucket_insert(ib,1,1));
31
        assert(int_bucket_length(ib) == 1);
32

    
33
        assert(0 == int_bucket_insert(ib,1,1));
34
        assert(int_bucket_length(ib) == 1);
35

    
36
        assert(0 == int_bucket_insert(ib,2,1));
37
        assert(int_bucket_length(ib) == 2);
38

    
39
        assert(0 == int_bucket_insert(ib,3,4));
40
        assert(int_bucket_length(ib) == 3);
41

    
42
        assert(0 == int_bucket_insert(ib,2,1));
43
        assert(int_bucket_length(ib) == 3);
44

    
45
        int_bucket_destroy(&ib);
46

    
47
        fprintf(stderr,"%s successfully passed!\n",__func__);
48
}
49

    
50
void int_bucket_occurr_norm_test()
51
{
52
        struct int_bucket * ib;
53

    
54
        assert(int_bucket_occurr_norm(NULL) == 0);
55

    
56
        ib = int_bucket_new(0);
57
        assert(0 == int_bucket_insert(ib,1,1));
58
        assert(int_bucket_occurr_norm(ib) == 1);
59

    
60
        assert(0 == int_bucket_insert(ib,1,2));
61
        assert(int_bucket_occurr_norm(ib) == 3);
62

    
63
        assert(0 == int_bucket_insert(ib,10,4));
64
        assert(int_bucket_occurr_norm(ib) == 5);
65

    
66
        assert(0 == int_bucket_insert(ib,343,7));
67
        assert(int_bucket_occurr_norm(ib) < 8.61);
68
        assert(int_bucket_occurr_norm(ib) > 8.6);
69

    
70
        int_bucket_destroy(&ib);
71

    
72
        fprintf(stderr,"%s successfully passed!\n",__func__);
73
}
74

    
75
void int_bucket_sum_test()
76
{
77
        struct int_bucket * ib1,* ib2;
78

    
79
        int_bucket_sum(NULL,NULL);
80

    
81
        ib1 = int_bucket_new(0);
82
        int_bucket_sum(ib1,NULL);
83
        int_bucket_sum(NULL,ib1);
84

    
85
        ib2 = int_bucket_new(0);
86
        int_bucket_sum(ib1,ib2);
87
        assert(int_bucket_length(ib1) == 0);
88
        assert(int_bucket_occurr_norm(ib1) == 0);
89

    
90
        int_bucket_insert(ib2,5,3);
91
        int_bucket_insert(ib2,67,2);
92
        int_bucket_insert(ib1,67,2);
93
        int_bucket_sum(ib1,ib2);
94
        assert(int_bucket_length(ib1) == 2);
95
        assert(int_bucket_occurr_norm(ib1) == 5);
96

    
97
        int_bucket_destroy(&ib1);
98
        int_bucket_destroy(&ib2);
99
        fprintf(stderr,"%s successfully passed!\n",__func__);
100
}
101

    
102
void int_bucket_iter_test()
103
{
104

    
105
        struct int_bucket * ib;
106
        const uint32_t * iter = NULL;
107

    
108
        ib = int_bucket_new(5);
109
        iter = int_bucket_iter(ib, iter);
110
        assert(iter == NULL);
111

    
112
        int_bucket_insert(ib,5,3);
113
        int_bucket_insert(ib,2,1);
114
        int_bucket_insert(ib,7,18);
115

    
116
        iter = int_bucket_iter(ib, iter);
117
        assert(iter);
118
        assert(*iter == 2);
119

    
120
        iter = int_bucket_iter(ib, iter);
121
        assert(iter);
122
        assert(*iter == 5);
123

    
124
        iter = int_bucket_iter(ib, iter);
125
        assert(iter);
126
        assert(*iter == 7);
127

    
128
        iter = int_bucket_iter(ib, iter);
129
        assert(!iter);
130

    
131
        int_bucket_destroy(&ib);
132
        fprintf(stderr,"%s successfully passed!\n",__func__);
133
}
134

    
135
int main(char ** argc,int argv)
136
{
137
        int_bucket_init_test();
138
        int_bucket_insert_test();
139
        int_bucket_occurr_norm_test();
140
        int_bucket_sum_test();
141
        int_bucket_iter_test();
142
        return 0;
143
}