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. */
16 #define MAX_TEST 1000000
23 gettimeofday(&start, NULL);
25 return (start.tv_sec * 1000000 + start.tv_usec);
28 int compare_xbt_heap_float_t(const void *a, const void *b);
29 void test_heap_validity(int size);
30 void test_heap_mean_operation(int size);
32 int compare_xbt_heap_float_t(const void *a, const void *b)
34 xbt_heap_float_t pa, pb;
36 pa = *((xbt_heap_float_t *) a);
37 pb = *((xbt_heap_float_t *) b);
46 void test_heap_validity(int size)
48 xbt_heap_t heap = xbt_heap_new(size, NULL);
49 xbt_heap_float_t *tab = calloc(size, sizeof(xbt_heap_float_t));
52 for (i = 0; i < size; i++) {
53 tab[i] = (10.0 * rand() / (RAND_MAX + 1.0));
54 xbt_heap_push(heap, NULL, tab[i]);
57 qsort(tab, size, sizeof(xbt_heap_float_t), compare_xbt_heap_float_t);
59 for (i = 0; i < size; i++) {
60 /* printf(XBT_HEAP_FLOAT_T " ", xbt_heap_maxkey(heap)); */
61 if (xbt_heap_maxkey(heap) != tab[i]) {
62 fprintf(stderr, "Problem !\n");
69 printf("Validity test complete!\n");
72 void test_heap_mean_operation(int size)
74 xbt_heap_t heap = xbt_heap_new(size, NULL);
80 for (i = 0; i < size; i++)
81 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
82 date = us_time() - date;
83 printf("Creation time %d size heap : %g\n", size, 0.0 + date);
86 for (j = 0; j < MAX_TEST; j++) {
87 val = xbt_heap_maxkey(heap);
89 xbt_heap_push(heap, NULL, 3.0 * val);
91 date = us_time() - date;
92 printf("Mean access time for a %d size heap : %g\n", size,
93 date * 1.0 / (MAX_TEST + 0.0));
98 int main(int argc, char **argv)
101 for (size = 100; size < 10000; size *= 10) {
102 test_heap_validity(size);
103 test_heap_mean_operation(size);