Subversion Repositories HelenOS

Rev

Rev 2416 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2416 Rev 2421
Line 26... Line 26...
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <test.h>
29
#include <test.h>
30
#include <print.h>
30
#include <print.h>
31
#include <adt/extavlreltree.h>
31
#include <adt/extavlrel.h>
32
#include <debug.h>
32
#include <debug.h>
33
 
33
 
34
#include <panic.h>
34
#include <panic.h>
35
 
35
 
36
 
36
 
Line 51... Line 51...
51
 */
51
 */
52
static extavlreltree_node_t *first_free_node = NULL;
52
static extavlreltree_node_t *first_free_node = NULL;
53
 
53
 
54
 
54
 
55
 
55
 
56
static int extreltree_test_height(extrelavltree_node_t *node,bool timeout);
56
static int extreltree_test_height(extavlreltree_node_t *node,bool timeout);
57
static extavlreltree_node_t *extreltree_test_parents(extavlireltree_node_t *node);
57
static extavlreltree_node_t *extreltree_test_parents(extavlreltree_node_t *node);
58
static void print_extreltree_structure_flat (extavlreltree_node_t *node, int level);
58
static void print_extreltree_structure_flat (extavlreltree_node_t *node, int level);
59
static bool extreltree_test_link(int node_count);
59
static bool extreltree_test_link(extavlreltree_t *tree, int node_count);
60
static void print_extreltree_link(int node_count);
60
static void print_extreltree_link(extavlreltree_t *tree);
61
static extavlreltree_node_t *alloc_extavlreltree_node(void);
61
static extavlreltree_node_t *alloc_extavlreltree_node(void);
62
 
62
 
63
 
63
 
64
 
64
 
