]> git.ozlabs.org Git - ccan/blob - junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_map.c
various: make the _info License: wording uniform for GPL variants.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / test / t_c_map.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 <string.h>\r
26 #include <assert.h>\r
27 #include <stdio.h>\r
28 \r
29 static int\r
30 compare_e ( void *left, void *right ) {\r
31     return strcmp ( (const char *)left, (const char *) right );\r
32 }\r
33 char *char_value[] = {  "A","B","C","D","E","F","G","H","I","J","K","L","M",\r
34                         "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};\r
35 int int_value[] = { 1,2,3,4,5,6,7,8,9,10,\r
36                       11,12,13,14,15,16,17,18,19,20,\r
37                       21,22,23,24,25,26};\r
38 \r
39 static void \r
40 insert_all ( struct clib_map* myMap) {\r
41     int size = sizeof(char_value)/sizeof(char_value[0]);\r
42     int i = 0;\r
43     for ( i = 0; i < size; i++ ) {\r
44         char *key = clib_strdup( char_value[i]);\r
45         int key_length = (int)strlen ( key ) + 1;\r
46         int value = int_value[i];\r
47                 printf ( "Inserting [%s -> %d]\n", key, value );\r
48         insert_clib_map ( myMap, key, key_length, &value, sizeof(int)); \r
49         free ( key );\r
50     }\r
51 }\r
52 static void \r
53 check_exists_all( struct clib_map* myMap) {\r
54     int size = sizeof(char_value)/sizeof(char_value[0]);\r
55     int i = 0;\r
56     for ( i = 0; i < size; i++ ) {\r
57         void *value ;\r
58         assert ( clib_true == exists_clib_map ( myMap, char_value[i]));\r
59         assert ( clib_true == find_clib_map( myMap, char_value[i], &value));\r
60                 printf ( "-----> [%s == %d]\n", char_value[i], *(int*)value);\r
61         assert ( *(int*)value == int_value[i]);\r
62         free ( value );\r
63     }\r
64 }\r
65 \r
66 static void \r
67 remove_some_exist(struct clib_map* myMap) {\r
68     assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "A"));\r
69     assert ( clib_false == exists_clib_map ( myMap, "A"));\r
70 \r
71     assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "X"));\r
72     assert ( clib_false == exists_clib_map ( myMap, "X"));\r
73 \r
74     assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "Z"));\r
75     assert ( clib_false == exists_clib_map ( myMap, "Z"));\r
76 \r
77     assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "H"));\r
78     assert ( clib_false == exists_clib_map ( myMap, "H"));\r
79 }\r
80 static void\r
81 add_removed_check_all(struct clib_map* myMap) {\r
82 \r
83     char *key       = clib_strdup ("A");\r
84     int  key_length = (int)strlen ( key ) + 1;\r
85     insert_clib_map ( myMap, key, key_length , &int_value[0], sizeof(int)); \r
86     free ( key );\r
87 \r
88     key        = clib_strdup ("X");\r
89     key_length = (int)strlen ( key ) + 1;\r
90     insert_clib_map ( myMap, key, key_length, &int_value[23], sizeof(int)); \r
91     free ( key );\r
92 \r
93     key        = clib_strdup ("Z");\r
94     key_length = (int)strlen ( key ) + 1;\r
95     insert_clib_map ( myMap, key, key_length, &int_value[25], sizeof(int)); \r
96     free ( key );\r
97 \r
98     key        = clib_strdup ("H");\r
99     key_length = (int)strlen ( key ) + 1;\r
100     insert_clib_map ( myMap, key, key_length, &int_value[7 ], sizeof(int)); \r
101     free ( key );\r
102 \r
103     check_exists_all(myMap);\r
104 }\r
105 void \r
106 test_clib_map() {\r
107     struct clib_map* myMap = new_clib_map ( compare_e, NULL, NULL);\r
108     insert_all(myMap);\r
109     check_exists_all(myMap);   \r
110     remove_some_exist(myMap);\r
111     add_removed_check_all(myMap);\r
112     delete_clib_map(myMap);\r
113 }\r