test/stringpair_list_test.cc
changeset 834 1108548edc67
parent 832 8a54e3a6b155
child 1513 e7f7e42385b5
     1.1 --- a/test/stringpair_list_test.cc	Mon Jul 11 15:48:19 2016 +0200
     1.2 +++ b/test/stringpair_list_test.cc	Mon Jul 11 16:15:51 2016 +0200
     1.3 @@ -22,14 +22,14 @@
     1.4  int main() {
     1.5      cout << "\n*** data structures: stringpair_list_test ***\n\n";
     1.6  
     1.7 -    const char* val_1_arr[4] = {"I am your father, Luke\n",
     1.8 -                                "These are not the droids you're looking for\n",
     1.9 -                                "Swooping is bad\n",
    1.10 -                                "I should go.\n"};
    1.11 -    const char* val_2_arr[4] = {"Had to be me.\n",
    1.12 -                                "Someone else might have gotten it wrong\n",
    1.13 -                                "Na via lerno victoria\n",
    1.14 -                                "I was told that there would be cake.\n"};
    1.15 +    const char* val_1_arr[4] = {"I am your father, Luke",
    1.16 +                                "These are not the droids you're looking for",
    1.17 +                                "Swooping is bad",
    1.18 +                                "I should go."};
    1.19 +    const char* val_2_arr[4] = {"Had to be me.",
    1.20 +                                "Someone else might have gotten it wrong",
    1.21 +                                "Na via lerno victoria",
    1.22 +                                "I was told that there would be cake."};
    1.23                                  
    1.24  //    const stringpair_t* stringpair_arr[4];
    1.25      
    1.26 @@ -60,17 +60,78 @@
    1.27      assert(srcpair->value != dstpair->value);
    1.28      assert(duplist->next == NULL);
    1.29      cout << "one-element stringpair_list duplicated.\n\n";
    1.30 -
    1.31      
    1.32      cout << "freeing stringpair_lists...\n";
    1.33 -    free_stringpair_list(pairlist); // should free strpair
    1.34 +    free_stringpair_list(pairlist); // will free strpair
    1.35      free_stringpair_list(duplist);
    1.36 +    pairlist = NULL;
    1.37 +    duplist = NULL;
    1.38 +    strpair = NULL;
    1.39 +    
    1.40 +    stringpair_list_t* p;
    1.41 +    cout << "\ncreating four-element list...\n";
    1.42 +    pairlist = stringpair_list_add(pairlist, new_stringpair(val_1_arr[0], val_2_arr[0]));
    1.43 +    for (i = 1; i < 4; i++) {
    1.44 +        p = stringpair_list_add(pairlist, new_stringpair(val_1_arr[i], val_2_arr[i]));
    1.45 +        assert(p);
    1.46 +    }
    1.47 +    
    1.48 +    p = pairlist;
    1.49 +    
    1.50 +    for (i = 0; i < 4; i++) {
    1.51 +        assert(p);
    1.52 +        
    1.53 +        strpair = p->value;
    1.54 +        assert(strpair);
    1.55 +        
    1.56 +        assert(strpair->key);
    1.57 +        assert(strcmp(val_1_arr[i], strpair->key) == 0);
    1.58 +        
    1.59 +        assert(strpair->value);
    1.60 +        assert(strcmp(val_2_arr[i], strpair->value) == 0);
    1.61 +        
    1.62 +        assert(val_1_arr[i] != strpair->key);
    1.63 +        assert(val_2_arr[i] != strpair->value);
    1.64 +        
    1.65 +        p = p->next;
    1.66 +    }
    1.67 +    assert(p == NULL);
    1.68 +    
    1.69 +    cout << "\nduplicating four-element list...\n\n";
    1.70 +    duplist = stringpair_list_dup(pairlist);
    1.71 +    
    1.72 +    p = pairlist;
    1.73 +    stringpair_list_t* dup_p = duplist;
    1.74 +    
    1.75 +    while (dup_p) {
    1.76 +        srcpair = p->value;
    1.77 +        dstpair = dup_p->value;
    1.78 +
    1.79 +        assert(dstpair);
    1.80 +        assert(dstpair->value);
    1.81 +        
    1.82 +        cout << srcpair->key << ":" << srcpair->value << " / " << dstpair->key << ":" << dstpair->value << "\n";
    1.83 +        assert(test_stringpair_equals(srcpair, dstpair));
    1.84 +
    1.85 +        assert(srcpair->key != dstpair->key);   // test deep copies (to be fixed in next 2 commits)
    1.86 +        assert(srcpair->value != dstpair->value);
    1.87 +
    1.88 +        i++;
    1.89 +        p = p->next;
    1.90 +
    1.91 +        dup_p = dup_p->next;
    1.92 +        assert((p == NULL) == (dup_p == NULL));
    1.93 +    }
    1.94 +    cout << "\nfour-element stringpair_list successfully duplicated.\n\n";
    1.95 +
    1.96 +    cout << "freeing stringpair_lists...\n";
    1.97 +    free_stringpair_list(pairlist); // will free strpair
    1.98 +    free_stringpair_list(duplist);
    1.99 +    pairlist = NULL;
   1.100 +    duplist = NULL;
   1.101 +    
   1.102      cout << "done.\n";
   1.103 -
   1.104 -    
   1.105 -    
   1.106 -    
   1.107 -    
   1.108 +        
   1.109      
   1.110      return 0;
   1.111  }