7a23855c5ee96686bdbdc4c551969956d7189672
[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
49 int
50 main (int argc, char **argv)
51 {
52   struct GNUNET_CONTAINER_Heap *myHeap;
53   struct TestItem neighbor1;
54   struct TestItem neighbor2;
55   struct TestItem neighbor3;
56   struct TestItem neighbor4;
57   struct TestItem neighbor5;
58   struct TestItem neighbor6;
59
60   GNUNET_log_setup ("test-container-heap", "WARNING", NULL);
61
62   myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
63
64   neighbor1.cost = 60;
65   neighbor2.cost = 50;
66   neighbor3.cost = 70;
67   neighbor4.cost = 120;
68   neighbor5.cost = 100;
69   neighbor6.cost = 30;
70
71   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor1, neighbor1.cost);
72   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
73   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor2, neighbor2.cost);
74   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
75   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor3, neighbor3.cost);
76   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
77   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor4, neighbor4.cost);
78   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
79   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor5, neighbor5.cost);
80   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
81   GNUNET_CONTAINER_heap_insert (myHeap, &neighbor6, neighbor6.cost);
82   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
83   GNUNET_CONTAINER_heap_remove_node (myHeap, &neighbor5);
84   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
85   GNUNET_CONTAINER_heap_remove_root (myHeap);
86   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
87   GNUNET_CONTAINER_heap_update_cost (myHeap, &neighbor6, 200);
88   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
89   GNUNET_CONTAINER_heap_destroy (myHeap);
90
91   return 0;
92 }
93
94 /* end of test_container_heap.c */