Commits

Shlomi Fish committed 4866096

Extract compare_records to dbm_kaztree_compare.h and fix a bug there.

The problem was that the sizeof was the size of the pointer instead of
its dereference. This was part of the problem with the erroneous behaviour
of the dbm_fc_solver.

A test program was added to test the new compare_records function.

git-svn-id: file:///home/shlomif/Backup/svn-dumps/google-code/svnsync-repos/fc-solve/fc-solve/trunk@4536 e7e8a897-7ba4-4ee7-b36f-f4c66519b19a

Comments (0)

Files changed (4)

fc-solve/source/dbm_kaztree.c

 #include "dbm_solver.h"
 #include "generic_tree.h"
 
-/*
- * TODO : We waste too much space and fragment it storing the
- * key/parent_move separatley from the dnode_t. We should use a struct
- * for that instead of a pointer.
- * */
-typedef struct
-{
-    fcs_encoded_state_buffer_t key;
-    fcs_encoded_state_buffer_t parent_and_move;
-} record_t;
-
+#include "dbm_kaztree_compare.h"
 
 typedef struct
 {
     fcs_compact_allocator_t allocator;
 } dbm_t;
 
-/* TODO: make sure the key is '\0'-padded. */
-static int compare_records(
-    const void * void_a, const void * void_b, void * context
-)
-{
-#define GET_PARAM(p) (&(((const record_t *)(p))->key))
-    return memcmp(GET_PARAM(void_a), GET_PARAM(void_b), sizeof(GET_PARAM(void_a)));
-#undef GET_PARAM
-}
-
 void fc_solve_dbm_store_init(fcs_dbm_store_t * store, const char * path)
 {
     dbm_t * db;

fc-solve/source/dbm_kaztree_compare.h

+/* Copyright (c) 2012 Shlomi Fish
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+/*
+ * dbm_kaztree_compare.h - contains the comparison routine and record_t
+ * definition of dbm_kaztree.c. Should only be included by it and programs
+ * that test it.
+ */
+#ifndef FC_SOLVE_DBM_KAZTREE_COMPARE_H
+#define FC_SOLVE_DBM_KAZTREE_COMPARE_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "delta_states.h"
+
+/*
+ * TODO : We waste too much space and fragment it storing the
+ * key/parent_move separatley from the dnode_t. We should use a struct
+ * for that instead of a pointer.
+ * */
+typedef struct
+{
+    fcs_encoded_state_buffer_t key;
+    fcs_encoded_state_buffer_t parent_and_move;
+} record_t;
+
+/* TODO: make sure the key is '\0'-padded. */
+static int compare_records(
+    const void * void_a, const void * void_b, void * context
+)
+{
+#define GET_PARAM(p) (((const record_t *)(p))->key)
+    return memcmp(&(GET_PARAM(void_a)), &(GET_PARAM(void_b)), sizeof(GET_PARAM(void_a)));
+#undef GET_PARAM
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*  FC_SOLVE_DBM_KAZTREE_COMPARE_H */
+

fc-solve/source/t/CMakeLists.txt

             GENERATED "1"
     )
 
+    SET (EXE_FILE "t/dbm-kaztree-compare-records-test.exe")
+
+    ADD_EXECUTABLE(
+        "${EXE_FILE}"
+        "dbm-kaztree-compare-records-test.c"
+    )
+
+    # This is a kind-of-temporary kludge to make 
+    # "dbm-kaztree-compare-records-test.c" build.
+    ADD_DEFINITIONS("-DFCS_DBM_USE_LIBAVL=1"
+        "-I${CMAKE_CURRENT_SOURCE_DIR}/.."
+        "-I${CMAKE_CURRENT_SOURCE_DIR}/../libavl/"
+        )
+    TARGET_LINK_LIBRARIES (${EXE_FILE} ${LIBTAP_LIB})
+
 ENDIF(FCS_WITH_TEST_SUITE)
 

fc-solve/source/t/dbm-kaztree-compare-records-test.c

+/* Copyright (c) 2011 Shlomi Fish
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+/*
+ * A test for the delta states routines.
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+#include <tap.h>
+
+#define FCS_COMPILE_DEBUG_FUNCTIONS
+#include "../dbm_kaztree_compare.h"
+
+int main_tests()
+{
+    {
+        int pos_idx;
+        int all_good = TRUE;
+
+        for (pos_idx = 0 ; pos_idx < sizeof( fcs_encoded_state_buffer_t ) ; pos_idx++)
+        {
+            record_t rec_a, rec_b;
+
+            memset(&rec_a, '\0', sizeof(rec_a));
+            memset(&rec_b, '\0', sizeof(rec_a));
+
+            rec_a.key.s[pos_idx] = '\x01';
+            
+            if (! (compare_records(&rec_a, &rec_b, NULL) > 0))
+            {
+                diag("compare_records(rec_a, rec_b) returned a wrong value for position %d.\n", pos_idx);
+                all_good = FALSE;
+                break;
+            }
+            if (! (compare_records(&rec_b, &rec_a, NULL) < 0))
+            {
+                diag("compare_records(rec_b, rec_a) returned a wrong value for position %d.\n", pos_idx);
+                all_good = FALSE;
+                break;
+            }
+        }
+
+        /* TEST
+         * */
+        ok (all_good, "All compare_records were successful.");
+    }
+    return 0;
+}
+
+int main(int argc, char * argv[])
+{
+    plan_tests(1);
+    main_tests();
+    return exit_status();
+}