Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Usage of xbt_new0 instead calloc
[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 #define MAX_TEST 1000000
22
23 #ifdef __BORLANDC__
24 int _XBT_CALL compare_double(const void *a, const void *b);
25 #else
26 int compare_double(const void *a, const void *b);
27 #endif
28
29 void test_heap_validity(int size);
30 void test_heap_mean_operation(int size);
31 void test_reset_heap(xbt_heap_t heap,int size);
32
33
34 int compare_double(const void *a, const void *b)
35 {
36   double pa, pb;
37
38   pa = *((double *) a);
39   pb = *((double *) b);
40
41   if (pa > pb)
42     return 1;
43   if (pa == pb)
44     return 0;
45   return -1;
46 }
47
48 void test_heap_validity(int size)
49 {
50   xbt_heap_t heap = xbt_heap_new(size, NULL);
51   double *tab = xbt_new0(double,size);
52
53   int i;
54
55   for (i = 0; i < size; i++) {
56     tab[i] = (double)(10.0 * rand() / (RAND_MAX + 1.0));
57     xbt_heap_push(heap, NULL, (double)tab[i]);
58   }
59
60   qsort(tab, size, sizeof(double), compare_double);
61
62   for (i = 0; i < size; i++) {
63     /*     printf("%lg" " ", xbt_heap_maxkey(heap)); */
64     if (xbt_heap_maxkey(heap) != tab[i]) {
65       fprintf(stderr, "Problem !\n");
66       exit(1);
67     }
68     xbt_heap_pop(heap);
69   }
70   xbt_heap_free(heap);
71   free(tab);
72   printf("Validity test complete!\n");
73 }
74
75 void test_heap_mean_operation(int size)
76 {
77   xbt_heap_t heap = xbt_heap_new(size, NULL);
78   double val;
79   double date = 0;
80   int i, j;
81
82   date = gras_os_time() * 1000000;
83   for (i = 0; i < size; i++)
84     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
85
86   date = gras_os_time() * 1000000 - date;
87   printf("Creation time  %d size heap : %g\n", size, date);
88
89   date = gras_os_time() * 1000000;
90   for (j = 0; j < MAX_TEST; j++) {
91     
92     if(!(j%size) && j)
93       test_reset_heap(heap,size);
94     
95     val = xbt_heap_maxkey(heap);
96     xbt_heap_pop(heap);
97     xbt_heap_push(heap, NULL, 3.0 * val);
98   }
99   date = gras_os_time() * 1000000 - date;
100   printf("Mean access time for a %d size heap : %g\n", size,
101          date * 1.0 / (MAX_TEST + 0.0));
102
103   xbt_heap_free(heap);
104 }
105
106 void test_reset_heap(xbt_heap_t heap,int size)
107 {
108   int i;
109   xbt_heap_free(heap);
110   heap = xbt_heap_new(size, NULL);
111
112   for (i = 0; i < size; i++){
113     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
114   }
115
116 }
117
118 #ifdef __BORLANDC__
119 #pragma argsused
120 #endif 
121
122 int main(int argc, char **argv)
123 {
124   int size;
125   for (size = 100; size < 10000; size *= 10) {
126     test_heap_validity(size);
127     test_heap_mean_operation(size);
128   }
129   return 0;
130 }