pdf-devel
[Top][All Lists]
Advanced

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

[pdf-devel] Hash Module patches


From: gerel
Subject: [pdf-devel] Hash Module patches
Date: Wed, 16 Apr 2008 21:51:29 -0300

I send here the patches. I also added two missing test cases "pdf_list_destroy"
and "pdf_list_iterator_free".

BTW, I also did some changes to the Hash Module API.

Here we go :-)

(first the patches and then the new files)
##

Index: ChangeLog
===================================================================
RCS file: /sources/pdf/libgnupdf/ChangeLog,v
retrieving revision 1.204
diff -u -r1.204 ChangeLog
--- ChangeLog   16 Apr 2008 16:28:10 -0000      1.204
+++ ChangeLog   17 Apr 2008 00:29:05 -0000
@@ -1,3 +1,49 @@
+2008-04-16  gerel  <address@hidden>
+
+       * doc/gnupdf-tsd.texi: added missing List and new Hash Module tests.
+
+       * doc/gnupdf.texi: modified Hash Module API.
+
+       * src/Makefile.am: added Hash Module sources.
+
+       * torture/unit/Makefile.am: added Hash Module test sources
+
+       * torture/unit/runtests.c: added Hash Module case suite.
+
+       * torture/unit/base/list/tsuite-list.c: added missing tests.
+
+       * src/base/pdf-hash.c: initial add
+
+       * src/base/pdf-hash.h: initial add
+
+       * torture/unit/base/list/pdf-list-destroy.c: initial add.
+
+       * torture/unit/base/list/pdf-list-iterator-free.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-add.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-create.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-destroy.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-iterator-free.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-iterator-next.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-iterator.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-key-p.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-remove.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-rename.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-search.c: initial add.
+
+       * torture/unit/base/hash/pdf-hash-size.c: initial add.
+
+       * torture/unit/base/hash/tsuite-hash.c: initial add.
+
 2008-04-16  Aleksander Morgado Juez  <address@hidden>
 
        * src/base/pdf-text-ucd-wordbreak.h: Create new block of self-generated
Index: doc/gnupdf-tsd.texi
===================================================================
RCS file: /sources/pdf/libgnupdf/doc/gnupdf-tsd.texi,v
retrieving revision 1.9
diff -u -r1.9 gnupdf-tsd.texi
--- doc/gnupdf-tsd.texi 13 Apr 2008 20:10:47 -0000      1.9
+++ doc/gnupdf-tsd.texi 17 Apr 2008 00:29:10 -0000
@@ -72,6 +72,7 @@
 * Stream Module::
 * Text Module::
 * List Module::
+* Hash Module::
 @end menu
 
 @node Memory Allocation Module
@@ -3132,6 +3133,7 @@
 * pdf_list_add_first::
 * pdf_list_add_last::
 * pdf_list_create::
+* pdf_list_destroy::
 * pdf_list_get_at::
 * pdf_list_indexof_from_to::
 * pdf_list_indexof_from::
@@ -3139,6 +3141,7 @@
 * pdf_list_iterator_from_to::
 * pdf_list_iterator_next::
 * pdf_list_iterator::
+* pdf_list_iterator_free::
 * pdf_list_next_node::
 * pdf_list_node_value::
 * pdf_list_previous_node::
@@ -3234,6 +3237,17 @@
 @end table
 @end deffn
 
address@hidden pdf_list_destroy
address@hidden pdf_list_destroy
+
address@hidden Test pdf_list_destroy_001
+Try to destroy an empty list.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
 @node pdf_list_get_at
 @subsubsection pdf_list_get_at
 
@@ -3428,6 +3442,16 @@
 @end table
 @end deffn
 
address@hidden pdf_list_iterator_free
address@hidden pdf_list_iterator_free
+
address@hidden Test pdf_list_iterator_free_001
+Try to destroy an iterator.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
 
 @node pdf_list_next_node
 @subsubsection pdf_list_next_node
@@ -3888,6 +3912,290 @@
 @end deffn
 
 
address@hidden Hash Module
address@hidden Hash Module
+
address@hidden
+* pdf_hash_create::
+* pdf_hash_detroy::
+* pdf_hash_size::
+* pdf_hash_key_p::
+* pdf_hash_rename::
+* pdf_hash_add::
+* pdf_hash_remove::
+* pdf_hash_search::
+* pdf_hash_iterator::
+* pdf_hash_iterator_next::
+* pdf_hash_iterator_free::
address@hidden menu
+
address@hidden pdf_hash_create
address@hidden pdf_hash_create
+
address@hidden Test pdf_hash_create_001
+Try to create an empty hash.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_create_002
+Try to create an empty hash with a NULL pointer.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
address@hidden pdf_hash_detroy
address@hidden pdf_hash_detroy
+
address@hidden Test pdf_hash_detroy_001
+Try to destroy an empty hash.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
address@hidden pdf_hash_size
address@hidden pdf_hash_size
+
address@hidden Test pdf_hash_size_001
+Try to get the size of an empty hash.
address@hidden @strong
address@hidden Success condition
+Returns 0
address@hidden table
address@hidden deffn
+
+
address@hidden pdf_hash_key_p
address@hidden pdf_hash_key_p
+
+
address@hidden Test pdf_hash_key_p_001
+Try to prove that a given key exist.
address@hidden @strong
address@hidden Success condition
+Returns PDF_TRUE
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_key_p_002
+Try to prove that a given key doesn't exist.
address@hidden @strong
address@hidden Success condition
+Returns PDF_FALSE
address@hidden table
address@hidden deffn
+
address@hidden pdf_hash_rename
address@hidden pdf_hash_rename
+
+
address@hidden Test pdf_hash_rename_001
+Try to rename an existent key and prove it was renamed.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_rename_002
+Try to rename a non-existent key.
address@hidden @strong
address@hidden Success condition
+Returns PDF_ERROR
address@hidden table
address@hidden deffn
+
+
+
address@hidden Test pdf_hash_rename_003
+Try to rename a key given a NULL key.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_rename_004
+Try to rename a key given a NULL new_key.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden pdf_hash_add
address@hidden pdf_hash_add
+
address@hidden Test pdf_hash_add_001
+Try to add key/value pair to an empty hash.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_add_002
+Try to add NULL/value pair to an empty hash.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_add_003
+Try to add key/NULL pair to an empty hash.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden pdf_hash_remove
address@hidden pdf_hash_remove
+
+
address@hidden Test pdf_hash_remove_001
+Try to remove an existent element.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_remove_002
+Try to remove a non-existent element.
address@hidden @strong
address@hidden Success condition
+Returns PDF_ERROR
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_remove_003
+Try to remove a element givn a NULL key.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden pdf_hash_search
address@hidden pdf_hash_search
+
+
address@hidden Test pdf_hash_search_001
+Try to search an existent element.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_search_002
+Try to search a non-existent element.
address@hidden @strong
address@hidden Success condition
+Returns PDF_ERROR
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_search_003
+Try to search an element given a NULL elem_pointer.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_search_004
+Try to search an element given a NULL key.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden pdf_hash_iterator
address@hidden pdf_hash_iterator
+
address@hidden Test pdf_hash_iterator_001
+Try to create an iterator.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
address@hidden Test pdf_hash_iterator_002
+Try to create an iterator given a NULL iterator pointer.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
address@hidden pdf_hash_iterator_next
address@hidden pdf_hash_iterator_next
+
+
address@hidden Test pdf_hash_iterator_next_001
+Try to iterate over an iterator.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_iterator_next_002
+Try to iterate over a NULL iterator.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
+
address@hidden Test pdf_hash_iterator_next_003
+Try to iterate over an iterator given a NULL key pointer.
address@hidden @strong
address@hidden Success condition
+Returns PDF_EBADDATA
address@hidden table
address@hidden deffn
+
address@hidden pdf_hash_iterator_free
address@hidden pdf_hash_iterator_free
+
address@hidden Test pdf_hash_iterator_free_001
+Try to free an iterator.
address@hidden @strong
address@hidden Success condition
+Returns PDF_OK
address@hidden table
address@hidden deffn
+
+
 @node Subsystem Testing
 @chapter Subsystem Testing
 
