test/stringlist_test.cc
changeset 825 c5b5500a978d
child 827 37f1b63ecf97
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/stringlist_test.cc	Mon Jul 11 02:53:09 2016 +0200
     1.3 @@ -0,0 +1,100 @@
     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 "stringlist.h"
    1.12 +
    1.13 +using namespace std;
    1.14 +
    1.15 +int main() {
    1.16 +    cout << "\n*** data structures: stringlist_test ***\n\n";
    1.17 +    
    1.18 +    // new_stringlist test code
    1.19 +    cout << "creating one-element stringlist…\n";
    1.20 +    
    1.21 +    stringlist_t* src = new_stringlist("I am your father, Luke\n");
    1.22 +    assert(src);
    1.23 +    assert(strcmp(src->value,"I am your father, Luke\n") == 0);
    1.24 +    cout << "Value: " << src->value;
    1.25 +    assert(src->next == NULL);
    1.26 +    cout << "one-element stringlist created, next element is NULL\n";
    1.27 +    
    1.28 +    cout << "freeing stringlist…\n\n";
    1.29 +    free_stringlist(src);
    1.30 +    src = NULL;
    1.31 +    
    1.32 +    // test stringlist_add with four-element list
    1.33 +    cout << "creating four-element stringlist…\n";
    1.34 +    const char* str1 = "String 1";
    1.35 +    const char* str2 = "\tString 2";
    1.36 +    const char* str3 = "\tString 3";
    1.37 +    const char* str4 = "\tString 4\n";
    1.38 +    const char* strarr[4] = {str1, str2, str3, str4};
    1.39 +    cout << "stringlist_add on empty list…\n";
    1.40 +    src = stringlist_add(src, str1); // src is NULL
    1.41 +    assert(src);
    1.42 +    assert(stringlist_add(src, str2)); // returns ptr to new elt
    1.43 +    assert(stringlist_add(src, str3));
    1.44 +    assert(stringlist_add(src, str4));
    1.45 +    
    1.46 +    cout << "checking contents\n";
    1.47 +    stringlist_t* p = src;
    1.48 +    int i = 0;
    1.49 +    while (p) {
    1.50 +        assert(p->value);
    1.51 +        assert(strcmp(p->value, strarr[i++]) == 0);
    1.52 +        assert(p->value != *(strarr + i)); // ensure this is a copy
    1.53 +        cout << p->value;
    1.54 +        p = p->next;
    1.55 +    }
    1.56 +    assert(p == NULL); // list ends properly
    1.57 +    
    1.58 +    cout << "\nduplicating four-element stringlist…\n";
    1.59 +    stringlist_t* dst = stringlist_dup(src);
    1.60 +    assert(dst);
    1.61 +    
    1.62 +    stringlist_t* p_dst = dst;
    1.63 +    p = src;
    1.64 +
    1.65 +    cout << "checking contents\n";    
    1.66 +    while (p_dst) {
    1.67 +        assert(p_dst->value);
    1.68 +        assert(strcmp(p->value, p_dst->value) == 0);
    1.69 +        assert(p->value != p_dst->value); // ensure this is a copy
    1.70 +        cout << p_dst->value;
    1.71 +        p = p->next;
    1.72 +        p_dst = p_dst->next;
    1.73 +        assert((p == NULL) == (p_dst == NULL));
    1.74 +    }
    1.75 +    assert(p_dst == NULL);
    1.76 +
    1.77 +    cout << "freeing stringlists…\n\n";
    1.78 +    free_stringlist(src);
    1.79 +    free_stringlist(dst);
    1.80 +    src = NULL;
    1.81 +    dst = NULL;
    1.82 +
    1.83 +    cout << "duplicating one-element stringlist…\n";    
    1.84 +    src = new_stringlist("I am your father, Luke\n");
    1.85 +    assert(src);
    1.86 +    dst = stringlist_dup(src);
    1.87 +    assert(strcmp(dst->value,"I am your father, Luke\n") == 0);
    1.88 +    cout << "Value: " << src->value;
    1.89 +    assert(dst->next == NULL);
    1.90 +    cout << "one-element stringlist duped, next element is NULL\n";
    1.91 +    
    1.92 +    cout << "freeing stringlists…\n\n";
    1.93 +    free_stringlist(src);
    1.94 +    free_stringlist(dst);
    1.95 +    
    1.96 +    src = NULL;
    1.97 +    dst = NULL;
    1.98 +    
    1.99 +    cout << "done.\n";
   1.100 +
   1.101 +    return 0;
   1.102 +}
   1.103 +