From 8b6d6e4756008d412bd5daf3db44f3537deacd3f Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 3 Aug 2020 15:49:36 -0600 Subject: Create program to display hash table --- src/02/04/hash_test.c | 12 +++++++----- src/02/04/main.c | 28 +++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) (limited to 'src') 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 +#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; +} -- cgit v1.2.3