Index: doc/gnupdf.texi
===================================================================
RCS file: /sources/pdf/libgnupdf/doc/gnupdf.texi,v
retrieving revision 1.49
diff -u -r1.49 gnupdf.texi
--- doc/gnupdf.texi     15 Apr 2008 19:51:31 -0000      1.49
+++ doc/gnupdf.texi     17 Apr 2008 00:29:20 -0000
@@ -913,19 +913,24 @@
 A function type for disposing hash elements.
 @end deftp
 
address@hidden {Data Type} void (*pdf_hash_key_dispose_fn_t) (const void *key)
+A function type for disposing hash keys.
address@hidden deftp
 
 @node Creating and Destroying Hash Tables
 @subsection Creating and Destroying Hash Tables
 
address@hidden pdf_status_t pdf_hash_create (pdf_hash_dispose_fn_t 
@var{dispose_fn}, pdf_hash_t @var{*table})
address@hidden pdf_status_t pdf_hash_create (pdf_hash_key_dispose_fn_t 
@var{dispose_key_fn}, pdf_hash_element_dispose_fn_t @var{dispose_fn}, 
pdf_hash_t @var{*table})
 
-Create a new empty hash table. When some element is removed @var{dispose_fn} 
is called. Can be NULL.
+Create a new empty hash table. When some element is removed @var{dispose_fn} 
and @var{dispose_key_fn} are called, can be NULL.
 
 @table @strong
 @item Parameters
 @table @var
address@hidden dispose_key_fn
+A pointer to a hash table element key dispose function or NULL.
 @item dispose_fn
-A pointer to a hash table dispose function.
+A pointer to a hash table element dispose function or NULL.
 @item table
 A pointer to a hash table.
 @end table
@@ -946,7 +951,7 @@
 @end table
 @end deftypefun
 
address@hidden pdf_status_t pdf_hash_destroy (pdf_hash_t @var{table})
address@hidden pdf_status_t pdf_hash_destroy (pdf_hash_t address@hidden)
 
 Destroy a hash table. The elements of the table are disposed first.
 
@@ -954,7 +959,7 @@
 @item Parameters
 @table @var
 @item table
-A hash table.
+A hash table pointer.
 @end table
 @item Returns
 A pdf status value:
@@ -1042,8 +1047,6 @@
 @table @code
 @item PDF_OK
 The operation succeded.
address@hidden PDF_ENOMEM
-Not enough memory.
 @item PDF_ERROR
 The @var{key} is not associated with any element in @var{table}.
 @item PDF_EBADDATA
@@ -1059,7 +1062,7 @@
 @node Adding and removing elements
 @subsection Adding and removing elements
 
address@hidden pdf_status_t pdf_hash_add (pdf_hash_t @var{table}, const char 
@var{*key}, void @var{*element}) 
address@hidden pdf_status_t pdf_hash_add (pdf_hash_t @var{table}, const char 
@var{*key}, const void @var{*element}) 
 
 Adds the element @var{element} with the associated key @var{key} to 
@var{table}. If @var{key} already exists nothing is done.
 
@@ -1122,7 +1125,7 @@
 @node Searching elements
 @subsection Searching elements
 
address@hidden pdf_status_t pdf_hash_search (pdf_hash_t @var{table}, const char 
@var{*key}, void address@hidden)
address@hidden pdf_status_t pdf_hash_search (pdf_hash_t @var{table}, const char 
@var{*key}, const void address@hidden)
 
 Searches for the element associated with the given @var{key} in @var{table} 
and store it in @var{elem_pointer}.
 
@@ -1173,6 +1176,8 @@
 @table @code
 @item PDF_OK
 The operation succeeded.
address@hidden PDF_ENOMEM
+Not enough memory.
 @item PDF_EBADDATA
 Either @var{iterator} is NULL or @var{table} is invalid.
 @end table
@@ -1183,7 +1188,7 @@
 @end table
 @end deftypefun
 
address@hidden pdf_status_t pdf_hash_iterator_next (pdf_hash_iterator_t 
@var{iterator}, const char address@hidden)
address@hidden pdf_status_t pdf_hash_iterator_next (pdf_hash_iterator_t 
address@hidden, const char address@hidden)
 
 Retrieves the next key from @var{iterator}.
 
