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