Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
resource file of context usage project
[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 #include <stdlib.h>
11 #include <stdio.h>
12
13 #include "xbt/heap.h"
14 #include "gras/virtu.h"         /* time manipulation in bench */
15
16 #define MAX_TEST 1000000
17
18 int compare_double(const void *a, const void *b);
19 void test_heap_validity(int size);
20 void test_heap_mean_operation(int size);
21 void test_reset_heap(xbt_heap_t heap,int size);
22
23
24 int compare_double(const void *a, const void *b)
25 {
26   double pa, pb;
27
28   pa = *((double *) a);
29   pb = *((double *) b);
30
31   if (pa > pb)
32     return 1;
33   if (pa == pb)
34     return 0;
35   return -1;
36 }
37
38 void test_heap_validity(int size)
39 {
40   xbt_heap_t heap = xbt_heap_new(size, NULL);
41   double *tab = calloc(size, sizeof(double));
42   int i;
43
44   for (i = 0; i < size; i++) {
45     tab[i] = (10.0 * rand() / (RAND_MAX + 1.0));
46     xbt_heap_push(heap, NULL, tab[i]);
47   }
48
49   qsort(tab, size, sizeof(double), compare_double);
50
51   for (i = 0; i < size; i++) {
52     /*     printf("%lg" " ", xbt_heap_maxkey(heap)); */
53     if (xbt_heap_maxkey(heap) != tab[i]) {
54       fprintf(stderr, "Problem !\n");
55       exit(1);
56     }
57     xbt_heap_pop(heap);
58   }
59   xbt_heap_free(heap);
60   free(tab);
61   printf("Validity test complete!\n");
62 }
63
64 void test_heap_mean_operation(int size)
65 {
66   xbt_heap_t heap = xbt_heap_new(size, NULL);
67   double val;
68   double date = 0;
69   int i, j;
70
71   date = gras_os_time() * 1000000;
72   for (i = 0; i < size; i++)
73     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
74   date = gras_os_time() * 1000000 - date;
75   printf("Creation time  %d size heap : %g\n", size, date);
76
77   date = gras_os_time() * 1000000;
78   for (j = 0; j < MAX_TEST; j++) {
79     
80     if(!(j%size) && j)
81       test_reset_heap(heap,size);
82     
83     val = xbt_heap_maxkey(heap);
84     xbt_heap_pop(heap);
85     xbt_heap_push(heap, NULL, 3.0 * val);
86   }
87   date = gras_os_time() * 1000000 - date;
88   printf("Mean access time for a %d size heap : %g\n", size,
89          date * 1.0 / (MAX_TEST + 0.0));
90
91   xbt_heap_free(heap);
92 }
93
94 void test_reset_heap(xbt_heap_t heap,int size)
95 {
96   int i;
97   xbt_heap_free(heap);
98   heap = xbt_heap_new(size, NULL);
99
100   for (i = 0; i < size; i++){
101     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
102   }
103
104 }
105
106 int main(int argc, char **argv)
107 {
108   int size;
109   for (size = 100; size < 10000; size *= 10) {
110     test_heap_validity(size);
111     test_heap_mean_operation(size);
112   }
113   return 0;
114 }