@@ -1191,7 +1196,7 @@
 @item Parameters
 @table @var
 @item iterator
-A Hash Table iterator.
+A Hash Table iterator pointer.
 @item key
 A pointer where to save the key.
 @end table
@@ -1212,15 +1217,15 @@
 @end table
 @end deftypefun
 
address@hidden pdf_status_t pdf_hash_iterator_free (pdf_hash_iterator_t 
@var{iterator})
address@hidden pdf_status_t pdf_hash_iterator_free (pdf_hash_iterator_t 
address@hidden)
 
 Free all resources used by @var{iterator}. 
 
 @table @strong
 @item Parameters
 @table @var
address@hidden table
-A Hash Table iterator.
address@hidden iterator
+A Hash Table iterator pointer.
 @end table
 @item Returns
 A pdf status value:
Index: src/Makefile.am
===================================================================
RCS file: /sources/pdf/libgnupdf/src/Makefile.am,v
retrieving revision 1.42
diff -u -r1.42 Makefile.am
--- src/Makefile.am     8 Apr 2008 10:02:52 -0000       1.42
+++ src/Makefile.am     17 Apr 2008 00:29:20 -0000
@@ -25,6 +25,8 @@
 
 LIST_MODULE_SOURCES = base/pdf-list.c base/pdf-list.h
 
+HASH_MODULE_SOURCES = base/pdf-hash.c base/pdf-hash.h
+
 ALLOC_MODULE_SOURCES = base/pdf-alloc.c base/pdf-alloc.h
 
 TYPES_MODULE_SOURCES = base/pdf-types.c base/pdf-types.h
@@ -64,7 +66,8 @@
                      $(STM_MODULE_SOURCES) \
                      $(ERROR_MODULE_SOURCES) \
                      $(LIST_MODULE_SOURCES) \
-                     $(TEXT_MODULE_SOURCES)
+                     $(TEXT_MODULE_SOURCES) \
+                     $(HASH_MODULE_SOURCES)
 
 # Library sources
 
@@ -89,6 +92,7 @@
               base/pdf-error.h \
               base/pdf-alloc.h \
               base/pdf-list.h \
+              base/pdf-hash.h \
               base/pdf-stm-f-a85.h \
               base/pdf-stm-f-ahex.h \
               base/pdf-stm-f-fax.h \
Index: torture/unit/Makefile.am
===================================================================
RCS file: /sources/pdf/libgnupdf/torture/unit/Makefile.am,v
retrieving revision 1.8
diff -u -r1.8 Makefile.am
--- torture/unit/Makefile.am    13 Apr 2008 20:05:22 -0000      1.8
+++ torture/unit/Makefile.am    17 Apr 2008 00:29:20 -0000
@@ -37,11 +37,24 @@
 # Unit tests for the Stream Module test suite
 TEST_SUITE_STM = base/stm/pdf-create-file-stm.c
 
+TEST_SUITE_HASH = base/hash/pdf-hash-create.c \
+                 base/hash/pdf-hash-destroy.c \
+                 base/hash/pdf-hash-add.c \
+                 base/hash/pdf-hash-remove.c \
+                 base/hash/pdf-hash-size.c \
+                 base/hash/pdf-hash-search.c \
+                 base/hash/pdf-hash-key-p.c \
+                 base/hash/pdf-hash-rename.c \
+                 base/hash/pdf-hash-iterator.c \
+                 base/hash/pdf-hash-iterator-next.c \
+                 base/hash/pdf-hash-iterator-free.c
+
 TEST_SUITE_LIST = base/list/pdf-list-add-first.c \
                   base/list/pdf-list-add-at.c \
                   base/list/pdf-list-add-last.c \
                   base/list/pdf-list-size.c \
                   base/list/pdf-list-create.c \
+                  base/list/pdf-list-destroy.c \
                   base/list/pdf-list-get-at.c \
                   base/list/pdf-list-indexof.c \
                   base/list/pdf-list-indexof-from.c \
@@ -49,6 +62,7 @@
                   base/list/pdf-list-iterator.c \
                   base/list/pdf-list-iterator-from-to.c \
                   base/list/pdf-list-iterator-next.c \
+                  base/list/pdf-list-iterator-free.c \
                   base/list/pdf-list-next-node.c \
                   base/list/pdf-list-node-value.c \
                   base/list/pdf-list-previous-node.c \
@@ -102,13 +116,14 @@
 
 TEST_FILES = $(TEST_SUITE_STM) \
              $(TEST_SUITE_LIST) \
-             $(TEST_SUITE_TEXT)
+             $(TEST_SUITE_TEXT) \
+             $(TEST_SUITE_HASH)
 
 TSUITE_FILES = base/alloc/tsuite-alloc.c \
                base/stm/tsuite-stm.c \
                base/list/tsuite-list.c \
-               base/text/tsuite-text.c
-
+               base/text/tsuite-text.c \
+               base/hash/tsuite-hash.c
 
 runtests_SOURCES = runtests.c $(TSUITE_FILES) $(TEST_FILES)
 
Index: torture/unit/runtests.c
===================================================================
RCS file: /sources/pdf/libgnupdf/torture/unit/runtests.c,v
retrieving revision 1.5
diff -u -r1.5 runtests.c
--- torture/unit/runtests.c     25 Mar 2008 10:56:44 -0000      1.5
+++ torture/unit/runtests.c     17 Apr 2008 00:29:20 -0000
@@ -13,6 +13,7 @@
 extern Suite *tsuite_stm (void);
 extern Suite *tsuite_list (void);
 extern Suite *tsuite_text (void);
+extern Suite *tsuite_hash (void);
 
 int
 main (int argc, char **argv)
@@ -27,6 +28,7 @@
   srunner_add_suite (sr, tsuite_stm ());
   srunner_add_suite (sr, tsuite_list ());
   srunner_add_suite (sr, tsuite_text ());
+  srunner_add_suite (sr, tsuite_hash ());
   /* Set log file */
   srunner_set_log (sr, "ut.log");
   /* Run all test suites */
