Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
surf:~/Work/GRAS/heap $ valgrind --leak-check=yes --show-reachable=yes ./msg_test les
[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
22 int compare_double(const void *a, const void *b)
23 {
24   double pa, pb;
25
26   pa = *((double *) a);
27   pb = *((double *) b);
28
29   if (pa > pb)
30     return 1;
31   if (pa == pb)
32     return 0;
33   return -1;
34 }
35
36 void test_heap_validity(int size)
37 {
38   xbt_heap_t heap = xbt_heap_new(size, NULL);
39   double *tab = calloc(size, sizeof(double));
40   int i;
41
42   for (i = 0; i < size; i++) {
43     tab[i] = (10.0 * rand() / (RAND_MAX + 1.0));
44     xbt_heap_push(heap, NULL, tab[i]);
45   }
46
47   qsort(tab, size, sizeof(double), compare_double);
48
49   for (i = 0; i < size; i++) {
50     /*     printf("%lg" " ", xbt_heap_maxkey(heap)); */
51     if (xbt_heap_maxkey(heap) != tab[i]) {
52       fprintf(stderr, "Problem !\n");
53       exit(1);
54     }
55     xbt_heap_pop(heap);
56   }
57   xbt_heap_free(heap);
58   free(tab);
59   printf("Validity test complete!\n");
60 }
61
62 void test_heap_mean_operation(int size)
63 {
64   xbt_heap_t heap = xbt_heap_new(size, NULL);
65   double val;
66   double date = 0;
67   int i, j;
68
69   date = gras_os_time() * 1000000;
70   for (i = 0; i < size; i++)
71     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
72   date = gras_os_time() * 1000000 - date;
73   printf("Creation time  %d size heap : %lg\n", size, date);
74
75   date = gras_os_time() * 1000000;
76   for (j = 0; j < MAX_TEST; j++) {
77     val = xbt_heap_maxkey(heap);
78     xbt_heap_pop(heap);
79     xbt_heap_push(heap, NULL, 3.0 * val);
80   }
81   date = gras_os_time() * 1000000 - date;
82   printf("Mean access time for a %d size heap : %lg\n", size,
83          date * 1.0 / (MAX_TEST + 0.0));
84
85   xbt_heap_free(heap);
86 }
87
88 int main(int argc, char **argv)
89 {
90   int size;
91   for (size = 100; size < 10000; size *= 10) {
92     test_heap_validity(size);
93     test_heap_mean_operation(size);
94   }
95   return 0;
96 }