Moved tests from tests/src to tests and their fixed namespace.
[libsex.git] / tests / TestException.cxx
blobaad236b238abdf25119b3ee71c4bc485e218955c
1 #include <tests/TestException.hxx>
2 #include <tests/framework/CountingException.hxx>
3 #include <tests/framework/UncloneableException.hxx>
5 #include <cstring>
6 #include <sstream>
8 void tests::TestException::testCopyingOfShortMessage()
10 char* const cstr = new char[80];
11 strcpy(cstr, "TEST0001");
12 libsex::Exception e(cstr);
13 delete cstr;
15 std::string exp("TEST0001");
16 std::string act(e.what());
17 CPPUNIT_ASSERT_EQUAL(exp, act);
20 void tests::TestException::testWhetherLongMessageDoesNotOverflow()
22 size_t length = libsex::Exception::LENGTH;
23 std::string str;
25 for (size_t i = 0; i < length; ++i) {
26 str += '.';
28 str += '!';
30 libsex::Exception e(str.c_str());
32 CPPUNIT_ASSERT_EQUAL(length, strlen(e.what()));
34 for (size_t i = 0; i < length; ++i) {
35 CPPUNIT_ASSERT_EQUAL('.', e.what()[i]);
39 void tests::TestException::testWhetherPreviousExceptionIsCloned()
41 CPPUNIT_ASSERT_EQUAL(0, CountingException::instances);
42 CountingException e1("e1");
43 CPPUNIT_ASSERT_EQUAL(1, CountingException::instances);
44 libsex::Exception e2("e2", e1);
45 CPPUNIT_ASSERT_EQUAL(2, CountingException::instances);
48 void tests::TestException::testWhetherClonedExceptionIsDeleted()
50 CPPUNIT_ASSERT_EQUAL(0, CountingException::instances);
52 CountingException e1("e1");
53 CPPUNIT_ASSERT_EQUAL(1, CountingException::instances);
55 libsex::Exception e2("e2", e1);
56 CPPUNIT_ASSERT_EQUAL(2, CountingException::instances);
58 CPPUNIT_ASSERT_EQUAL(1, CountingException::instances);
61 void tests::TestException::testWhetherBadAllocOnCloneIsSwallowed()
63 UncloneableException e1;
64 libsex::Exception e2("e2", e1);
67 void tests::TestException::testWhetherNoticeIsAppendedOnBadAlloc()
69 UncloneableException e1;
70 libsex::Exception e2("e2", e1);
72 std::string exp
73 = "e2\nDOUBLE FAULT! "
74 "Caught std::bad_alloc in constructor "
75 "of an exception.";
76 std::string act = e2.what();
77 CPPUNIT_ASSERT_EQUAL(exp, act);
80 void tests::TestException::testWhetherNoticeDoesNotOverflowIfBufferTooSmall()
82 size_t length = libsex::Exception::LENGTH;
83 std::string str;
85 for (size_t i = 0; i < length; ++i) {
86 str += '.';
89 UncloneableException e1;
90 libsex::Exception e2(str.c_str(), e1);
92 CPPUNIT_ASSERT_EQUAL(length, strlen(e2.what()));
93 CPPUNIT_ASSERT_EQUAL(str, std::string(e2.what()));
96 void tests::TestException::testWritingToStream()
98 std::string exp("TestException");
99 libsex::Exception e(exp.c_str());
101 std::stringstream act;
102 e.write(act);
104 CPPUNIT_ASSERT_EQUAL(exp, act.str());
107 void tests::TestException::testBacktrace()
109 libsex::Exception cause("Failed to foo.");
110 libsex::Exception e("Failed to bar.", cause);
112 std::stringstream act;
113 e.backtrace(act);
115 CPPUNIT_ASSERT_EQUAL(
116 std::string("Failed to bar.\nFailed to foo."),
117 act.str());