ng
[oweals/gnunet.git] / src / util / test_container_heap.c
1 /*
2  This file is part of GNUnet.
3  (C) 2008 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 2, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @author Nathan Evans
23  * @file util/containers/heaptest.c
24  * @brief Test of heap operations
25  */
26
27 #include "gnunet_util.h"
28 #include "gnunet_util_containers.h"
29 #include "dv.h"
30
31 static int
32 iterator_callback (void *element, GNUNET_CONTAINER_HeapCost cost,
33                    struct GNUNET_CONTAINER_Heap *root, void *cls)
34 {
35   struct GNUNET_dv_neighbor *node;
36   node = (struct GNUNET_dv_neighbor *) element;
37   fprintf (stdout, "%d\n", node->cost);
38   //fprintf (stdout, "%d\n", ((struct GNUNET_dv_neighbor *)element)->cost);
39
40   return GNUNET_OK;
41 }
42
43
44 int
45 main (int argc, char **argv)
46 {
47   struct GNUNET_CONTAINER_Heap *myHeap;
48   struct GNUNET_dv_neighbor *neighbor1;
49   struct GNUNET_dv_neighbor *neighbor2;
50   struct GNUNET_dv_neighbor *neighbor3;
51   struct GNUNET_dv_neighbor *neighbor4;
52   struct GNUNET_dv_neighbor *neighbor5;
53   struct GNUNET_dv_neighbor *neighbor6;
54
55   GNUNET_log_setup ("test-container-heap", "WARNING", NULL);
56
57   myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
58
59   neighbor1 = malloc (sizeof (struct GNUNET_dv_neighbor));
60   neighbor2 = malloc (sizeof (struct GNUNET_dv_neighbor));
61   neighbor3 = malloc (sizeof (struct GNUNET_dv_neighbor));
62   neighbor4 = malloc (sizeof (struct GNUNET_dv_neighbor));
63   neighbor5 = malloc (sizeof (struct GNUNET_dv_neighbor));
64   neighbor6 = malloc (sizeof (struct GNUNET_dv_neighbor));
65
66   neighbor1->cost = 60;
67   neighbor2->cost = 50;
68   neighbor3->cost = 70;
69   neighbor4->cost = 120;
70   neighbor5->cost = 100;
71   neighbor6->cost = 30;
72
73   GNUNET_CONTAINER_heap_insert (myHeap, neighbor1, neighbor1->cost);
74   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
75
76   fprintf (stdout, "\n");
77   GNUNET_CONTAINER_heap_insert (myHeap, neighbor2, neighbor2->cost);
78
79   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
80   fprintf (stdout, "\n");
81   GNUNET_CONTAINER_heap_insert (myHeap, neighbor3, neighbor3->cost);
82
83   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
84   fprintf (stdout, "\n");
85   GNUNET_CONTAINER_heap_insert (myHeap, neighbor4, neighbor4->cost);
86
87   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
88   fprintf (stdout, "\n");
89   GNUNET_CONTAINER_heap_insert (myHeap, neighbor5, neighbor5->cost);
90
91   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
92   fprintf (stdout, "\n");
93   GNUNET_CONTAINER_heap_insert (myHeap, neighbor6, neighbor6->cost);
94
95   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
96   fprintf (stdout, "\n");
97   GNUNET_CONTAINER_heap_remove_node (myHeap, neighbor5);
98
99   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
100   fprintf (stdout, "\n");
101   GNUNET_CONTAINER_heap_remove_root (myHeap);
102
103   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
104   fprintf (stdout, "\n");
105   GNUNET_CONTAINER_heap_update_cost (myHeap, neighbor6, 200);
106
107   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
108   fprintf (stdout, "\n");
109   return 0;
110 }
111
112 /* end of heaptest.c */