2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @author Nathan Evans
23 * @file util/test_container_heap.c
24 * @brief Test of heap operations
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
32 iterator_callback (void *cls,
33 struct GNUNET_CONTAINER_HeapNode *node,
35 GNUNET_CONTAINER_HeapCostType cost)
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;
52 myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
53 n1 = GNUNET_CONTAINER_heap_insert (myHeap, "11", 11);
54 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
55 GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
56 n2 = GNUNET_CONTAINER_heap_insert (myHeap, "78", 78);
57 GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
58 GNUNET_assert (0 == strcmp ("78",
59 GNUNET_CONTAINER_heap_remove_node (myHeap, n2)));
60 GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
61 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
63 n3 = GNUNET_CONTAINER_heap_insert (myHeap, "15", 5);
64 GNUNET_CONTAINER_heap_update_cost (myHeap, n3, 15);
65 GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
66 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
68 n4 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
69 GNUNET_assert (3 == GNUNET_CONTAINER_heap_get_size (myHeap));
70 GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
72 n5 = GNUNET_CONTAINER_heap_insert (myHeap, "100", 100);
73 n6 = GNUNET_CONTAINER_heap_insert (myHeap, "30/200", 30);
74 GNUNET_assert (5 == GNUNET_CONTAINER_heap_get_size (myHeap));
75 GNUNET_CONTAINER_heap_remove_node (myHeap, n5);
76 GNUNET_assert (0 == strcmp ("11",
77 GNUNET_CONTAINER_heap_remove_root (myHeap))); /* n1 */
78 GNUNET_CONTAINER_heap_update_cost (myHeap, n6, 200);
79 GNUNET_CONTAINER_heap_remove_node (myHeap, n3);
80 GNUNET_assert (0 == strcmp ("50",
81 GNUNET_CONTAINER_heap_remove_root (myHeap))); /* n4 */
82 GNUNET_assert (0 == strcmp ("30/200",
83 GNUNET_CONTAINER_heap_remove_root (myHeap))); /* n6 */
84 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (myHeap));
85 GNUNET_CONTAINER_heap_destroy (myHeap);
91 main (int argc, char **argv)
93 GNUNET_log_setup ("test-container-heap", "WARNING", NULL);
97 /* end of test_container_heap.c */