Logo AND Algorithmique Numérique Distribuée

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