test/stringpair_list_test.cc
author Krista Grothoff <krista@pep-project.org>
Mon, 11 Jul 2016 16:15:51 +0200
changeset 834 1108548edc67
parent 832 8a54e3a6b155
child 1513 e7f7e42385b5
permissions -rw-r--r--
Added list duplication / add test code
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@834
    25
    const char* val_1_arr[4] = {"I am your father, Luke",
krista@834
    26
                                "These are not the droids you're looking for",
krista@834
    27
                                "Swooping is bad",
krista@834
    28
                                "I should go."};
krista@834
    29
    const char* val_2_arr[4] = {"Had to be me.",
krista@834
    30
                                "Someone else might have gotten it wrong",
krista@834
    31
                                "Na via lerno victoria",
krista@834
    32
                                "I was told that there would be cake."};
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
    cout << "freeing stringpair_lists...\n";
krista@834
    65
    free_stringpair_list(pairlist); // will free strpair
krista@832
    66
    free_stringpair_list(duplist);
krista@834
    67
    pairlist = NULL;
krista@834
    68
    duplist = NULL;
krista@834
    69
    strpair = NULL;
krista@834
    70
    
krista@834
    71
    stringpair_list_t* p;
krista@834
    72
    cout << "\ncreating four-element list...\n";
krista@834
    73
    pairlist = stringpair_list_add(pairlist, new_stringpair(val_1_arr[0], val_2_arr[0]));
krista@834
    74
    for (i = 1; i < 4; i++) {
krista@834
    75
        p = stringpair_list_add(pairlist, new_stringpair(val_1_arr[i], val_2_arr[i]));
krista@834
    76
        assert(p);
krista@834
    77
    }
krista@834
    78
    
krista@834
    79
    p = pairlist;
krista@834
    80
    
krista@834
    81
    for (i = 0; i < 4; i++) {
krista@834
    82
        assert(p);
krista@834
    83
        
krista@834
    84
        strpair = p->value;
krista@834
    85
        assert(strpair);
krista@834
    86
        
krista@834
    87
        assert(strpair->key);
krista@834
    88
        assert(strcmp(val_1_arr[i], strpair->key) == 0);
krista@834
    89
        
krista@834
    90
        assert(strpair->value);
krista@834
    91
        assert(strcmp(val_2_arr[i], strpair->value) == 0);
krista@834
    92
        
krista@834
    93
        assert(val_1_arr[i] != strpair->key);
krista@834
    94
        assert(val_2_arr[i] != strpair->value);
krista@834
    95
        
krista@834
    96
        p = p->next;
krista@834
    97
    }
krista@834
    98
    assert(p == NULL);
krista@834
    99
    
krista@834
   100
    cout << "\nduplicating four-element list...\n\n";
krista@834
   101
    duplist = stringpair_list_dup(pairlist);
krista@834
   102
    
krista@834
   103
    p = pairlist;
krista@834
   104
    stringpair_list_t* dup_p = duplist;
krista@834
   105
    
krista@834
   106
    while (dup_p) {
krista@834
   107
        srcpair = p->value;
krista@834
   108
        dstpair = dup_p->value;
krista@834
   109
krista@834
   110
        assert(dstpair);
krista@834
   111
        assert(dstpair->value);
krista@834
   112
        
krista@834
   113
        cout << srcpair->key << ":" << srcpair->value << " / " << dstpair->key << ":" << dstpair->value << "\n";
krista@834
   114
        assert(test_stringpair_equals(srcpair, dstpair));
krista@834
   115
krista@834
   116
        assert(srcpair->key != dstpair->key);   // test deep copies (to be fixed in next 2 commits)
krista@834
   117
        assert(srcpair->value != dstpair->value);
krista@834
   118
krista@834
   119
        i++;
krista@834
   120
        p = p->next;
krista@834
   121
krista@834
   122
        dup_p = dup_p->next;
krista@834
   123
        assert((p == NULL) == (dup_p == NULL));
krista@834
   124
    }
krista@834
   125
    cout << "\nfour-element stringpair_list successfully duplicated.\n\n";
krista@834
   126
krista@834
   127
    cout << "freeing stringpair_lists...\n";
krista@834
   128
    free_stringpair_list(pairlist); // will free strpair
krista@834
   129
    free_stringpair_list(duplist);
krista@834
   130
    pairlist = NULL;
krista@834
   131
    duplist = NULL;
krista@834
   132
    
krista@830
   133
    cout << "done.\n";
krista@834
   134
        
krista@832
   135
    
krista@830
   136
    return 0;
krista@830
   137
}
krista@830
   138