bug-recutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-recutils] [PATCH 09/13] src,torture: suppo rt duplicating index tre


From: Michał Masłowski
Subject: [bug-recutils] [PATCH 09/13] src,torture: suppo rt duplicating index tree objects.
Date: Mon, 20 Aug 2012 18:21:30 +0200

The only use I expect for this is duplicating rsets with references to
their indexes which will be implemented in a separate commit.
---
 ChangeLog                                  | 12 ++++
 src/rec-idx-tree.c                         | 43 ++++++++++++++
 src/rec.h                                  |  6 ++
 torture/Makefile.am                        |  1 +
 torture/rec-idx-tree/rec-idx-tree-dup.c    | 93 ++++++++++++++++++++++++++++++
 torture/rec-idx-tree/tsuite-rec-idx-tree.c |  2 +
 6 files changed, 157 insertions(+)
 create mode 100644 torture/rec-idx-tree/rec-idx-tree-dup.c

diff --git a/ChangeLog b/ChangeLog
index bef69c0..f5360e1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2012-08-19  Michał Masłowski  <address@hidden>
+
+       src,torture: support duplicating index tree objects.
+       * src/rec-idx-tree.c (rec_idx_tree_dup): New function.
+       * src/rec.h: Add prototype for rec_idx_tree_dup.
+
+       * torture/Makefile.am (REC_IDX_TREE_TSUITE): Add
+       rec-idx-tree-dup.c.
+       * torture/rec-idx-tree/rec-idx-tree-dup.c: New file.
+       * torture/rec-idx-tree/tsuite-rec-idx-tree.c: Add
+       test_rec_idx_tree_dup.
+
 2012-08-18  Michał Masłowski  <address@hidden>
 
        utils: add recfix --add-index command.
diff --git a/src/rec-idx-tree.c b/src/rec-idx-tree.c
index da45b95..c641e71 100644
--- a/src/rec-idx-tree.c
+++ b/src/rec-idx-tree.c
@@ -311,6 +311,49 @@ rec_idx_tree_destroy (rec_idx_tree_t idx_tree)
   free (idx_tree);
 }
 
