]> git.ozlabs.org Git - ccan/blobdiff - junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_map.c
junkcode: upload via website.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / test / t_c_map.c
diff --git a/junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_map.c b/junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_map.c
new file mode 100644 (file)
index 0000000..5305b45
--- /dev/null
@@ -0,0 +1,113 @@
+/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\r
+ *  This file is part of clib library\r
+ *  Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )\r
+ *\r
+ *  Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ *  of this software and associated documentation files (the "Software"), to deal\r
+ *  in the Software without restriction, including without limitation the rights\r
+ *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ *  copies of the Software, and to permit persons to whom the Software is\r
+ *  furnished to do so, subject to the following conditions:\r
+ * \r
+ *  The above copyright notice and this permission notice shall be included in\r
+ *  all copies or substantial portions of the Software.\r
+ * \r
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ *  THE SOFTWARE.\r
+ ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/\r
+\r
+#include "c_lib.h"\r
+#include <string.h>\r
+#include <assert.h>\r
+#include <stdio.h>\r
+\r
+static int\r
+compare_e ( void *left, void *right ) {\r
+    return strcmp ( (const char *)left, (const char *) right );\r
+}\r
+char *char_value[] = {  "A","B","C","D","E","F","G","H","I","J","K","L","M",\r
+                        "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};\r
+int int_value[] = { 1,2,3,4,5,6,7,8,9,10,\r
+                      11,12,13,14,15,16,17,18,19,20,\r
+                      21,22,23,24,25,26};\r
+\r
+static void \r
+insert_all ( struct clib_map* myMap) {\r
+    int size = sizeof(char_value)/sizeof(char_value[0]);\r
+    int i = 0;\r
+    for ( i = 0; i < size; i++ ) {\r
+        char *key = clib_strdup( char_value[i]);\r
+        int key_length = (int)strlen ( key ) + 1;\r
+        int value = int_value[i];\r
+               printf ( "Inserting [%s -> %d]\n", key, value );\r
+        insert_clib_map ( myMap, key, key_length, &value, sizeof(int)); \r
+        free ( key );\r
+    }\r
+}\r
+static void \r
+check_exists_all( struct clib_map* myMap) {\r
+    int size = sizeof(char_value)/sizeof(char_value[0]);\r
+    int i = 0;\r
+    for ( i = 0; i < size; i++ ) {\r
+        void *value ;\r
+        assert ( clib_true == exists_clib_map ( myMap, char_value[i]));\r
+        assert ( clib_true == find_clib_map( myMap, char_value[i], &value));\r
+               printf ( "-----> [%s == %d]\n", char_value[i], *(int*)value);\r
+        assert ( *(int*)value == int_value[i]);\r
+        free ( value );\r
+    }\r
+}\r
+\r
+static void \r
+remove_some_exist(struct clib_map* myMap) {\r
+    assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "A"));\r
+    assert ( clib_false == exists_clib_map ( myMap, "A"));\r
+\r
+    assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "X"));\r
+    assert ( clib_false == exists_clib_map ( myMap, "X"));\r
+\r
+    assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "Z"));\r
+    assert ( clib_false == exists_clib_map ( myMap, "Z"));\r
+\r
+    assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "H"));\r
+    assert ( clib_false == exists_clib_map ( myMap, "H"));\r
+}\r
+static void\r
+add_removed_check_all(struct clib_map* myMap) {\r
+\r
+    char *key       = clib_strdup ("A");\r
+    int  key_length = (int)strlen ( key ) + 1;\r
+    insert_clib_map ( myMap, key, key_length , &int_value[0], sizeof(int)); \r
+    free ( key );\r
+\r
+    key        = clib_strdup ("X");\r
+    key_length = (int)strlen ( key ) + 1;\r
+    insert_clib_map ( myMap, key, key_length, &int_value[23], sizeof(int)); \r
+    free ( key );\r
+\r
+    key        = clib_strdup ("Z");\r
+    key_length = (int)strlen ( key ) + 1;\r
+    insert_clib_map ( myMap, key, key_length, &int_value[25], sizeof(int)); \r
+    free ( key );\r
+\r
+    key        = clib_strdup ("H");\r
+    key_length = (int)strlen ( key ) + 1;\r
+    insert_clib_map ( myMap, key, key_length, &int_value[7 ], sizeof(int)); \r
+    free ( key );\r
+\r
+    check_exists_all(myMap);\r
+}\r
+void \r
+test_clib_map() {\r
+    struct clib_map* myMap = new_clib_map ( compare_e, NULL, NULL);\r
+    insert_all(myMap);\r
+    check_exists_all(myMap);   \r
+    remove_some_exist(myMap);\r
+    add_removed_check_all(myMap);\r
+    delete_clib_map(myMap);\r
+}\r