In-source preparation of release.
[libsex.git] / tests / TestUtility.cxx
blob377266d6ca132acf8f72facb7ef7699d66576b49
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 };
13 CPPUNIT_ASSERT(
14 libsex::format(
15 buf, 100,
16 "foobar.cxx", 42,
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 };
27 CPPUNIT_ASSERT(
28 !libsex::format(
29 buf, 16,
30 "foobar.cxx", 42,
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 };
41 CPPUNIT_ASSERT(
42 libsex::format(
43 buf, 33,
44 "foobar.cxx", 42,
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 };
58 CPPUNIT_ASSERT(
59 !libsex::format(
60 buf, 10,
61 "foobar.cxx", 42,
62 "Message template."));
64 assertBoundary(buf, 10, 100);
67 void TestUtility::testWhetherSimpleFormattingWorks()
69 char buf[100] = { 0 };
71 CPPUNIT_ASSERT(
72 libsex::format(
73 buf, 100,
74 "foobar.cxx", 42,
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 };
88 CPPUNIT_ASSERT(
89 !libsex::format(
90 buf, 10,
91 "foobar.cxx", 42,
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 };
104 CPPUNIT_ASSERT(
105 !libsex::format(
106 buf, 28,
107 "foobar.cxx", 42,
108 "String: '%s'.", "STRING"));
110 assertBoundary(buf, 28, 100);
113 void TestUtility::testMultipleFormatParameters()
115 char buf[100] = { 0 };
117 CPPUNIT_ASSERT(
118 libsex::format(
119 buf, 100,
120 "foobar.cxx", 42,
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 };
131 CPPUNIT_ASSERT(
132 !libsex::format(
133 buf, 10,
134 "foobar.cxx", 42,
135 "%s=%d", "var", 42));
137 assertBoundary(buf, 10, 100);
140 void TestUtility::testWhetherMultipleParametersDontOverflowInFormatstring()
142 char buf[100] = { 0 };
144 CPPUNIT_ASSERT(
145 !libsex::format(
146 buf, 18,
147 "foobar.cxx", 42,
148 "%s=%d", "var", 42));
150 assertBoundary(buf, 18, 100);
153 void TestUtility::testTemplateFactory()
155 MockException e
156 = libsex::formatted<MockException>(
157 "foobar.cxx", 42,
158 "VAR", 42);
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,
167 size_t boundary,
168 size_t length)
170 for (size_t i = boundary; i < length; ++i) {
171 CPPUNIT_ASSERT_EQUAL('\0', buffer[i]);