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