BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file bavl_test.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <stdlib.h> |
||
31 | |||
32 | #include <misc/offset.h> |
||
33 | #include <misc/debug.h> |
||
34 | #include <misc/balloc.h> |
||
35 | #include <misc/compare.h> |
||
36 | #include <structure/BAVL.h> |
||
37 | #include <security/BRandom.h> |
||
38 | |||
39 | struct mynode { |
||
40 | int used; |
||
41 | int num; |
||
42 | BAVLNode avl_node; |
||
43 | }; |
||
44 | |||
45 | static int int_comparator (void *user, int *val1, int *val2) |
||
46 | { |
||
47 | return B_COMPARE(*val1, *val2); |
||
48 | } |
||
49 | |||
50 | static void verify (BAVL *tree) |
||
51 | { |
||
52 | printf("Verifying...\n"); |
||
53 | BAVL_Verify(tree); |
||
54 | } |
||
55 | |||
56 | int main (int argc, char **argv) |
||
57 | { |
||
58 | int num_nodes; |
||
59 | int num_random_delete; |
||
60 | |||
61 | if (argc != 3 || (num_nodes = atoi(argv[1])) <= 0 || (num_random_delete = atoi(argv[2])) < 0) { |
||
62 | fprintf(stderr, "Usage: %s <num> <numrandomdelete>\n", (argc > 0 ? argv[0] : NULL)); |
||
63 | return 1; |
||
64 | } |
||
65 | |||
66 | struct mynode *nodes = (struct mynode *)BAllocArray(num_nodes, sizeof(*nodes)); |
||
67 | ASSERT_FORCE(nodes) |
||
68 | |||
69 | int *values_ins = (int *)BAllocArray(num_nodes, sizeof(int)); |
||
70 | ASSERT_FORCE(values_ins) |
||
71 | |||
72 | int *values = (int *)BAllocArray(num_random_delete, sizeof(int)); |
||
73 | ASSERT_FORCE(values) |
||
74 | |||
75 | BAVL avl; |
||
76 | BAVL_Init(&avl, OFFSET_DIFF(struct mynode, num, avl_node), (BAVL_comparator)int_comparator, NULL); |
||
77 | verify(&avl); |
||
78 | |||
79 | printf("Inserting random values...\n"); |
||
80 | int inserted = 0; |
||
81 | BRandom_randomize((uint8_t *)values_ins, num_nodes * sizeof(int)); |
||
82 | for (int i = 0; i < num_nodes; i++) { |
||
83 | nodes[i].num = values_ins[i]; |
||
84 | if (BAVL_Insert(&avl, &nodes[i].avl_node, NULL)) { |
||
85 | nodes[i].used = 1; |
||
86 | inserted++; |
||
87 | } else { |
||
88 | nodes[i].used = 0; |
||
89 | printf("Insert collision!\n"); |
||
90 | } |
||
91 | } |
||
92 | printf("Inserted %d entries\n", inserted); |
||
93 | verify(&avl); |
||
94 | |||
95 | printf("Removing random entries...\n"); |
||
96 | int removed1 = 0; |
||
97 | BRandom_randomize((uint8_t *)values, num_random_delete * sizeof(int)); |
||
98 | for (int i = 0; i < num_random_delete; i++) { |
||
99 | int index = (((unsigned int *)values)[i] % num_nodes); |
||
100 | struct mynode *node = nodes + index; |
||
101 | if (node->used) { |
||
102 | BAVL_Remove(&avl, &node->avl_node); |
||
103 | node->used = 0; |
||
104 | removed1++; |
||
105 | } |
||
106 | } |
||
107 | printf("Removed %d entries\n", removed1); |
||
108 | verify(&avl); |
||
109 | |||
110 | printf("Removing remaining...\n"); |
||
111 | int removed2 = 0; |
||
112 | while (!BAVL_IsEmpty(&avl)) { |
||
113 | struct mynode *node = UPPER_OBJECT(BAVL_GetFirst(&avl), struct mynode, avl_node); |
||
114 | ASSERT_FORCE(node->used) |
||
115 | BAVL_Remove(&avl, &node->avl_node); |
||
116 | node->used = 0; |
||
117 | removed2++; |
||
118 | } |
||
119 | printf("Removed %d entries\n", removed2); |
||
120 | ASSERT_FORCE(BAVL_IsEmpty(&avl)) |
||
121 | ASSERT_FORCE(removed1 + removed2 == inserted) |
||
122 | verify(&avl); |
||
123 | |||
124 | BFree(nodes); |
||
125 | BFree(values_ins); |
||
126 | BFree(values); |
||
127 | |||
128 | return 0; |
||
129 | } |