1 #include <tests/TestUtility.hxx>
2 #include <tests/MockException.hxx>
4 #include <libsex/utility.hxx>
6 void TestUtility::setUp() {}
7 void TestUtility::tearDown() {}
9 void TestUtility::testShortTemplateWithoutFormatting()
11 char buf
[100] = { 0 };
17 "Message template."));
19 std::string
exp("foobar.cxx:42: Message template.");
20 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
23 void TestUtility::testWhetherFalseIsReturnedWhenHeaderFillsBuffer()
25 char buf
[100] = { 0 };
31 "Message template."));
33 std::string
exp("foobar.cxx:42: ");
34 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
37 void TestUtility::testWhetherTrueIsReturnedOnExactFit()
39 char buf
[100] = { 0 };
45 "Message template."));
47 std::string
exp("foobar.cxx:42: Message template.");
48 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
51 void TestUtility::testWhetherUnformattedOverflowsArePrevented()
53 // Array should be large enough to not really get
54 // overwritten. Just adjust the parameter 'length'
55 // to libsex::format().
56 char buf
[100] = { 0 };
62 "Message template."));
64 assertBoundary(buf
, 10, 100);
67 void TestUtility::testWhetherSimpleFormattingWorks()
69 char buf
[100] = { 0 };
75 "String: '%s'.", "STRING"));
77 std::string
exp("foobar.cxx:42: String: 'STRING'.");
78 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
81 void TestUtility::testWhetherFormattedOverflowsArePreventedBeforeFormatstring()
83 // Array should be large enough to not really get
84 // overwritten. Just adjust the parameter 'length'
85 // to libsex::format().
86 char buf
[100] = { 0 };
92 "String: '%s'.", "STRING"));
94 assertBoundary(buf
, 10, 100);
97 void TestUtility::testWhetherFormattedOverflowsArePreventedInFormatstring()
99 // Array should be large enough to not really get
100 // overwritten. Just adjust the parameter 'length'
101 // to libsex::format().
102 char buf
[100] = { 0 };
108 "String: '%s'.", "STRING"));
110 assertBoundary(buf
, 28, 100);
113 void TestUtility::testMultipleFormatParameters()
115 char buf
[100] = { 0 };
121 "%s=%d", "var", 42));
123 std::string
exp("foobar.cxx:42: var=42");
124 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
127 void TestUtility::testWhetherMultipleParametersDontOverflowBeforeFormatstring()
129 char buf
[100] = { 0 };
135 "%s=%d", "var", 42));
137 assertBoundary(buf
, 10, 100);
140 void TestUtility::testWhetherMultipleParametersDontOverflowInFormatstring()
142 char buf
[100] = { 0 };
148 "%s=%d", "var", 42));
150 assertBoundary(buf
, 18, 100);
153 void TestUtility::testTemplateFactory()
156 = libsex::formatted
<MockException
>(
160 CPPUNIT_ASSERT_EQUAL(
161 std::string("foobar.cxx:42: VAR=42"),
162 std::string(e
.what()));
165 void TestUtility::assertBoundary(
166 const char* const buffer
,
170 for (size_t i
= boundary
; i
< length
; ++i
) {
171 CPPUNIT_ASSERT_EQUAL('\0', buffer
[i
]);