1 #include <tests/Exception.hxx>
2 #include <tests/framework/CountingException.hxx>
3 #include <tests/framework/UncloneableException.hxx>
8 void tests::Exception::testCopyingOfShortMessage()
10 char* const cstr
= new char[80];
11 strcpy(cstr
, "TEST0001");
12 std::string
exp(cstr
);
14 libsex::Exception
e(cstr
);
16 strcpy(cstr
, "Invalid.");
19 std::string
act(e
.what());
20 CPPUNIT_ASSERT_EQUAL(exp
, act
);
23 void tests::Exception::testWhetherLongMessageDoesNotOverflow()
25 size_t length
= libsex::Exception::LENGTH
;
28 for (size_t i
= 0; i
< length
; ++i
) {
33 libsex::Exception
e(str
.c_str());
35 CPPUNIT_ASSERT_EQUAL(length
, strlen(e
.what()));
37 for (size_t i
= 0; i
< length
; ++i
) {
38 CPPUNIT_ASSERT_EQUAL('.', e
.what()[i
]);
42 void tests::Exception::testWhetherPreviousExceptionIsCloned()
44 CPPUNIT_ASSERT_EQUAL(0, CountingException::instances
);
45 CountingException
e1("e1");
46 CPPUNIT_ASSERT_EQUAL(1, CountingException::instances
);
47 libsex::Exception
e2("e2", e1
);
48 CPPUNIT_ASSERT_EQUAL(2, CountingException::instances
);
51 void tests::Exception::testWhetherClonedExceptionIsDeleted()
53 CPPUNIT_ASSERT_EQUAL(0, CountingException::instances
);
55 CountingException
e1("e1");
56 CPPUNIT_ASSERT_EQUAL(1, CountingException::instances
);
58 libsex::Exception
e2("e2", e1
);
59 CPPUNIT_ASSERT_EQUAL(2, CountingException::instances
);
61 CPPUNIT_ASSERT_EQUAL(1, CountingException::instances
);
64 void tests::Exception::testWhetherBadAllocOnCloneIsSwallowed()
66 UncloneableException e1
;
67 libsex::Exception
e2("e2", e1
);
70 void tests::Exception::testWhetherNoticeIsAppendedOnBadAlloc()
72 UncloneableException e1
;
73 libsex::Exception
e2("e2", e1
);
77 "std::bad_alloc while cloning Exception!";
78 std::string act
= e2
.what();
79 CPPUNIT_ASSERT_EQUAL(exp
, act
);
82 void tests::Exception::testWhetherNoticeDoesNotOverflowIfBufferTooSmall()
84 size_t length
= libsex::Exception::LENGTH
;
87 for (size_t i
= 0; i
< length
; ++i
) {
91 UncloneableException e1
;
92 libsex::Exception
e2(str
.c_str(), e1
);
94 CPPUNIT_ASSERT_EQUAL(length
, strlen(e2
.what()));
95 CPPUNIT_ASSERT_EQUAL(str
, std::string(e2
.what()));
98 void tests::Exception::testWritingToStream()
100 std::string
exp("Exception");
101 libsex::Exception
e(exp
.c_str());
103 std::stringstream act
;
106 CPPUNIT_ASSERT_EQUAL(exp
, act
.str());
109 void tests::Exception::testBacktrace()
111 libsex::Exception
cause("Failed to foo.");
112 libsex::Exception
e("Failed to bar.", cause
);
114 std::stringstream act
;
117 CPPUNIT_ASSERT_EQUAL(
118 std::string("Failed to bar.\nFailed to foo."),