]> git.ozlabs.org Git - ccan/blobdiff - junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_set.c
junkcode: upload via website.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / test / t_c_set.c
diff --git a/junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_set.c b/junkcode/dongre.avinash@gmail.com-clibutils/test/t_c_set.c
new file mode 100644 (file)
index 0000000..3efea3e
--- /dev/null
@@ -0,0 +1,111 @@
+/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\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
+\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <stdio.h>\r
+#include <assert.h>\r
+\r
+static void \r
+delete_e ( void *ptr ) {\r
+    if ( ptr ) \r
+       free ( ptr );\r
+}\r
+static int\r
+compare_e ( void *left, void *right ) {\r
+    char *l = (char*)left;\r
+    char *r = (char*)right;\r
+    return strcmp ( (const char *)l, (const char *) r );\r
+}\r
+static int \r
+compare_int ( void *left, void *right ) {\r
+    int *l = (int*)left;\r
+    int *r = (int*)right;\r
+\r
+    if ( *l < *r ) \r
+        return -1;\r
+    else if ( *l > *r ) \r
+        return 1;\r
+    return 0;\r
+}\r
+void \r
+test_clib_set(){\r
+    {\r
+        int test[] = { 0,1,2,3,4,5,6,7,8,9 };\r
+        int index  = 0;\r
+        int size   = sizeof (test) /sizeof(test[0]);\r
+        void* outKey;\r
+\r
+        struct clib_set *pSet = new_clib_set ( compare_int, NULL);\r
+\r
+        for ( index = 0; index < size; index++ ) {\r
+            int v = test[index];\r
+            insert_clib_set ( pSet, &v, sizeof(int));\r
+        }\r
+        for ( index = 0; index < size; index++ ) {\r
+            int v = test[index];\r
+            assert ( clib_true == exists_clib_set ( pSet, &v));\r
+        }\r
+\r
+        index = 9;\r
+        find_clib_set ( pSet, &index, &outKey);\r
+        assert ( 9 == *(int*)outKey);\r
+        free ( outKey );\r
+        delete_clib_set(pSet);\r
+    }\r
+    {\r
+        typedef struct test {\r
+            char *string;\r
+        } TEST_INPUT;\r
+\r
+        int index = 0;\r
+        int size = 0;\r
+        char *v;\r
+\r
+        TEST_INPUT ti[] ={\r
+            {"A for APPLE"},{"B for BALL"},{"C for CAT"}, {"D for DOG"},\r
+            {"E for ELEPHANT"},{"F for FISH"},{"G for GOAT"},\r
+            {"H for HORSE"},{"I for ICECREAM"},{"J for JOKER"},\r
+            {"K for KITE"},{"L for LAMB"},{"M for MONKEY"},\r
+            {"N for NEST"},{"O for ORANGE"},{"P for POT"},\r
+            {"Q for QUEEN"},{"R for RAT"},{"S for SHEEP"},\r
+            {"T for TABLE"},{"U for UMBRELLA"},{"V for VIOLIN"},{"W for WAX"},\r
+            {"X for XEROX"},{"Y for YUMMY"},{"Z for ZEBRA"}\r
+        };\r
+        struct clib_set *pSet = new_clib_set ( compare_e, delete_e);\r
+        size = sizeof ( ti ) / sizeof ( ti[0]);\r
+        \r
+        for ( index = 0; index < size; index++ ){\r
+            char *temp = clib_strdup ( ti[index].string );\r
+            insert_clib_set ( pSet, temp, strlen(temp) + 1 );\r
+            free ( temp );\r
+        }\r
+        for ( index = 0; index < size; index++ ){\r
+            v = ti[index].string;\r
+            assert ( clib_true == exists_clib_set ( pSet, v));\r
+        }\r
+        delete_clib_set(pSet);\r
+    }\r
+}\r