Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include "hashmap.h"
  5.  
  6. #ifdef COUNTING_WORDS
  7. #include "wordcount.h"
  8. #endif
  9.  
  10. size_t mystrlen(const char *str) {
  11. return strlen(str) + 1;
  12. }
  13.  
  14. void print_element(const char *key, void *data) {
  15. printf("(\"%s\", %s)\n", key, (char*)data);
  16. }
  17.  
  18. void *resolve_collision(void *old_value, void *new_value) {
  19. return new_value;
  20. }
  21.  
  22.  
  23. unsigned your_own_hash(const char *key) {
  24. return (unsigned) strlen(key) + (unsigned) *key;
  25. }
  26.  
  27. int main() {
  28.  
  29. unsigned int key_space = 1024;
  30. HashMap * hm = create_hashmap(key_space);
  31.  
  32. char * string_1 = "CSE2425";
  33. char * string_2 = "Embedded";
  34. char * string_3 = "Software";
  35. char * string_4 = "Pietro";
  36. char * string_5 = "Rustici";
  37. char * string_6 = "Italy";
  38. const char * key_1 = "abc";
  39. const char * key_2 = "acb";
  40. const char * key_3 = "bac";
  41. const char * key_4 = "bca";
  42. const char * key_5 = "cab";
  43. const char * key_6 = "cba";
  44. const char * key_7 = "xyz";
  45. const char * key_8 = "waz";
  46.  
  47. // Insert ("abc" -> "CSE2425").
  48. insert_data(hm, key_1, string_1, resolve_collision);
  49. assert(memcmp(get_data(hm, key_1), string_1, mystrlen(string_1)) == 0);
  50.  
  51. // Insert ("acb" -> "Embedded").
  52. insert_data(hm, key_2, string_2, resolve_collision);
  53. assert(memcmp(get_data(hm, key_2), string_2, mystrlen(string_2)) == 0);
  54.  
  55. // Insert ("bac" -> "Software").
  56. insert_data(hm, key_3, string_3, resolve_collision);
  57. assert(memcmp(get_data(hm, key_3), string_3, mystrlen(string_3)) == 0);
  58.  
  59. // Insert ("bca" -> "Pietro").
  60. insert_data(hm, key_4, string_4, resolve_collision);
  61. assert(memcmp(get_data(hm, key_4), string_4, mystrlen(string_4)) == 0);
  62.  
  63. // Insert ("cab" -> "Rustici").
  64. insert_data(hm, key_5, string_5, resolve_collision);
  65. assert(memcmp(get_data(hm, key_5), string_5, mystrlen(string_5)) == 0);
  66.  
  67. // Insert ("cba" -> "Italy").
  68. insert_data(hm, key_6, string_6, resolve_collision);
  69. assert(memcmp(get_data(hm, key_6), string_6, mystrlen(string_6)) == 0);
  70.  
  71. // Insert ("xyz" -> "Italy").
  72. insert_data(hm, key_7, string_6, resolve_collision);
  73. assert(memcmp(get_data(hm, key_7), string_6, mystrlen(string_6)) == 0);
  74.  
  75. // Insert ("xyz" -> "Italy").
  76. insert_data(hm, key_1, string_6, resolve_collision);
  77. assert(memcmp(get_data(hm, key_1), string_6, mystrlen(string_6)) == 0);
  78.  
  79. // Get data for a not inserted key.
  80. assert(get_data(hm, key_8) == NULL);
  81.  
  82. // Iterate the hash map
  83. iterate(hm, print_element);
  84.  
  85. // remove the first element
  86. remove_data(hm, key_1, NULL);
  87. assert(get_data(hm, key_1) == NULL);
  88.  
  89. // check if linked list is broken
  90. iterate(hm, print_element);
  91.  
  92. // remove last element
  93. remove_data(hm, key_6, NULL);
  94. assert(get_data(hm, key_6) == NULL);
  95.  
  96. // check if linked list is broken
  97. iterate(hm, print_element);
  98.  
  99. // Remove middle element
  100. remove_data(hm, key_3, NULL);
  101. assert(get_data(hm, key_3) == NULL);
  102.  
  103. // Try to remove already removed element
  104. remove_data(hm, key_3, NULL);
  105. assert(get_data(hm, key_3) == NULL);
  106.  
  107. iterate(hm, print_element);
  108.  
  109. #ifdef NEW_HASH
  110. printf("SETTING NEW HASH FUNCTION\n");
  111. set_hash_function(hm, your_own_hash);
  112.  
  113. printf("\nHERE WE GO AGAIN!\n\n");
  114.  
  115. // Iterate the hash map
  116. iterate(hm, print_element);
  117. #endif
  118. // Delete the hash map.
  119. delete_hashmap(hm, NULL);
  120.  
  121. printf("TEST INTEGERS\n");
  122. const char * kkey_1 = "abc";
  123. const char * kkey_2 = "dfr";
  124. const int num1 = 10;
  125. const int num2 = 33;
  126. const int num3 = 5566;
  127. HashMap *hm2 = create_hashmap(8);
  128. printf("%p\n",&num1);
  129. insert_data(hm2, kkey_1, &num1, resolve_collision);
  130. assert(*(int *)get_data(hm2, kkey_1) == num1);
  131.  
  132. insert_data(hm2, kkey_1, &num2, resolve_collision);
  133. assert(*(int *)get_data(hm2, kkey_1) == num2);
  134.  
  135. insert_data(hm2, kkey_2, &num3, resolve_collision);
  136. assert(*(int *)get_data(hm2, kkey_2) == num3);
  137.  
  138. remove_data(hm, kkey_1, NULL);
  139. assert(get_data(hm, kkey_1) == NULL);
  140.  
  141. delete_hashmap(hm2, NULL);
  142. // wtf?
  143. //assert(get_data(hm, key_3) == NULL);
  144.  
  145. #ifdef COUNTING_WORDS
  146. // Create a temporary file
  147. FILE *stream = tmpfile();
  148.  
  149. // Write to the stream
  150. fprintf(stream, "\"Sed ut perspiciatis unde omnis iste natus error sit "
  151. "voluptatem accusantium doloremque laudantium, totam rem "
  152. "aperiam, eaque ipsa quae ab illo inventore veritatis et "
  153. "quasi architecto beatae 3vitae dicta sunt explicabo. Nemo "
  154. "enim ipsam voluptatem quia voluptas sit aspernatur aut "
  155. "odit aut fugit, sed quia consequuntur magni dolores eos "
  156. "qui ratione voluptatem £$%sequi nesciunt. Neque porro quisquam "
  157. "est, qui dolorem ipsum quia dolor sit amet, consectetur, "
  158. "adipisci velit, sed (9quia non numquam eius modi tempora "
  159. "incidunt ut labore et dolore magnam aliquam quaerat "
  160. "voluptatem. Ut enim ad minima veniam, quis nostrum "
  161. "exercitationem ullam corporis suscipit laboriosam, "
  162. "nisi ut aliquid ex ea commodi consequatur? Quis autem "
  163. "vel eum iure reprehenderit 12qui in ea voluptate velit "
  164. "esse quam nihil molestiae?^ consequatur, vel illum qui "
  165. "dolorem eum fugiat quo /voluptas nulla pariatur?\n");
  166.  
  167. // Set the position to the start of the stream
  168. fseek(stream, 0, SEEK_SET);
  169.  
  170. // Count the words
  171. count_words(stream);
  172.  
  173. // Close the file
  174. fclose(stream);
  175. #endif
  176.  
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement