Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a few more cosmetics
[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 <xbt/xbt_os_time.h>
11
12 #include "xbt/heap.h"
13 #include "xbt/sysdep.h"         /* calloc, printf */
14
15 #define MAX_TEST 1000000
16
17 static int compare_double(const void *a, const void *b)
18 {
19   double pa = *((double *) a);
20   double pb = *((double *) b);
21
22   if (pa > pb)
23     return 1;
24   if (pa == pb)
25     return 0;
26   return -1;
27 }
28
29 static void test_heap_validity(int size)
30 {
31   xbt_heap_t heap = xbt_heap_new(size, NULL);
32   double *tab = xbt_new0(double, size);
33   int i;
34
35   for (i = 0; i < size; i++) {
36     tab[i] = (double) (10.0 * rand() / (RAND_MAX + 1.0));
37     xbt_heap_push(heap, NULL, (double) tab[i]);
38   }
39
40   qsort(tab, size, sizeof(double), compare_double);
41
42   for (i = 0; i < size; i++) {
43     /*     printf("%g" " ", xbt_heap_maxkey(heap)); */
44     if (xbt_heap_maxkey(heap) != tab[i]) {
45       fprintf(stderr, "Problem !\n");
46       exit(1);
47     }
48     xbt_heap_pop(heap);
49   }
50   xbt_heap_free(heap);
51   free(tab);
52   printf("Validity test complete!\n");
53 }
54
55 static void test_heap_mean_operation(int size)
56 {
57   xbt_heap_t heap = xbt_heap_new(size, NULL);
58
59   double date = xbt_os_time() * 1000000;
60   for (int i = 0; i < size; i++)
61     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
62
63   date = xbt_os_time() * 1000000 - date;
64   printf("Creation time  %d size heap : %g\n", size, date);
65
66   date = xbt_os_time() * 1000000;
67   for (int j = 0; j < MAX_TEST; j++) {
68
69     if (!(j % size) && j)
70       test_reset_heap(&heap, size);
71
72     double val = xbt_heap_maxkey(heap);
73     xbt_heap_pop(heap);
74     xbt_heap_push(heap, NULL, 3.0 * val);
75   }
76   date = xbt_os_time() * 1000000 - date;
77   printf("Mean access time for a %d size heap : %g\n", size, date * 1.0 / (MAX_TEST + 0.0));
78
79   xbt_heap_free(heap);
80 }
81
82 static void test_reset_heap(xbt_heap_t * heap, int size)
83 {
84   xbt_heap_free(*heap);
85   *heap = xbt_heap_new(size, NULL);
86
87   for (int i = 0; i < size; i++) {
88     xbt_heap_push(*heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
89   }
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 }