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. */
18 #include "gras/virtu.h" /* time manipulation in bench */
20 #define MAX_TEST 1000000
23 int _XBT_CALL compare_double(const void *a, const void *b);
25 int compare_double(const void *a, const void *b);
28 void test_heap_validity(int size);
29 void test_heap_mean_operation(int size);
30 void test_reset_heap(xbt_heap_t heap,int size);
33 int compare_double(const void *a, const void *b)
47 void test_heap_validity(int size)
49 xbt_heap_t heap = xbt_heap_new(size, NULL);
50 double *tab = calloc(size, sizeof(double));
53 for (i = 0; i < size; i++) {
54 tab[i] = (10.0 * rand() / (RAND_MAX + 1.0));
55 xbt_heap_push(heap, NULL, tab[i]);
58 qsort(tab, size, sizeof(double), compare_double);
60 for (i = 0; i < size; i++) {
61 /* printf("%lg" " ", xbt_heap_maxkey(heap)); */
62 if (xbt_heap_maxkey(heap) != tab[i]) {
63 fprintf(stderr, "Problem !\n");
70 printf("Validity test complete!\n");
73 void test_heap_mean_operation(int size)
75 xbt_heap_t heap = xbt_heap_new(size, NULL);
80 date = gras_os_time() * 1000000;
81 for (i = 0; i < size; i++)
82 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
83 date = gras_os_time() * 1000000 - date;
84 printf("Creation time %d size heap : %g\n", size, date);
86 date = gras_os_time() * 1000000;
87 for (j = 0; j < MAX_TEST; j++) {
90 test_reset_heap(heap,size);
92 val = xbt_heap_maxkey(heap);
94 xbt_heap_push(heap, NULL, 3.0 * val);
96 date = gras_os_time() * 1000000 - date;
97 printf("Mean access time for a %d size heap : %g\n", size,
98 date * 1.0 / (MAX_TEST + 0.0));
103 void test_reset_heap(xbt_heap_t heap,int size)
107 heap = xbt_heap_new(size, NULL);
109 for (i = 0; i < size; i++){
110 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
119 int main(int argc, char **argv)
122 for (size = 100; size < 10000; size *= 10) {
123 test_heap_validity(size);
124 test_heap_mean_operation(size);