Index: torture/unit/base/list/tsuite-list.c
===================================================================
RCS file: /sources/pdf/libgnupdf/torture/unit/base/list/tsuite-list.c,v
retrieving revision 1.2
diff -u -r1.2 tsuite-list.c
--- torture/unit/base/list/tsuite-list.c        13 Apr 2008 20:05:22 -0000      
1.2
+++ torture/unit/base/list/tsuite-list.c        17 Apr 2008 00:29:20 -0000
@@ -1,4 +1,4 @@
-/* -*- mode: C -*- Time-stamp: "2008-04-13 15:32:48 gerel"
+/* -*- mode: C -*- Time-stamp: "2008-04-16 20:40:33 gerel"
  *
  *       File:         tsuite-list.c
  *       Date:         Wed Mar  12 12:43:00 2008
@@ -26,6 +26,8 @@
 #include <check.h>
 
 extern TCase *test_pdf_list_create (void);
+extern TCase *test_pdf_list_destroy (void);
+extern TCase *test_pdf_list_iterator_free (void);
 extern TCase *test_pdf_list_size (void);
 extern TCase *test_pdf_list_add_first (void);
 extern TCase *test_pdf_list_add_at (void);
@@ -64,6 +66,7 @@
   s = suite_create("list");
   
   suite_add_tcase (s, test_pdf_list_create ());
+  suite_add_tcase (s, test_pdf_list_destroy ());
   suite_add_tcase (s, test_pdf_list_size ());
   suite_add_tcase (s, test_pdf_list_add_first ());
   suite_add_tcase (s, test_pdf_list_add_at ());
@@ -85,6 +88,7 @@
   suite_add_tcase (s, test_pdf_list_iterator ());
   suite_add_tcase (s, test_pdf_list_iterator_from_to ());
   suite_add_tcase (s, test_pdf_list_iterator_next ());
+  suite_add_tcase (s, test_pdf_list_iterator_free ());
   suite_add_tcase (s, test_pdf_list_sorted_add ());
   suite_add_tcase (s, test_pdf_list_sorted_remove ());
   suite_add_tcase (s, test_pdf_list_sorted_search ());


###


New files

##

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:41:00 gerel"
 *
 *       File:         pdf-list-destroy.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_list_destroy
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_list_destroy_001
 * Description:
 *   Try to destroy an empty list.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_list_destroy_001)
{
  pdf_list_t list;
  
  pdf_list_create (NULL, NULL, 0, &list);

  fail_if (pdf_list_destroy (list) != PDF_OK);

}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_list_destroy (void)
{
  TCase *tc = tcase_create("pdf_list_destroy");
  tcase_add_test(tc, pdf_list_destroy_001);
  return tc;
}

/* End of pdf-list-destroy.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:41:29 gerel"
 *
 *       File:         pdf-list-iterator-free.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_list_iterator_free
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_list_iterator_free_001
 * Description:
 *   Try to destroy an iterator.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_list_iterator_free_001)
{
  pdf_list_t list;
  pdf_list_iterator_t itr;

  pdf_list_create (NULL, NULL, 0, &list);

  pdf_list_iterator (list, &itr);

  fail_if (pdf_list_iterator_free (&itr) != PDF_OK);

  pdf_list_destroy (list);
}
END_TEST



/*
 * Test case creation function
 */
TCase *
test_pdf_list_iterator_free (void)
{
  TCase *tc = tcase_create("pdf_list_iterator_free");
  tcase_add_test(tc, pdf_list_iterator_free_001);

  return tc;
}

/* End of pdf-list-iterator-free.c */


/* -*- mode: C -*- Time-stamp: "2008-04-16 20:16:29 gerel"
 *
 *       File:         pdf-hash-create.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_create
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>


/*
 * Test: pdf_hash_create_001
 * Description:
 *   Try to create an empty hash.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_create_001)
{
  pdf_hash_t table;

  fail_if (pdf_hash_create (NULL, NULL, &table) != PDF_OK);

  pdf_hash_destroy (&table);
}
END_TEST

/*
 * Test: pdf_hash_create_002
 * Description:
 *   Try to create an empty hash with a NULL pointer.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_create_002)
{
  fail_if (pdf_hash_create (NULL, NULL, NULL) != PDF_EBADDATA);
}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_create (void)
{
  TCase *tc = tcase_create("pdf_hash_create");
  tcase_add_test(tc, pdf_hash_create_001);
  tcase_add_test(tc, pdf_hash_create_002);
  return tc;
}

/* End of pdf-hash-create.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:25:16 gerel"
 *
 *       File:         pdf-hash-destroy.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_destroy
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_destroy_001
 * Description:
 *   Try to destroy an empty hash.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_destroy_001)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_destroy (&table) != PDF_OK);
}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_destroy (void)
{
  TCase *tc = tcase_create("pdf_hash_destroy");
  tcase_add_test(tc, pdf_hash_destroy_001);
  return tc;
}

/* End of pdf-hash-destroy.c */


/* -*- mode: C -*- Time-stamp: "2008-04-16 20:29:44 gerel"
 *
 *       File:         pdf-hash-add.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_add
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_add_001
 * Description:
 *   Try to add key/value pair to an empty table.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_add_001)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_add (table, "key", "val") != PDF_OK);

  pdf_hash_destroy (&table);
}
END_TEST


/*
 * Test: pdf_hash_add_002
 * Description:
 *   Try to add NULL/value pair to an empty table.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_add_002)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_add (table, NULL, "val") != PDF_EBADDATA);

  pdf_hash_destroy (&table);
}
END_TEST


/*
 * Test: pdf_hash_add_003
 * Description:
 *   Try to add key/NULL pair to an empty table.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_add_003)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_add (table, "key", NULL) != PDF_EBADDATA);

  pdf_hash_destroy (&table);
}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_add (void)
{
  TCase *tc = tcase_create("pdf_hash_add");
  tcase_add_test(tc, pdf_hash_add_001);
  tcase_add_test(tc, pdf_hash_add_002);
  tcase_add_test(tc, pdf_hash_add_003);
  return tc;
}

/* End of pdf-hash-add.c */


/* -*- mode: C -*- Time-stamp: "2008-04-16 20:30:42 gerel"
 *
 *       File:         pdf-hash-remove.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_remove
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_remove_001
 * Description:
 *   Try to remove an existent element.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_remove_001)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");

  fail_if (pdf_hash_remove (table, "key") != PDF_OK);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test: pdf_hash_remove_002
 * Description:
 *   Try to remove a non-existent element.
 * Success condition:
 *   Returns PDF_ERROR
 */
