chiark / gitweb /
tests and doxygen for selection.c
authorRichard Kettlewell <rjk@greenend.org.uk>
Thu, 29 Nov 2007 19:09:50 +0000 (19:09 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Thu, 29 Nov 2007 19:09:50 +0000 (19:09 +0000)
lib/selection.c
lib/selection.h
lib/test.c

index a7db75f41fc20d736d6e557b30c18ae32ec067dd..c69e83d2310ed1b48fb6e68fbb2db9416f70e035 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2006 Richard Kettlewell
+ * Copyright (C) 2006, 2007 Richard Kettlewell
  *
  * 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
@@ -17,6 +17,9 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
+/** @file lib/selection.c
+ * @brief Select management for Disobedience
+ */
 
 #include <config.h>
 #include "types.h"
 #include "hash.h"
 #include "selection.h"
 
+/** @brief Create a new selection manager
+ * @return Pointer to @ref hash used to manage the selection
+ */
 hash *selection_new(void) {
   return hash_new(sizeof (int));
 }
 
+/** @brief Add or remove a key in a selection
+ * @param h Hash representing selection
+ * @param key Key to insert
+ * @param selected non-0 if key is selected, 0 if it is not
+ *
+ * @p key is copied so the pointer need not remain valid.  Newly selected keys
+ * are not marked as live.
+ */
 void selection_set(hash *h, const char *key, int selected) {
   if(selected)
     hash_add(h, key, xmalloc_noptr(sizeof (int)), HASH_INSERT_OR_REPLACE);
@@ -36,14 +50,32 @@ void selection_set(hash *h, const char *key, int selected) {
     hash_remove(h, key);
 }
 
+/** @brief Test whether a key is set in a selection
+ * @param h Hash representing selection
+ * @param key Key to check
+ * @return non-0 if key is present, 0 if it is not
+ */
 int selection_selected(hash *h, const char *key) {
   return hash_find(h, key) != 0;
 }
 
+/** @brief Invert a key's selection status
+ * @param h Hash representing selection
+ * @param key Key to flip
+ *
+ * If the key is selected as a result it is not marked as live.
+ */
 void selection_flip(hash *h, const char *key) {
   selection_set(h, key, !selection_selected(h, key));
 }
 
+/** @brief Mark a selection key as live
+ * @param h Hash representing selection
+ * @param key Key to mark as live
+ *
+ * Live keys will survive a call to selection_cleanup().  @p need not be in the
+ * selection (if it is not then the call will be ignored).
+ */
 void selection_live(hash *h, const char *key) {
   int *ptr = hash_find(h, key);
 
@@ -61,6 +93,11 @@ static int selection_cleanup_callback(const char *key,
   return 0;
 }
 
+/** @brief Delete all non-live keys from a selection
+ * @param h Hash representing selection
+ *
+ * See selection_live().
+ */
 void selection_cleanup(hash *h) {
   hash_foreach(h, selection_cleanup_callback, h);
 }
@@ -72,6 +109,9 @@ static int selection_empty_callback(const char *key,
   return 0;
 }
 
+/** @brief Remove all keys from a selection
+ * @param h Hash representing selection
+ */
 void selection_empty(hash *h) {
   hash_foreach(h, selection_empty_callback, h);
 }
index e625e8930050c5bb0972a00595073acf7c04b2b0..90896495f1e5901c2af988f24f2ce0418ac990a0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2006 Richard Kettlewell
+ * Copyright (C) 2006, 2007 Richard Kettlewell
  *
  * 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
@@ -17,6 +17,9 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
+/** @file lib/selection.c
+ * @brief Select management for Disobedience
+ */
 
 #ifndef SELECTION_H
 #define SELECTION_H
index e574976bfb1673dedab3fdc94b74b6612367c24c..71847fff1a16cbc026080d762fffa986acf1f9d3 100644 (file)
@@ -47,6 +47,8 @@
 #include "signame.h"
 #include "cache.h"
 #include "filepart.h"
+#include "hash.h"
+#include "selection.h"
 
 static int tests, errors;
 static int fail_first;
@@ -737,6 +739,44 @@ static void test_filepart(void) {
   check_string(extension("./foo.c"), ".c");
 }
 
+static void test_selection(void) {
+  hash *h;
+  fprintf(stderr, "test_selection\n");
+  insist((h = selection_new()) != 0);
+  selection_set(h, "one", 1);
+  selection_set(h, "two", 1);
+  selection_set(h, "three", 0);
+  selection_set(h, "four", 1);
+  insist(selection_selected(h, "one") == 1);
+  insist(selection_selected(h, "two") == 1);
+  insist(selection_selected(h, "three") == 0);
+  insist(selection_selected(h, "four") == 1);
+  insist(selection_selected(h, "five") == 0);
+  insist(hash_count(h) == 3);
+  selection_flip(h, "one"); 
+  selection_flip(h, "three"); 
+  insist(selection_selected(h, "one") == 0);
+  insist(selection_selected(h, "three") == 1);
+  insist(hash_count(h) == 3);
+  selection_live(h, "one");
+  selection_live(h, "two");
+  selection_live(h, "three");
+  selection_cleanup(h);
+  insist(selection_selected(h, "one") == 0);
+  insist(selection_selected(h, "two") == 1);
+  insist(selection_selected(h, "three") == 1);
+  insist(selection_selected(h, "four") == 0);
+  insist(selection_selected(h, "five") == 0);
+  insist(hash_count(h) == 2);
+  selection_empty(h);
+  insist(selection_selected(h, "one") == 0);
+  insist(selection_selected(h, "two") == 0);
+  insist(selection_selected(h, "three") == 0);
+  insist(selection_selected(h, "four") == 0);
+  insist(selection_selected(h, "five") == 0);
+  insist(hash_count(h) == 0);
+}
+
 int main(void) {
   fail_first = !!getenv("FAIL_FIRST");
   insist('\n' == 0x0A);
@@ -791,6 +831,8 @@ int main(void) {
   test_signame();
   /* cache.c */
   test_cache();
+  /* selection.c */
+  test_selection();
   fprintf(stderr,  "%d errors out of %d tests\n", errors, tests);
   return !!errors;
 }