]> git.ozlabs.org Git - ccan/blob - junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_heap.c
junkcode: upload via website.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / test / t_c_heap.c
1 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\r
2  *  This file is part of clib library\r
3  *  Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )\r
4  *\r
5  *  Permission is hereby granted, free of charge, to any person obtaining a copy\r
6  *  of this software and associated documentation files (the "Software"), to deal\r
7  *  in the Software without restriction, including without limitation the rights\r
8  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
9  *  copies of the Software, and to permit persons to whom the Software is\r
10  *  furnished to do so, subject to the following conditions:\r
11  * \r
12  *  The above copyright notice and this permission notice shall be included in\r
13  *  all copies or substantial portions of the Software.\r
14  * \r
15  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
17  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
18  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
19  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
20  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
21  *  THE SOFTWARE.\r
22  ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/\r
23 \r
24 #include "c_lib.h"\r
25 #include <stdio.h>\r
26 \r
27 static int \r
28 compare_int ( void *left, void *right ) {\r
29     int *l = (int*)left;\r
30     int *r = (int*)right;\r
31 \r
32     if ( *l < *r ) \r
33         return -1;\r
34     else if ( *l > *r ) \r
35         return 1;\r
36     return 0;\r
37 }\r
38 static void\r
39 print_element ( void *ptr ) {\r
40         printf ( "%d\n", *(int*)ptr);\r
41 }\r
42 void \r
43 test_clib_heap_max() {\r
44         int test[] = {4,1,3,2,16,9,10,14,8,7};\r
45         int index  = 0;\r
46         int size   = sizeof (test) /sizeof(test[0]);\r
47         void *maxElem;\r
48         struct clib_heap* pHeap = new_clib_heap ( 8, compare_int, NULL);\r
49 \r
50         for ( index = 0; index < size; index++ ) {\r
51                 int v = test[index];\r
52                 insert_clib_heap ( pHeap, &v, sizeof(int));                     \r
53         }\r
54         build_max_clib_heap( pHeap);\r
55         printf ( "---------------------------------\n");\r
56         for_each_clib_heap ( pHeap, print_element);\r
57         printf ( "---------------------------------\n");\r
58         while ( empty_clib_heap(pHeap) != clib_true ) {\r
59                 maxElem  = extract_max_clib_heap ( pHeap );\r
60                 printf ( "MAX ELEMENT = %d\n", *(int*)maxElem);\r
61                 free ( maxElem );\r
62         }\r
63         delete_clib_heap ( pHeap );\r
64 }\r
65 \r
66 void\r
67 test_clib_heap_min() {\r
68         int test[] = {4,1,3,2,16,9,10,14,8,7};\r
69         int index  = 0;\r
70         int size   = sizeof (test) /sizeof(test[0]);\r
71         void *maxElem;\r
72         struct clib_heap* pHeap = new_clib_heap ( 8, compare_int, NULL);\r
73 \r
74         for ( index = 0; index < size; index++ ) {\r
75                 int v = test[index];\r
76                 insert_clib_heap ( pHeap, &v, sizeof(int));                     \r
77         }\r
78         build_min_clib_heap( pHeap);\r
79         printf ( "---------------------------------\n");\r
80         for_each_clib_heap ( pHeap, print_element);\r
81         printf ( "---------------------------------\n");\r
82         while ( empty_clib_heap(pHeap) != clib_true ) {\r
83                 maxElem  = extract_min_clib_heap ( pHeap );\r
84                 printf ( "MIN ELEMENT = %d\n", *(int*)maxElem);\r
85                 free ( maxElem );\r
86         }\r
87         delete_clib_heap ( pHeap );\r
88 \r
89 }\r
90 \r
91 void \r
92 test_clib_heap() {\r
93         test_clib_heap_max();\r
94         test_clib_heap_min();\r
95 }