merge
authorEdouard Tisserant
Mon, 11 Jul 2016 18:15:09 +0200
changeset 839d2740d2c67b3
parent 838 70119c10b0f5
parent 836 8e7dac747b49
child 840 3d5e68bfc548
merge
     1.1 --- a/src/stringlist.c	Mon Jul 11 18:11:36 2016 +0200
     1.2 +++ b/src/stringlist.c	Mon Jul 11 18:15:09 2016 +0200
     1.3 @@ -88,6 +88,8 @@
     1.4      )
     1.5  {
     1.6      assert(stringlist);
     1.7 +    if (stringlist == NULL)
     1.8 +        return NULL;
     1.9  
    1.10      if (second == NULL || second->value == NULL)
    1.11          return stringlist;
     2.1 --- a/src/stringlist.h	Mon Jul 11 18:11:36 2016 +0200
     2.2 +++ b/src/stringlist.h	Mon Jul 11 18:15:09 2016 +0200
     2.3 @@ -67,6 +67,7 @@
     2.4  //
     2.5  //  return value:
     2.6  //      pointer to last element in stringlist or NULL if out of memory
     2.7 +//      or stringpair_list is NULL
     2.8  //
     2.9  //  caveat:
    2.10  //      all values are being copied before being added to the list
     3.1 --- a/src/stringpair.c	Mon Jul 11 18:11:36 2016 +0200
     3.2 +++ b/src/stringpair.c	Mon Jul 11 18:15:09 2016 +0200
     3.3 @@ -101,20 +101,30 @@
     3.4      if (stringpair_list == NULL)
     3.5          return new_stringpair_list(value);
     3.6  
     3.7 -    if (stringpair_list->next)
     3.8 -        return stringpair_list_add(stringpair_list->next, value);
     3.9 +    stringpair_list_t* list_curr = stringpair_list;
    3.10 +    
    3.11 +    while (list_curr->next)
    3.12 +        list_curr = list_curr->next;
    3.13 + 
    3.14 +    // if list end exists without value,
    3.15 +    // we fill it in here instead of adding
    3.16 +    // a new node.
    3.17 +    if (list_curr->value == NULL) {
    3.18 +        list_curr->value = value; // ownership goes to us
    3.19 +        assert(list_curr->value);
    3.20 +        if (list_curr->value == NULL)
    3.21 +            return NULL;
    3.22 +        return list_curr;
    3.23 +    }
    3.24 +    
    3.25 +    list_curr->next = new_stringpair_list(value);
    3.26  
    3.27 -    if (stringpair_list->value == NULL) {
    3.28 -        assert(stringpair_list->next == NULL);
    3.29 -        stringpair_list->value = value;
    3.30 -        return stringpair_list;
    3.31 -    }
    3.32 -
    3.33 -    stringpair_list->next = new_stringpair_list(value);
    3.34 -    if (stringpair_list->next == NULL)
    3.35 +    assert(list_curr->next);
    3.36 +    if (list_curr->next == NULL)
    3.37          return NULL;
    3.38  
    3.39 -    return stringpair_list->next;
    3.40 +    return list_curr->next;
    3.41 +    
    3.42  }
    3.43  
    3.44  DYNAMIC_API stringpair_list_t *stringpair_list_append(
    3.45 @@ -123,6 +133,8 @@
    3.46      )
    3.47  {
    3.48      assert(stringpair_list);
    3.49 +    if (stringpair_list == NULL)
    3.50 +        return NULL;
    3.51  
    3.52      if (second == NULL || second->value == NULL)
    3.53          return stringpair_list;
     4.1 --- a/src/stringpair.h	Mon Jul 11 18:11:36 2016 +0200
     4.2 +++ b/src/stringpair.h	Mon Jul 11 18:15:09 2016 +0200
     4.3 @@ -109,6 +109,7 @@
     4.4  //
     4.5  //  return value:
     4.6  //      pointer to last element in stringpair_list or NULL if out of memory
     4.7 +//      or stringpair_list is NULL
     4.8  //
     4.9  //  caveat:
    4.10  //      all values are being copied before being added to the list
     5.1 --- a/test/stringpair_list_test.cc	Mon Jul 11 18:11:36 2016 +0200
     5.2 +++ b/test/stringpair_list_test.cc	Mon Jul 11 18:15:09 2016 +0200
     5.3 @@ -22,14 +22,14 @@
     5.4  int main() {
     5.5      cout << "\n*** data structures: stringpair_list_test ***\n\n";
     5.6  
     5.7 -    const char* val_1_arr[4] = {"I am your father, Luke\n",
     5.8 -                                "These are not the droids you're looking for\n",
     5.9 -                                "Swooping is bad\n",
    5.10 -                                "I should go.\n"};
    5.11 -    const char* val_2_arr[4] = {"Had to be me.\n",
    5.12 -                                "Someone else might have gotten it wrong\n",
    5.13 -                                "Na via lerno victoria\n",
    5.14 -                                "I was told that there would be cake.\n"};
    5.15 +    const char* val_1_arr[4] = {"I am your father, Luke",
    5.16 +                                "These are not the droids you're looking for",
    5.17 +                                "Swooping is bad",
    5.18 +                                "I should go."};
    5.19 +    const char* val_2_arr[4] = {"Had to be me.",
    5.20 +                                "Someone else might have gotten it wrong",
    5.21 +                                "Na via lerno victoria",
    5.22 +                                "I was told that there would be cake."};
    5.23                                  
    5.24  //    const stringpair_t* stringpair_arr[4];
    5.25      
    5.26 @@ -60,17 +60,78 @@
    5.27      assert(srcpair->value != dstpair->value);
    5.28      assert(duplist->next == NULL);
    5.29      cout << "one-element stringpair_list duplicated.\n\n";
    5.30 -
    5.31      
    5.32      cout << "freeing stringpair_lists...\n";
    5.33 -    free_stringpair_list(pairlist); // should free strpair
    5.34 +    free_stringpair_list(pairlist); // will free strpair
    5.35      free_stringpair_list(duplist);
    5.36 +    pairlist = NULL;
    5.37 +    duplist = NULL;
    5.38 +    strpair = NULL;
    5.39 +    
    5.40 +    stringpair_list_t* p;
    5.41 +    cout << "\ncreating four-element list...\n";
    5.42 +    pairlist = stringpair_list_add(pairlist, new_stringpair(val_1_arr[0], val_2_arr[0]));
    5.43 +    for (i = 1; i < 4; i++) {
    5.44 +        p = stringpair_list_add(pairlist, new_stringpair(val_1_arr[i], val_2_arr[i]));
    5.45 +        assert(p);
    5.46 +    }
    5.47 +    
    5.48 +    p = pairlist;
    5.49 +    
    5.50 +    for (i = 0; i < 4; i++) {
    5.51 +        assert(p);
    5.52 +        
    5.53 +        strpair = p->value;
    5.54 +        assert(strpair);
    5.55 +        
    5.56 +        assert(strpair->key);
    5.57 +        assert(strcmp(val_1_arr[i], strpair->key) == 0);
    5.58 +        
    5.59 +        assert(strpair->value);
    5.60 +        assert(strcmp(val_2_arr[i], strpair->value) == 0);
    5.61 +        
    5.62 +        assert(val_1_arr[i] != strpair->key);
    5.63 +        assert(val_2_arr[i] != strpair->value);
    5.64 +        
    5.65 +        p = p->next;
    5.66 +    }
    5.67 +    assert(p == NULL);
    5.68 +    
    5.69 +    cout << "\nduplicating four-element list...\n\n";
    5.70 +    duplist = stringpair_list_dup(pairlist);
    5.71 +    
    5.72 +    p = pairlist;
    5.73 +    stringpair_list_t* dup_p = duplist;
    5.74 +    
    5.75 +    while (dup_p) {
    5.76 +        srcpair = p->value;
    5.77 +        dstpair = dup_p->value;
    5.78 +
    5.79 +        assert(dstpair);
    5.80 +        assert(dstpair->value);
    5.81 +        
    5.82 +        cout << srcpair->key << ":" << srcpair->value << " / " << dstpair->key << ":" << dstpair->value << "\n";
    5.83 +        assert(test_stringpair_equals(srcpair, dstpair));
    5.84 +
    5.85 +        assert(srcpair->key != dstpair->key);   // test deep copies (to be fixed in next 2 commits)
    5.86 +        assert(srcpair->value != dstpair->value);
    5.87 +
    5.88 +        i++;
    5.89 +        p = p->next;
    5.90 +
    5.91 +        dup_p = dup_p->next;
    5.92 +        assert((p == NULL) == (dup_p == NULL));
    5.93 +    }
    5.94 +    cout << "\nfour-element stringpair_list successfully duplicated.\n\n";
    5.95 +
    5.96 +    cout << "freeing stringpair_lists...\n";
    5.97 +    free_stringpair_list(pairlist); // will free strpair
    5.98 +    free_stringpair_list(duplist);
    5.99 +    pairlist = NULL;
   5.100 +    duplist = NULL;
   5.101 +    
   5.102      cout << "done.\n";
   5.103 -
   5.104 -    
   5.105 -    
   5.106 -    
   5.107 -    
   5.108 +        
   5.109      
   5.110      return 0;
   5.111  }