Added tests for the bloblist datastructure.
authorKrista Grothoff <krista@pep-project.org>
Sun, 17 Jul 2016 16:29:58 +0200
changeset 896f07f9ebea462
parent 895 79a4cc341524
child 897 1b478abf5b5a
Added tests for the bloblist datastructure.
test/bloblist_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/bloblist_test.cc	Sun Jul 17 16:29:58 2016 +0200
     1.3 @@ -0,0 +1,128 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <string.h>
     1.6 +#include "platform.h"
     1.7 +#include <iostream>
     1.8 +#include <fstream>
     1.9 +#include <assert.h>
    1.10 +
    1.11 +#include "bloblist.h"
    1.12 +
    1.13 +using namespace std;
    1.14 +
    1.15 +/*
    1.16 + *     char *address;              // C string with address UTF-8 encoded
    1.17 +    char *fpr;                  // C string with fingerprint UTF-8 encoded
    1.18 +    char *user_id;              // C string with user ID UTF-8 encoded
    1.19 +    char *username;             // C string with user name UTF-8 encoded
    1.20 +    PEP_comm_type comm_type;    // type of communication with this ID
    1.21 +    char lang[3];               // language of conversation
    1.22 +                                // ISO 639-1 ALPHA-2, last byte is 0
    1.23 +    bool me;                    // if this is the local user herself/himself
    1.24 +    */
    1.25 +
    1.26 +bool test_blob_equals(size_t size1, char* blob1, size_t size2, char* blob2) {
    1.27 +    if (size1 != size2)
    1.28 +        return false;
    1.29 +    size_t i;
    1.30 +    for (i = 0; i < size1; i++) {
    1.31 +        if (blob1[i] != blob2[i])
    1.32 +            return false;
    1.33 +    }
    1.34 +    return true;
    1.35 +}
    1.36 +
    1.37 +bool test_bloblist_node_equals(bloblist_t* val1, bloblist_t* val2) {
    1.38 +    assert(val1);
    1.39 +    assert(val2);
    1.40 +    assert(val1->size == val2->size);
    1.41 +    assert(test_blob_equals(val1->size, val1->value, val2->size, val2->value));
    1.42 +    return( ((!val1->mime_type && !val2->mime_type) || (strcmp(val1->mime_type, val2->mime_type) == 0))
    1.43 +        && ((!val1->filename && !val2->filename) || (strcmp(val1->filename, val2->filename) == 0)));
    1.44 +}
    1.45 +
    1.46 +int main() {
    1.47 +    cout << "\n*** data structures: bloblist_test ***\n\n";
    1.48 +    char* text1 = strdup("This is just some text.");
    1.49 +    char* text2 = strdup("More text.");
    1.50 +    char* text3 = strdup("Unpleasant news and witty one-liners.");
    1.51 +    char* text4 = strdup("I AM URDNOT WREX AND THIS IS MY PLANET!");
    1.52 +    bloblist_t* bl1 = new_bloblist(text1, strlen(text1) + 1, "text/plain", NULL);
    1.53 +    bloblist_t* bl2 = new_bloblist(text2, strlen(text2) + 1, "text/richtext", "bob.rtf");
    1.54 +    bloblist_t* bl3 = new_bloblist(text3, strlen(text3) + 1, NULL, "dummy.bin");
    1.55 +    bloblist_t* bl4 = new_bloblist(text4, strlen(text4) + 1, NULL, NULL);
    1.56 +    
    1.57 +    bloblist_t* bl_arr[4] = {bl1, bl2, bl3, bl4};
    1.58 +        
    1.59 +    int i;
    1.60 +        
    1.61 +    cout << "duping one-element bloblist...\n";
    1.62 +    
    1.63 +    bloblist_t* new_bl = bloblist_dup(bl1);
    1.64 +    assert(new_bl);
    1.65 +    assert(test_bloblist_node_equals(bl1, new_bl));
    1.66 +    assert(new_bl->next == NULL);
    1.67 +    assert(bl1->value != new_bl->value);
    1.68 +    assert(bl1->mime_type != new_bl->mime_type || !(bl1->mime_type || new_bl->mime_type));
    1.69 +    assert(bl1->filename != new_bl->filename || !(bl1->filename || new_bl->filename));
    1.70 +    cout << "one-element bloblist duplicated.\n\n";
    1.71 +    
    1.72 +    cout << "freeing bloblist...\n";
    1.73 +    free_bloblist(new_bl); // will free srcid
    1.74 +    new_bl = NULL;
    1.75 +    
    1.76 +    bloblist_t* p;
    1.77 +    cout << "\ncreating four-element list...\n";
    1.78 +    bloblist_t* to_copy = bl_arr[0];
    1.79 +    new_bl = bloblist_add(new_bl, strdup(to_copy->value), to_copy->size, to_copy->mime_type, to_copy->filename);
    1.80 +    for (i = 1; i < 4; i++) {
    1.81 +        to_copy = bl_arr[i];
    1.82 +        p = bloblist_add(new_bl, strdup(to_copy->value), to_copy->size, to_copy->mime_type, to_copy->filename);
    1.83 +
    1.84 +        assert(p);
    1.85 +    }
    1.86 +    
    1.87 +    p = new_bl;
    1.88 +    
    1.89 +    for (i = 0; i < 4; i++) {
    1.90 +        assert(p);
    1.91 +        
    1.92 +        assert(test_bloblist_node_equals(p, bl_arr[i]));
    1.93 +        assert(p->value != bl_arr[i]->value);
    1.94 +        assert(p->mime_type != bl_arr[i]->mime_type || !(p->mime_type || bl_arr[i]->mime_type));
    1.95 +        assert(p->filename != bl_arr[i]->filename || !(p->filename || bl_arr[i]->filename));
    1.96 +        
    1.97 +        p = p->next;
    1.98 +    }
    1.99 +    assert(p == NULL);
   1.100 +    
   1.101 +    cout << "\nduplicating four-element list...\n\n";
   1.102 +    bloblist_t* duplist = bloblist_dup(new_bl);
   1.103 +    
   1.104 +    p = new_bl;
   1.105 +    bloblist_t* dup_p = duplist;
   1.106 +    
   1.107 +    while (dup_p) {
   1.108 +        assert(test_bloblist_node_equals(p, dup_p));
   1.109 +        assert(p != dup_p);
   1.110 +        assert(p->value != dup_p->value);
   1.111 +        assert(p->mime_type != dup_p->mime_type || !(p->mime_type || dup_p->mime_type));
   1.112 +        assert(p->filename != dup_p->filename || !(p->filename || dup_p->filename));
   1.113 +
   1.114 +        dup_p = dup_p->next;
   1.115 +        p = p->next;
   1.116 +        assert((p == NULL) == (dup_p == NULL));
   1.117 +    }
   1.118 +    cout << "\nfour-element bloblist successfully duplicated.\n\n";
   1.119 +
   1.120 +    cout << "freeing bloblists...\n";
   1.121 +    free_bloblist(new_bl); // will free srcid
   1.122 +    free_bloblist(duplist);
   1.123 +    new_bl = NULL;
   1.124 +    duplist = NULL;
   1.125 +    
   1.126 +    cout << "done.\n";
   1.127 +        
   1.128 +    
   1.129 +    return 0;
   1.130 +}
   1.131 +