Merge branch 'main' of feistymeow.org:feisty_meow
[feisty_meow.git] / nucleus / library / tests_structures / test_hash_table.cpp
index 0e2e214c65fec5ed087cf14ea82220e5cd916225..4d06bab46f11d5a14e64dd09d48d58fd49976ce0 100644 (file)
 * Please send any updates to: fred@gruntose.com                               *
 */
 
-#include <mathematics/chaos.h>
+#include <application/hoople_main.h>
 #include <basis/guards.h>
 #include <basis/astring.h>
-#include <structures/set.h>
-#include <structures/byte_hasher.h>
-#include <structures/hash_table.h>
-#include <timely/time_stamp.h>
-#include <application/hoople_main.h>
 #include <loggers/console_logger.h>
 #include <loggers/file_logger.h>
+#include <mathematics/chaos.h>
+#include <structures/byte_hasher.h>
+#include <structures/hash_table.h>
+#include <structures/set.h>
 #include <structures/static_memory_gremlin.h>
 #include <textual/string_manipulation.h>
+#include <timely/time_stamp.h>
 #include <unit_test/unit_base.h>
 
 using namespace application;
 using namespace basis;
-///using namespace configuration;
 using namespace mathematics;
 using namespace filesystem;
 using namespace loggers;
@@ -48,44 +47,24 @@ const int MAX_ELEMENTS = 8;
 
 //////////////
 
-enum test_actions {
-  FIRST_TEST = 38,  // place-holder.
-  ADD = FIRST_TEST,
-    // adds an item that is probably new.
-  ADD_ADD,
-    // adds an item that is probably new, followed by another item under the
-    // same key id.  this ensures the overwriting gets tested.
-  ZAP,
-    // finds an item we know is in the list and whacks it.
-  ADD_ZAP,
-    // adds a new item and immediately finds and zaps it.
-  ZAP_ADD,
-    // zaps an item that we know about and then adds a new item with the same
-    // identifier.
-  FIND,
-    // locates an item in the list which we know should exist.
-  ACQUIRE,
-    // grabs an item out of the list (and tosses it).
-  FIND_ZAP_ADD,
-    // finds an item we know should exist, zaps it out of the list, then adds
-    // a new item with the same id.
-  ACQUIRE_ADD_ZAP,
-    // removes an item from the list that we know should be there, adds it back
-    // in, and then whacks it.
-  FIND_ADD_FIND,
-    // find an item with a particular id (one that we know should be in the
-    // list) and then adds a different item using the same id.  the new item
-    // is then sought.
-  RESET,
-    // tosses all data out of the hash table.  not done very often.
-  CHECK_SANITY,
-    // look for any problems or irregularities; print the contents of the list
-    // if any are found.
-  REHASH,
-    // resizes the hash table.
-  COPY,
-    // copies a hash table to another hash table.
-  LAST_TEST = COPY // place-holder; must equal test just prior.
+enum test_actions
+{
+       FIRST_TEST = 38,  // place-holder.
+       ADD = FIRST_TEST,  // adds an item that is probably new.
+       ADD_ADD,  // adds a probably new item, then adds different item under same key id to test overwriting.
+       ZAP,  // finds an item we know is in the list and whacks it.
+       ADD_ZAP,  // adds a new item and immediately finds and zaps it.
+       ZAP_ADD,  // zaps an item that we know about and then adds a new item with the same identifier.
+       FIND,  // locates an item in the list which we know should exist.
+       ACQUIRE,  // grabs an item out of the list (and tosses it).
+       FIND_ZAP_ADD,  // finds an item we know should exist, zaps it out of the list, then adds a new item with the same id.
+       ACQUIRE_ADD_ZAP,  // removes an item from the list that we know should be there, adds it back in, and then whacks it.
+       FIND_ADD_FIND,  // finds item with particular id, adds different item using same id, refinds new item.
+       RESET,  // tosses all data out of the hash table.  not done very often.
+       CHECK_SANITY,  // look for any problems or irregularities; print the contents of the list if any are found.
+       REHASH,  // resizes the hash table.
+       COPY,  // copies a hash table to another hash table.
+       LAST_TEST = COPY  // place-holder; must equal test just prior.
 };
 
 //////////////
@@ -103,7 +82,7 @@ public:
 
   data_shuttle()
   : snacky_string(string_manipulation::make_random_name()),
-    chunk(chao.inclusive(100, 10000)) {}
+    chunk(chao.inclusive(100, 10000)), food_bar(0), hungry(false) {}
 };
 
 //////////////
