#define BOOST_TEST_MODULE DLCL_Tests #include #include // new to catch output #include #include "QueueT.h" #include // 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(QueueMethods) BOOST_AUTO_TEST_CASE (Queue_Constructor) { QueueT q; BOOST_TEST(q.IsEmpty()); BOOST_TEST(not q.IsFull()); BOOST_TEST(q.Size() == 0); return; } BOOST_AUTO_TEST_CASE (Simple_ENQUEUE_DEQUEUE) { QueueT q; string item = "word"; q.Enqueue(item); BOOST_TEST(not q.IsEmpty()); BOOST_TEST(not q.IsFull()); BOOST_TEST(q.Size() == 1); BOOST_TEST(q.Front() == item); q.Dequeue(); BOOST_TEST(q.IsEmpty()); BOOST_TEST(not q.IsFull()); BOOST_TEST(q.Size() == 0); return; } BOOST_AUTO_TEST_CASE(Front_Error_Test) { output_test_stream output; streambuf * old = cout.rdbuf(output.rdbuf()); string goodMessage = "Invalid Front on Empty Queue\n"; string badMessage = "invalid front on empty q\n"; // force the error QueueT q; q.Front(); // restore cout. cout.rdbuf(old); // is_empty check if it is empty. BOOST_TEST(not output.is_empty(false)); // check_length check the length BOOST_TEST(output.check_length(goodMessage.size(),false)); BOOST_TEST(output.check_length(badMessage.size(),false), "This should fail"); // is_equal check against a constant string BOOST_TEST(output.is_equal(goodMessage,false)); BOOST_TEST(output.is_equal(badMessage,false)); output.flush(); return; } bool CorrectMessage(out_of_range e) { string MESSAGE = "Can not run dequeue on an empty queue"; cout << endl << endl; cout << e.what() << endl; cout << endl << endl; return e.what() == MESSAGE; } // this test case will be fine for all but the last item. // Notice, it will abort the test on the REQUIRE_NO_THROW that actually // catches an exception. BOOST_AUTO_TEST_CASE(Dequeue_Exception_Test_1) { QueueT q; // these two will be fine. BOOST_CHECK_THROW(q.Dequeue(),out_of_range); BOOST_CHECK_THROW(q.Dequeue(),exception); BOOST_REQUIRE_THROW(q.Dequeue(),exception); BOOST_WARN_THROW(q.Dequeue(),exception); BOOST_REQUIRE_EXCEPTION(q.Dequeue(),out_of_range, CorrectMessage); q.Enqueue("word"); // This is fine BOOST_REQUIRE_NO_THROW(q.Dequeue()); // this should produce a error q.Enqueue("word"); BOOST_CHECK_THROW(q.Dequeue(),exception); cout << endl << endl; cout << "***************************" << endl; cout << " I got here" << endl; cout << "***************************" << endl; cout << endl << endl; // when this throws an error, the routine will be aborted. BOOST_REQUIRE_NO_THROW(q.Dequeue()); // this will never be executed. cout << endl << endl; cout << "***************************" << endl; cout << " I FINISHED the exception test " << endl; cout << "***************************" << endl; cout << endl << endl; return; } // the first case will be fine, CHECK_THROW will throw an exception // but I catch it. // The second case is problematic, as that is not the exception that was thrown BOOST_AUTO_TEST_CASE(Dequeue_Exception_Test_2) { class MyExceptionT{}; QueueT q; try { BOOST_CHECK_THROW(q.Dequeue(),MyExceptionT); } catch (...) { BOOST_ERROR("Failed to catch MyException"); } BOOST_CHECK_THROW(q.Dequeue(),MyExceptionT); return; } // just an example of an uncaught exception BOOST_AUTO_TEST_CASE(Dequeue_Exception_Test_3) { QueueT q; q.Dequeue(); return; } BOOST_AUTO_TEST_SUITE_END()