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