Logo AND Algorithmique Numérique Distribuée

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