test/stringpair_list_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 01 Jun 2018 10:27:30 +0200
branchENGINE-135
changeset 2708 11e836fe7f0a
parent 1513 e7f7e42385b5
permissions -rw-r--r--
close branch
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include "platform.h"
     7 #include <iostream>
     8 #include <fstream>
     9 #include <assert.h>
    10 
    11 #include "stringpair.h"
    12 
    13 using namespace std;
    14 
    15 int test_stringpair_equals(stringpair_t* val1, stringpair_t* val2) {
    16     assert(val1);
    17     assert(val2);
    18     assert(val1->key);
    19     assert(val2->key);
    20     assert(val1->value);
    21     assert(val2->value);
    22     return((strcmp(val1->key, val2->key) == 0) && (strcmp(val1->value, val2->value) == 0));
    23 }
    24 
    25 int main() {
    26     cout << "\n*** data structures: stringpair_list_test ***\n\n";
    27 
    28     const char* val_1_arr[4] = {"I am your father, Luke",
    29                                 "These are not the droids you're looking for",
    30                                 "Swooping is bad",
    31                                 "I should go."};
    32     const char* val_2_arr[4] = {"Had to be me.",
    33                                 "Someone else might have gotten it wrong",
    34                                 "Na via lerno victoria",
    35                                 "I was told that there would be cake."};
    36                                 
    37 //    const stringpair_t* stringpair_arr[4];
    38     
    39     int i;
    40     
    41 //    for (i = 0; i < 4; i++) {
    42 //        stringpair_arr[i] = new stringpair(val_1_arr[i], val_2_arr[i]);
    43 //    }
    44     
    45     cout << "creating one-element stringpair_list...\n";
    46     
    47     stringpair_t* strpair = new_stringpair(val_1_arr[0], val_2_arr[0]);
    48     assert(strpair);
    49     stringpair_list_t* pairlist = new_stringpair_list(strpair);
    50     assert(pairlist->value);
    51     assert(test_stringpair_equals(strpair, pairlist->value));
    52     assert(pairlist->next == NULL);
    53     cout << "one-element stringpair_list created, next element is NULL\n\n";
    54     
    55     cout << "duplicating one-element list...\n";
    56     stringpair_list_t* duplist = stringpair_list_dup(pairlist);
    57     stringpair_t* srcpair = pairlist->value;
    58     stringpair_t* dstpair = duplist->value;
    59     assert(dstpair);
    60     assert(dstpair->value);
    61     assert(test_stringpair_equals(srcpair, dstpair));
    62     assert(srcpair->key != dstpair->key);   // test deep copies (to be fixed in next 2 commits)
    63     assert(srcpair->value != dstpair->value);
    64     assert(duplist->next == NULL);
    65     cout << "one-element stringpair_list duplicated.\n\n";
    66     
    67     cout << "freeing stringpair_lists...\n";
    68     free_stringpair_list(pairlist); // will free strpair
    69     free_stringpair_list(duplist);
    70     pairlist = NULL;
    71     duplist = NULL;
    72     strpair = NULL;
    73     
    74     stringpair_list_t* p;
    75     cout << "\ncreating four-element list...\n";
    76     pairlist = stringpair_list_add(pairlist, new_stringpair(val_1_arr[0], val_2_arr[0]));
    77     for (i = 1; i < 4; i++) {
    78         p = stringpair_list_add(pairlist, new_stringpair(val_1_arr[i], val_2_arr[i]));
    79         assert(p);
    80     }
    81     
    82     p = pairlist;
    83     
    84     for (i = 0; i < 4; i++) {
    85         assert(p);
    86         
    87         strpair = p->value;
    88         assert(strpair);
    89         
    90         assert(strpair->key);
    91         assert(strcmp(val_1_arr[i], strpair->key) == 0);
    92         
    93         assert(strpair->value);
    94         assert(strcmp(val_2_arr[i], strpair->value) == 0);
    95         
    96         assert(val_1_arr[i] != strpair->key);
    97         assert(val_2_arr[i] != strpair->value);
    98         
    99         p = p->next;
   100     }
   101     assert(p == NULL);
   102     
   103     cout << "\nduplicating four-element list...\n\n";
   104     duplist = stringpair_list_dup(pairlist);
   105     
   106     p = pairlist;
   107     stringpair_list_t* dup_p = duplist;
   108     
   109     while (dup_p) {
   110         srcpair = p->value;
   111         dstpair = dup_p->value;
   112 
   113         assert(dstpair);
   114         assert(dstpair->value);
   115         
   116         cout << srcpair->key << ":" << srcpair->value << " / " << dstpair->key << ":" << dstpair->value << "\n";
   117         assert(test_stringpair_equals(srcpair, dstpair));
   118 
   119         assert(srcpair->key != dstpair->key);   // test deep copies (to be fixed in next 2 commits)
   120         assert(srcpair->value != dstpair->value);
   121 
   122         i++;
   123         p = p->next;
   124 
   125         dup_p = dup_p->next;
   126         assert((p == NULL) == (dup_p == NULL));
   127     }
   128     cout << "\nfour-element stringpair_list successfully duplicated.\n\n";
   129 
   130     cout << "freeing stringpair_lists...\n";
   131     free_stringpair_list(pairlist); // will free strpair
   132     free_stringpair_list(duplist);
   133     pairlist = NULL;
   134     duplist = NULL;
   135     
   136     cout << "done.\n";
   137         
   138     
   139     return 0;
   140 }
   141