1 #include <tests/src/TestException.hxx>
2 #include <tests/framework/CountingException.hxx>
3 #include <tests/framework/UncloneableException.hxx>
8 void TestException::testCopyingOfShortMessage()
10 char* const cstr
= new char[80];
11 strcpy(cstr
, "TEST0001");
12 libsex::Exception
e(cstr
);
15 std::string
exp("TEST0001");
16 std::string
act(e
.what());
17 CPPUNIT_ASSERT_EQUAL(exp
, act
);
20 void TestException::testWhetherLongMessageDoesNotOverflow()
22 size_t length
= libsex::Exception::LENGTH
;
25 for (int i
= 0; i
< length
; ++i
) {
30 libsex::Exception
e(str
.c_str());
32 CPPUNIT_ASSERT_EQUAL(length
, strlen(e
.what()));
34 for (int i
= 0; i
< length
; ++i
) {
35 CPPUNIT_ASSERT_EQUAL('.', e
.what()[i
]);
39 void 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 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 TestException::testWhetherBadAllocOnCloneIsSwallowed()
63 UncloneableException e1
;
64 libsex::Exception
e2("e2", e1
);
67 void TestException::testWhetherNoticeIsAppendedOnBadAlloc()
69 UncloneableException e1
;
70 libsex::Exception
e2("e2", e1
);
73 = "e2\nDOUBLE FAULT! "
74 "Caught std::bad_alloc in constructor "
76 std::string act
= e2
.what();
77 CPPUNIT_ASSERT_EQUAL(exp
, act
);
80 void TestException::testWhetherNoticeDoesNotOverflowIfBufferTooSmall()
82 size_t length
= libsex::Exception::LENGTH
;
85 for (int i
= 0; i
< length
; ++i
) {
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 TestException::testWritingToStream()
98 std::string
exp("TestException");
99 libsex::Exception
e(exp
.c_str());
101 std::stringstream act
;
104 CPPUNIT_ASSERT_EQUAL(exp
, act
.str());
107 void TestException::testBacktrace()
109 libsex::Exception
cause("Failed to foo.");
110 libsex::Exception
e("Failed to bar.", cause
);
112 std::stringstream act
;
115 CPPUNIT_ASSERT_EQUAL(
116 std::string("Failed to bar.\nFailed to foo."),