START_TEST (pdf_hash_remove_002)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");

  fail_if (pdf_hash_remove (table, "ke") != PDF_ERROR);

  pdf_hash_destroy (&table);

}
END_TEST

/*
 * Test: pdf_hash_remove_003
 * Description:
 *   Try to remove a element given a NULL key.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_remove_003)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");

  fail_if (pdf_hash_remove (table, NULL) != PDF_EBADDATA);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_remove (void)
{
  TCase *tc = tcase_create("pdf_hash_remove");
  tcase_add_test(tc, pdf_hash_remove_001);
  tcase_add_test(tc, pdf_hash_remove_002);
  tcase_add_test(tc, pdf_hash_remove_003);
  return tc;
}

/* End of pdf-hash-remove.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:30:54 gerel"
 *
 *       File:         pdf-hash-search.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_search
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_search_001
 * Description:
 *   Try to search an existent element.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_search_001)
{
  pdf_hash_t table;
  char *s;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
  
  fail_if (pdf_hash_search (table, "key", &s) != PDF_OK);

  pdf_hash_destroy (&table);

}
END_TEST

/*
 * Test: pdf_hash_search_002
 * Description:
 *   Try to search a non-existent element.
 * Success condition:
 *   Returns PDF_ERROR
 */
START_TEST (pdf_hash_search_002)
{
  pdf_hash_t table;
  char *s;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
  
  fail_if (pdf_hash_search (table, "ke", &s) != PDF_ERROR);

  pdf_hash_destroy (&table);

}
END_TEST

/*
 * Test: pdf_hash_search_003
 * Description:
 *   Try to search an element given a NULL elem_pointer.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_search_003)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
  
  fail_if (pdf_hash_search (table, "key", NULL) != PDF_EBADDATA);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test: pdf_hash_search_004
 * Description:
 *   Try to search an element given a NULL key.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_search_004)
{
  pdf_hash_t table;
  char *s;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
  
  fail_if (pdf_hash_search (table, NULL, &s) != PDF_EBADDATA);

  pdf_hash_destroy (&table);

}
END_TEST



/*
 * Test case creation function
 */
TCase *
test_pdf_hash_search (void)
{
  TCase *tc = tcase_create("pdf_hash_search");
  tcase_add_test(tc, pdf_hash_search_001);
  tcase_add_test(tc, pdf_hash_search_002);
  tcase_add_test(tc, pdf_hash_search_003);
  tcase_add_test(tc, pdf_hash_search_004);
  return tc;
}

/* End of pdf-hash-search.c */


/* -*- mode: C -*- Time-stamp: "2008-04-16 20:30:36 gerel"
 *
 *       File:         pdf-hash-key-p.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_key_p
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_key_p_001
 * Description:
 *   Try to prove that a given key exist.
 * Success condition:
 *   Returns PDF_TRUE
 */
START_TEST (pdf_hash_key_p_001)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
 
  fail_if (pdf_hash_key_p (table, "key") != PDF_TRUE);

  pdf_hash_destroy (&table);
}
END_TEST


/*
 * Test: pdf_hash_key_p_002
 * Description:
 *   Try to prove that a given key doesn't exist.
 * Success condition:
 *   Returns PDF_FALSE
 */
START_TEST (pdf_hash_key_p_002)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
 
  fail_if (pdf_hash_key_p (table, "ke") != PDF_FALSE);

  pdf_hash_destroy (&table);
}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_key_p (void)
{
  TCase *tc = tcase_create("pdf_hash_key_p");
  tcase_add_test(tc, pdf_hash_key_p_001);
  tcase_add_test(tc, pdf_hash_key_p_002);
  return tc;
}

/* End of pdf-hash-key-p.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:30:47 gerel"
 *
 *       File:         pdf-hash-rename.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_rename
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_rename_001
 * Description:
 *   Try to rename an existent key and prove it was renamed.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_rename_001)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");

  fail_if (pdf_hash_rename (table, "key", "ke") != PDF_OK);
  fail_if (pdf_hash_key_p (table, "ke") != PDF_TRUE);

  pdf_hash_destroy (&table);
}
END_TEST

/*
 * Test: pdf_hash_rename_002
 * Description:
 *   Try to rename a non-existent key.
 * Success condition:
 *   Returns PDF_ERROR
 */
START_TEST (pdf_hash_rename_002)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");

  fail_if (pdf_hash_rename (table, "ke", "key") != PDF_ERROR);

  pdf_hash_destroy (&table);
}
END_TEST

/*
 * Test: pdf_hash_rename_003
 * Description:
 *   Try to rename a key given a NULL key.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_rename_003)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_rename (table, NULL, "ke") != PDF_EBADDATA);

  pdf_hash_destroy (&table);
}
END_TEST

/*
 * Test: pdf_hash_rename_004
 * Description:
 *   Try to rename a key given a NULL new_key.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_rename_004)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_rename (table, "key", NULL) != PDF_EBADDATA);

  pdf_hash_destroy (&table);
}
END_TEST



/*
 * Test case creation function
 */
TCase *
test_pdf_hash_rename (void)
{
  TCase *tc = tcase_create("pdf_hash_rename");
  tcase_add_test(tc, pdf_hash_rename_001);
  tcase_add_test(tc, pdf_hash_rename_002);
  tcase_add_test(tc, pdf_hash_rename_003);
  tcase_add_test(tc, pdf_hash_rename_004);
  return tc;
}

/* End of pdf-hash-rename.c */


/* -*- mode: C -*- Time-stamp: "2008-04-16 19:14:23 gerel"
 *
 *       File:         pdf-hash-size.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_size
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_size_001
 * Description:
 *   Try to get the size of an empty hash.
 * Success condition:
 *   Returns 0
 */
START_TEST (pdf_hash_size_001)
{
  pdf_hash_t table;

  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_size (table) != 0);

  pdf_hash_destroy (&table);
}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_size (void)
{
  TCase *tc = tcase_create("pdf_hash_size");
  tcase_add_test(tc, pdf_hash_size_001);
  return tc;
}

