1 #include <tests/Utility.hxx>
2 #include <tests/framework/MockException.hxx>
4 #include <libsex/utility.hxx>
8 void tests::Utility::testShortTemplateWithoutFormatting()
10 char buf
[100] = { 0 };
16 "Message template."));
18 std::string
exp("foobar.cxx:42: Message template.");
19 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
22 void tests::Utility::testWhetherFalseIsReturnedWhenHeaderFillsBuffer()
24 char buf
[100] = { 0 };
30 "Message template."));
32 std::string
exp("foobar.cxx:42: ");
33 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
36 void tests::Utility::testWhetherTrueIsReturnedOnExactFit()
38 char buf
[100] = { 0 };
44 "Message template."));
46 std::string
exp("foobar.cxx:42: Message template.");
47 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
50 void tests::Utility::testWhetherUnformattedOverflowsArePrevented()
52 // Array should be large enough to not really get
53 // overwritten. Just adjust the parameter 'length'
54 // to libsex::format().
55 char buf
[100] = { 0 };
61 "Message template."));
63 assertBoundary(buf
, 10, 100);
66 void tests::Utility::testWhetherSimpleFormattingWorks()
68 char buf
[100] = { 0 };
74 "String: '%s'.", "STRING"));
76 std::string
exp("foobar.cxx:42: String: 'STRING'.");
77 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
80 void tests::Utility::testWhetherFormattedOverflowsArePreventedBeforeFormatstring()
82 // Array should be large enough to not really get
83 // overwritten. Just adjust the parameter 'length'
84 // to libsex::format().
85 char buf
[100] = { 0 };
91 "String: '%s'.", "STRING"));
93 assertBoundary(buf
, 10, 100);
96 void tests::Utility::testWhetherFormattedOverflowsArePreventedInFormatstring()
98 // Array should be large enough to not really get
99 // overwritten. Just adjust the parameter 'length'
100 // to libsex::format().
101 char buf
[100] = { 0 };
107 "String: '%s'.", "STRING"));
109 assertBoundary(buf
, 28, 100);
112 void tests::Utility::testMultipleFormatParameters()
114 char buf
[100] = { 0 };
120 "%s=%d", "var", 42));
122 std::string
exp("foobar.cxx:42: var=42");
123 CPPUNIT_ASSERT_EQUAL(exp
, std::string(buf
));
126 void tests::Utility::testWhetherMultipleParametersDontOverflowBeforeFormatstring()
128 char buf
[100] = { 0 };
134 "%s=%d", "var", 42));
136 assertBoundary(buf
, 10, 100);
139 void tests::Utility::testWhetherMultipleParametersDontOverflowInFormatstring()
141 char buf
[100] = { 0 };
147 "%s=%d", "var", 42));
149 assertBoundary(buf
, 18, 100);
152 void tests::Utility::testTemplateFactory()
155 = libsex::formatted
<MockException
>(
159 CPPUNIT_ASSERT_EQUAL(
160 std::string("foobar.cxx:42: VAR=42"),
161 std::string(e
.what()));
164 void tests::Utility::testChainedTemplateFactory()
167 = libsex::formatted
<MockException
>(
171 MockException e
= libsex::formatted
<MockException
>(
176 std::stringstream act
;
179 CPPUNIT_ASSERT_EQUAL(
180 std::string("foobar.cxx:84: #exceptions=1\n"
181 "foobar.cxx:42: VAR=42"),
185 void tests::Utility::assertBoundary(
186 const char* const buffer
,
190 for (size_t i
= boundary
; i
< length
; ++i
) {
191 CPPUNIT_ASSERT_EQUAL('\0', buffer
[i
]);