]> git.ozlabs.org Git - ccan/blob - junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_deque.c
pushpull: new module.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / test / t_c_deque.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 \r
26 #include <stdio.h>\r
27 #include <stdlib.h>\r
28 #include <string.h>\r
29 #include <assert.h>\r
30 \r
31 static int \r
32 compare_e ( void *left, void *right ) {\r
33     int *l = (int*) left;\r
34     int *r = (int*) right;\r
35     return *l == *r ;\r
36 }\r
37 static void \r
38 free_e ( void *ptr ) {\r
39     if ( ptr )\r
40     free ( ptr);\r
41 }\r
42 void \r
43 test_clib_deque() {\r
44     int flip = 1;\r
45     int i = 0;\r
46     int limit = 20;\r
47     void*  element;\r
48     int j = 0;\r
49 \r
50     struct clib_deque* myDeq = new_clib_deque ( 10, compare_e, NULL);\r
51     assert ( (struct clib_deque*)0 != myDeq );\r
52 \r
53     for ( i = 0; i <= limit; i++ ) { \r
54         if ( flip ) {\r
55             push_back_clib_deque ( myDeq, &i , sizeof(int));\r
56             flip = 0;\r
57         } else {\r
58             push_front_clib_deque ( myDeq, &i, sizeof(int) );\r
59             flip = 1;\r
60         }\r
61     }\r
62     front_clib_deque ( myDeq, &element );\r
63     assert ( *(int*)element == limit - 1 );\r
64     free ( element );\r
65 \r
66     back_clib_deque ( myDeq, &element );\r
67     assert ( *(int*)element == limit);\r
68     free ( element );\r
69 \r
70     while ( empty_clib_deque(myDeq) != clib_true ) {\r
71         pop_front_clib_deque ( myDeq);\r
72     }\r
73     delete_clib_deque(myDeq);\r
74 \r
75     myDeq = new_clib_deque ( 10, compare_e, free_e); \r
76     for ( i = 0; i <= limit; i ++ ) { \r
77         int *v = (int*)malloc(sizeof(int ));\r
78         memcpy ( v, &i, sizeof ( int ));\r
79         push_back_clib_deque ( myDeq, v , sizeof(int*));\r
80         free ( v );\r
81     }   \r
82     for ( i = myDeq->head + 1; i < myDeq->tail; i++ ){\r
83         void *elem;\r
84         if ( element_at_clib_deque( myDeq, i, &elem ) == CLIB_ERROR_SUCCESS ) {\r
85                 assert ( *(int*)elem == j++ );\r
86                 free ( elem );\r
87         }\r
88     }\r
89     delete_clib_deque(myDeq);\r
90 }\r