/* End of pdf-hash-size.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:30:29 gerel"
 *
 *       File:         pdf-hash-iterator.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_iterator
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_iterator_001
 * Description:
 *   Try to create an iterator.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_iterator_001)
{
  pdf_hash_t table;
  pdf_hash_iterator_t itr;
  
  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_iterator (table, &itr) != PDF_OK);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test: pdf_hash_iterator_002
 * Description:
 *   Try to create an iterator given a NULL iterator pointer.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_iterator_002)
{
  pdf_hash_t table;
  
  pdf_hash_create (NULL, NULL, &table);

  fail_if (pdf_hash_iterator (table, NULL) != PDF_EBADDATA);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_iterator (void)
{
  TCase *tc = tcase_create("pdf_hash_iterator");
  tcase_add_test(tc, pdf_hash_iterator_001);
  tcase_add_test(tc, pdf_hash_iterator_002);
  return tc;
}

/* End of pdf-hash-iterator.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:30:23 gerel"
 *
 *       File:         pdf-hash-iterator-next.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_iterator_next
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>


/*
 * Test: pdf_hash_iterator_next_001
 * Description:
 *   Try to iterate over an iterator.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_iterator_next_001)
{
  pdf_hash_t table;
  pdf_hash_iterator_t itr;
  char *key;
  
  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
  pdf_hash_iterator (table, &itr);

  fail_if (pdf_hash_iterator_next (&itr, &key) != PDF_OK);

  pdf_hash_destroy (&table);

}
END_TEST

/*
 * Test: pdf_hash_iterator_next_002
 * Description:
 *   Try to iterate over a NULL iterator.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_iterator_next_002)
{
  pdf_hash_t table;
  char *key;
  
  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");

  fail_if (pdf_hash_iterator_next (NULL, &key) != PDF_EBADDATA);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test: pdf_hash_iterator_next_003
 * Description:
 *   Try to iterate over an iterator given a NULL key pointer.
 * Success condition:
 *   Returns PDF_EBADDATA
 */
