fix 98: stringlist_add now iterative
authorKrista Grothoff <krista@pep-project.org>
Mon, 11 Jul 2016 09:08:34 +0200
changeset 82737f1b63ecf97
parent 826 51b1d46df1ae
child 828 ea7e5920eda3
fix 98: stringlist_add now iterative
src/stringlist.c
test/stringlist_test.cc
     1.1 --- a/src/stringlist.c	Mon Jul 11 09:07:31 2016 +0200
     1.2 +++ b/src/stringlist.c	Mon Jul 11 09:08:34 2016 +0200
     1.3 @@ -51,28 +51,35 @@
     1.4          stringlist_t *stringlist,
     1.5          const char *value
     1.6      )
     1.7 -{
     1.8 +{  
     1.9      assert(value);
    1.10  
    1.11      if (stringlist == NULL)
    1.12          return new_stringlist(value);
    1.13  
    1.14 -    if (stringlist->next != NULL)
    1.15 -        return stringlist_add(stringlist->next, value);
    1.16 -    if (stringlist->value == NULL) {
    1.17 -        stringlist->value = strdup(value);
    1.18 -        assert(stringlist->value);
    1.19 -        if (stringlist->value == NULL)
    1.20 +    stringlist_t* list_curr = stringlist;
    1.21 +    
    1.22 +    while (list_curr->next)
    1.23 +        list_curr = list_curr->next;
    1.24 + 
    1.25 +    // if list end exists without value,
    1.26 +    // we fill it in here instead of adding
    1.27 +    // a new node.
    1.28 +    if (list_curr->value == NULL) {
    1.29 +        list_curr->value = strdup(value);
    1.30 +        assert(list_curr->value);
    1.31 +        if (list_curr->value == NULL)
    1.32              return NULL;
    1.33 -        return stringlist;
    1.34 +        return list_curr;
    1.35      }
    1.36 +    
    1.37 +    list_curr->next = new_stringlist(value);
    1.38  
    1.39 -    stringlist->next = new_stringlist(value);
    1.40 -    assert(stringlist->next);
    1.41 -    if (stringlist->next == NULL)
    1.42 +    assert(list_curr->next);
    1.43 +    if (list_curr->next == NULL)
    1.44          return NULL;
    1.45  
    1.46 -    return stringlist->next;
    1.47 +    return list_curr->next;
    1.48  }
    1.49  
    1.50  DYNAMIC_API stringlist_t *stringlist_append(
     2.1 --- a/test/stringlist_test.cc	Mon Jul 11 09:07:31 2016 +0200
     2.2 +++ b/test/stringlist_test.cc	Mon Jul 11 09:08:34 2016 +0200
     2.3 @@ -11,13 +11,15 @@
     2.4  
     2.5  int main() {
     2.6      cout << "\n*** data structures: stringlist_test ***\n\n";
     2.7 +
     2.8 +    const char* str0 = "I am your father, Luke\n";
     2.9      
    2.10      // new_stringlist test code
    2.11      cout << "creating one-element stringlist…\n";
    2.12      
    2.13 -    stringlist_t* src = new_stringlist("I am your father, Luke\n");
    2.14 +    stringlist_t* src = new_stringlist(str0);
    2.15      assert(src);
    2.16 -    assert(strcmp(src->value,"I am your father, Luke\n") == 0);
    2.17 +    assert(strcmp(src->value,str0) == 0);
    2.18      cout << "Value: " << src->value;
    2.19      assert(src->next == NULL);
    2.20      cout << "one-element stringlist created, next element is NULL\n";
    2.21 @@ -70,7 +72,32 @@
    2.22          assert((p == NULL) == (p_dst == NULL));
    2.23      }
    2.24      assert(p_dst == NULL);
    2.25 -
    2.26 +    
    2.27 +    cout << "\nAdd to 4-element stringlist with tail with no value…\n";
    2.28 +    // get tail
    2.29 +    p = src;
    2.30 +    while (p->next)
    2.31 +        p = p->next;
    2.32 +    
    2.33 +    if (p->value)
    2.34 +        free(p->value);
    2.35 +    p->value = NULL;
    2.36 +    
    2.37 +    strarr[3] = str0;
    2.38 +    stringlist_add(src, str0);
    2.39 +    
    2.40 +    cout << "checking contents\n";
    2.41 +    p = src;
    2.42 +    i = 0;
    2.43 +    while (p) {
    2.44 +        assert(p->value);
    2.45 +        assert(strcmp(p->value, strarr[i++]) == 0);
    2.46 +        assert(p->value != *(strarr + i)); // ensure this is a copy
    2.47 +        cout << p->value;
    2.48 +        p = p->next;
    2.49 +    }
    2.50 +    assert(p == NULL); // list ends properly
    2.51 +    
    2.52      cout << "freeing stringlists…\n\n";
    2.53      free_stringlist(src);
    2.54      free_stringlist(dst);
    2.55 @@ -78,15 +105,25 @@
    2.56      dst = NULL;
    2.57  
    2.58      cout << "duplicating one-element stringlist…\n";    
    2.59 -    src = new_stringlist("I am your father, Luke\n");
    2.60 +    src = new_stringlist(str0);
    2.61      assert(src);
    2.62      dst = stringlist_dup(src);
    2.63 -    assert(strcmp(dst->value,"I am your father, Luke\n") == 0);
    2.64 +    assert(strcmp(dst->value, str0) == 0);
    2.65      cout << "Value: " << src->value;
    2.66      assert(dst->next == NULL);
    2.67      cout << "one-element stringlist duped, next element is NULL\n";
    2.68      
    2.69 -    cout << "freeing stringlists…\n\n";
    2.70 +    cout << "\nAdd to one-element stringlist with no value…\n";
    2.71 +    if (src->value)
    2.72 +        free(src->value);
    2.73 +    src->value = NULL;
    2.74 +    stringlist_add(src, str2);
    2.75 +    assert(src->value);
    2.76 +    assert(strcmp(src->value, str2) == 0);
    2.77 +    assert(src->value != str2); // ensure this is a copy
    2.78 +    cout << src->value;
    2.79 +
    2.80 +    cout << "\nfreeing stringlists…\n\n";
    2.81      free_stringlist(src);
    2.82      free_stringlist(dst);
    2.83