Fixed stringpair_list_dup to make deep copies, as intended
authorKrista Grothoff <krista@pep-project.org>
Mon, 11 Jul 2016 12:08:39 +0200
changeset 8328a54e3a6b155
parent 831 1d2fc388c491
child 833 e9ee49d7ab5a
Fixed stringpair_list_dup to make deep copies, as intended
src/stringpair.c
src/stringpair.h
test/stringpair_list_test.cc
     1.1 --- a/src/stringpair.c	Mon Jul 11 11:43:03 2016 +0200
     1.2 +++ b/src/stringpair.c	Mon Jul 11 12:08:39 2016 +0200
     1.3 @@ -71,7 +71,9 @@
     1.4      if (src == NULL)
     1.5          return NULL;
     1.6  
     1.7 -    stringpair_list_t *dst = new_stringpair_list(src->value);
     1.8 +    stringpair_t* copy_pair = stringpair_dup(src->value);
     1.9 +    
    1.10 +    stringpair_list_t *dst = new_stringpair_list(copy_pair);
    1.11      if (dst == NULL)
    1.12          return NULL;
    1.13  
    1.14 @@ -79,7 +81,8 @@
    1.15      stringpair_list_t** dst_curr_ptr = &dst->next;
    1.16  
    1.17      while (src_curr) {
    1.18 -        *dst_curr_ptr = new_stringpair_list(src_curr->value);
    1.19 +        copy_pair = stringpair_dup(src_curr->value);
    1.20 +        *dst_curr_ptr = new_stringpair_list(copy_pair);
    1.21          src_curr = src_curr->next;
    1.22          dst_curr_ptr = &((*dst_curr_ptr)->next);
    1.23      }
     2.1 --- a/src/stringpair.h	Mon Jul 11 11:43:03 2016 +0200
     2.2 +++ b/src/stringpair.h	Mon Jul 11 12:08:39 2016 +0200
     2.3 @@ -63,17 +63,19 @@
     2.4  //
     2.5  //  caveat:
     2.6  //      the ownership of the value goes to the stringpair_list
     2.7 +//      next pointer explicitly set to NULL
     2.8  
     2.9  DYNAMIC_API stringpair_list_t *new_stringpair_list(stringpair_t *value);
    2.10  
    2.11  
    2.12 -// stringpair_list_dup() - duplicate a stringpair_list
    2.13 +// stringpair_list_dup() - duplicate a stringpair_list (deep copy)
    2.14  //
    2.15  //  parameters:
    2.16  //      src (in)                stringpair_list to copy
    2.17  //
    2.18  //  return value:
    2.19  //      pointer to stringpair_list_t object or NULL if out of memory
    2.20 +//      stringpair value copies created by this function belong to the returned list
    2.21  
    2.22  DYNAMIC_API stringpair_list_t *stringpair_list_dup(
    2.23          const stringpair_list_t *src
     3.1 --- a/test/stringpair_list_test.cc	Mon Jul 11 11:43:03 2016 +0200
     3.2 +++ b/test/stringpair_list_test.cc	Mon Jul 11 12:08:39 2016 +0200
     3.3 @@ -46,16 +46,32 @@
     3.4      stringpair_list_t* pairlist = new_stringpair_list(strpair);
     3.5      assert(pairlist->value);
     3.6      assert(test_stringpair_equals(strpair, pairlist->value));
     3.7 -//    assert(strpair->key != pairlist->value->key);   // test deep copies (to be fixed in next 2 commits)
     3.8 -//    assert(strpair->value != pairlist->value->value);
     3.9      assert(pairlist->next == NULL);
    3.10 -    cout << "one-element stringpair_list created, next element is NULL\n";
    3.11 -        
    3.12 -    cout << "freeing stringpair_list...\n";
    3.13 -    free_stringpair_list(pairlist);
    3.14 -    // free_stringpair(strpair); // copy still shallow (to be fixed in next 2 commits);
    3.15 +    cout << "one-element stringpair_list created, next element is NULL\n\n";
    3.16 +    
    3.17 +    cout << "duplicating one-element list...\n";
    3.18 +    stringpair_list_t* duplist = stringpair_list_dup(pairlist);
    3.19 +    stringpair_t* srcpair = pairlist->value;
    3.20 +    stringpair_t* dstpair = duplist->value;
    3.21 +    assert(dstpair);
    3.22 +    assert(dstpair->value);
    3.23 +    assert(test_stringpair_equals(srcpair, dstpair));
    3.24 +    assert(srcpair->key != dstpair->key);   // test deep copies (to be fixed in next 2 commits)
    3.25 +    assert(srcpair->value != dstpair->value);
    3.26 +    assert(duplist->next == NULL);
    3.27 +    cout << "one-element stringpair_list duplicated.\n\n";
    3.28 +
    3.29 +    
    3.30 +    cout << "freeing stringpair_lists...\n";
    3.31 +    free_stringpair_list(pairlist); // should free strpair
    3.32 +    free_stringpair_list(duplist);
    3.33      cout << "done.\n";
    3.34  
    3.35 +    
    3.36 +    
    3.37 +    
    3.38 +    
    3.39 +    
    3.40      return 0;
    3.41  }
    3.42