test/stringpair_list_test.cc
author Krista Grothoff <krista@pep-project.org>
Mon, 11 Jul 2016 12:08:39 +0200
changeset 832 8a54e3a6b155
parent 830 4d7551272344
child 834 1108548edc67
permissions -rw-r--r--
Fixed stringpair_list_dup to make deep copies, as intended
     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\n",
    26                                 "These are not the droids you're looking for\n",
    27                                 "Swooping is bad\n",
    28                                 "I should go.\n"};
    29     const char* val_2_arr[4] = {"Had to be me.\n",
    30                                 "Someone else might have gotten it wrong\n",
    31                                 "Na via lerno victoria\n",
    32                                 "I was told that there would be cake.\n"};
    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     
    65     cout << "freeing stringpair_lists...\n";
    66     free_stringpair_list(pairlist); // should free strpair
    67     free_stringpair_list(duplist);
    68     cout << "done.\n";
    69 
    70     
    71     
    72     
    73     
    74     
    75     return 0;
    76 }
    77