Advertisement
gur111

StrToStr Map Tester Matam Spring 2020

Apr 18th, 2020
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. // MAKE SURE TO HAVE THESE FILES AS WELL AS map.c IN THE CURRENT FOLDER AND THAT YOUR COMPILE THE TESTER WITH YOUR map.c
  6. // THEN SIMPLY RUN THE EXECUTABLE
  7. #include "test_utilities.h"
  8. #include "map.h"
  9.  
  10. bool testMapCreate() {
  11.     printf("Testing mapCreate\n");
  12.     Map map1 = mapCreate();
  13.     Map map2 = mapCreate();
  14.     Map map3 = mapCreate();
  15.     mapDestroy(map3);
  16.     ASSERT_TEST(map1 != NULL);
  17.     ASSERT_TEST(map2 != NULL);
  18.     ASSERT_TEST(mapGetSize(map1) == 0);
  19.     ASSERT_TEST(mapGetFirst(map1) == NULL);
  20.     mapPut(map1, "mary", "poppins");
  21.     mapDestroy(map1);
  22.     map1 = NULL;
  23.     mapDestroy(map1);
  24.     ASSERT_TEST(mapGet(map1, "mary") == NULL);
  25.     ASSERT_TEST(mapGetSize(map1) == -1);
  26.     ASSERT_TEST(map2 != NULL);
  27.     mapDestroy(map2);
  28.     return true;
  29. }
  30.  
  31. bool testMapPutGet() {
  32.     printf("Testing mapPut, mapGet and mapGetSize\n");
  33.     Map map = mapCreate();
  34.  
  35.     const char *key1 = "key1";
  36.     const char *key2 = "key2";
  37.     const char *key3 = "key3";
  38.  
  39.     int count = 0;
  40.  
  41.     printf("Verify putting keys\n");
  42.     ASSERT_TEST(mapPut(map, key1, "value1") == MAP_SUCCESS);
  43.     count++;
  44.     ASSERT_TEST(mapPut(map, key2, "value2") == MAP_SUCCESS);
  45.     count++;
  46.     ASSERT_TEST(mapPut(map, key3, "value3") == MAP_SUCCESS);
  47.     count++;
  48.  
  49.     printf("Verify sizes change correctly\n");
  50.     ASSERT_TEST(mapGetSize(map) == count);
  51.     ASSERT_TEST(mapPut(map, key3, "value4") == MAP_SUCCESS);
  52.     ASSERT_TEST(mapGetSize(map) == count);
  53.     ASSERT_TEST(mapRemove(map, key2) == MAP_SUCCESS);
  54.     count--;
  55.     ASSERT_TEST(mapGetSize(map) == count);
  56.     ASSERT_TEST(mapRemove(map, key1) == MAP_SUCCESS);
  57.     count--;
  58.     ASSERT_TEST(mapRemove(map, key3) == MAP_SUCCESS);
  59.     count--;
  60.     ASSERT_TEST(mapPut(map, key3, "value1") == MAP_SUCCESS);
  61.     count++;
  62.  
  63.     printf("Verify `PUT` with NULL arguments\n");
  64.     ASSERT_TEST(mapPut(map, key3, NULL) == MAP_NULL_ARGUMENT);
  65.     ASSERT_TEST(mapPut(map, NULL, "value3") == MAP_NULL_ARGUMENT);
  66.     ASSERT_TEST(mapPut(map, NULL, NULL) == MAP_NULL_ARGUMENT);
  67.     ASSERT_TEST(mapPut(NULL, key3, "value3") == MAP_NULL_ARGUMENT);
  68.     ASSERT_TEST(mapPut(NULL, NULL, NULL) == MAP_NULL_ARGUMENT);
  69.     ASSERT_TEST(mapGetSize(map) == count);
  70.  
  71.     printf("Make sure added key has the correct value\n");
  72.     ASSERT_TEST(mapPut(map, key1, "value2") == MAP_SUCCESS);
  73.     count++;
  74.     ASSERT_TEST(strcmp(mapGet(map, key1), "value2") == 0);
  75.     printf("Verify value of key changed\n");
  76.     ASSERT_TEST(mapPut(map, key1, "value1") == MAP_SUCCESS);
  77.     ASSERT_TEST(strcmp(mapGet(map, key1), "value1") == 0);
  78.     ASSERT_TEST(mapRemove(map, key1) == MAP_SUCCESS);
  79.     count--;
  80.     printf("Verify key doesn't exist\n");
  81.     ASSERT_TEST(mapGet(map, key1) == NULL);
  82.  
  83.     printf("Verify string value is duplicated on PUT\n");
  84.     const char *value1 = "value1";
  85.     ASSERT_TEST(mapPut(map, key1, value1) == MAP_SUCCESS);
  86.     count++;
  87.     ASSERT_TEST(mapGet(map, key1) != value1);
  88.     ASSERT_TEST(mapGetSize(map) == count);
  89.  
  90.     printf("Verify string isn't duplicated on GET\n");
  91.     ASSERT_TEST(mapGet(map, key1) == mapGet(map, key1));
  92.  
  93.     printf("Verify override lookup by value and not by address\n");
  94.     ASSERT_TEST(mapPut(map, "key1", value1) == MAP_SUCCESS);
  95.     ASSERT_TEST(mapGetSize(map) == count);
  96.     ASSERT_TEST(strcmp(mapGet(map, key1), value1) == 0);
  97.  
  98.     printf("Testing emptry strings\n");
  99.     ASSERT_TEST(mapPut(map, key1, "") == MAP_SUCCESS);
  100.     ASSERT_TEST(mapPut(map, key2, "") == MAP_SUCCESS);
  101.     count++;
  102.     ASSERT_TEST(mapGetSize(map) == count);
  103.     ASSERT_TEST(mapPut(map, "", value1) == MAP_SUCCESS);
  104.     count++;
  105.     ASSERT_TEST(mapGetSize(map) == count);
  106.     ASSERT_TEST(strcmp(mapGet(map, ""), value1) == 0);
  107.     ASSERT_TEST(strcmp(mapGet(map, key1), "") == 0);
  108.  
  109.     printf("Test a super long string\n");
  110.     const char *super_long_string =
  111.         "Hi— I’m Ted Mosby. And exactly 45 days from now you and I are "
  112.         "going to meet and we’re going to fall in love and we’re going to "
  113.         "get married and we’re going to have 2 kids and we’re going to "
  114.         "love them and each other so much. All that is 45 days away, but "
  115.         "I’m here now I guess because… I want those extra 45 days with "
  116.         "you. I want each one of them. Look and if I can’t have them I’ll "
  117.         "take the 45 seconds before your boyfriend shows up and punches me "
  118.         "in the face, because… I love you. I’m always gonna love you, til "
  119.         "the end of my days and beyond. You’ll see.Hi— I’m Ted Mosby. And "
  120.         "exactly 45 days from now you and I are "
  121.         "going to meet and we’re going to fall in love and we’re going to "
  122.         "get married and we’re going to have 2 kids and we’re going to "
  123.         "love them and each other so much. All that is 45 days away, but "
  124.         "I’m here now I guess because… I want those extra 45 days with "
  125.         "you. I want each one of them. Look and if I can’t have them I’ll "
  126.         "take the 45 seconds before your boyfriend shows up and punches me "
  127.         "in the face, because… I love you. I’m always gonna love you, til "
  128.         "the end of my days and beyond. You’ll see.Hi— I’m Ted Mosby. And "
  129.         "exactly 45 days from now you and I are "
  130.         "going to meet and we’re going to fall in love and we’re going to "
  131.         "get married and we’re going to have 2 kids and we’re going to "
  132.         "love them and each other so much. All that is 45 days away, but "
  133.         "I’m here now I guess because… I want those extra 45 days with "
  134.         "you. I want each one of them. Look and if I can’t have them I’ll "
  135.         "take the 45 seconds before your boyfriend shows up and punches me "
  136.         "in the face, because… I love you. I’m always gonna love you, til "
  137.         "the end of my days and beyond. You’ll see.Hi— I’m Ted Mosby. And "
  138.         "exactly 45 days from now you and I are "
  139.         "going to meet and we’re going to fall in love and we’re going to "
  140.         "get married and we’re going to have 2 kids and we’re going to "
  141.         "love them and each other so much. All that is 45 days away, but "
  142.         "I’m here now I guess because… I want those extra 45 days with "
  143.         "you. I want each one of them. Look and if I can’t have them I’ll "
  144.         "take the 45 seconds before your boyfriend shows up and punches me "
  145.         "in the face, because… I love you. I’m always gonna love you, til "
  146.         "the end of my days and beyond. You’ll see.Hi— I’m Ted Mosby. And "
  147.         "exactly 45 days from now you and I are "
  148.         "going to meet and we’re going to fall in love and we’re going to "
  149.         "get married and we’re going to have 2 kids and we’re going to "
  150.         "love them and each other so much. All that is 45 days away, but "
  151.         "I’m here now I guess because… I want those extra 45 days with "
  152.         "you. I want each one of them. Look and if I can’t have them I’ll "
  153.         "take the 45 seconds before your boyfriend shows up and punches me "
  154.         "in the face, because… I love you. I’m always gonna love you, til "
  155.         "the end of my days and beyond. You’ll see.";
  156.     ASSERT_TEST(mapPut(map, key1, super_long_string) == MAP_SUCCESS);
  157.     ASSERT_TEST(strcmp(mapGet(map, key1), super_long_string) == 0);
  158.     ASSERT_TEST(mapPut(map, super_long_string, value1) == MAP_SUCCESS);
  159.     ASSERT_TEST(strcmp(mapGet(map, super_long_string), value1) == 0);
  160.     count++;
  161.     ASSERT_TEST(strcmp(mapGet(map, super_long_string), value1) == 0);
  162.     ASSERT_TEST(mapGetSize(map) == count);
  163.     ASSERT_TEST(strcmp(mapGet(map, key2), "") == 0);
  164.     mapDestroy(map);
  165.     printf(
  166.         "Destroy the list without removing all the keys. To let valgrind catch "
  167.         "de-allocation errors\n");
  168.     return true;
  169. }
  170.  
  171. bool testMapCopy() {
  172.     printf("Test mapCopy function\n");
  173.     ASSERT_TEST(mapCopy(NULL) == NULL);
  174.     Map map1 = mapCreate();
  175.     ASSERT_TEST(mapPut(map1, "key1", "value1") == MAP_SUCCESS);
  176.     ASSERT_TEST(mapPut(map1, "key2", "value2") == MAP_SUCCESS);
  177.     Map map2 = mapCopy(map1);
  178.     ASSERT_TEST(strcmp(mapGet(map2, "key1"), mapGet(map1, "key1")) == 0);
  179.     ASSERT_TEST(strcmp(mapGet(map2, "key2"), mapGet(map1, "key2")) == 0);
  180.  
  181.     printf("Verify size are the same\n");
  182.     ASSERT_TEST(mapGetSize(map2) == mapGetSize(map1));
  183.  
  184.     printf("Verify mapCopy duplicated the strings\n");
  185.     ASSERT_TEST(mapGet(map2, "key2") != mapGet(map1, "key2"));
  186.     ASSERT_TEST(mapRemove(map1, "key1") == MAP_SUCCESS);
  187.  
  188.     // Try re-allocating the same memory so it will change
  189.     ASSERT_TEST(mapPut(map1, "key4", "value1") == MAP_SUCCESS);
  190.     ASSERT_TEST(mapPut(map1, "key5", "value1") == MAP_SUCCESS);
  191.     ASSERT_TEST(mapPut(map1, "key6", "value1") == MAP_SUCCESS);
  192.     ASSERT_TEST(mapPut(map1, "key7", "value1") == MAP_SUCCESS);
  193.     ASSERT_TEST(mapGet(map2, "key2") != NULL);
  194.  
  195.     mapDestroy(map1);
  196.     ASSERT_TEST(strcmp(mapGet(map2, "key1"), "value1") == 0);
  197.     ASSERT_TEST(strcmp(mapGet(map2, "key2"), "value2") == 0);
  198.     mapDestroy(map2);
  199.     return true;
  200. }
  201.  
  202. bool testMapGetSize() {
  203.     Map map = mapCreate();
  204.  
  205.     ASSERT_TEST(mapGetSize(map) == 0);
  206.     ASSERT_TEST(mapPut(map, "key1", "value1") == MAP_SUCCESS);
  207.     ASSERT_TEST(mapPut(map, "key1", "value2") == MAP_SUCCESS);
  208.     ASSERT_TEST(mapGetSize(map) == 1);
  209.     ASSERT_TEST(mapRemove(map, "key1") == MAP_SUCCESS);
  210.     ASSERT_TEST(mapGetSize(map) == 0);
  211.     ASSERT_TEST(mapGetSize(NULL) == -1);
  212.     mapDestroy(map);
  213.     return true;
  214. }
  215.  
  216. bool testMapContains() {
  217.     Map map1 = mapCreate();
  218.     mapPut(map1, "key1", "value1");
  219.     ASSERT_TEST(mapContains(map1, "key1") == true);
  220.     ASSERT_TEST(mapContains(NULL, "key1") == false);
  221.     ASSERT_TEST(mapContains(map1, NULL) == false);
  222.     ASSERT_TEST(mapContains(map1, "dani") == false);
  223.     mapDestroy(map1);
  224.     return true;
  225. }
  226.  
  227. bool testMapRemove() {
  228.     Map map1 = mapCreate();
  229.     mapPut(map1, "key1", "value1");
  230.     mapPut(map1, "key2", "value2");
  231.     mapPut(map1, "key3", "value3");
  232.     mapRemove(map1, "key1");
  233.     ASSERT_TEST(mapContains(map1, "key1") == false);
  234.     ASSERT_TEST(mapGetSize(map1) == 2);
  235.     mapPut(map1, "key1", "value1");
  236.     mapRemove(map1, "key3");
  237.     ASSERT_TEST(mapContains(map1, "key3") == false);
  238.     ASSERT_TEST(mapGetSize(map1) == 2);
  239.     mapPut(map1, "key3", "value3");
  240.     mapRemove(map1, "key3");
  241.     ASSERT_TEST(mapContains(map1, "key3") == false);
  242.     ASSERT_TEST(mapGetSize(map1) == 2);
  243.     mapDestroy(map1);
  244.     return true;
  245. }
  246.  
  247. char *randString(int length) {
  248.     char *string =
  249.         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
  250.     size_t string_len = strlen(string);
  251.     char *random_string = NULL;
  252.  
  253.     if (length < 1) {
  254.         length = 1;
  255.     }
  256.     random_string = malloc(sizeof(char) * (length + 1));
  257.     if (random_string == NULL) {
  258.         return NULL;
  259.     }
  260.     short key = 0;
  261.     for (int n = 0; n < length; n++) {
  262.         key = rand() % string_len;
  263.         random_string[n] = string[key];
  264.     }
  265.  
  266.     random_string[length] = '\0';
  267.  
  268.     return random_string;
  269. }
  270.  
  271. bool doomsDay() {
  272.     Map map = mapCreate();
  273.     const int repeat = 10000;
  274.     const int length = 7;
  275.     char *arr[repeat];
  276.     static int my_seed = 25011984;
  277.     srand(time(NULL) * length + ++my_seed);
  278.     char *str;
  279.     for (int i = 0; i < repeat; i++) {
  280.         str = randString(length);
  281.         arr[i] = str;
  282.         ASSERT_TEST(mapPut(map, str, str) == MAP_SUCCESS);
  283.     }
  284.     ASSERT_TEST(mapGetSize(map) == repeat);
  285.     for (int i = 1; i <= repeat; i++) {
  286.         ASSERT_TEST(strcmp(mapGet(map, arr[repeat - i]), arr[repeat - i]) == 0);
  287.         ASSERT_TEST(mapRemove(map, arr[repeat - i]) == MAP_SUCCESS);
  288.         free(arr[repeat - i]);
  289.     }
  290.     mapDestroy(map);
  291.     return true;
  292. }
  293.  
  294. int main(int argc, char *argv[]) {
  295.     printf("Start Map Tests\n");
  296.     testMapPutGet();
  297.     testMapCopy();
  298.     testMapGetSize();
  299.     testMapCreate();
  300.     testMapContains();
  301.     testMapRemove();
  302.     doomsDay();
  303.     printf("end Map Tests\n");
  304.     return 0;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement