Browse Source

fix #104: fixed incorrect/confusing behaviour in stringlist_add/append stringpair_list_add/append.

doc_update_sequoia
Krista Grothoff 6 years ago
parent
commit
8a109ad9ec
3 changed files with 42 additions and 53 deletions
  1. +20
    -14
      src/stringlist.c
  2. +20
    -12
      src/stringpair.c
  3. +2
    -27
      test/stringlist_test.cc

+ 20
- 14
src/stringlist.c View File

@ -19,8 +19,9 @@ DYNAMIC_API stringlist_t *new_stringlist(const char *value)
free(result);
return NULL;
}
result->next = NULL; // needed for one-element lists
}
result->next = NULL; // needed for one-element lists
return result;
}
@ -54,25 +55,29 @@ DYNAMIC_API stringlist_t *stringlist_add(
{
assert(value);
// empty list (no nodes)
if (stringlist == NULL)
return new_stringlist(value);
stringlist_t* list_curr = stringlist;
while (list_curr->next)
list_curr = list_curr->next;
// if list end exists without value,
// we fill it in here instead of adding
// a new node.
if (list_curr->value == NULL) {
list_curr->value = strdup(value);
assert(list_curr->value);
if (list_curr->value == NULL)
// empty list (one node, no value)
if (stringlist->value == NULL) {
if (stringlist->next)
return NULL; // invalid list
stringlist->value = strdup(value);
assert(stringlist->value);
if (stringlist->value == NULL)
return NULL;
return list_curr;
return stringlist;
}
stringlist_t* list_curr = stringlist;
while (list_curr->next)
list_curr = list_curr->next;
list_curr->next = new_stringlist(value);
assert(list_curr->next);
@ -91,6 +96,7 @@ DYNAMIC_API stringlist_t *stringlist_append(
if (stringlist == NULL)
return NULL;
// Second list is empty
if (second == NULL || second->value == NULL)
return stringlist;


+ 20
- 12
src/stringpair.c View File

@ -47,6 +47,9 @@ DYNAMIC_API void free_stringpair(stringpair_t * pair)
DYNAMIC_API stringpair_t * stringpair_dup(const stringpair_t *src)
{
assert(src);
if (src == NULL)
return NULL;
return new_stringpair(src->key, src->value);
}
@ -98,25 +101,29 @@ DYNAMIC_API stringpair_list_t *stringpair_list_add(
{
assert(value);
// empty list (no nodes)
if (stringpair_list == NULL)
return new_stringpair_list(value);
// empty list (one node, no value)
if (stringpair_list->value == NULL) {
if (stringpair_list->next)
return NULL; // invalid list
stringpair_list->value = value;
assert(stringpair_list->value);
if (stringpair_list->value == NULL)
return NULL;
return stringpair_list;
}
stringpair_list_t* list_curr = stringpair_list;
while (list_curr->next)
list_curr = list_curr->next;
// if list end exists without value,
// we fill it in here instead of adding
// a new node.
if (list_curr->value == NULL) {
list_curr->value = value; // ownership goes to us
assert(list_curr->value);
if (list_curr->value == NULL)
return NULL;
return list_curr;
}
list_curr->next = new_stringpair_list(value);
assert(list_curr->next);
@ -136,6 +143,7 @@ DYNAMIC_API stringpair_list_t *stringpair_list_append(
if (stringpair_list == NULL)
return NULL;
// second list is empty
if (second == NULL || second->value == NULL)
return stringpair_list;


+ 2
- 27
test/stringlist_test.cc View File

@ -72,32 +72,7 @@ int main() {
assert((p == NULL) == (p_dst == NULL));
}
assert(p_dst == NULL);
cout << "\nAdd to 4-element stringlist with tail with no value…\n";
// get tail
p = src;
while (p->next)
p = p->next;
if (p->value)
free(p->value);
p->value = NULL;
strarr[3] = str0;
stringlist_add(src, str0);
cout << "checking contents\n";
p = src;
i = 0;
while (p) {
assert(p->value);
assert(strcmp(p->value, strarr[i++]) == 0);
assert(p->value != *(strarr + i)); // ensure this is a copy
cout << p->value;
p = p->next;
}
assert(p == NULL); // list ends properly
cout << "freeing stringlists…\n\n";
free_stringlist(src);
free_stringlist(dst);
@ -113,7 +88,7 @@ int main() {
assert(dst->next == NULL);
cout << "one-element stringlist duped, next element is NULL\n";
cout << "\nAdd to one-element stringlist with no value…\n";
cout << "\nAdd to empty stringlist (node exists, but no value…)\n";
if (src->value)
free(src->value);
src->value = NULL;


Loading…
Cancel
Save