Logo AND Algorithmique Numérique Distribuée

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