test/stringlist_test.cc
author Edouard Tisserant <edouard@pep-project.org>
Mon, 24 Oct 2016 20:44:48 +0200
changeset 1329 876ad7615d75
parent 843 b1695eef237b
child 1513 e7f7e42385b5
permissions -rw-r--r--
sync : fix encrypted sync messages not being injected when decrypted from non-sync session
krista@825
     1
#include <stdlib.h>
krista@825
     2
#include <string.h>
krista@825
     3
#include "platform.h"
krista@825
     4
#include <iostream>
krista@825
     5
#include <fstream>
krista@825
     6
#include <assert.h>
krista@825
     7
krista@825
     8
#include "stringlist.h"
krista@825
     9
krista@825
    10
using namespace std;
krista@825
    11
krista@825
    12
int main() {
krista@825
    13
    cout << "\n*** data structures: stringlist_test ***\n\n";
krista@827
    14
krista@827
    15
    const char* str0 = "I am your father, Luke\n";
krista@825
    16
    
krista@825
    17
    // new_stringlist test code
krista@825
    18
    cout << "creating one-element stringlist…\n";
krista@825
    19
    
krista@827
    20
    stringlist_t* src = new_stringlist(str0);
krista@825
    21
    assert(src);
krista@827
    22
    assert(strcmp(src->value,str0) == 0);
krista@825
    23
    cout << "Value: " << src->value;
krista@825
    24
    assert(src->next == NULL);
krista@825
    25
    cout << "one-element stringlist created, next element is NULL\n";
krista@825
    26
    
krista@825
    27
    cout << "freeing stringlist…\n\n";
krista@825
    28
    free_stringlist(src);
krista@825
    29
    src = NULL;
krista@825
    30
    
krista@825
    31
    // test stringlist_add with four-element list
krista@825
    32
    cout << "creating four-element stringlist…\n";
krista@825
    33
    const char* str1 = "String 1";
krista@825
    34
    const char* str2 = "\tString 2";
krista@825
    35
    const char* str3 = "\tString 3";
krista@825
    36
    const char* str4 = "\tString 4\n";
krista@825
    37
    const char* strarr[4] = {str1, str2, str3, str4};
krista@825
    38
    cout << "stringlist_add on empty list…\n";
krista@825
    39
    src = stringlist_add(src, str1); // src is NULL
krista@825
    40
    assert(src);
krista@825
    41
    assert(stringlist_add(src, str2)); // returns ptr to new elt
krista@825
    42
    assert(stringlist_add(src, str3));
krista@825
    43
    assert(stringlist_add(src, str4));
krista@825
    44
    
krista@825
    45
    cout << "checking contents\n";
krista@825
    46
    stringlist_t* p = src;
krista@825
    47
    int i = 0;
krista@825
    48
    while (p) {
krista@825
    49
        assert(p->value);
krista@825
    50
        assert(strcmp(p->value, strarr[i++]) == 0);
krista@825
    51
        assert(p->value != *(strarr + i)); // ensure this is a copy
krista@825
    52
        cout << p->value;
krista@825
    53
        p = p->next;
krista@825
    54
    }
krista@825
    55
    assert(p == NULL); // list ends properly
krista@825
    56
    
krista@825
    57
    cout << "\nduplicating four-element stringlist…\n";
krista@825
    58
    stringlist_t* dst = stringlist_dup(src);
krista@825
    59
    assert(dst);
krista@825
    60
    
krista@825
    61
    stringlist_t* p_dst = dst;
krista@825
    62
    p = src;
krista@825
    63
krista@825
    64
    cout << "checking contents\n";    
krista@825
    65
    while (p_dst) {
krista@825
    66
        assert(p_dst->value);
krista@825
    67
        assert(strcmp(p->value, p_dst->value) == 0);
krista@825
    68
        assert(p->value != p_dst->value); // ensure this is a copy
krista@825
    69
        cout << p_dst->value;
krista@825
    70
        p = p->next;
krista@825
    71
        p_dst = p_dst->next;
krista@825
    72
        assert((p == NULL) == (p_dst == NULL));
krista@825
    73
    }
krista@825
    74
    assert(p_dst == NULL);
krista@843
    75
        
krista@825
    76
    cout << "freeing stringlists…\n\n";
krista@825
    77
    free_stringlist(src);
krista@825
    78
    free_stringlist(dst);
krista@825
    79
    src = NULL;
krista@825
    80
    dst = NULL;
krista@825
    81
krista@825
    82
    cout << "duplicating one-element stringlist…\n";    
krista@827
    83
    src = new_stringlist(str0);
krista@825
    84
    assert(src);
krista@825
    85
    dst = stringlist_dup(src);
krista@827
    86
    assert(strcmp(dst->value, str0) == 0);
krista@825
    87
    cout << "Value: " << src->value;
krista@825
    88
    assert(dst->next == NULL);
krista@825
    89
    cout << "one-element stringlist duped, next element is NULL\n";
krista@825
    90
    
krista@843
    91
    cout << "\nAdd to empty stringlist (node exists, but no value…)\n";
krista@827
    92
    if (src->value)
krista@827
    93
        free(src->value);
krista@827
    94
    src->value = NULL;
krista@827
    95
    stringlist_add(src, str2);
krista@827
    96
    assert(src->value);
krista@827
    97
    assert(strcmp(src->value, str2) == 0);
krista@827
    98
    assert(src->value != str2); // ensure this is a copy
krista@827
    99
    cout << src->value;
krista@827
   100
krista@827
   101
    cout << "\nfreeing stringlists…\n\n";
krista@825
   102
    free_stringlist(src);
krista@825
   103
    free_stringlist(dst);
krista@825
   104
    
krista@825
   105
    src = NULL;
krista@825
   106
    dst = NULL;
krista@825
   107
    
krista@825
   108
    cout << "done.\n";
krista@825
   109
krista@825
   110
    return 0;
krista@825
   111
}
krista@825
   112