Logo AND Algorithmique Numérique Distribuée

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