@@ -116,7 +95,9 @@ public:
   DEFINE_CLASS_NAME("test_hash_table");
 
   int raw_random_id();  //!< returns an unvetted random number.
-  int unused_random_id();  //!< returns an unused (so far) random number.
+
+  //! returns an unused (so far) random number.
+  int unused_random_id();
 
   int execute();
     // the main startup for the test.
@@ -180,6 +161,7 @@ int test_hash_table::unused_random_id()
     int checking = raw_random_id();
     if (!_keys_in_use.member(checking)) return checking;  // got one.
   } // keep going until we find unused id.
+  return -1;  // this is a failure, but we will never get here.
 }
 
 int test_hash_table::execute()
@@ -265,7 +247,8 @@ bool test_hash_table::test_add()
   to_add->snacky_string = string_manipulation::make_random_name();
   to_add->food_bar = random_id;
   outcome expected = common::IS_NEW;
-  if (_keys_in_use.member(random_id)) common::EXISTING;
+  // make sure it doesn't exist already.
+  if (_keys_in_use.member(random_id)) return false;
   ASSERT_EQUAL(_the_table.add(random_id, to_add).value(), expected.value(),
       "add should give proper outcome based on expectation");
   if (_keys_in_use.member(random_id))
@@ -276,7 +259,7 @@ bool test_hash_table::test_add()
 
 //////////////
 
-hash_table<int, data_shuttle> *_hang_on = NIL;
+hash_table<int, data_shuttle> *_hang_on = NULL_POINTER;
   // must be set before calling the apply method.
 
 #undef UNIT_BASE_THIS_OBJECT
@@ -441,9 +424,9 @@ bool test_hash_table::test_find()
   _hits[FIND - FIRST_TEST]++;
   int rand_indy = randomizer().inclusive(0, _keys_in_use.elements() - 1);
   int find_key = _keys_in_use[rand_indy];
-  data_shuttle *found = NIL;
+  data_shuttle *found = NULL_POINTER;
   ASSERT_TRUE(_the_table.find(find_key, found), "key should be there as expected");
-  ASSERT_NON_NULL(found, "contents should not be NIL");
+  ASSERT_NON_NULL(found, "contents should not be null");
   ASSERT_EQUAL(found->food_bar, find_key, "stored key should be same as real key");
   ASSERT_TRUE(found->snacky_string.length(), "stored string should have length");
   return true;
@@ -478,9 +461,9 @@ bool test_hash_table::test_find_zap_add()
   int rand_indy = randomizer().inclusive(0, _keys_in_use.elements() - 1);
   // this is another key list invariant function, if it works.
   int find_key = _keys_in_use[rand_indy];
-  data_shuttle *found = NIL;
+  data_shuttle *found = NULL_POINTER;
   ASSERT_TRUE(_the_table.find(find_key, found), "key should be locateable");
-  ASSERT_NON_NULL(found, "key should not have NIL contents");
+  ASSERT_NON_NULL(found, "key should not have null contents");
   ASSERT_EQUAL(found->food_bar, find_key, "stored key should be equal to real key");
   ASSERT_TRUE(found->snacky_string.length(), "stored string should not have zero length");
   // zap.
@@ -538,3 +521,4 @@ return false;
 
 HOOPLE_MAIN(test_hash_table, )
 
+