START_TEST (pdf_hash_iterator_next_003)
{
  pdf_hash_t table;
  pdf_hash_iterator_t itr;
 
  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_add (table, "key", "val");
  pdf_hash_iterator (table, &itr);

  fail_if (pdf_hash_iterator_next (&itr, NULL) != PDF_EBADDATA);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_iterator_next (void)
{
  TCase *tc = tcase_create("pdf_hash_iterator_next");
  tcase_add_test(tc, pdf_hash_iterator_next_001);
  tcase_add_test(tc, pdf_hash_iterator_next_002);
  tcase_add_test(tc, pdf_hash_iterator_next_003);
  return tc;
}

/* End of pdf-hash-iterator-next.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:30:15 gerel"
 *
 *       File:         pdf-hash-iterator-free.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Unit tests for pdf_hash_iterator_free
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdbool.h>
#include <stdio.h>
#include <pdf.h>
#include <check.h>

/*
 * Test: pdf_hash_iterator_free_001
 * Description:
 *   Try to free an iterator.
 * Success condition:
 *   Returns PDF_OK
 */
START_TEST (pdf_hash_iterator_free_001)
{
  pdf_hash_t table;
  pdf_hash_iterator_t itr;

  pdf_hash_create (NULL, NULL, &table);
  pdf_hash_iterator (table, &itr);

  fail_if (pdf_hash_iterator_free (&itr) != PDF_OK);

  pdf_hash_destroy (&table);

}
END_TEST


/*
 * Test case creation function
 */
TCase *
test_pdf_hash_iterator_free (void)
{
  TCase *tc = tcase_create("pdf_hash_iterator_free");
  tcase_add_test(tc, pdf_hash_iterator_free_001);
  return tc;
}

/* End of pdf-hash-iterator-free.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:13:49 gerel"
 *
 *       File:         tsuite-hash.c
 *       Date:         Wed Mar  12 12:43:00 2008
 *
 *       GNU PDF Library - Testcase definition for the Hash module
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <check.h>

extern TCase *test_pdf_hash_create (void);
extern TCase *test_pdf_hash_destroy (void);
extern TCase *test_pdf_hash_key_p (void);
extern TCase *test_pdf_hash_rename (void);
extern TCase *test_pdf_hash_size (void);
extern TCase *test_pdf_hash_add (void);
extern TCase *test_pdf_hash_remove (void);
extern TCase *test_pdf_hash_search (void);
extern TCase *test_pdf_hash_iterator (void);
extern TCase *test_pdf_hash_iterator_next (void);
extern TCase *test_pdf_hash_iterator_free (void);

Suite *
tsuite_hash ()
{
  Suite *s;

  s = suite_create("hash");
  
  suite_add_tcase (s, test_pdf_hash_create ());
  suite_add_tcase (s, test_pdf_hash_destroy ());
  suite_add_tcase (s, test_pdf_hash_size ());
  suite_add_tcase (s, test_pdf_hash_add ());
  suite_add_tcase (s, test_pdf_hash_remove ());
  suite_add_tcase (s, test_pdf_hash_search ());
  suite_add_tcase (s, test_pdf_hash_key_p ());
  suite_add_tcase (s, test_pdf_hash_rename ());
  suite_add_tcase (s, test_pdf_hash_iterator ());
  suite_add_tcase (s, test_pdf_hash_iterator_next ());
  suite_add_tcase (s, test_pdf_hash_iterator_free ());

  return s;
}


/* End of tsuite-hash.c */

/* -*- mode: C -*- Time-stamp: "2008-04-16 14:20:33 gerel"
 *
 *       File:         pdf-hash.h
 *       Date:         Sat Apr  12 12:22:05 2008
 *
 *       GNU PDF Library - Header file for the Hash module
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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/>.
 */

#ifndef PDF_HASH_H
#define PDF_HASH_H

#include <stdio.h>
#include <pdf-list.h>

struct pdf_hash_element_s
{
  const char * key;
  const void * value;
};

typedef struct pdf_hash_element_s pdf_hash_element_t;

/* BEGIN PUBLIC */

typedef void (*pdf_hash_element_dispose_fn_t) (const void *elt);
typedef void (*pdf_hash_key_dispose_fn_t) (const void *key);

struct pdf_hash_s
{
  /* Both are gl_lists */
  void *elements, *keys;
  pdf_hash_element_dispose_fn_t dispose_fn;
};

struct pdf_hash_iterator_s
{
  void *gl_itr;
};

typedef struct pdf_hash_s pdf_hash_t;
typedef struct pdf_hash_iterator_s pdf_hash_iterator_t;


/* Creating and Destroying Hash Tables */

pdf_status_t
pdf_hash_create (pdf_hash_key_dispose_fn_t dispose_key_fn,
                 pdf_hash_element_dispose_fn_t dispose_fn, pdf_hash_t *table);

pdf_status_t
pdf_hash_destroy (pdf_hash_t *table);


/* Hash Table properties */

pdf_size_t
pdf_hash_size (pdf_hash_t table);


/* Working with keys */

pdf_bool_t
pdf_hash_key_p (pdf_hash_t table, const char *key);

pdf_status_t
pdf_hash_rename (pdf_hash_t table, const char *key, const char *new_key);


 /* Adding and removing elements */

pdf_status_t
pdf_hash_add (pdf_hash_t table, const char *key, const void *element);

pdf_status_t
pdf_hash_remove (pdf_hash_t table, const char *key);


/* Searching elements */

pdf_status_t
pdf_hash_search (pdf_hash_t table, const char *key, const void **elem_pointer);


/* Working with iterators  */

pdf_status_t
pdf_hash_iterator (pdf_hash_t table, pdf_hash_iterator_t *iterator);

pdf_status_t
pdf_hash_iterator_next (pdf_hash_iterator_t *iterator, const char **key);

pdf_status_t
pdf_hash_iterator_free (pdf_hash_iterator_t *iterator);



/* END PUBLIC */

#endif /* PDF_HASH_H */

/* End of pdf-hash.h */

/* -*- mode: C -*- Time-stamp: "2008-04-16 20:32:34 gerel"
 *
 *       File:         pdf-hash.c
 *       Date:         Sat Apr  12 12:22:05 2008
 *
 *       GNU PDF Library - Implementation file for the Hash module
 *
 */

/* Copyright (C) 2008 Free Software Foundation, Inc. */

/* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pdf-global.h>
#include <pdf-error.h>
#include <pdf-alloc.h>
#include <gl_array_list.h>
#include <gl_linkedhash_list.h>

#include <pdf-hash.h>

#define SIZE_BITS (sizeof (size_t) * CHAR_BIT)

/* Compute a hash code for a NUL-terminated string starting at X,
   and return the hash code.
   The result is platform dependent: it depends on the size of the 'size_t'
   type and on the signedness of the 'char' type.  */
static size_t hash_pjw (const void *x);

/* Returns true if both element string keys are equal */
static bool elem_key_equal (const void *elt1, const void *elt2);

/* Returns true if both string keys are equal */
static bool key_equal (const void *key1, const void *key2);

/* Returns 1 if str is only composed by numbers, 0 otherwise. */
static int key_numeric_p (const char *str);

/* Returns <0,=0,>0 if key1 is <,==, or > than key2 */
static int key_numeric_cmp (const char *key1, const char *key2);

/* Returns <0,=0,>0 if key1 is <,==, or > than key2 */
static int key_compare (const void *key1, const void *key2);

pdf_status_t
pdf_hash_create (pdf_hash_key_dispose_fn_t dispose_key_fn,
                 pdf_hash_element_dispose_fn_t dispose_fn, pdf_hash_t *table)
{
  pdf_status_t st;

  st = PDF_OK;

  if (table != NULL)
    {
      table->dispose_fn = dispose_fn;
      table->elements = gl_list_create_empty (GL_LINKEDHASH_LIST,
                                              elem_key_equal, hash_pjw,
                                              NULL, 0);
      table->keys = gl_list_create_empty (GL_ARRAY_LIST, key_equal, NULL,
                                          dispose_key_fn, 0);

      if (table->elements == NULL || table->keys == NULL)
        {
          st = PDF_ENOMEM;
        }
    }
  else
    {
      st = PDF_EBADDATA;
    }

  return st;
}


pdf_status_t
pdf_hash_destroy (pdf_hash_t *table)
{
  gl_list_iterator_t itr;
  const void *elt;

  if (table != NULL && table->elements != NULL && table->keys != NULL)
    {
      itr = gl_list_iterator ((gl_list_t)table->elements);
      while (gl_list_iterator_next (&itr, &elt, NULL))
        {
          if (table->dispose_fn != NULL)
            {
              table->dispose_fn ((void*) ((pdf_hash_element_t*)elt)->value);
            }
        }
      gl_list_iterator_free (&itr);
  
      gl_list_free((gl_list_t)table->elements);
      gl_list_free((gl_list_t)table->keys);
    }
  
  return PDF_OK;
}


pdf_size_t
pdf_hash_size (pdf_hash_t table)
{
  return (gl_list_size ((gl_list_t) table.keys));
}


pdf_bool_t
pdf_hash_key_p (pdf_hash_t table, const char *key)
{
  if (gl_sortedlist_search ((gl_list_t) table.keys, key_compare,
                      (const void*) key) != NULL)
    {
      return PDF_TRUE;
    }
  
  return PDF_FALSE;
}


pdf_status_t
pdf_hash_rename (pdf_hash_t table, const char *key, const char *new_key)
{
  pdf_status_t st;
  gl_list_node_t knode, vnode;
  pdf_hash_element_t elem, *updated;
  
  st = PDF_OK;

  if (key != NULL && new_key != NULL)
    {
      knode = gl_sortedlist_search ((gl_list_t) table.keys, key_compare, key);
      if (knode != NULL)
        {
          /* get the node from key and update its key*/
          elem.key = key;
          vnode = gl_list_search ((gl_list_t)table.elements, &elem);
          updated = (pdf_hash_element_t*)
            gl_list_node_value ((gl_list_t)table.elements, vnode);
          updated->key = new_key;

          /* update the lists */
          gl_list_remove_node ((gl_list_t)table.elements, vnode);
          gl_list_add_first ((gl_list_t)table.elements, updated);
          gl_sortedlist_remove ((gl_list_t)table.keys, key_compare, key);
          gl_sortedlist_add ((gl_list_t)table.keys, key_compare, new_key);
        }
      else
        {
          st = PDF_ERROR;
        }
    }
  else
    {
      st = PDF_EBADDATA;
    }

  return st;

}


pdf_status_t
pdf_hash_add (pdf_hash_t table, const char *key, const void *element)
{
  pdf_status_t st;
  pdf_hash_element_t *newelt;
  st = PDF_OK;

  if (key != NULL && element != NULL)
    {
      newelt = pdf_alloc (sizeof (pdf_hash_element_t));
      if (newelt != NULL)
        {
          newelt->key = key;
          newelt->value = element;
          gl_list_add_first ((gl_list_t)table.elements, newelt);
          gl_sortedlist_add ((gl_list_t)table.keys, key_compare, key);
        }
      else
        {
          st = PDF_ENOMEM;
        }
    }
  else
    {
      st = PDF_EBADDATA;
    }
  

  return st;

}


pdf_status_t
pdf_hash_remove (pdf_hash_t table, const char *key)
{
  pdf_status_t st;
  pdf_hash_element_t elt, *removed;
  gl_list_node_t node;
  
  st = PDF_OK;

  if (key != NULL)
    {
      elt.key = key;
      node = gl_list_search ((gl_list_t)table.elements, &elt);
      if (node != NULL)
        {
          if (table.dispose_fn != NULL)
            {
              removed = (pdf_hash_element_t*)
                gl_list_node_value((gl_list_t)table.elements, node);
              table.dispose_fn (removed->value);
            }
          gl_list_remove_node ((gl_list_t)table.elements, node);
          gl_sortedlist_remove ((gl_list_t)table.keys, key_compare, key);
        }
      else
        {
          st = PDF_ERROR;
        }
    }
  else
    {
      st = PDF_EBADDATA;
    }
  

  return st;

}


pdf_status_t
pdf_hash_search (pdf_hash_t table, const char *key, const void **elem_pointer)
{
  pdf_status_t st;
  gl_list_node_t node;
  pdf_hash_element_t elem, *searched;

  st = PDF_OK;

  if (key != NULL && elem_pointer != NULL)
    {
      elem.key = key;
      node = gl_list_search ((gl_list_t)table.elements, &elem);
      if (node != NULL)
        {
          searched = (pdf_hash_element_t*)
            gl_list_node_value ((gl_list_t)table.elements, node);
          *elem_pointer = searched->value;
        }
      else
        {
          st = PDF_ERROR;
        }
    }
  else
    {
      st = PDF_EBADDATA;
    }  

  return st;

}


pdf_status_t
pdf_hash_iterator (pdf_hash_t table, pdf_hash_iterator_t *iterator)
{
  pdf_status_t st;

  st = PDF_OK;

  if (iterator != NULL)
    {
      iterator->gl_itr = pdf_alloc (sizeof(gl_list_iterator_t));
      if (iterator->gl_itr != NULL)
        {
          *((gl_list_iterator_t*)iterator->gl_itr) =
            gl_list_iterator ((gl_list_t)table.keys);
        }
      else
        {
          st = PDF_ENOMEM;
        }
    }
  else
    {
      st = PDF_EBADDATA;
    }

  return st;

}


pdf_status_t
pdf_hash_iterator_next (pdf_hash_iterator_t *iterator, const char **key)
{
  pdf_status_t st;

  st = PDF_OK;

  if (iterator != NULL && key != NULL)
    {
      if (!gl_list_iterator_next((gl_list_iterator_t*)iterator->gl_itr,
                                 (const void**)key, NULL))
        {
          st = PDF_ERROR;
        }    
    }
  else
    {
      st = PDF_EBADDATA;
    }

  return st;
}


pdf_status_t
pdf_hash_iterator_free (pdf_hash_iterator_t *iterator)
{

  if (iterator != NULL && iterator->gl_itr != NULL)
    {
      gl_list_iterator_free ((gl_list_iterator_t*)iterator->gl_itr);
      pdf_dealloc (iterator->gl_itr);
    }

  return PDF_OK;
}

static size_t
hash_pjw (const void *elt)
{
  const pdf_hash_element_t *hashelem;
  const char *s;
  size_t h = 0;

  hashelem = (const pdf_hash_element_t*) elt;

  for (s = hashelem->key; *s; s++)
    h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));

  return h;
}


