]> git.ozlabs.org Git - ccan/blob - junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_set.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / test / t_c_set.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 <stdlib.h>\r
27 #include <string.h>\r
28 #include <stdio.h>\r
29 #include <assert.h>\r
30 \r
31 static void \r
32 delete_e ( void *ptr ) {\r
33     if ( ptr ) \r
34         free ( ptr );\r
35 }\r
36 static int\r
37 compare_e ( void *left, void *right ) {\r
38     char *l = (char*)left;\r
39     char *r = (char*)right;\r
40     return strcmp ( (const char *)l, (const char *) r );\r
41 }\r
42 static int \r
43 compare_int ( void *left, void *right ) {\r
44     int *l = (int*)left;\r
45     int *r = (int*)right;\r
46 \r
47     if ( *l < *r ) \r
48         return -1;\r
49     else if ( *l > *r ) \r
50         return 1;\r
51     return 0;\r
52 }\r
53 void \r
54 test_clib_set(){\r
55     {\r
56         int test[] = { 0,1,2,3,4,5,6,7,8,9 };\r
57         int index  = 0;\r
58         int size   = sizeof (test) /sizeof(test[0]);\r
59         void* outKey;\r
60 \r
61         struct clib_set *pSet = new_clib_set ( compare_int, NULL);\r
62 \r
63         for ( index = 0; index < size; index++ ) {\r
64             int v = test[index];\r
65             insert_clib_set ( pSet, &v, sizeof(int));\r
66         }\r
67         for ( index = 0; index < size; index++ ) {\r
68             int v = test[index];\r
69             assert ( clib_true == exists_clib_set ( pSet, &v));\r
70         }\r
71 \r
72         index = 9;\r
73         find_clib_set ( pSet, &index, &outKey);\r
74         assert ( 9 == *(int*)outKey);\r
75         free ( outKey );\r
76         delete_clib_set(pSet);\r
77     }\r
78     {\r
79         typedef struct test {\r
80             char *string;\r
81         } TEST_INPUT;\r
82 \r
83         int index = 0;\r
84         int size = 0;\r
85         char *v;\r
86 \r
87         TEST_INPUT ti[] ={\r
88             {"A for APPLE"},{"B for BALL"},{"C for CAT"}, {"D for DOG"},\r
89             {"E for ELEPHANT"},{"F for FISH"},{"G for GOAT"},\r
90             {"H for HORSE"},{"I for ICECREAM"},{"J for JOKER"},\r
91             {"K for KITE"},{"L for LAMB"},{"M for MONKEY"},\r
92             {"N for NEST"},{"O for ORANGE"},{"P for POT"},\r
93             {"Q for QUEEN"},{"R for RAT"},{"S for SHEEP"},\r
94             {"T for TABLE"},{"U for UMBRELLA"},{"V for VIOLIN"},{"W for WAX"},\r
95             {"X for XEROX"},{"Y for YUMMY"},{"Z for ZEBRA"}\r
96         };\r
97         struct clib_set *pSet = new_clib_set ( compare_e, delete_e);\r
98         size = sizeof ( ti ) / sizeof ( ti[0]);\r
99         \r
100         for ( index = 0; index < size; index++ ){\r
101             char *temp = clib_strdup ( ti[index].string );\r
102             insert_clib_set ( pSet, temp, strlen(temp) + 1 );\r
103             free ( temp );\r
104         }\r
105         for ( index = 0; index < size; index++ ){\r
106             v = ti[index].string;\r
107             assert ( clib_true == exists_clib_set ( pSet, v));\r
108         }\r
109         delete_clib_set(pSet);\r
110     }\r
111 }\r