test/bloblist_test.cc
author Volker Birk <vb@pep.foundation>
Tue, 27 Dec 2016 21:13:41 +0100
changeset 1513 e7f7e42385b5
parent 897 1b478abf5b5a
child 1714 a6712af7a964
permissions -rw-r--r--
adding license info to each file
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
krista@896
     4
#include <stdlib.h>
krista@896
     5
#include <string.h>
krista@896
     6
#include "platform.h"
krista@896
     7
#include <iostream>
krista@896
     8
#include <fstream>
krista@896
     9
#include <assert.h>
krista@896
    10
krista@896
    11
#include "bloblist.h"
krista@896
    12
krista@896
    13
using namespace std;
krista@896
    14
krista@896
    15
/*
krista@896
    16
 *     char *address;              // C string with address UTF-8 encoded
krista@896
    17
    char *fpr;                  // C string with fingerprint UTF-8 encoded
krista@896
    18
    char *user_id;              // C string with user ID UTF-8 encoded
krista@896
    19
    char *username;             // C string with user name UTF-8 encoded
krista@896
    20
    PEP_comm_type comm_type;    // type of communication with this ID
krista@896
    21
    char lang[3];               // language of conversation
krista@896
    22
                                // ISO 639-1 ALPHA-2, last byte is 0
krista@896
    23
    bool me;                    // if this is the local user herself/himself
krista@896
    24
    */
krista@896
    25
krista@896
    26
bool test_blob_equals(size_t size1, char* blob1, size_t size2, char* blob2) {
krista@896
    27
    if (size1 != size2)
krista@896
    28
        return false;
krista@896
    29
    size_t i;
krista@896
    30
    for (i = 0; i < size1; i++) {
krista@896
    31
        if (blob1[i] != blob2[i])
krista@896
    32
            return false;
krista@896
    33
    }
krista@896
    34
    return true;
krista@896
    35
}
krista@896
    36
krista@896
    37
bool test_bloblist_node_equals(bloblist_t* val1, bloblist_t* val2) {
krista@896
    38
    assert(val1);
krista@896
    39
    assert(val2);
krista@896
    40
    assert(val1->size == val2->size);
krista@896
    41
    assert(test_blob_equals(val1->size, val1->value, val2->size, val2->value));
krista@896
    42
    return( ((!val1->mime_type && !val2->mime_type) || (strcmp(val1->mime_type, val2->mime_type) == 0))
krista@896
    43
        && ((!val1->filename && !val2->filename) || (strcmp(val1->filename, val2->filename) == 0)));
krista@896
    44
}
krista@896
    45
krista@896
    46