static bool
elem_key_equal (const void *elt1, const void *elt2)
{
  const char *key1, *key2;

  key1 = ((pdf_hash_element_t*)elt1)->key;
  key2 = ((pdf_hash_element_t*)elt2)->key;
  
  return (!strcmp(key1,key2));
}


static bool
key_equal (const void *key1, const void *key2)
{
  return (!strcmp((const char*)key1,(const char*)key2));
}


static int
key_numeric_p (const char *str)
{
  while (str != NULL)
    {
      if (*str < '0' || *str > '9')
        {
          return 0;
        }         
      str++;
    }
  return 1;
}


static int
key_numeric_cmp (const char *key1, const char *key2)
{
  unsigned int num1, num2;

  num1 = atoi (key1);
  num2 = atoi (key2);

  if (num1 > num2)
    {
      return 1;
    }
  else if (num1 < num2)
    {
      return -1;
    }
  return 0;
}


static int
key_compare (const void *key1, const void *key2)
{
  if (key_numeric_p(key1))
    {
      if (key_numeric_p(key2))
        {
          return (key_numeric_cmp(key1, key2));
        }
      /* key2 is non-numeric so key1 > key2 */
      return 1;
    }
  /* key1 non-numeric */
  if (key_numeric_p(key2))
    {
      return -1;
    }
  /* both are non-numeric */
  return (strcmp((const char*)key1,(const char*)key2));
}


/* End of pdf-hash.c */

###


I hope I'm not missing anything.


Regarding the Hash Module implementation, I prefered to add a dispose
function for string keys, thus, the caller decides. I see one drawback but one
benefit also which I considered of greater importance.

The drawback, we can't use hash tables mixed of dinamically and statically
allocated string keys (since the caller decides whether to do free() or not when
defining the disose function). This doesn't seem a really bad thing, I think
that most work will be about reading and dinamically allocate PDF objects.

The benefit, we will use less memory when using tables composed only by
statically allocated string keys. (e.g. object creation)

If you think this is not a good idea I'll patch the code, no problem.

cheers

-gerel





reply via email to

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