test/stringlist_test.cc
author Edouard Tisserant <edouard@pep-project.org>
Mon, 24 Oct 2016 20:44:48 +0200
changeset 1329 876ad7615d75
parent 843 b1695eef237b
child 1513 e7f7e42385b5
permissions -rw-r--r--
sync : fix encrypted sync messages not being injected when decrypted from non-sync session
     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 "stringlist.h"
     9 
    10 using namespace std;
    11 
    12 int main() {
    13     cout << "\n*** data structures: stringlist_test ***\n\n";
    14 
    15     const char* str0 = "I am your father, Luke\n";
    16     
    17     // new_stringlist test code
    18     cout << "creating one-element stringlist…\n";
    19     
    20     stringlist_t* src = new_stringlist(str0);
    21     assert(src);
    22     assert(strcmp(src->value,str0) == 0);
    23     cout << "Value: " << src->value;
    24     assert(src->next == NULL);
    25     cout << "one-element stringlist created, next element is NULL\n";
    26     
    27     cout << "freeing stringlist…\n\n";
    28     free_stringlist(src);
    29     src = NULL;
    30     
    31     // test stringlist_add with four-element list
    32     cout << "creating four-element stringlist…\n";
    33     const char* str1 = "String 1";
    34     const char* str2 = "\tString 2";
    35     const char* str3 = "\tString 3";
    36     const char* str4 = "\tString 4\n";
    37     const char* strarr[4] = {str1, str2, str3, str4};
    38     cout << "stringlist_add on empty list…\n";
    39     src = stringlist_add(src, str1); // src is NULL
    40     assert(src);
    41     assert(stringlist_add(src, str2)); // returns ptr to new elt
    42     assert(stringlist_add(src, str3));
    43     assert(stringlist_add(src, str4));
    44     
    45     cout << "checking contents\n";
    46     stringlist_t* p = src;
    47     int i = 0;
    48     while (p) {
    49         assert(p->value);
    50         assert(strcmp(p->value, strarr[i++]) == 0);
    51         assert(p->value != *(strarr + i)); // ensure this is a copy
    52         cout << p->value;
    53         p = p->next;
    54     }
    55     assert(p == NULL); // list ends properly
    56     
    57     cout << "\nduplicating four-element stringlist…\n";
    58     stringlist_t* dst = stringlist_dup(src);
    59     assert(dst);
    60     
    61     stringlist_t* p_dst = dst;
    62     p = src;
    63 
    64     cout << "checking contents\n";    
    65     while (p_dst) {
    66         assert(p_dst->value);
    67         assert(strcmp(p->value, p_dst->value) == 0);
    68         assert(p->value != p_dst->value); // ensure this is a copy
    69         cout << p_dst->value;
    70         p = p->next;
    71         p_dst = p_dst->next;
    72         assert((p == NULL) == (p_dst == NULL));
    73     }
    74     assert(p_dst == NULL);
    75         
    76     cout << "freeing stringlists…\n\n";
    77     free_stringlist(src);
    78     free_stringlist(dst);
    79     src = NULL;
    80     dst = NULL;
    81 
    82     cout << "duplicating one-element stringlist…\n";    
    83     src = new_stringlist(str0);
    84     assert(src);
    85     dst = stringlist_dup(src);
    86     assert(strcmp(dst->value, str0) == 0);
    87     cout << "Value: " << src->value;
    88     assert(dst->next == NULL);
    89     cout << "one-element stringlist duped, next element is NULL\n";
    90     
    91     cout << "\nAdd to empty stringlist (node exists, but no value…)\n";
    92     if (src->value)
    93         free(src->value);
    94     src->value = NULL;
    95     stringlist_add(src, str2);
    96     assert(src->value);
    97     assert(strcmp(src->value, str2) == 0);
    98     assert(src->value != str2); // ensure this is a copy
    99     cout << src->value;
   100 
   101     cout << "\nfreeing stringlists…\n\n";
   102     free_stringlist(src);
   103     free_stringlist(dst);
   104     
   105     src = NULL;
   106     dst = NULL;
   107     
   108     cout << "done.\n";
   109 
   110     return 0;
   111 }
   112