b24624ad8c194c2788a529c341d9acf8bae3849e
[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 static int
32 iterator_callback (void *cls,
33                    struct GNUNET_CONTAINER_HeapNode *node,
34                    void *element, 
35                    GNUNET_CONTAINER_HeapCostType cost)
36 {
37   return GNUNET_OK;
38 }
39
40
41 static int
42 check ()
43 {
44   struct GNUNET_CONTAINER_Heap *myHeap;
45   struct GNUNET_CONTAINER_HeapNode *n1;
46   struct GNUNET_CONTAINER_HeapNode *n2;
47   struct GNUNET_CONTAINER_HeapNode *n3;
48   struct GNUNET_CONTAINER_HeapNode *n4;
49   struct GNUNET_CONTAINER_HeapNode *n5;
50   struct GNUNET_CONTAINER_HeapNode *n6;
51
52   myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
53   n1 = GNUNET_CONTAINER_heap_insert (myHeap, "11", 11);
54   GNUNET_assert (NULL != n1);
55   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
56   GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
57   n2 = GNUNET_CONTAINER_heap_insert (myHeap, "78", 78);
58   GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
59   GNUNET_assert (0 == strcmp ("78",
60                               GNUNET_CONTAINER_heap_remove_node (myHeap, n2)));
61   GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
62   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
63
64   n3 = GNUNET_CONTAINER_heap_insert (myHeap, "15", 5);
65   GNUNET_CONTAINER_heap_update_cost (myHeap, n3, 15);
66   GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
67   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
68
69   n4 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
70   GNUNET_CONTAINER_heap_update_cost (myHeap, n4, 0);
71   GNUNET_assert (3 == GNUNET_CONTAINER_heap_get_size (myHeap));
72   GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
73
74   n5 = GNUNET_CONTAINER_heap_insert (myHeap, "100", 100);
75   n6 = GNUNET_CONTAINER_heap_insert (myHeap, "30/200", 30);
76   GNUNET_assert (5 == GNUNET_CONTAINER_heap_get_size (myHeap));
77   GNUNET_CONTAINER_heap_remove_node (myHeap, n5);
78   GNUNET_assert (0 == strcmp ("11",
79                               GNUNET_CONTAINER_heap_remove_root (myHeap))); /* n1 */
80   GNUNET_CONTAINER_heap_update_cost (myHeap, n6, 200);
81   GNUNET_CONTAINER_heap_remove_node (myHeap, n3); 
82   GNUNET_assert (0 == strcmp ("50",
83                               GNUNET_CONTAINER_heap_remove_root (myHeap))); /* n4 */
84   GNUNET_assert (0 == strcmp ("30/200",
85                               GNUNET_CONTAINER_heap_remove_root (myHeap))); /* n6 */
86   GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (myHeap));
87   GNUNET_CONTAINER_heap_destroy (myHeap);
88   return 0;
89 }
90
91
92 int
93 main (int argc, char **argv)
94 {
95   GNUNET_log_setup ("test-container-heap", "WARNING", NULL);
96   return check();
97 }
98
99 /* end of test_container_heap.c */