Logo AND Algorithmique Numérique Distribuée

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