Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3d2c542600b256957493058ef6b59ba1825583f9
[simgrid.git] / teshsuite / xbt / heap_bench / heap_bench.c
1 /* A few tests for the xbt_heap module                                      */
2
3 /* Copyright (c) 2004-2010, 2012-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdio.h>
10 #include <math.h>
11 #include <xbt/xbt_os_time.h>
12
13 #include "xbt/heap.h"
14 #include "xbt/sysdep.h"
15
16 #define MAX_TEST 1000000
17
18 static int compare_double(const void *a, const void *b)
19 {
20   double pa = *((double *) a);
21   double pb = *((double *) b);
22
23   if (pa > pb)
24     return 1;
25   if (pa < pb)
26     return -1;
27   return 0;
28 }
29
30 static void test_reset_heap(xbt_heap_t * heap, int size)
31 {
32   xbt_heap_free(*heap);
33   *heap = xbt_heap_new(size, NULL);
34
35   for (int i = 0; i < size; i++) {
36     xbt_heap_push(*heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
37   }
38 }
39
40 static void test_heap_validity(int size)
41 {
42   xbt_heap_t heap = xbt_heap_new(size, NULL);
43   double *tab = xbt_new0(double, size);
44   int i;
45
46   for (i = 0; i < size; i++) {
47     tab[i] = (double) (10.0 * rand() / (RAND_MAX + 1.0));
48     xbt_heap_push(heap, NULL, (double) tab[i]);
49   }
50
51   qsort(tab, size, sizeof(double), compare_double);
52
53   for (i = 0; i < size; i++) {
54     if (fabs(xbt_heap_maxkey(heap) - tab[i]) > 1e-9) {
55       fprintf(stderr, "Problem !\n");
56       exit(1);
57     }
58     xbt_heap_pop(heap);
59   }
60   xbt_heap_free(heap);
61   free(tab);
62   printf("Validity test complete!\n");
63 }
64
65 static void test_heap_mean_operation(int size)
66 {
67   xbt_heap_t heap = xbt_heap_new(size, NULL);
68
69   double date = xbt_os_time() * 1000000;
70   for (int i = 0; i < size; i++)
71     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
72
73   date = xbt_os_time() * 1000000 - date;
74   printf("Creation time  %d size heap : %g\n", size, date);
75
76   date = xbt_os_time() * 1000000;
77   for (int j = 0; j < MAX_TEST; j++) {
78
79     if (!(j % size) && j)
80       test_reset_heap(&heap, size);
81
82     double val = xbt_heap_maxkey(heap);
83     xbt_heap_pop(heap);
84     xbt_heap_push(heap, NULL, 3.0 * val);
85   }
86   date = xbt_os_time() * 1000000 - date;
87   printf("Mean access time for a %d size heap : %g\n", size, date * 1.0 / (MAX_TEST + 0.0));
88
89   xbt_heap_free(heap);
90 }
91
92 int main(int argc, char **argv)
93 {
94   int size;
95   for (size = 100; size < 10000; size *= 10) {
96     test_heap_validity(size);
97     test_heap_mean_operation(size);
98   }
99   return 0;
100 }