int main() {
krista@896
    47
    cout << "\n*** data structures: bloblist_test ***\n\n";
krista@896
    48
    char* text1 = strdup("This is just some text.");
krista@896
    49
    char* text2 = strdup("More text.");
krista@896
    50
    char* text3 = strdup("Unpleasant news and witty one-liners.");
krista@896
    51
    char* text4 = strdup("I AM URDNOT WREX AND THIS IS MY PLANET!");
krista@896
    52
    bloblist_t* bl1 = new_bloblist(text1, strlen(text1) + 1, "text/plain", NULL);
krista@896
    53
    bloblist_t* bl2 = new_bloblist(text2, strlen(text2) + 1, "text/richtext", "bob.rtf");
krista@896
    54
    bloblist_t* bl3 = new_bloblist(text3, strlen(text3) + 1, NULL, "dummy.bin");
krista@896
    55
    bloblist_t* bl4 = new_bloblist(text4, strlen(text4) + 1, NULL, NULL);
krista@896
    56
    
krista@896
    57
    bloblist_t* bl_arr[4] = {bl1, bl2, bl3, bl4};
krista@896
    58
        
krista@896
    59
    int i;
krista@896
    60
        
krista@896
    61
    cout << "duping one-element bloblist...\n";
krista@896
    62
    
krista@896
    63
    bloblist_t* new_bl = bloblist_dup(bl1);
krista@896
    64
    assert(new_bl);
krista@896
    65
    assert(test_bloblist_node_equals(bl1, new_bl));
krista@896
    66
    assert(new_bl->next == NULL);
krista@896
    67
    assert(bl1->value != new_bl->value);
krista@896
    68
    assert(bl1->mime_type != new_bl->mime_type || !(bl1->mime_type || new_bl->mime_type));
krista@896
    69
    assert(bl1->filename != new_bl->filename || !(bl1->filename || new_bl->filename));
krista@896
    70
    cout << "one-element bloblist duplicated.\n\n";
krista@896
    71
    
krista@896
    72
    cout << "freeing bloblist...\n";
krista@897
    73
    free_bloblist(new_bl);
krista@896
    74
    new_bl = NULL;
krista@896
    75
    
krista@896
    76
    bloblist_t* p;
krista@896
    77
    cout << "\ncreating four-element list...\n";
krista@896
    78
    bloblist_t* to_copy = bl_arr[0];
krista@896
    79
    new_bl = bloblist_add(new_bl, strdup(to_copy->value), to_copy->size, to_copy->mime_type, to_copy->filename);
krista@896
    80
    for (i = 1; i < 4; i++) {
krista@896
    81
        to_copy = bl_arr[i];
krista@896
    82
        p = bloblist_add(new_bl, strdup(to_copy->value), to_copy->size, to_copy->mime_type, to_copy->filename);
krista@896
    83
krista@896
    84
        assert(p);
krista@896
    85
    }
krista@896
    86
    
krista@896
    87
    p = new_bl;
krista@896
    88
    
krista@896
    89
    for (i = 0; i < 4; i++) {
krista@896
    90
        assert(p);
krista@896
    91
        
krista@896
    92
        assert(test_bloblist_node_equals(p, bl_arr[i]));
krista@896
    93
        assert(p->value != bl_arr[i]->value);
krista@896
    94
        assert(p->mime_type != bl_arr[i]->mime_type || !(p->mime_type || bl_arr[i]->mime_type));
krista@896
    95
        assert(p->filename != bl_arr[i]->filename || !(p->filename || bl_arr[i]->filename));
krista@896
    96
        
krista@896
    97
        p = p->next;
krista@896
    98
    }
krista@896
    99
    assert(p == NULL);
krista@896
   100
    
krista@896
   101
    cout << "\nduplicating four-element list...\n\n";
krista@896
   102
    bloblist_t* duplist = bloblist_dup(new_bl);
krista@896
   103
    
krista@896
   104
    p = new_bl;
krista@896
   105
    bloblist_t* dup_p = duplist;
krista@896
   106
    
krista@896
   107
    while (dup_p) {
krista@896
   108
        assert(test_bloblist_node_equals(p, dup_p));
krista@896
   109
        assert(p != dup_p);
krista@896
   110
        assert(p->value != dup_p->value);
krista@896
   111
        assert(p->mime_type != dup_p->mime_type || !(p->mime_type || dup_p->mime_type));
krista@896
   112
        assert(p->filename != dup_p->filename || !(p->filename || dup_p->filename));
krista@896
   113
krista@896
   114
        dup_p = dup_p->next;
krista@896
   115
        p = p->next;
krista@896
   116
        assert((p == NULL) == (dup_p == NULL));
krista@896
   117
    }
krista@896
   118
    cout << "\nfour-element bloblist successfully duplicated.\n\n";
krista@896
   119
krista@896
   120
    cout << "freeing bloblists...\n";
krista@897
   121
    free_bloblist(new_bl);
krista@896
   122
    free_bloblist(duplist);
krista@896
   123
    new_bl = NULL;
krista@896
   124
    duplist = NULL;
krista@897
   125
    free(text1);
krista@897
   126
    free(text2);
krista@897
   127
    free(text3);
krista@897
   128
    free(text4);    
krista@896
   129
    cout << "done.\n";
krista@896
   130
        
krista@896
   131
    
krista@896
   132
    return 0;
krista@896
   133
}
krista@896
   134