test/src/engine_tests/StringpairListTests.cc
author Krista Bennett <krista@pep-project.org>
Wed, 09 May 2018 11:39:10 +0200
branchlocal_cpptest
changeset 2669 1b103e8a95f7
parent 2653 232367954cc5
child 2685 01edd04b3c38
permissions -rw-r--r--
Added default test assert messages.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include "platform.h"
     7 #include <iostream>
     8 #include <fstream>
     9 #include <assert.h>
    10 
    11 #include "stringpair.h"
    12 
    13 #include "EngineTestSuite.h"
    14 #include "StringpairListTests.h"
    15 
    16 using namespace std;
    17 
    18 StringpairListTests::StringpairListTests(string suitename, string test_home_dir) : 
    19     EngineTestSuite::EngineTestSuite(suitename, test_home_dir) {            
    20     TEST_ADD(StringpairListTests::check_stringpair_lists);
    21 }
    22 
    23 bool StringpairListTests::test_stringpair_equals(stringpair_t* val1, stringpair_t* val2) {
    24     assert(val1);
    25     assert(val2);
    26     assert(val1->key);
    27     assert(val2->key);
    28     assert(val1->value);
    29     assert(val2->value);
    30     return((strcmp(val1->key, val2->key) == 0) && (strcmp(val1->value, val2->value) == 0));
    31 }
    32 
    33 void StringpairListTests::check_stringpair_lists() {
    34     cout << "\n*** data structures: stringpair_list_test ***\n\n";
    35 
    36     const char* val_1_arr[4] = {"I am your father, Luke",
    37                                 "These are not the droids you're looking for",
    38                                 "Swooping is bad",
    39                                 "I should go."};
    40     const char* val_2_arr[4] = {"Had to be me.",
    41                                 "Someone else might have gotten it wrong",
    42                                 "Na via lerno victoria",
    43                                 "I was told that there would be cake."};
    44                                 
    45 //    const stringpair_t* stringpair_arr[4];
    46     
    47     int i;
    48     
    49 //    for (i = 0; i < 4; i++) {
    50 //        stringpair_arr[i] = new stringpair(val_1_arr[i], val_2_arr[i]);
    51 //    }
    52     
    53     cout << "creating one-element stringpair_list...\n";
    54     
    55     stringpair_t* strpair = new_stringpair(val_1_arr[0], val_2_arr[0]);
    56     TEST_ASSERT_MSG((strpair), "strpair");
    57     stringpair_list_t* pairlist = new_stringpair_list(strpair);
    58     TEST_ASSERT_MSG((pairlist->value), "pairlist->value");
    59     TEST_ASSERT_MSG((test_stringpair_equals(strpair, pairlist->value)), "test_stringpair_equals(strpair, pairlist->value)");
    60     TEST_ASSERT_MSG((pairlist->next == NULL), "pairlist->next == NULL");
    61     cout << "one-element stringpair_list created, next element is NULL\n\n";
    62     
    63     cout << "duplicating one-element list...\n";
    64     stringpair_list_t* duplist = stringpair_list_dup(pairlist);
    65     stringpair_t* srcpair = pairlist->value;
    66     stringpair_t* dstpair = duplist->value;
    67     TEST_ASSERT_MSG((dstpair), "dstpair");
    68     TEST_ASSERT_MSG((dstpair->value), "dstpair->value");
    69     TEST_ASSERT_MSG((test_stringpair_equals(srcpair, dstpair)), "test_stringpair_equals(srcpair, dstpair)");
    70     TEST_ASSERT_MSG((srcpair->key != dstpair->key), "srcpair->key != dstpair->key");   // test deep copies (to be fixed in next 2 commits)
    71     TEST_ASSERT_MSG((srcpair->value != dstpair->value), "srcpair->value != dstpair->value");
    72     TEST_ASSERT_MSG((duplist->next == NULL), "duplist->next == NULL");
    73     cout << "one-element stringpair_list duplicated.\n\n";
    74     
    75     cout << "freeing stringpair_lists...\n";
    76     free_stringpair_list(pairlist); // will free strpair
    77     free_stringpair_list(duplist);
    78     pairlist = NULL;
    79     duplist = NULL;
    80     strpair = NULL;
    81     
    82     stringpair_list_t* p;
    83     cout << "\ncreating four-element list...\n";
    84     pairlist = stringpair_list_add(pairlist, new_stringpair(val_1_arr[0], val_2_arr[0]));
    85     for (i = 1; i < 4; i++) {
    86         p = stringpair_list_add(pairlist, new_stringpair(val_1_arr[i], val_2_arr[i]));
    87         TEST_ASSERT_MSG((p), "p");
    88     }
    89     
    90     p = pairlist;
    91     
    92     for (i = 0; i < 4; i++) {
    93         TEST_ASSERT_MSG((p), "p");
    94         
    95         strpair = p->value;
    96         TEST_ASSERT_MSG((strpair), "strpair");
    97         
    98         TEST_ASSERT_MSG((strpair->key), "strpair->key");
    99         TEST_ASSERT_MSG((strcmp(val_1_arr[i], strpair->key) == 0), "strcmp(val_1_arr[i], strpair->key) == 0");
   100         
   101         TEST_ASSERT_MSG((strpair->value), "strpair->value");
   102         TEST_ASSERT_MSG((strcmp(val_2_arr[i], strpair->value) == 0), "strcmp(val_2_arr[i], strpair->value) == 0");
   103         
   104         TEST_ASSERT_MSG((val_1_arr[i] != strpair->key), "val_1_arr[i] != strpair->key");
   105         TEST_ASSERT_MSG((val_2_arr[i] != strpair->value), "val_2_arr[i] != strpair->value");
   106         
   107         p = p->next;
   108     }
   109     TEST_ASSERT_MSG((p == NULL), "p == NULL");
   110     
   111     cout << "\nduplicating four-element list...\n\n";
   112     duplist = stringpair_list_dup(pairlist);
   113     
   114     p = pairlist;
   115     stringpair_list_t* dup_p = duplist;
   116     
   117     while (dup_p) {
   118         srcpair = p->value;
   119         dstpair = dup_p->value;
   120 
   121         TEST_ASSERT_MSG((dstpair), "dstpair");
   122         TEST_ASSERT_MSG((dstpair->value), "dstpair->value");
   123         
   124         cout << srcpair->key << ":" << srcpair->value << " / " << dstpair->key << ":" << dstpair->value << "\n";
   125         TEST_ASSERT_MSG((test_stringpair_equals(srcpair, dstpair)), "test_stringpair_equals(srcpair, dstpair)");
   126 
   127         TEST_ASSERT_MSG((srcpair->key != dstpair->key), "srcpair->key != dstpair->key");   // test deep copies (to be fixed in next 2 commits)
   128         TEST_ASSERT_MSG((srcpair->value != dstpair->value), "srcpair->value != dstpair->value");
   129 
   130         i++;
   131         p = p->next;
   132 
   133         dup_p = dup_p->next;
   134         TEST_ASSERT_MSG(((p == NULL) == (dup_p == NULL)), "(p == NULL) == (dup_p == NULL)");
   135     }
   136     cout << "\nfour-element stringpair_list successfully duplicated.\n\n";
   137 
   138     cout << "freeing stringpair_lists...\n";
   139     free_stringpair_list(pairlist); // will free strpair
   140     free_stringpair_list(duplist);
   141     pairlist = NULL;
   142     duplist = NULL;
   143     
   144     cout << "done.\n";
   145 }