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
krista@830
     1
#include <stdlib.h>
krista@830
     2
#include <string.h>
krista@830
     3
#include "platform.h"
krista@830
     4
#include <iostream>
krista@830
     5
#include <fstream>
krista@830
     6
#include <assert.h>
krista@830
     7
krista@830
     8
#include "stringpair.h"
krista@830
     9
krista@830
    10
using namespace std;
krista@830
    11
krista@830
    12
int test_stringpair_equals(stringpair_t* val1, stringpair_t* val2) {
krista@830
    13
    assert(val1);
krista@830
    14
    assert(val2);
krista@830
    15
    assert(val1->key);
krista@830
    16
    assert(val2->key);
krista@830
    17
    assert(val1->value);
krista@830
    18
    assert(val2->value);
krista@830
    19
    return((strcmp(val1->key, val2->key) == 0) && (strcmp(val1->value, val2->value) == 0));
krista@830
    20
}
krista@830
    21
krista@830
    22
int main() {
krista@830
    23
    cout << "\n*** data structures: stringpair_list_test ***\n\n";
krista@830
    24
krista@830
    25
    const char* val_1_arr[4] = {"I am your father, Luke\n",
krista@830
    26
                                "These are not the droids you're looking for\n",
krista@830
    27
                                "Swooping is bad\n",
krista@830
    28
                                "I should go.\n"};
krista@830
    29
    const char* val_2_arr[4] = {"Had to be me.\n",
krista@830
    30
                                "Someone else might have gotten it wrong\n",
krista@830
    31
                                "Na via lerno victoria\n",
krista@830
    32
                                "I was told that there would be cake.\n"};
krista@830
    33
                                
krista@830
    34
//    const stringpair_t* stringpair_arr[4];
krista@830
    35
    
krista@830
    36
    int i;
krista@830
    37
    
krista@830
    38
//    for (i = 0; i < 4; i++) {
krista@830
    39
//        stringpair_arr[i] = new stringpair(val_1_arr[i], val_2_arr[i]);
krista@830
    40
//    }
krista@830
    41
    
krista@830
    42
    cout << "creating one-element stringpair_list...\n";
krista@830
    43
    
krista@830
    44
    stringpair_t* strpair = new_stringpair(val_1_arr[0], val_2_arr[0]);
krista@830
    45
    assert(strpair);
krista@830
    46
    stringpair_list_t* pairlist = new_stringpair_list(strpair);
krista@830
    47
    assert(pairlist->value);
krista@830
    48
    assert(test_stringpair_equals(strpair, pairlist->value));
krista@830
    49
    assert(pairlist->next == NULL);
krista@832
    50
    cout << "one-element stringpair_list created, next element is NULL\n\n";
krista@832
    51
    
krista@832
    52
    cout << "duplicating one-element list...\n";
krista@832
    53
    stringpair_list_t* duplist = stringpair_list_dup(pairlist);
krista@832
    54
    stringpair_t* srcpair = pairlist->value;
krista@832
    55
    stringpair_t* dstpair = duplist->value;
krista@832
    56
    assert(dstpair);
krista@832
    57
    assert(dstpair->value);
krista@832
    58
    assert(test_stringpair_equals(srcpair, dstpair));
krista@832
    59
    assert(srcpair->key != dstpair->key);   // test deep copies (to be fixed in next 2 commits)
krista@832
    60
    assert(srcpair->value != dstpair->value);
krista@832
    61
    assert(duplist->next == NULL);
krista@832
    62
    cout << "one-element stringpair_list duplicated.\n\n";
krista@832
    63
krista@832
    64
    
krista@832
    65
    cout << "freeing stringpair_lists...\n";
krista@832
    66
    free_stringpair_list(pairlist); // should free strpair
krista@832
    67
    free_stringpair_list(duplist);
krista@830
    68
    cout << "done.\n";
krista@830
    69
krista@832
    70
    
krista@832
    71
    
krista@832
    72
    
krista@832
    73
    
krista@832
    74
    
krista@830
    75
    return 0;
krista@830
    76
}
krista@830
    77