]> git.meshlink.io Git - catta/blob - avahi-core/prioq-test.c
rename libavahi-core to avahi-core
[catta] / avahi-core / prioq-test.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <time.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include "prioq.h"
31
32 static gint compare_int(gconstpointer a, gconstpointer b) {
33     gint i = GPOINTER_TO_INT(a), j = GPOINTER_TO_INT(b);
34
35     return i < j ? -1 : (i > j ? 1 : 0);
36 }
37
38 static int compare_ptr(gconstpointer a, gconstpointer b) {
39     return a < b ? -1 : (a > b ? 1 : 0);
40 }
41
42 static void rec(AvahiPrioQueueNode *n) {
43     if (!n)
44         return;
45
46     if (n->left)
47         g_assert(n->left->parent == n);
48
49     if (n->right)
50         g_assert(n->right->parent == n);
51
52     if (n->parent) {
53         g_assert(n->parent->left == n || n->parent->right == n);
54
55         if (n->parent->left == n)
56             g_assert(n->next == n->parent->right);
57     }
58
59     if (!n->next) {
60         g_assert(n->queue->last == n);
61
62         if (n->parent && n->parent->left == n)
63             g_assert(n->parent->right == NULL);
64     }
65
66     
67     if (n->parent) {
68         int a = GPOINTER_TO_INT(n->parent->data), b = GPOINTER_TO_INT(n->data);
69         if (a > b) {
70             printf("%i <= %i: NO\n", a, b);
71             abort();
72         }
73     }
74
75     rec(n->left);
76     rec(n->right);
77 }
78
79 int main(int argc, char *argv[]) {
80     AvahiPrioQueue *q, *q2;
81     gint i;
82
83     q = avahi_prio_queue_new(compare_int);
84     q2 = avahi_prio_queue_new(compare_ptr);
85
86     srand(time(NULL));
87
88     for (i = 0; i < 10000; i++)
89         avahi_prio_queue_put(q2, avahi_prio_queue_put(q, GINT_TO_POINTER(random() & 0xFFFF)));
90
91     while (q2->root) {
92         rec(q->root);
93         rec(q2->root);
94
95         g_assert(q->n_nodes == q2->n_nodes);
96
97         printf("%i\n", GPOINTER_TO_INT(((AvahiPrioQueueNode*)q2->root->data)->data));
98         
99         avahi_prio_queue_remove(q, q2->root->data);
100         avahi_prio_queue_remove(q2, q2->root);
101     }
102
103         
104 /*     prev = 0; */
105 /*     while (q->root) { */
106 /*         gint v = GPOINTER_TO_INT(q->root->data); */
107 /*         rec(q->root); */
108 /*         printf("%i\n", v); */
109 /*         avahi_prio_queue_remove(q, q->root); */
110 /*         g_assert(v >= prev); */
111 /*         prev = v; */
112 /*     } */
113
114     avahi_prio_queue_free(q);
115     return 0;
116 }