Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b5e5997d1466d1f470a56edf204b342f844fcf68
[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     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     /*     printf("%lg" " ", xbt_heap_maxkey(heap)); */
60     if (xbt_heap_maxkey(heap) != tab[i]) {
61       fprintf(stderr, "Problem !\n");
62       exit(1);
63     }
64
65     xbt_heap_pop(heap);
66   }
67
68   xbt_heap_free(heap);
69   free(tab);
70   printf("Validity test complete!\n");
71 }
72
73 void test_heap_mean_operation(int size)
74 {
75   xbt_heap_t heap = xbt_heap_new(size, NULL);
76   double val;
77   double date = 0;
78   int i, j;
79
80   date = gras_os_time() * 1000000;
81
82   for (i = 0; i < size; i++)
83     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
84
85   date = gras_os_time() * 1000000 - date;
86
87   printf("Creation time  %d size heap : %g\n", size, date);
88
89   date = gras_os_time() * 1000000;
90
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
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 #pragma argsused
121
122 int main(int argc, char **argv)
123 {
124   int size;
125
126   for (size = 100; size < 10000; size *= 10) {
127     test_heap_validity(size);
128     test_heap_mean_operation(size);
129   }
130   return 0;
131 }
132
133 //---------------------------------------------------------------------------