#define BOOST_TEST_MODULE DLCL_Tests #include #include // new to catch output #include #include "DListT.h" // so we can use the elements of the output_test_stream without // a horrid list of namespaces using boost::test_tools::output_test_stream; using namespace std; BOOST_AUTO_TEST_SUITE( DLCL_Methods_Tests) void DataTest(DListT & list, string data, string error) { output_test_stream output; // change stdout to be the new output buffer streambuf * old = cout.rdbuf(output.rdbuf()); // force the error string x = list.Data(); // restore cout. cout.rdbuf(old); BOOST_TEST(x == data); BOOST_TEST(output.is_equal(error,false)); BOOST_TEST(output.check_length(error.size(), false)); if (error.size() > 0){ BOOST_TEST(!output.is_empty(false)); } else { BOOST_TEST(output.is_empty(false)); } return; } void CompareLists(DListT & aList, const vector & standard){ aList.Home(); size_t i; if( aList.Size() != standard.size()) { BOOST_CHECK(aList.Size() != standard.size()); BOOST_FAIL("Size of the list does not match the standard size"); } for(i = 0; i < standard.size(); i++) { DataTest(aList, standard[i],""); aList.Right(); } return; } BOOST_AUTO_TEST_CASE(DLCL_Constructor_Test) { DListT aList; string errorMessage = "Error: Attempt to access Empty List.\n"; string data = "NO DATA"; BOOST_TEST(aList.Size() == 0); DataTest(aList, data, errorMessage); aList.Right(); DataTest(aList, data, errorMessage); aList.Home(); DataTest(aList, data, errorMessage); aList.Left(); DataTest(aList, data, errorMessage); } BOOST_AUTO_TEST_CASE(DLCL_Single_Insert_Test) { DListT aList; string error = ""; string data = "data"; vector standard; standard.push_back(data); aList.Insert(data); CompareLists(aList, standard); // just to test the test. //standard.erase(begin(standard), end(standard)); //CompareLists(aList, standard); BOOST_TEST(aList.Size() == 1); DataTest(aList, data, error); aList.Right(); DataTest(aList, data, error); aList.Home(); DataTest(aList, data, error); aList.Left(); DataTest(aList, data, error); } BOOST_AUTO_TEST_CASE(DLCL_Multiple_FRONT_Insert_Test) { DListT aList; vector standard; vector testData{"A","B","C","D","E"}; for (auto x: testData){ aList.Home(); aList.Insert(x); standard.insert(standard.begin(), x); CompareLists(aList, standard); } } BOOST_AUTO_TEST_CASE(DLCL_Multiple_Insert_Test) { DListT aList; vector standard; // this one can't be quite as automated. aList.Insert("B"); aList.Insert("A"); // The list should now be A, B but we have tested that before. // I will still test that just to make suer we are on solid grounds. standard.push_back("A"); standard.push_back("B"); CompareLists(aList, standard); // now check for an insert at the second position aList.Home(); aList.Right(); aList.Insert("C"); // current should point at C. Check this first. DataTest(aList, "C", ""); // the list should be A C B standard.insert(begin(standard)+1,"C"); CompareLists(aList, standard); // now move to B and try it again. aList.Home(); aList.Right(); // point to C aList.Right(); // point to B aList.Insert("D"); DataTest(aList, "D", ""); // the list should be A C D B standard.insert(begin(standard)+2,"D"); CompareLists(aList, standard); // I have tested one past the beginning and at the end, try in the middle aList.Home(); aList.Right(); // point to C aList.Right(); // point to D aList.Insert("E"); DataTest(aList, "E", ""); standard.insert(begin(standard)+2,"E"); CompareLists(aList, standard); // just to be sure, I want another Insert at the front. aList.Home(); aList.Insert("1"); DataTest(aList, "1", ""); standard.insert(begin(standard),"1"); CompareLists(aList, standard); } BOOST_AUTO_TEST_CASE(DLCL_Random_Insert_Test) { DListT aList; vector data{"C","D","E","F","G","H","I","J","alpha","beta","delta","gamma", "1", "2", "3","4","5","6","7","8","9","10","11"}; vector standard {"A","B"}; size_t pos; size_t i; aList.Insert("B"); aList.Insert("A"); CompareLists(aList, standard); // make sure we do the same test every time. srand(0); for(auto x: data) { pos = rand()%standard.size(); // move to the correct position in the list. // and isert the new value aList.Home(); for(i = 0; i < pos; i++) { aList.Right(); } aList.Insert(x); // check to make sure current is correct DataTest(aList, x, ""); // insert the intem into the comparison vector. standard.insert(begin(standard)+pos, x); // check to make sure that the two are the same. CompareLists(aList, standard); } return; } BOOST_AUTO_TEST_SUITE_END()