1 #include <tests/src/TestUtility.hxx>
2 #include <tests/framework/MockException.hxx>
4 #include <libsex/utility.hxx>
6 void TestUtility::testShortTemplateWithoutFormatting()
14 "Message template."));
16 std::string
exp("foobar.cxx:42: Message template.");
17 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
20 void TestUtility::testWhetherFalseIsReturnedWhenHeaderFillsBuffer()
22 char buf
[100] = { 0 };
28 "Message template."));
30 std::string
exp("foobar.cxx:42: ");
31 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
34 void TestUtility::testWhetherTrueIsReturnedOnExactFit()
36 char buf
[100] = { 0 };
42 "Message template."));
44 std::string
exp("foobar.cxx:42: Message template.");
45 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
48 void TestUtility::testWhetherUnformattedOverflowsArePrevented()
50 // Array should be large enough to not really get
51 // overwritten. Just adjust the parameter 'length'
52 // to libsex::format().
53 char buf
[100] = { 0 };
59 "Message template."));
61 assertBoundary(buf
, 10, 100);
64 void TestUtility::testWhetherSimpleFormattingWorks()
66 char buf
[100] = { 0 };
72 "String: '%s'.", "STRING"));
74 std::string
exp("foobar.cxx:42: String: 'STRING'.");
75 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
78 void TestUtility::testWhetherFormattedOverflowsArePreventedBeforeFormatstring()
80 // Array should be large enough to not really get
81 // overwritten. Just adjust the parameter 'length'
82 // to libsex::format().
83 char buf
[100] = { 0 };
89 "String: '%s'.", "STRING"));
91 assertBoundary(buf
, 10, 100);
94 void TestUtility::testWhetherFormattedOverflowsArePreventedInFormatstring()
96 // Array should be large enough to not really get
97 // overwritten. Just adjust the parameter 'length'
98 // to libsex::format().
99 char buf
[100] = { 0 };
105 "String: '%s'.", "STRING"));
107 assertBoundary(buf
, 28, 100);
110 void TestUtility::testMultipleFormatParameters()
112 char buf
[100] = { 0 };
118 "%s=%d", "var", 42));
120 std::string
exp("foobar.cxx:42: var=42");
121 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
124 void TestUtility::testWhetherMultipleParametersDontOverflowBeforeFormatstring()
126 char buf
[100] = { 0 };
132 "%s=%d", "var", 42));
134 assertBoundary(buf
, 10, 100);
137 void TestUtility::testWhetherMultipleParametersDontOverflowInFormatstring()
139 char buf
[100] = { 0 };
145 "%s=%d", "var", 42));
147 assertBoundary(buf
, 18, 100);
150 void TestUtility::testTemplateFactory()
153 = libsex::formatted
<MockException
>(
157 CPPUNIT_ASSERT_EQUAL(
158 std::string("foobar.cxx:42: VAR=42"),
159 std::string(e
.what()));
162 void TestUtility::assertBoundary(
163 const char* const buffer
,
167 for (size_t i
= boundary
; i
< length
; ++i
) {
168 CPPUNIT_ASSERT_EQUAL('\0', buffer
[i
]);