3 /* A few tests for the xbt_heap module */
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
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. */
19 #include "gras/virtu.h" /* time manipulation in bench */
21 #include "xbt/sysdep.h" /* calloc, printf */
23 #define MAX_TEST 1000000
26 int _XBT_CALL compare_double(const void *a, const void *b);
28 int compare_double(const void *a, const void *b);
31 void test_heap_validity(int size);
32 void test_heap_mean_operation(int size);
33 void test_reset_heap(xbt_heap_t heap,int size);
36 int compare_double(const void *a, const void *b)
50 void test_heap_validity(int size)
52 xbt_heap_t heap = xbt_heap_new(size, NULL);
53 double *tab = xbt_new0(double,size);
57 for (i = 0; i < size; i++) {
58 tab[i] = (double)(10.0 * rand() / (RAND_MAX + 1.0));
59 xbt_heap_push(heap, NULL, (double)tab[i]);
62 qsort(tab, size, sizeof(double), compare_double);
64 for (i = 0; i < size; i++) {
65 /* printf("%lg" " ", xbt_heap_maxkey(heap)); */
66 if (xbt_heap_maxkey(heap) != tab[i]) {
67 fprintf(stderr, "Problem !\n");
74 printf("Validity test complete!\n");
77 void test_heap_mean_operation(int size)
79 xbt_heap_t heap = xbt_heap_new(size, NULL);
84 date = gras_os_time() * 1000000;
85 for (i = 0; i < size; i++)
86 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
88 date = gras_os_time() * 1000000 - date;
89 printf("Creation time %d size heap : %g\n", size, date);
91 date = gras_os_time() * 1000000;
92 for (j = 0; j < MAX_TEST; j++) {
95 test_reset_heap(heap,size);
97 val = xbt_heap_maxkey(heap);
99 xbt_heap_push(heap, NULL, 3.0 * val);
101 date = gras_os_time() * 1000000 - date;
102 printf("Mean access time for a %d size heap : %g\n", size,
103 date * 1.0 / (MAX_TEST + 0.0));
108 void test_reset_heap(xbt_heap_t heap,int size)
112 heap = xbt_heap_new(size, NULL);
114 for (i = 0; i < size; i++){
115 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
124 int main(int argc, char **argv)
127 for (size = 100; size < 10000; size *= 10) {
128 test_heap_validity(size);
129 test_heap_mean_operation(size);