9e6a29ea487fb4183cc29d83024e1a08f7b05c46
[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/test_container_heap.c
24  * @brief Test of heap operations
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30
31 struct TestItem
32 {
33   unsigned int cost;
34 };
35
36 static int
37 iterator_callback (void *cls, void *element, GNUNET_CONTAINER_HeapCost cost)
38 {
39   struct TestItem *node;
40   node = (struct TestItem *) element;
41 #ifdef VERBOSE
42   fprintf (stdout, "%d\n", node->cost);
43 #endif
44
45   return GNUNET_OK;
46 }
47
48 int
49 main (int argc, char **argv)
50 {
51   struct GNUNET_CONTAINER_Heap *myHeap;
52   struct TestItem neighbor1;
53   struct TestItem neighbor2;
54   struct TestItem neighbor3;
55   struct TestItem neighbor4;
56   struct TestItem neighbor5;
57   struct TestItem neighbor6;
58
59   GNUNET_log_setup ("test-container-heap", "WARNING", NULL);
60
61   myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
62
63   neighbor1.cost = 60;
64   neighbor2.cost = 50;
65   neighbor3.cost = 70;
66   neighbor4.cost = 120;
67   neighbor5.cost = 100;
68   neighbor6.cost = 30;
69
70   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor1, neighbor1.cost);
71   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
72   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor2, neighbor2.cost);
73   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
74   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor3, neighbor3.cost);
75   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
76   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor4, neighbor4.cost);
77   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
78   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor5, neighbor5.cost);
79   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
80   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor6, neighbor6.cost);
81   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
82   GNUNET_CONTAINER_heap_remove_node (myHeap, &neighbor5);
83   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
84   GNUNET_CONTAINER_heap_remove_root (myHeap);
85   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
86   GNUNET_CONTAINER_heap_update_cost (myHeap, &neighbor6, 200);
87   GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
88   GNUNET_CONTAINER_heap_destroy (myHeap);
89
90   return 0;
91 }
92
93 /* end of test_container_heap.c */