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