Commit 8b6d6e4

mo khan <mo.khan@gmail.com>
2020-08-03 21:49:36
Create program to display hash table
1 parent 3bf3ca9
Changed files (2)
src/02/04/hash_test.c
@@ -38,15 +38,16 @@ Ensure(HashTable, when_a_hash_collision_occurs) {
 
 Ensure(HashTable, when_inserting_multiple_items_into_the_hash_table) {
   Hash *hash = hash_init(13);
-
   int items[] = {1, 5, 21, 26, 39, 14, 15, 16, 17, 18, 19, 20, 111, 145, 146};
+  int n = sizeof(items) / sizeof(int);
 
-  for (int i = 0; i < sizeof(items); i++) {
-    int value = i * 10;
-    hash_set(hash, items[i], &value);
+  for (int i = 0; i < n; i++) {
+    int key = items[i];
+    long value = key * 10;
+    hash_set(hash, key, (void *)value);
   }
 
-  for (int i = 0; i < sizeof(items); i++) {
+  for (int i = 0; i < n; i++) {
     int key = items[i];
     assert_that(hash_get(hash, key), is_equal_to(key * 10));
   }
@@ -62,6 +63,7 @@ TestSuite *hash_table_tests() {
   add_test_with_context(suite, HashTable,
                         when_getting_a_values_for_a_key_that_has_been_inserted);
   add_test_with_context(suite, HashTable, when_a_hash_collision_occurs);
+  add_test_with_context(suite, HashTable, when_inserting_multiple_items_into_the_hash_table);
   return suite;
 }
 
src/02/04/main.c
@@ -1,3 +1,29 @@
 #include <stdio.h>
+#include "hash.h"
 
-int main(int argc, char *argv[]) { return 0; }
+int main(int argc, char *argv[]) {
+  printf("=== COMP-272 - Assignment 02 - Question 04 ===\n");
+  Hash *hash = hash_init(13);
+  int items[] = {1, 5, 21, 26, 39, 14, 15, 16, 17, 18, 19, 20, 111, 145, 146};
+  int n = sizeof(items) / sizeof(int);
+
+  printf("Insert items into hash\n");
+  for (int i = 0; i < n; i++) {
+    int key = items[i];
+    long value = key * 10;
+    printf("(%d:%d) ", key, value);
+    hash_set(hash, key, (void *)value);
+  }
+
+  printf("\nInspect hash table\n");
+  hash_inspect(hash);
+
+  printf("Retrieve each item from the table\n");
+  for (int i = 0; i < n; i++) {
+    int key = items[i];
+    printf("(%d:%d) ", key, hash_get(hash, key));
+  }
+
+  printf("\nBye\n");
+  return 0;
+}