fix #11: made stringlist_dup iterative, added NULL to new_stringlist for next ptr (this wasn't set for one-element lists).
authorKrista Grothoff <krista@pep-project.org>
Mon, 11 Jul 2016 02:53:09 +0200
changeset 825c5b5500a978d
parent 824 7e83e5846e4a
child 826 51b1d46df1ae
fix #11: made stringlist_dup iterative, added NULL to new_stringlist for next ptr (this wasn't set for one-element lists).
src/stringlist.c
test/stringlist_test.cc
     1.1 --- a/src/stringlist.c	Sun Jul 10 19:44:46 2016 +0200
     1.2 +++ b/src/stringlist.c	Mon Jul 11 02:53:09 2016 +0200
     1.3 @@ -19,6 +19,7 @@
     1.4              free(result);
     1.5              return NULL;
     1.6          }
     1.7 +        result->next = NULL; // needed for one-element lists
     1.8      }
     1.9  
    1.10      return result;
    1.11 @@ -34,12 +35,13 @@
    1.12      if (dst == NULL)
    1.13          return NULL;
    1.14  
    1.15 -    if (src->next) {
    1.16 -        dst->next = stringlist_dup(src->next);
    1.17 -        if (dst->next == NULL) {
    1.18 -            free_stringlist(dst);
    1.19 -            return NULL;
    1.20 -        }
    1.21 +    stringlist_t* src_curr = src->next;
    1.22 +    stringlist_t** dst_curr_ptr = &dst->next;
    1.23 +    
    1.24 +    while (src_curr) {
    1.25 +        *dst_curr_ptr = new_stringlist(src_curr->value);
    1.26 +        src_curr = src_curr->next;
    1.27 +        dst_curr_ptr = &((*dst_curr_ptr)->next);
    1.28      }
    1.29  
    1.30      return dst;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/stringlist_test.cc	Mon Jul 11 02:53:09 2016 +0200
     2.3 @@ -0,0 +1,100 @@
     2.4 +#include <stdlib.h>
     2.5 +#include <string.h>
     2.6 +#include "platform.h"
     2.7 +#include <iostream>
     2.8 +#include <fstream>
     2.9 +#include <assert.h>
    2.10 +
    2.11 +#include "stringlist.h"
    2.12 +
    2.13 +using namespace std;
    2.14 +
    2.15 +int main() {
    2.16 +    cout << "\n*** data structures: stringlist_test ***\n\n";
    2.17 +    
    2.18 +    // new_stringlist test code
    2.19 +    cout << "creating one-element stringlist…\n";
    2.20 +    
    2.21 +    stringlist_t* src = new_stringlist("I am your father, Luke\n");
    2.22 +    assert(src);
    2.23 +    assert(strcmp(src->value,"I am your father, Luke\n") == 0);
    2.24 +    cout << "Value: " << src->value;
    2.25 +    assert(src->next == NULL);
    2.26 +    cout << "one-element stringlist created, next element is NULL\n";
    2.27 +    
    2.28 +    cout << "freeing stringlist…\n\n";
    2.29 +    free_stringlist(src);
    2.30 +    src = NULL;
    2.31 +    
    2.32 +    // test stringlist_add with four-element list
    2.33 +    cout << "creating four-element stringlist…\n";
    2.34 +    const char* str1 = "String 1";
    2.35 +    const char* str2 = "\tString 2";
    2.36 +    const char* str3 = "\tString 3";
    2.37 +    const char* str4 = "\tString 4\n";
    2.38 +    const char* strarr[4] = {str1, str2, str3, str4};
    2.39 +    cout << "stringlist_add on empty list…\n";
    2.40 +    src = stringlist_add(src, str1); // src is NULL
    2.41 +    assert(src);
    2.42 +    assert(stringlist_add(src, str2)); // returns ptr to new elt
    2.43 +    assert(stringlist_add(src, str3));
    2.44 +    assert(stringlist_add(src, str4));
    2.45 +    
    2.46 +    cout << "checking contents\n";
    2.47 +    stringlist_t* p = src;
    2.48 +    int i = 0;
    2.49 +    while (p) {
    2.50 +        assert(p->value);
    2.51 +        assert(strcmp(p->value, strarr[i++]) == 0);
    2.52 +        assert(p->value != *(strarr + i)); // ensure this is a copy
    2.53 +        cout << p->value;
    2.54 +        p = p->next;
    2.55 +    }
    2.56 +    assert(p == NULL); // list ends properly
    2.57 +    
    2.58 +    cout << "\nduplicating four-element stringlist…\n";
    2.59 +    stringlist_t* dst = stringlist_dup(src);
    2.60 +    assert(dst);
    2.61 +    
    2.62 +    stringlist_t* p_dst = dst;
    2.63 +    p = src;
    2.64 +
    2.65 +    cout << "checking contents\n";    
    2.66 +    while (p_dst) {
    2.67 +        assert(p_dst->value);
    2.68 +        assert(strcmp(p->value, p_dst->value) == 0);
    2.69 +        assert(p->value != p_dst->value); // ensure this is a copy
    2.70 +        cout << p_dst->value;
    2.71 +        p = p->next;
    2.72 +        p_dst = p_dst->next;
    2.73 +        assert((p == NULL) == (p_dst == NULL));
    2.74 +    }
    2.75 +    assert(p_dst == NULL);
    2.76 +
    2.77 +    cout << "freeing stringlists…\n\n";
    2.78 +    free_stringlist(src);
    2.79 +    free_stringlist(dst);
    2.80 +    src = NULL;
    2.81 +    dst = NULL;
    2.82 +
    2.83 +    cout << "duplicating one-element stringlist…\n";    
    2.84 +    src = new_stringlist("I am your father, Luke\n");
    2.85 +    assert(src);
    2.86 +    dst = stringlist_dup(src);
    2.87 +    assert(strcmp(dst->value,"I am your father, Luke\n") == 0);
    2.88 +    cout << "Value: " << src->value;
    2.89 +    assert(dst->next == NULL);
    2.90 +    cout << "one-element stringlist duped, next element is NULL\n";
    2.91 +    
    2.92 +    cout << "freeing stringlists…\n\n";
    2.93 +    free_stringlist(src);
    2.94 +    free_stringlist(dst);
    2.95 +    
    2.96 +    src = NULL;
    2.97 +    dst = NULL;
    2.98 +    
    2.99 +    cout << "done.\n";
   2.100 +
   2.101 +    return 0;
   2.102 +}
   2.103 +