]> git.meshlink.io Git - catta/blob - libavahi-core/prioq-test.c
autotoolize
[catta] / libavahi-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 #include <time.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include "prioq.h"
27
28 static gint compare_int(gconstpointer a, gconstpointer b) {
29     gint i = GPOINTER_TO_INT(a), j = GPOINTER_TO_INT(b);
30
31     return i < j ? -1 : (i > j ? 1 : 0);
32 }
33
34 static int compare_ptr(gconstpointer a, gconstpointer b) {
35     return a < b ? -1 : (a > b ? 1 : 0);
36 }
37
38 static void rec(AvahiPrioQueueNode *n) {
39     if (!n)
40         return;
41
42     if (n->left)
43         g_assert(n->left->parent == n);
44
45     if (n->right)
46         g_assert(n->right->parent == n);
47
48     if (n->parent) {
49         g_assert(n->parent->left == n || n->parent->right == n);
50
51         if (n->parent->left == n)
52             g_assert(n->next == n->parent->right);
53     }
54
55     if (!n->next) {
56         g_assert(n->queue->last == n);
57
58         if (n->parent && n->parent->left == n)
59             g_assert(n->parent->right == NULL);
60     }
61
62     
63     if (n->parent) {
64         int a = GPOINTER_TO_INT(n->parent->data), b = GPOINTER_TO_INT(n->data);
65         if (a > b) {
66             printf("%i <= %i: NO\n", a, b);
67             abort();
68         }
69     }
70
71     rec(n->left);
72     rec(n->right);
73 }
74
75 int main(int argc, char *argv[]) {
76     AvahiPrioQueue *q, *q2;
77     gint i;
78
79     q = avahi_prio_queue_new(compare_int);
80     q2 = avahi_prio_queue_new(compare_ptr);
81
82     srand(time(NULL));
83
84     for (i = 0; i < 10000; i++)
85         avahi_prio_queue_put(q2, avahi_prio_queue_put(q, GINT_TO_POINTER(random() & 0xFFFF)));
86
87     while (q2->root) {
88         rec(q->root);
89         rec(q2->root);
90
91         g_assert(q->n_nodes == q2->n_nodes);
92
93         printf("%i\n", GPOINTER_TO_INT(((AvahiPrioQueueNode*)q2->root->data)->data));
94         
95         avahi_prio_queue_remove(q, q2->root->data);
96         avahi_prio_queue_remove(q2, q2->root);
97     }
98
99         
100 /*     prev = 0; */
101 /*     while (q->root) { */
102 /*         gint v = GPOINTER_TO_INT(q->root->data); */
103 /*         rec(q->root); */
104 /*         printf("%i\n", v); */
105 /*         avahi_prio_queue_remove(q, q->root); */
106 /*         g_assert(v >= prev); */
107 /*         prev = v; */
108 /*     } */
109
110     avahi_prio_queue_free(q);
111     return 0;
112 }