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