65
//vraci hloubku stromu
65
//vraci hloubku stromu
66
static int extreltree_test_height(extavltree_node_t *node,bool timeout)
66
static int extreltree_test_height(extavlreltree_node_t *node,bool timeout)
67
{
67
{
68
    int h1, h2;
68
    int h1, h2;
69
 
69
 
70
    if (!node) return -1;
70
    if (!node) return -1;
71
 
71
 
Line 88... Line 88...
88
/** Tests par atribute of every tree node
88
/** Tests par atribute of every tree node
89
 *
89
 *
90
 */
90
 */
91
static extavlreltree_node_t *extreltree_test_parents(extavlreltree_node_t *node)
91
static extavlreltree_node_t *extreltree_test_parents(extavlreltree_node_t *node)
92
{
92
{
93
    extavltree_node_t *tmp;
93
    extavlreltree_node_t *tmp;
94
   
94
   
95
    if (!node) return NULL;
95
    if (!node) return NULL;
96
 
96
 
97
    if (node->lft) {
97
    if (node->lft) {
98
        tmp = extreltree_test_parents(node->lft);
98
        tmp = extreltree_test_parents(node->lft);
Line 110... Line 110...
110
}
110
}
111
 
111
 
112
/** Checks list of nodes
112
/** Checks list of nodes
113
 *
113
 *
114
 */
114
 */
115
static bool extreltree_test_link(int node_count)
115
static bool extreltree_test_link(extavlreltree_t *tree, int node_count)
116
{
116
{
117
    extavltree_node_t *node;
117
    extavlreltree_node_t *node;
118
    int i;
118
    int i;
119
    bool test_link = true;
119
    bool test_link = true;
120
 
120
 
121
    for (i = 0,node = exttree.head.next; node != &(exttree.head); i++,node = node->next)
121
    for (i = 0,node = tree->head.next; node != &(tree->head); i++,node = node->next)
122
        ;
122
        ;
123
    if (node_count && i != node_count) {
123
    if (i != node_count) {
124
        printf("\nBad node count!!! Counted: %d, right number: %d", i, node_count);
124
        printf("\nBad node count!!! Counted: %d, right number: %d", i, node_count);
125
        test_link = false;
125
        test_link = false;
126
    }
126
    }
127
    for (i = 0,node = exttree.head.prev; node != &(exttree.head); i++,node = node->prev)
127
    for (i = 0,node = tree->head.prev; node != &(tree->head); i++,node = node->prev)
128
        ;
128
        ;
129
    if (node_count && i != node_count) {
129
    if (i != node_count) {
130
        printf("\nBad node count!!! Counted: %d, right number: %d", i, node_count);
130
        printf("\nBad node count!!! Counted: %d, right number: %d", i, node_count);
131
        test_link = false;
131
        test_link = false;
132
    }
132
    }
133
    return test_link;
133
    return test_link;
134
}
134
}
Line 143... Line 143...
143
        sum += _sum_extreltree(node->lft);
143
        sum += _sum_extreltree(node->lft);
144
   
144
   
145
    return sum + node->key;
145
    return sum + node->key;
146
}
146
}
147
 
147
 
148
static int extreltree_test_maxsum(extavlreltree_node_t *node)
148
static int extreltree_test_rgtsum(extavlreltree_node_t *node)
149
{
149
{
150
    int rs, ls;
150
    int rs, ls;
151
   
151
   
152
    if (!node) return 0;
152
    if (!node) return 0;
153
   
153
   
154
    rs = extreltree_test_maxsum(node->rgt);
154
    rs = extreltree_test_rgtsum(node->rgt);
155
    ls = extreltree_test_maxsum(node->lft);
155
    ls = extreltree_test_rgtsum(node->lft);
156
 
156
 
157
    if (rs != node->rgt_max_key) {
157
    if (rs != node->rgt_sum) {
158
        printf("\nBad RGT_SUM: %d, should be: %d node: %d, address: %p\n",node->rgt_max_key,rs,node->key,node);
158
        printf("\nBad RGT_SUM: %d, should be: %d node: %d, address: %p\n",node->rgt_sum,rs,node->key,node);
159
        getchar();
-
 
160
    }
159
    }
161
    return rs + ls + node->key;
160
    return rs + ls + node->key;
162
}
161
}
163
 
162
 
164
/** Prints the structure of node, which is level levels from the top of the tree.
163
/** Prints the structure of node, which is level levels from the top of the tree.
Line 179... Line 178...
179
        return;
178
        return;
180
   
179
   
181
    for (tmp = node->next,i = 0; (tmp->key == 0) && (tmp != &(extreltree.head)); tmp = tmp->next,i++)
180
    for (tmp = node->next,i = 0; (tmp->key == 0) && (tmp != &(extreltree.head)); tmp = tmp->next,i++)
182
        ;
181
        ;
183
 
182
 
184
    printf ("%d[%d,%d]", node->key,node->rgt_max_key,i);
183
    printf ("%d[%d,%d]", node->key,node->rgt_sum,i);
185
    if (node->lft != NULL || node->rgt != NULL)
184
    if (node->lft != NULL || node->rgt != NULL)
186
    {
185
    {
187
        putchar ('(');
186
        printf ("(");
188
 
187
 
189
        print_extreltree_structure_flat (node->lft, level + 1);
188
        print_extreltree_structure_flat (node->lft, level + 1);
190
        if (node->rgt != NULL)
189
        if (node->rgt != NULL)
191
        {
190
        {
192
            putchar (',');
191
            printf(",");
193
            print_extreltree_structure_flat (node->rgt, level + 1);
192
            print_extreltree_structure_flat (node->rgt, level + 1);
194
        }
193
        }
195
 
194
 
196
        putchar (')');
195
        printf(")");
197
    }
196
    }
198
}
197
}
199
 
198
 
200
/** Prints list of nodes
199
/** Prints list of nodes
201
 *
200
 *
202
 */
201
 */
203
static void print_extreltree_link(int node_count)
202
static void print_extreltree_link(extavlreltree_t *tree)
204
{
203
{
205
    extavltree_node_t *node;
204
    extavlreltree_node_t *node;
206
    printf("\n");
205
    printf("\n");
207
    for (node = exttree.head.next; node != &(exttree.head); node = node->next) {
206
    for (node = tree->head.next; node != &(tree->head); node = node->next) {
208
        printf(" %d,",node->key);
207
        printf(" %d,",node->key);
209
    }
208
    }
210
    for (node = exttree.head.prev; node != &(exttree.head); node = node->prev) {
209
    for (node = tree->head.prev; node != &(tree->head); node = node->prev) {
211
        printf(" %d,",node->key);
210
        printf(" %d,",node->key);
212
    }
211
    }
213
}
212
}
214
 
213
 
215
//****************************************************************
214
//****************************************************************
216
static void alloc_extavltree_node_prepare(void)
215
static void alloc_extavlreltree_node_prepare(void)
217
{
216
{
218
    int i;
217
    int i;
219
 
218
 
220
    for (i = 0; i < NODE_COUNT - 1; i++) {
219
    for (i = 0; i < NODE_COUNT - 1; i++) {
221
        extavlreltree_nodes[i].next = &(extavlreltree_nodes[i+1]);
220
        extavlreltree_nodes[i].next = &(extavlreltree_nodes[i+1]);
Line 278... Line 277...
278
    /*
277
    /*
279
     * Initialize tree before using.
278
     * Initialize tree before using.
280
     */
279
     */
281
    extavlreltree_create(tree);
280
    extavlreltree_create(tree);
282
   
281
   
283
    if (!quiet) printf("\n\nInserting %d nodes ...\n", node_count);
282
    if (!quiet) printf("\nInserting %d nodes ...\n", node_count);
284
 
283
 
285
    for (i = 0; i < node_count; i++) {
284
    for (i = 0; i < node_count; i++) {
286
        newnode = alloc_extavlreltree_node();
285
        newnode = alloc_extavlreltree_node();
287
        //if (!quiet) printf("[[[%d]]]\n",newnode->key);
286
        //if (!quiet) printf("[[[%d]]]\n",newnode->key);
288
       
287
       
289
        extavlreltree_insert(tree, newnode);
288
        extavlreltree_insert(tree, newnode);
290
        if (!quiet) {
289
        if (!quiet) {
291
            if (!extreltree_test_link(i+1)) {
290
            if (!extreltree_test_link(tree,i+1)) {
292
                print_extreltree_link(i+1);
291
                print_extreltree_link(tree);
293
                printf("\n");
292
                printf("\n");
294
            }
293
            }
295
            extreltree_test_parents(tree->root);
294
            extreltree_test_parents(tree->root);
296
            extreltree_test_height(tree->root,1);
295
            extreltree_test_height(tree->root,1);
297
            extreltree_test_rgtsum(tree->root);
296
            extreltree_test_rgtsum(tree->root);
Line 309... Line 308...
309
    //aktualni pocet tiku:
308
    //aktualni pocet tiku:
310
    if (!quiet) printf("Deleting tree...\n");
309
    if (!quiet) printf("Deleting tree...\n");
311
 
310
 
312
    switch(node_position) {
311
    switch(node_position) {
313
        case 0: //mazani vzdy korene
312
        case 0: //mazani vzdy korene
314
            if (!quiet) printf("\n\nDelete root nodes\n");
313
            if (!quiet) printf("\nDelete root nodes\n");
315
            i = node_count;
314
            i = node_count - 1;
316
            while(tree->root != NULL) {
315
            while(tree->root != NULL) {
317
                delnode = tree->root;
316
                delnode = tree->root;
318
                extavlreltree_delete(tree,delnode);
317
                extavlreltree_delete(tree,delnode);
319
                if (!quiet) {
318
                if (!quiet) {
320
                    if (!extreltree_test_link(i)) {
319
                    if (!extreltree_test_link(tree,i)) {
321
                        print_extreltree_link(i);
320
                        print_extreltree_link(tree);
322
                        printf("\n");
321
                        printf("\n");
323
                    }
322
                    }
324
                    extreltree_test_parents(tree->root);
323
                    extreltree_test_parents(tree->root);
325
                    extreltree_test_height(tree->root,1);
324
                    extreltree_test_height(tree->root,1);
326
                    extreltree_test_rgtsum(tree->root);
325
                    extreltree_test_rgtsum(tree->root);
Line 328... Line 327...
328
                i--;
327
                i--;
329
            }
328
            }
330
           
329
           
331
            break;
330
            break;
332
        case 1:
331
        case 1:
333
            if (!quiet) printf("\n\nDelete nodes according to their time of origin\n");
332
            if (!quiet) printf("\nDelete nodes according to their time of origin\n");
334
            for (i = 0; i < node_count; i++) {
333
            for (i = 0; i < node_count; i++) {
335
                extavlreltree_delete(tree,&(extavlreltree_nodes[i]));
334
                extavlreltree_delete(tree,&(extavlreltree_nodes[i]));
336
                if (!quiet) {
335
                if (!quiet) {
337
                    if (!extreltree_test_link(i+1)) {
336
                    if (!extreltree_test_link(tree,node_count-i-1)) {
338
                        print_extreltree_link(i+1);
337
                        print_extreltree_link(tree);
339
                        printf("\n");
338
                        printf("\n");
340
                    }
339
                    }
341
                    extreltree_test_parents(tree->root);
340
                    extreltree_test_parents(tree->root);
342
                    extreltree_test_height(tree->root,1);
341
                    extreltree_test_height(tree->root,1);
343
                    extreltree_test_rgtsum(tree->root);
342
                    extreltree_test_rgtsum(tree->root);
Line 350... Line 349...
350
    if (!quiet) printf("Deletion was finished\n");
349
    if (!quiet) printf("Deletion was finished\n");
351
}
350
}
352
 
351
 
353
static void timeout_extreltree(extavlreltree_t *tree, int node_count, bool quiet)
352
static void timeout_extreltree(extavlreltree_t *tree, int node_count, bool quiet)
354
{
353
{
355
    int i = node_count;
354
    int i = node_count - 1;
356
   
355
   
357
    if (!quiet) printf("Timeout tree ...\n");
356
    if (!quiet) printf("\nTimeout tree ...\n");
358
   
357
   
359
    while(tree->head.next != &(tree->head)) {
358
    while(tree->head.next != &(tree->head)) {
360
        extavlreltree_delete_min(tree);
359
        extavlreltree_delete_min(tree);
361
        if (!quiet) {
360
        if (!quiet) {
362
            if (!extreltree_test_link(i)) {
361
            if (!extreltree_test_link(tree,i)) {
363
                print_extreltree_link(i);
362
                print_extreltree_link(tree);
364
                printf("\n");
363
                printf("\n");
365
            }
364
            }
366
            extreltree_test_parents(tree->root);
365
            extreltree_test_parents(tree->root);
367
            extreltree_test_height(tree->root,1);
366
            extreltree_test_height(tree->root,1);
368
            extreltree_test_rgtsum(tree->root);
367
            extreltree_test_rgtsum(tree->root);
369
        }
368
        }
370
        i--;
369
        i--;
371
    }
370
    }
372
 
371
 
373
    if (!quiet && (i != node_count)) printf("Bad node count. Some nodes have been lost!");
372
    if (!quiet && (i != -1)) printf("Bad node count. Some nodes have been lost!\n");
374
 
373
 
375
    if (!quiet) printf("Timeout tree finished\n");
374
    if (!quiet) printf("Timeout tree finished\n");
376
}
375
}
377
 
376
 
378
char * test_extavlreltree1(bool quiet)
377
char * test_extavlreltree1(bool quiet)