aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_container_heap.c
blob: 9e6a29ea487fb4183cc29d83024e1a08f7b05c46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 This file is part of GNUnet.
 (C) 2008 Christian Grothoff (and other contributing authors)

 GNUnet is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published
 by the Free Software Foundation; either version 2, or (at your
 option) any later version.

 GNUnet is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNUnet; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 */

/**
 * @author Nathan Evans
 * @file util/test_container_heap.c
 * @brief Test of heap operations
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_container_lib.h"

struct TestItem
{
  unsigned int cost;
};

static int
iterator_callback (void *cls, void *element, GNUNET_CONTAINER_HeapCost cost)
{
  struct TestItem *node;
  node = (struct TestItem *) element;
#ifdef VERBOSE
  fprintf (stdout, "%d\n", node->cost);
#endif

  return GNUNET_OK;
}

int
main (int argc, char **argv)
{
  struct GNUNET_CONTAINER_Heap *myHeap;
  struct TestItem neighbor1;
  struct TestItem neighbor2;
  struct TestItem neighbor3;
  struct TestItem neighbor4;
  struct TestItem neighbor5;
  struct TestItem neighbor6;

  GNUNET_log_setup ("test-container-heap", "WARNING", NULL);

  myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);

  neighbor1.cost = 60;
  neighbor2.cost = 50;
  neighbor3.cost = 70;
  neighbor4.cost = 120;
  neighbor5.cost = 100;
  neighbor6.cost = 30;

  GNUNET_CONTAINER_heap_insert (myHeap, &neighbor1, neighbor1.cost);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_insert (myHeap, &neighbor2, neighbor2.cost);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_insert (myHeap, &neighbor3, neighbor3.cost);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_insert (myHeap, &neighbor4, neighbor4.cost);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_insert (myHeap, &neighbor5, neighbor5.cost);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_insert (myHeap, &neighbor6, neighbor6.cost);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_remove_node (myHeap, &neighbor5);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_remove_root (myHeap);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_update_cost (myHeap, &neighbor6, 200);
  GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL);
  GNUNET_CONTAINER_heap_destroy (myHeap);

  return 0;
}

/* end of test_container_heap.c */