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 #define MAX_TEST 1000000
24 int _XBT_CALL compare_double(const void *a, const void *b);
26 int compare_double(const void *a, const void *b);
29 void test_heap_validity(int size);
30 void test_heap_mean_operation(int size);
31 void test_reset_heap(xbt_heap_t heap,int size);
34 int compare_double(const void *a, const void *b)
48 void test_heap_validity(int size)
50 xbt_heap_t heap = xbt_heap_new(size, NULL);
51 double *tab = (double*)calloc(size,sizeof(double));
55 for (i = 0; i < size; i++) {
56 tab[i] = (double)(10.0 * rand() / (RAND_MAX + 1.0));
57 xbt_heap_push(heap, NULL, (double)tab[i]);
60 qsort(tab, size, sizeof(double), compare_double);
62 for (i = 0; i < size; i++) {
63 /* printf("%lg" " ", xbt_heap_maxkey(heap)); */
64 if (xbt_heap_maxkey(heap) != tab[i]) {
65 fprintf(stderr, "Problem !\n");
72 printf("Validity test complete!\n");
75 void test_heap_mean_operation(int size)
77 xbt_heap_t heap = xbt_heap_new(size, NULL);
82 date = gras_os_time() * 1000000;
83 for (i = 0; i < size; i++)
84 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
86 date = gras_os_time() * 1000000 - date;
87 printf("Creation time %d size heap : %g\n", size, date);
89 date = gras_os_time() * 1000000;
90 for (j = 0; j < MAX_TEST; j++) {
93 test_reset_heap(heap,size);
95 val = xbt_heap_maxkey(heap);
97 xbt_heap_push(heap, NULL, 3.0 * val);
99 date = gras_os_time() * 1000000 - date;
100 printf("Mean access time for a %d size heap : %g\n", size,
101 date * 1.0 / (MAX_TEST + 0.0));
106 void test_reset_heap(xbt_heap_t heap,int size)
110 heap = xbt_heap_new(size, NULL);
112 for (i = 0; i < size; i++){
113 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
122 int main(int argc, char **argv)
125 for (size = 100; size < 10000; size *= 10) {
126 test_heap_validity(size);
127 test_heap_mean_operation(size);