Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] fixing tracing tesh'es due to latest changes in link trace identifiers
[simgrid.git] / testsuite / xbt / heap_bench.c
1 /* A few tests for the xbt_heap module                                      */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifdef __BORLANDC__
10 #pragma hdrstop
11 #endif
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <time.h>
16
17 #include "xbt/heap.h"
18 #include "gras/virtu.h"         /* time manipulation in bench */
19
20 #include "xbt/sysdep.h"         /* calloc, printf */
21
22 #define MAX_TEST 1000000
23
24 #ifdef __BORLANDC__
25 int _XBT_CALL compare_double(const void *a, const void *b);
26 #else
27 int compare_double(const void *a, const void *b);
28 #endif
29
30 void test_heap_validity(int size);
31 void test_heap_mean_operation(int size);
32 void test_reset_heap(xbt_heap_t * heap, int size);
33
34
35 int compare_double(const void *a, const void *b)
36 {
37   double pa, pb;
38
39   pa = *((double *) a);
40   pb = *((double *) b);
41
42   if (pa > pb)
43     return 1;
44   if (pa == pb)
45     return 0;
46   return -1;
47 }
48
49 void test_heap_validity(int size)
50 {
51   xbt_heap_t heap = xbt_heap_new(size, NULL);
52   double *tab = xbt_new0(double, size);
53
54   int i;
55
56   for (i = 0; i < size; i++) {
57     tab[i] = (double) (10.0 * rand() / (RAND_MAX + 1.0));
58     xbt_heap_push(heap, NULL, (double) tab[i]);
59   }
60
61   qsort(tab, size, sizeof(double), compare_double);
62
63   for (i = 0; i < size; i++) {
64     /*     printf("%lg" " ", xbt_heap_maxkey(heap)); */
65     if (xbt_heap_maxkey(heap) != tab[i]) {
66       fprintf(stderr, "Problem !\n");
67       exit(1);
68     }
69     xbt_heap_pop(heap);
70   }
71   xbt_heap_free(heap);
72   free(tab);
73   printf("Validity test complete!\n");
74 }
75
76 void test_heap_mean_operation(int size)
77 {
78   xbt_heap_t heap = xbt_heap_new(size, NULL);
79   double val;
80   double date = 0;
81   int i, j;
82
83   date = gras_os_time() * 1000000;
84   for (i = 0; i < size; i++)
85     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
86
87   date = gras_os_time() * 1000000 - date;
88   printf("Creation time  %d size heap : %g\n", size, date);
89
90   date = gras_os_time() * 1000000;
91   for (j = 0; j < MAX_TEST; j++) {
92
93     if (!(j % size) && j)
94       test_reset_heap(&heap, size);
95
96     val = xbt_heap_maxkey(heap);
97     xbt_heap_pop(heap);
98     xbt_heap_push(heap, NULL, 3.0 * val);
99   }
100   date = gras_os_time() * 1000000 - date;
101   printf("Mean access time for a %d size heap : %g\n", size,
102          date * 1.0 / (MAX_TEST + 0.0));
103
104   xbt_heap_free(heap);
105 }
106
107 void test_reset_heap(xbt_heap_t * heap, int size)
108 {
109   int i;
110   xbt_heap_free(*heap);
111   *heap = xbt_heap_new(size, NULL);
112
113   for (i = 0; i < size; i++) {
114     xbt_heap_push(*heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
115   }
116
117 }
118
119 #ifdef __BORLANDC__
120 #pragma argsused
121 #endif
122
123 int main(int argc, char **argv)
124 {
125   int size;
126   for (size = 100; size < 10000; size *= 10) {
127     test_heap_validity(size);
128     test_heap_mean_operation(size);
129   }
130   return 0;
131 }