00001 #include "kal_eheap.h" 00002 00003 #include <assert.h> 00004 #include <linux/aquosa/qos_debug.h> 00005 00006 kal_define_eheap(int, int); 00007 00008 kal_eheap_t(int, int) h; 00009 00010 #define HEAP_BITS 10 00011 #define HEAP_SIZE (1 << HEAP_BITS) 00012 00013 int arr[HEAP_SIZE]; 00014 int num_elems = 0; 00015 00016 void insert(int x) { 00017 arr[num_elems++] = x; 00018 } 00019 00020 int get_min() { 00021 int i; 00022 int min = arr[0]; 00023 for (i = 1; i < num_elems; i++) 00024 if (arr[i] < min) 00025 min = arr[i]; 00026 return min; 00027 } 00028 00029 void del_min() { 00030 int i; 00031 int min_pos = 0; 00032 for (i = 1; i < num_elems; i++) 00033 if (arr[i] < arr[min_pos]) 00034 min_pos = i; 00035 arr[min_pos] = arr[--num_elems]; 00036 } 00037 00038 int main(int argc, char **argv) { 00039 int i; 00040 int j; 00041 00042 qos_chk_ok_exit(kal_eheap_init(&h, HEAP_BITS)); 00043 00044 for (j = 0; j < 1024; ++j) { 00045 assert(kal_eheap_size(&h) == num_elems); 00046 for (i = 0; i < HEAP_SIZE; i++) { 00047 int k = 1 + (int) (1000.0 * (rand() / (RAND_MAX + 1.0))); 00048 int v = 1 + (int) (1000.0 * (rand() / (RAND_MAX + 1.0))); 00049 qos_chk_ok_exit(kal_eheap_add(&h, k, v, NULL)); 00050 insert(k); 00051 assert(kal_eheap_size(&h) == num_elems); 00052 } 00053 for (i = 0; i < HEAP_SIZE; i++) { 00054 int k, v; 00055 assert((kal_eheap_get_min(&h, &k, &v) == QOS_OK) && (k == get_min())); 00056 qos_chk_ok_exit(kal_eheap_del_min(&h)); 00057 del_min(); 00058 assert(kal_eheap_size(&h) == num_elems); 00059 } 00060 } 00061 kal_eheap_cleanup(&h); 00062 00063 qos_log_debug("Test successful\n"); 00064 return 0; 00065 }