-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbt_test.c
143 lines (134 loc) · 2.99 KB
/
rbt_test.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "rbt.h"
void invariant_check1(rbnode_t *node)
{
if (node != NULL)
{
if (node->leftChild != NULL)
{
if (node->isRed)
assert(node->leftChild->isRed == 0);
invariant_check1(node->leftChild);
}
if (node->rightChild != NULL)
{
if (node->isRed)
assert(node->rightChild->isRed == 0);
invariant_check1(node->rightChild);
}
}
}
void dump_dot2(rbnode_t *node, FILE *o)
{
if (node != NULL)
{
fprintf(o, "%zu [label=\"%zu_%c\"];\n", node->data.vruntime, node->data.vruntime, node->isRed ? 'R' : 'B');
if (node->leftChild != NULL)
{
fprintf(o, "%zu -> %zu;\n", node->data.vruntime, node->leftChild->data.vruntime);
dump_dot2(node->leftChild, o);
}
if (node->rightChild != NULL)
{
fprintf(o, "%zu -> %zu;\n", node->data.vruntime, node->rightChild->data.vruntime);
dump_dot2(node->rightChild, o);
}
}
}
void dump_dot(rbtree_t *tree)
{
FILE *o = fopen("dotdump.txt", "w");
fprintf(o, "digraph sample {");
dump_dot2(tree->root, o);
fprintf(o, "}");
fclose(o);
}
void test_pop()
{
rbtree_t tree;
tree.root = NULL;
int min = RAND_MAX;
for (size_t i = 0; i < 5000; i++)
{
task_t task;
task.name = "task1";
task.vruntime = rand();
insert(&tree, task);
if (min > task.vruntime)
min = task.vruntime;
}
task_t t = pop_min(&tree);
assert(min == t.vruntime);
}
void test_pop2()
{
rbtree_t tree;
tree.root = NULL;
for (size_t i = 0; i < 5000; i++)
{
task_t task;
task.name = "task1";
task.vruntime = rand();
insert(&tree, task);
}
while (tree.root != NULL)
{
static size_t count = 0;
printf("deleted: %zu\n", count++);
pop_min(&tree);
invariant_check1(tree.root);
}
}
void test_pop_dump()
{
rbtree_t tree;
tree.root = NULL;
size_t counter = 1;
size_t cap = (rand() + 1) % 50 + 15;
for (size_t i = 0; i < cap; i++)
{
task_t task;
task.name = "task";
task.vruntime = counter++;
insert(&tree, task);
}
for (size_t i = 0; i < cap / 2; i++)
{
pop_min(&tree);
}
for (size_t i = 0; i < cap / 3; i++)
{
task_t task;
task.name = "task";
task.vruntime = counter++;
insert(&tree, task);
}
for (size_t i = 0; i < cap / 4; i++)
{
pop_min(&tree);
}
dump_dot(&tree);
}
void test_increasing()
{
rbtree_t tree;
tree.root = NULL;
for (size_t i = 0; i < 5000; i++)
{
task_t task;
task.name = "task1";
task.vruntime = i;
insert(&tree, task);
}
}
int main()
{
test_pop_dump();
test_pop();
test_pop2();
test_increasing();
printf("Success \n");
return 0;
}