Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include "hashmap.h"
- #ifdef COUNTING_WORDS
- #include "wordcount.h"
- #endif
- size_t mystrlen(const char *str) {
- return strlen(str) + 1;
- }
- void print_element(const char *key, void *data) {
- printf("(\"%s\", %s)\n", key, (char*)data);
- }
- void *resolve_collision(void *old_value, void *new_value) {
- return new_value;
- }
- unsigned your_own_hash(const char *key) {
- return (unsigned) strlen(key) + (unsigned) *key;
- }
- int main() {
- unsigned int key_space = 1024;
- HashMap * hm = create_hashmap(key_space);
- char * string_1 = "CSE2425";
- char * string_2 = "Embedded";
- char * string_3 = "Software";
- char * string_4 = "Pietro";
- char * string_5 = "Rustici";
- char * string_6 = "Italy";
- const char * key_1 = "abc";
- const char * key_2 = "acb";
- const char * key_3 = "bac";
- const char * key_4 = "bca";
- const char * key_5 = "cab";
- const char * key_6 = "cba";
- const char * key_7 = "xyz";
- const char * key_8 = "waz";
- // Insert ("abc" -> "CSE2425").
- insert_data(hm, key_1, string_1, resolve_collision);
- assert(memcmp(get_data(hm, key_1), string_1, mystrlen(string_1)) == 0);
- // Insert ("acb" -> "Embedded").
- insert_data(hm, key_2, string_2, resolve_collision);
- assert(memcmp(get_data(hm, key_2), string_2, mystrlen(string_2)) == 0);
- // Insert ("bac" -> "Software").
- insert_data(hm, key_3, string_3, resolve_collision);
- assert(memcmp(get_data(hm, key_3), string_3, mystrlen(string_3)) == 0);
- // Insert ("bca" -> "Pietro").
- insert_data(hm, key_4, string_4, resolve_collision);
- assert(memcmp(get_data(hm, key_4), string_4, mystrlen(string_4)) == 0);
- // Insert ("cab" -> "Rustici").
- insert_data(hm, key_5, string_5, resolve_collision);
- assert(memcmp(get_data(hm, key_5), string_5, mystrlen(string_5)) == 0);
- // Insert ("cba" -> "Italy").
- insert_data(hm, key_6, string_6, resolve_collision);
- assert(memcmp(get_data(hm, key_6), string_6, mystrlen(string_6)) == 0);
- // Insert ("xyz" -> "Italy").
- insert_data(hm, key_7, string_6, resolve_collision);
- assert(memcmp(get_data(hm, key_7), string_6, mystrlen(string_6)) == 0);
- // Insert ("xyz" -> "Italy").
- insert_data(hm, key_1, string_6, resolve_collision);
- assert(memcmp(get_data(hm, key_1), string_6, mystrlen(string_6)) == 0);
- // Get data for a not inserted key.
- assert(get_data(hm, key_8) == NULL);
- // Iterate the hash map
- iterate(hm, print_element);
- // remove the first element
- remove_data(hm, key_1, NULL);
- assert(get_data(hm, key_1) == NULL);
- // check if linked list is broken
- iterate(hm, print_element);
- // remove last element
- remove_data(hm, key_6, NULL);
- assert(get_data(hm, key_6) == NULL);
- // check if linked list is broken
- iterate(hm, print_element);
- // Remove middle element
- remove_data(hm, key_3, NULL);
- assert(get_data(hm, key_3) == NULL);
- // Try to remove already removed element
- remove_data(hm, key_3, NULL);
- assert(get_data(hm, key_3) == NULL);
- iterate(hm, print_element);
- #ifdef NEW_HASH
- printf("SETTING NEW HASH FUNCTION\n");
- set_hash_function(hm, your_own_hash);
- printf("\nHERE WE GO AGAIN!\n\n");
- // Iterate the hash map
- iterate(hm, print_element);
- #endif
- // Delete the hash map.
- delete_hashmap(hm, NULL);
- printf("TEST INTEGERS\n");
- const char * kkey_1 = "abc";
- const char * kkey_2 = "dfr";
- const int num1 = 10;
- const int num2 = 33;
- const int num3 = 5566;
- HashMap *hm2 = create_hashmap(8);
- printf("%p\n",&num1);
- insert_data(hm2, kkey_1, &num1, resolve_collision);
- assert(*(int *)get_data(hm2, kkey_1) == num1);
- insert_data(hm2, kkey_1, &num2, resolve_collision);
- assert(*(int *)get_data(hm2, kkey_1) == num2);
- insert_data(hm2, kkey_2, &num3, resolve_collision);
- assert(*(int *)get_data(hm2, kkey_2) == num3);
- remove_data(hm, kkey_1, NULL);
- assert(get_data(hm, kkey_1) == NULL);
- delete_hashmap(hm2, NULL);
- // wtf?
- //assert(get_data(hm, key_3) == NULL);
- #ifdef COUNTING_WORDS
- // Create a temporary file
- FILE *stream = tmpfile();
- // Write to the stream
- fprintf(stream, "\"Sed ut perspiciatis unde omnis iste natus error sit "
- "voluptatem accusantium doloremque laudantium, totam rem "
- "aperiam, eaque ipsa quae ab illo inventore veritatis et "
- "quasi architecto beatae 3vitae dicta sunt explicabo. Nemo "
- "enim ipsam voluptatem quia voluptas sit aspernatur aut "
- "odit aut fugit, sed quia consequuntur magni dolores eos "
- "qui ratione voluptatem £$%sequi nesciunt. Neque porro quisquam "
- "est, qui dolorem ipsum quia dolor sit amet, consectetur, "
- "adipisci velit, sed (9quia non numquam eius modi tempora "
- "incidunt ut labore et dolore magnam aliquam quaerat "
- "voluptatem. Ut enim ad minima veniam, quis nostrum "
- "exercitationem ullam corporis suscipit laboriosam, "
- "nisi ut aliquid ex ea commodi consequatur? Quis autem "
- "vel eum iure reprehenderit 12qui in ea voluptate velit "
- "esse quam nihil molestiae?^ consequatur, vel illum qui "
- "dolorem eum fugiat quo /voluptas nulla pariatur?\n");
- // Set the position to the start of the stream
- fseek(stream, 0, SEEK_SET);
- // Count the words
- count_words(stream);
- // Close the file
- fclose(stream);
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement