Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "dictionary.h"
  5. #define SIZE 1000
  6.  
  7. void showDictionary(Dictionary* dictionary) {
  8.     printf("Size: %d\nDictionary: ", sizeOfDictionary(dictionary));
  9.     printDictionary(dictionary);
  10.     printf("\n");
  11. }  
  12.  
  13. void doWait() {
  14.     char c;
  15.     do {
  16.         printf("Enter ENTER to continue..\n");
  17.         c = getchar();
  18.     } while (c != '\n');
  19. }
  20.  
  21. void failTests() {
  22.         Dictionary* dictionary;
  23.         printf("This section is fail tests, comment it out from main if you want to continue\n");
  24.         getFromDictionary(dictionary,1);
  25.         removeFromDictionary(dictionary,1);
  26.         showDictionary(dictionary);
  27.         putInDictionary(dictionary,1,1);
  28.         destroyDictionary(dictionary);
  29. }
  30. void main() {
  31.     // failTests();
  32.     Dictionary* dictionary;
  33.     int keys[SIZE];
  34.     int values[SIZE];
  35.     int randKey = rand() % 1000;
  36.  
  37.     printf("First test: creating dictionary from large arrays (%d)\n\n",SIZE);
  38.     doWait();
  39.     for (int i = 0; i < SIZE; i++) {
  40.         keys[i] = i;
  41.         values[i] = rand() % 1000;
  42.     }
  43.     dictionary = createDictionaryFromArrays(keys,values,SIZE);
  44.     showDictionary(dictionary);
  45.     printf("Random fetch:\nKey: %d Value: %d\n",randKey,getFromDictionary(dictionary,randKey));
  46.     destroyDictionary;
  47.  
  48.     printf("\n\nSecond test: creating dictionary from large inputs (%d)\n\n",SIZE);
  49.     doWait();
  50.     randKey = rand() % 1000;
  51.     dictionary = initDictionary();
  52.     for (int i = 0; i < SIZE; i++)  
  53.         putInDictionary(dictionary,i,rand() % 1000);
  54.     showDictionary(dictionary);
  55.     printf("Random fetch:\nKey: %d Value: %d\n",randKey,getFromDictionary(dictionary,randKey));
  56.  
  57.     printf("\n\nThird test: Adding and removing large inputs (positive and negetives) (%d)\n\n",SIZE);
  58.     doWait();
  59.     randKey = rand() % 1000;
  60.     dictionary = initDictionary();
  61.     printf("Insertion\n\n\n");
  62.     for (int i = - (SIZE / 2); i < (SIZE / 2); i++)  
  63.         putInDictionary(dictionary,i,rand() % 1000);
  64.     showDictionary(dictionary);
  65.     printf("\n\n\nDeletion\n\n\n");
  66.     for (int i = - (SIZE / 2); i < (SIZE / 2); i++)  {
  67.         printf("Removing %d\n",i);
  68.         removeFromDictionary(dictionary,i);
  69.     }
  70.     showDictionary(dictionary);
  71.     printf("Random fetch:\nKey: %d Value: %d\n",randKey,getFromDictionary(dictionary,randKey));
  72.     destroyDictionary;
  73.  
  74.     printf("\n\nForth test: invalid insertions and deletions (Some are commented out at main, check them out)\n\n");
  75.     doWait();
  76.     dictionary = initDictionary();
  77.     showDictionary(dictionary);
  78.  
  79.     randKey = rand() % 1000;
  80.     int result = getFromDictionary(dictionary,randKey);
  81.     printf("Value for (key) %d: %d\n",randKey,result);
  82.     (result) ? printf("Failed\n") : printf("Passed\n");
  83.  
  84.     result = removeFromDictionary(dictionary,randKey);
  85.     printf("Removing (key) %d\nResult = %d\n",randKey,result);
  86.     (result != 1) ? printf("Failed\n") : printf("Passed\n");
  87.  
  88.     putInDictionary(dictionary,1,1);
  89.     showDictionary(dictionary);
  90.     result = removeFromDictionary(dictionary,2);
  91.     printf("Removing (key) %d\nResult = %d\n",2,result);
  92.     (result != 1) ? printf("Failed\n") : printf("Passed\n");
  93.     showDictionary(dictionary);
  94.  
  95.     destroyDictionary;
  96.  
  97.     printf("\n\nFifth test: Rapid massive insertion and deletion of elements (%d))\n\n",SIZE);
  98.     doWait();
  99.     dictionary = initDictionary();
  100.     for (int i = 0; i < SIZE; i++) {
  101.         printf("Inserting 1\n");
  102.         putInDictionary(dictionary,1,1);
  103.         printf("Deleting 1\n");
  104.         removeFromDictionary(dictionary,1);
  105.         showDictionary(dictionary);
  106.     }
  107.     destroyDictionary;
  108.  
  109.     printf("\n\nSixth test: changing the same key multiple times (%d))\n\n",SIZE);
  110.     doWait();
  111.     dictionary = initDictionary();
  112.     randKey = rand() % 1000;
  113.     for (int i = 0; i < SIZE; i++) {
  114.         putInDictionary(dictionary,randKey,rand() % 1000);
  115.         showDictionary(dictionary);
  116.     }
  117.     destroyDictionary;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement