summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-08-03 15:49:36 -0600
committermo khan <mo.khan@gmail.com>2020-08-03 15:49:36 -0600
commit8b6d6e4756008d412bd5daf3db44f3537deacd3f (patch)
treedc06b9839087b36db120c4ddcd6abb1af7a25f99
parent3bf3ca9b3aede625fdc818e01ad0128db7cce9f6 (diff)
Create program to display hash table
-rw-r--r--src/02/04/hash_test.c12
-rw-r--r--src/02/04/main.c28
2 files changed, 34 insertions, 6 deletions
diff --git a/src/02/04/hash_test.c b/src/02/04/hash_test.c
index 5f7b2ad..b10e9e1 100644
--- a/src/02/04/hash_test.c
+++ b/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;
}
diff --git a/src/02/04/main.c b/src/02/04/main.c
index 1044d74..0db6346 100644
--- a/src/02/04/main.c
+++ b/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;
+}