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