+rec_idx_tree_t
+rec_idx_tree_dup (rec_idx_tree_t idx_tree)
+{
+  rec_idx_tree_t res = NULL;
+  size_t n_fields, n;
+
+  res = malloc (sizeof (*res));
+  if (!res)
+    {
+      return res;
+    }
+
+  res->buffer = idx_tree->buffer;
+  res->end = idx_tree->end;
+  res->parser = idx_tree->parser;
+  res->rset = idx_tree->rset;
+  res->root = idx_tree->root;
+
+  /* Allocate the per-field arrays.  */
+  n_fields = rec_idx_tree_get_num_fields (res);
+
+  res->names = malloc (sizeof (res->names[0]) * n_fields);
+  if (!res->names)
+    {
+      free (res);
+      return NULL;
+    }
+
+  res->types = malloc (sizeof (res->types[0]) * n_fields);
+  if (!res->types)
+    {
+      free (res->names);
+      free (res);
+      return NULL;
+    }
+
+  /* Set the key data.  */
+  memcpy (res->types, idx_tree->types, sizeof (res->types[0]) * n_fields);
+  memcpy (res->names, idx_tree->names, sizeof (res->names[0]) * n_fields);
+
+  return res;
+}
+
 size_t
 rec_idx_tree_get_rset_number (rec_idx_tree_t idx_tree)
 {
diff --git a/src/rec.h b/src/rec.h
index 0aaad13..e71005e 100644
--- a/src/rec.h
+++ b/src/rec.h
@@ -2490,6 +2490,12 @@ void rec_idx_tree_set_rset (rec_idx_tree_t idx_tree, 
rec_parser_t parser, rec_rs
 
 void rec_idx_tree_destroy (rec_idx_tree_t idx_tree);
 
+/* Create a copy of an index tree and return a reference to it.  The
+   memory area used for reading the index is not copied.  NULL is
+   returned if there is no enough memory to perform the operation.  */
+
+rec_idx_tree_t rec_idx_tree_dup (rec_idx_tree_t idx_tree);
+
 /**************** Reading index tree metadata ************************/
 
 /* Return the zero-based number of the rset referred to by this index
diff --git a/torture/Makefile.am b/torture/Makefile.am
index dc01501..17d95fd 100644
--- a/torture/Makefile.am
+++ b/torture/Makefile.am
@@ -150,6 +150,7 @@ REC_ITER_TSUITE = rec-iter/rec-iter-mset.c \
 REC_IDX_TREE_TSUITE = rec-idx-tree/rec-idx-tree-new.c \
                       rec-idx-tree/rec-idx-tree-scan.c \
                       rec-idx-tree/rec-idx-tree-build.c \
+                      rec-idx-tree/rec-idx-tree-dup.c \
                       rec-idx-tree/tsuite-rec-idx-tree.c
 
 runtests_SOURCES = runtests.c \
diff --git a/torture/rec-idx-tree/rec-idx-tree-dup.c 
b/torture/rec-idx-tree/rec-idx-tree-dup.c
new file mode 100644
index 0000000..061fa0a
--- /dev/null
+++ b/torture/rec-idx-tree/rec-idx-tree-dup.c
@@ -0,0 +1,93 @@
+/* -*- mode: C -*-
+ *
+ *       File:         rec-idx-tree-dup.c
+ *       Date:         Sun Aug 19 16:59:14 2012
+ *
+ *       GNU recutils - rec_idx_tree_dup unit tests.
+ *
+ */
+
+/* Copyright (C) 2012 Michał Masłowski */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <check.h>
+
+#include <rec.h>
+
+/* Header and key description for a simple index tree. */
+static const uint8_t sample_header[] =
+  {
+    1 /* second rset */, 0 /* only leaf node */,
+    2 /* two fields in the key */,
+    1 /* string */, 'f', 'o', 'o', '\0',
+    1 /* string */, 'b', 'a', 'r', '\0',
+    0, 0, 0 /* padding */
+  };
+
+/*-
+ * Test: rec_idx_tree_dup_nominal
+ * Unit: rec_idx_tree_dup
+ * Description:
+ * + Duplicate an index tree and check its metadata.
+ */
+START_TEST(rec_idx_tree_dup_nominal)
+{
+  rec_idx_tree_t tree1, tree2;
+
+  /* Open the tree. */
+  tree1 = rec_idx_tree_new (sample_header, 1 /* the type */,
+                            sizeof(sample_header));
+  fail_if (!tree1);
+
+  /* Duplicate it and close the original. */
+  tree2 = rec_idx_tree_dup (tree1);
+  fail_if (!tree2);
+
+  rec_idx_tree_destroy (tree1);
+
+  /* Check the header. */
+  fail_if (rec_idx_tree_get_rset_number (tree2) != 1);
+  fail_if (rec_idx_tree_get_num_fields (tree2) != 2);
+
+  /* Check the key. */
+  fail_if (strcmp (rec_idx_tree_get_field_name (tree2, 0), "foo"));
+  fail_if (rec_idx_tree_get_field_type (tree2, 0) != REC_IDX_KEY_STRING);
+  fail_if (strcmp (rec_idx_tree_get_field_name (tree2, 1), "bar"));
+  fail_if (rec_idx_tree_get_field_type (tree2, 1) != REC_IDX_KEY_STRING);
+
+  /* Close. */
+  rec_idx_tree_destroy (tree2);
+}
+END_TEST
+
+/*
+ * Test creation function
+ */
+TCase *
+test_rec_idx_tree_dup (void)
+{
+  TCase *tc = tcase_create ("rec_idx_tree_dup");
+  tcase_add_test (tc, rec_idx_tree_dup_nominal);
+
+  return tc;
+}
+
+/* End of rec-idx-tree-dup.c */
diff --git a/torture/rec-idx-tree/tsuite-rec-idx-tree.c 
b/torture/rec-idx-tree/tsuite-rec-idx-tree.c
index 6328836..eb7feaf 100644
--- a/torture/rec-idx-tree/tsuite-rec-idx-tree.c
+++ b/torture/rec-idx-tree/tsuite-rec-idx-tree.c
@@ -29,6 +29,7 @@
 extern TCase *test_rec_idx_tree_new (void);
 extern TCase *test_rec_idx_tree_scan (void);
 extern TCase *test_rec_idx_tree_build (void);
+extern TCase *test_rec_idx_tree_dup (void);
 
 Suite *
 tsuite_rec_idx_tree ()
@@ -39,6 +40,7 @@ tsuite_rec_idx_tree ()
   suite_add_tcase (s, test_rec_idx_tree_new ());
   suite_add_tcase (s, test_rec_idx_tree_scan ());
   suite_add_tcase (s, test_rec_idx_tree_build ());
+  suite_add_tcase (s, test_rec_idx_tree_dup ());
 
   return s;
 }
-- 
1.7.11.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]