Browse Source

Fixed stringpair_list_dup to make deep copies, as intended

doc_update_sequoia
Krista Grothoff 6 years ago
parent
commit
ce8a86eb9b
3 changed files with 31 additions and 10 deletions
  1. +5
    -2
      src/stringpair.c
  2. +3
    -1
      src/stringpair.h
  3. +23
    -7
      test/stringpair_list_test.cc

+ 5
- 2
src/stringpair.c View File

@ -71,7 +71,9 @@ DYNAMIC_API stringpair_list_t *stringpair_list_dup(
if (src == NULL)
return NULL;
stringpair_list_t *dst = new_stringpair_list(src->value);
stringpair_t* copy_pair = stringpair_dup(src->value);
stringpair_list_t *dst = new_stringpair_list(copy_pair);
if (dst == NULL)
return NULL;
@ -79,7 +81,8 @@ DYNAMIC_API stringpair_list_t *stringpair_list_dup(
stringpair_list_t** dst_curr_ptr = &dst->next;
while (src_curr) {
*dst_curr_ptr = new_stringpair_list(src_curr->value);
copy_pair = stringpair_dup(src_curr->value);
*dst_curr_ptr = new_stringpair_list(copy_pair);
src_curr = src_curr->next;
dst_curr_ptr = &((*dst_curr_ptr)->next);
}


+ 3
- 1
src/stringpair.h View File

@ -63,17 +63,19 @@ typedef struct _stringpair_list_t {
//
// caveat:
// the ownership of the value goes to the stringpair_list
// next pointer explicitly set to NULL
DYNAMIC_API stringpair_list_t *new_stringpair_list(stringpair_t *value);
// stringpair_list_dup() - duplicate a stringpair_list
// stringpair_list_dup() - duplicate a stringpair_list (deep copy)
//
// parameters:
// src (in) stringpair_list to copy
//
// return value:
// pointer to stringpair_list_t object or NULL if out of memory
// stringpair value copies created by this function belong to the returned list
DYNAMIC_API stringpair_list_t *stringpair_list_dup(
const stringpair_list_t *src


+ 23
- 7
test/stringpair_list_test.cc View File

@ -46,16 +46,32 @@ int main() {
stringpair_list_t* pairlist = new_stringpair_list(strpair);
assert(pairlist->value);
assert(test_stringpair_equals(strpair, pairlist->value));
// assert(strpair->key != pairlist->value->key); // test deep copies (to be fixed in next 2 commits)
// assert(strpair->value != pairlist->value->value);
assert(pairlist->next == NULL);
cout << "one-element stringpair_list created, next element is NULL\n";
cout << "freeing stringpair_list...\n";
free_stringpair_list(pairlist);
// free_stringpair(strpair); // copy still shallow (to be fixed in next 2 commits);
cout << "one-element stringpair_list created, next element is NULL\n\n";
cout << "duplicating one-element list...\n";
stringpair_list_t* duplist = stringpair_list_dup(pairlist);
stringpair_t* srcpair = pairlist->value;
stringpair_t* dstpair = duplist->value;
assert(dstpair);
assert(dstpair->value);
assert(test_stringpair_equals(srcpair, dstpair));
assert(srcpair->key != dstpair->key); // test deep copies (to be fixed in next 2 commits)
assert(srcpair->value != dstpair->value);
assert(duplist->next == NULL);
cout << "one-element stringpair_list duplicated.\n\n";
cout << "freeing stringpair_lists...\n";
free_stringpair_list(pairlist); // should free strpair
free_stringpair_list(duplist);
cout << "done.\n";
return 0;
}

Loading…
Cancel
Save